blob: 87c128f7aefb4e87778af28cd37d553fd999aa16 [file] [log] [blame]
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001/* preproc.c macro preprocessor for the Netwide Assembler
2 *
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the licence given in the file "Licence"
6 * distributed in the NASM archive.
7 *
8 * initial version 18/iii/97 by Simon Tatham
9 */
10
H. Peter Anvin4836e332002-04-30 20:56:43 +000011/* Typical flow of text through preproc
12 *
13 * pp_getline gets tokenised lines, either
14 *
15 * from a macro expansion
16 *
17 * or
18 * {
19 * read_line gets raw text from stdmacpos, or predef, or current input file
20 * tokenise converts to tokens
21 * }
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 Anvind7ed89e2002-04-30 20:52:08 +000036#include <stdio.h>
H. Peter Anvinaf535c12002-04-30 20:59:21 +000037#include <stdarg.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000038#include <stdlib.h>
39#include <stddef.h>
40#include <string.h>
41#include <ctype.h>
H. Peter Anvin76690a12002-04-30 20:52:49 +000042#include <limits.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000043
44#include "nasm.h"
45#include "nasmlib.h"
46
47typedef struct SMacro SMacro;
48typedef struct MMacro MMacro;
49typedef struct Context Context;
50typedef struct Token Token;
51typedef struct Line Line;
52typedef struct Include Include;
53typedef struct Cond Cond;
H. Peter Anvin6768eb72002-04-30 20:52:26 +000054typedef struct IncPath IncPath;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000055
56/*
57 * Store the definition of a single-line macro.
58 */
59struct SMacro {
60 SMacro *next;
61 char *name;
62 int casesense;
63 int nparam;
64 int in_progress;
65 Token *expansion;
66};
67
68/*
H. Peter Anvin76690a12002-04-30 20:52:49 +000069 * Store the definition of a multi-line macro. This is also used to
70 * store the interiors of `%rep...%endrep' blocks, which are
71 * effectively self-re-invoking multi-line macros which simply
72 * don't have a name or bother to appear in the hash tables. %rep
73 * blocks are signified by having a NULL `name' field.
74 *
75 * In a MMacro describing a `%rep' block, the `in_progress' field
76 * isn't merely boolean, but gives the number of repeats left to
77 * run.
78 *
79 * The `next' field is used for storing MMacros in hash tables; the
80 * `next_active' field is for stacking them on istk entries.
81 *
82 * When a MMacro is being expanded, `params', `iline', `nparam',
83 * `paramlen', `rotate' and `unique' are local to the invocation.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000084 */
85struct MMacro {
86 MMacro *next;
87 char *name;
88 int casesense;
89 int nparam_min, nparam_max;
90 int plus; /* is the last parameter greedy? */
H. Peter Anvin6768eb72002-04-30 20:52:26 +000091 int nolist; /* is this macro listing-inhibited? */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000092 int in_progress;
H. Peter Anvineba20a72002-04-30 20:53:55 +000093 Token *dlist; /* All defaults as one list */
94 Token **defaults; /* Parameter default pointers */
H. Peter Anvin76690a12002-04-30 20:52:49 +000095 int ndefs; /* number of default parameters */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000096 Line *expansion;
H. Peter Anvin76690a12002-04-30 20:52:49 +000097
98 MMacro *next_active;
H. Peter Anvinb64535f2002-04-30 20:55:37 +000099 MMacro *rep_nest; /* used for nesting %rep */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000100 Token **params; /* actual parameters */
101 Token *iline; /* invocation line */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000102 int nparam, rotate, *paramlen;
103 unsigned long unique;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000104 int lineno; /* Current line number on expansion */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000105};
106
107/*
108 * The context stack is composed of a linked list of these.
109 */
110struct Context {
111 Context *next;
112 SMacro *localmac;
113 char *name;
114 unsigned long number;
115};
116
117/*
118 * This is the internal form which we break input lines up into.
119 * Typically stored in linked lists.
120 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000121 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
122 * necessarily used as-is, but is intended to denote the number of
123 * the substituted parameter. So in the definition
124 *
125 * %define a(x,y) ( (x) & ~(y) )
126 *
127 * the token representing `x' will have its type changed to
128 * TOK_SMAC_PARAM, but the one representing `y' will be
129 * TOK_SMAC_PARAM+1.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000130 *
131 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
132 * which doesn't need quotes around it. Used in the pre-include
133 * mechanism as an alternative to trying to find a sensible type of
134 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000135 */
136struct Token {
137 Token *next;
138 char *text;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000139 SMacro *mac; /* associated macro for TOK_SMAC_END */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000140 int type;
141};
142enum {
143 TOK_WHITESPACE = 1, TOK_COMMENT, TOK_ID, TOK_PREPROC_ID, TOK_STRING,
H. Peter Anvineba20a72002-04-30 20:53:55 +0000144 TOK_NUMBER, TOK_SMAC_END, TOK_OTHER, TOK_SMAC_PARAM,
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000145 TOK_INTERNAL_STRING
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000146};
147
148/*
149 * Multi-line macro definitions are stored as a linked list of
150 * these, which is essentially a container to allow several linked
151 * lists of Tokens.
152 *
153 * Note that in this module, linked lists are treated as stacks
154 * wherever possible. For this reason, Lines are _pushed_ on to the
155 * `expansion' field in MMacro structures, so that the linked list,
156 * if walked, would give the macro lines in reverse order; this
157 * means that we can walk the list when expanding a macro, and thus
158 * push the lines on to the `expansion' field in _istk_ in reverse
159 * order (so that when popped back off they are in the right
160 * order). It may seem cockeyed, and it relies on my design having
161 * an even number of steps in, but it works...
162 *
163 * Some of these structures, rather than being actual lines, are
164 * markers delimiting the end of the expansion of a given macro.
H. Peter Anvin76690a12002-04-30 20:52:49 +0000165 * This is for use in the cycle-tracking and %rep-handling code.
166 * Such structures have `finishes' non-NULL, and `first' NULL. All
167 * others have `finishes' NULL, but `first' may still be NULL if
168 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000169 */
170struct Line {
171 Line *next;
172 MMacro *finishes;
173 Token *first;
174};
175
176/*
177 * To handle an arbitrary level of file inclusion, we maintain a
178 * stack (ie linked list) of these things.
179 */
180struct Include {
181 Include *next;
182 FILE *fp;
183 Cond *conds;
184 Line *expansion;
185 char *fname;
186 int lineno, lineinc;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000187 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000188};
189
190/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000191 * Include search path. This is simply a list of strings which get
192 * prepended, in turn, to the name of an include file, in an
193 * attempt to find the file if it's not in the current directory.
194 */
195struct IncPath {
196 IncPath *next;
197 char *path;
198};
199
200/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000201 * Conditional assembly: we maintain a separate stack of these for
202 * each level of file inclusion. (The only reason we keep the
203 * stacks separate is to ensure that a stray `%endif' in a file
204 * included from within the true branch of a `%if' won't terminate
205 * it and cause confusion: instead, rightly, it'll cause an error.)
206 */
207struct Cond {
208 Cond *next;
209 int state;
210};
211enum {
212 /*
213 * These states are for use just after %if or %elif: IF_TRUE
214 * means the condition has evaluated to truth so we are
215 * currently emitting, whereas IF_FALSE means we are not
216 * currently emitting but will start doing so if a %else comes
217 * up. In these states, all directives are admissible: %elif,
218 * %else and %endif. (And of course %if.)
219 */
220 COND_IF_TRUE, COND_IF_FALSE,
221 /*
222 * These states come up after a %else: ELSE_TRUE means we're
223 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
224 * any %elif or %else will cause an error.
225 */
226 COND_ELSE_TRUE, COND_ELSE_FALSE,
227 /*
228 * This state means that we're not emitting now, and also that
229 * nothing until %endif will be emitted at all. It's for use in
230 * two circumstances: (i) when we've had our moment of emission
231 * and have now started seeing %elifs, and (ii) when the
232 * condition construct in question is contained within a
233 * non-emitting branch of a larger condition construct.
234 */
235 COND_NEVER
236};
237#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
238
239/*
240 * Condition codes. Note that we use c_ prefix not C_ because C_ is
241 * used in nasm.h for the "real" condition codes. At _this_ level,
242 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
243 * ones, so we need a different enum...
244 */
245static char *conditions[] = {
246 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
247 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
248 "np", "ns", "nz", "o", "p", "pe", "po", "s", "z"
249};
250enum {
251 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
252 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
253 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_S, c_Z
254};
255static int inverse_ccs[] = {
256 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
257 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,
258 c_Z, c_NO, c_NP, c_PO, c_PE, c_NS, c_NZ
259};
260
H. Peter Anvin76690a12002-04-30 20:52:49 +0000261/*
262 * Directive names.
263 */
264static char *directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000265#ifdef TASM_COMPAT
266 "%arg",
267#endif
H. Peter Anvin76690a12002-04-30 20:52:49 +0000268 "%assign", "%clear", "%define", "%elif", "%elifctx", "%elifdef",
269 "%elifid", "%elifidn", "%elifidni", "%elifnctx", "%elifndef",
270 "%elifnid", "%elifnidn", "%elifnidni", "%elifnnum", "%elifnstr",
271 "%elifnum", "%elifstr", "%else", "%endif", "%endm", "%endmacro",
272 "%endrep", "%error", "%exitrep", "%iassign", "%idefine", "%if",
273 "%ifctx", "%ifdef", "%ifid", "%ifidn", "%ifidni", "%ifnctx",
274 "%ifndef", "%ifnid", "%ifnidn", "%ifnidni", "%ifnnum",
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000275 "%ifnstr", "%ifnum", "%ifstr", "%imacro", "%include",
276 "%ixdefine", "%line",
277#ifdef TASM_COMPAT
278 "%local",
279#endif
280 "%macro", "%pop", "%push", "%rep", "%repl", "%rotate",
281#ifdef TASM_COMPAT
282 "%stacksize",
283#endif
284 "%strlen", "%substr", "%undef", "%xdefine"
H. Peter Anvin76690a12002-04-30 20:52:49 +0000285};
286enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000287#ifdef TASM_COMPAT
288 PP_ARG,
289#endif
H. Peter Anvin76690a12002-04-30 20:52:49 +0000290 PP_ASSIGN, PP_CLEAR, PP_DEFINE, PP_ELIF, PP_ELIFCTX, PP_ELIFDEF,
291 PP_ELIFID, PP_ELIFIDN, PP_ELIFIDNI, PP_ELIFNCTX, PP_ELIFNDEF,
292 PP_ELIFNID, PP_ELIFNIDN, PP_ELIFNIDNI, PP_ELIFNNUM, PP_ELIFNSTR,
293 PP_ELIFNUM, PP_ELIFSTR, PP_ELSE, PP_ENDIF, PP_ENDM, PP_ENDMACRO,
294 PP_ENDREP, PP_ERROR, PP_EXITREP, PP_IASSIGN, PP_IDEFINE, PP_IF,
295 PP_IFCTX, PP_IFDEF, PP_IFID, PP_IFIDN, PP_IFIDNI, PP_IFNCTX,
296 PP_IFNDEF, PP_IFNID, PP_IFNIDN, PP_IFNIDNI, PP_IFNNUM,
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000297 PP_IFNSTR, PP_IFNUM, PP_IFSTR, PP_IMACRO, PP_INCLUDE,
298 PP_IXDEFINE, PP_LINE,
299#ifdef TASM_COMPAT
300 PP_LOCAL,
301#endif
302 PP_MACRO, PP_POP, PP_PUSH, PP_REP, PP_REPL, PP_ROTATE,
303#ifdef TASM_COMPAT
304 PP_STACKSIZE,
305#endif
306 PP_STRLEN, PP_SUBSTR, PP_UNDEF, PP_XDEFINE
H. Peter Anvin76690a12002-04-30 20:52:49 +0000307};
308
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000309#ifdef TASM_COMPAT
310
311/* For TASM compatibility we need to be able to recognise TASM compatible
312 * conditional compilation directives. Using the NASM pre-processor does
313 * not work, so we look for them specifically from the following list and
314 * then jam in the equivalent NASM directive into the input stream.
315 */
316
317#ifndef MAX
318# define MAX(a,b) ( ((a) > (b)) ? (a) : (b))
319#endif
320
321enum {
322 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
323 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
324};
325
326static char *tasm_directives[] = {
327 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
328 "ifndef", "include", "local"
329};
330
331static int StackSize = 4;
332static char *StackPointer = "ebp";
333static int ArgOffset = 8;
334static int LocalOffset = 4;
335
336#endif
H. Peter Anvin76690a12002-04-30 20:52:49 +0000337
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000338static Context *cstk;
339static Include *istk;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000340static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000341
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000342static efunc __error; /* Pointer to client-provided error reporting function */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000343static evalfunc evaluate;
344
H. Peter Anvin620515a2002-04-30 20:57:38 +0000345static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000346
347static unsigned long unique; /* unique identifier numbers */
348
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000349static Line *predef = NULL;
350
351static ListGen *list;
352
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000353/*
354 * The number of hash values we use for the macro lookup tables.
H. Peter Anvineba20a72002-04-30 20:53:55 +0000355 * FIXME: We should *really* be able to configure this at run time,
356 * or even have the hash table automatically expanding when necessary.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000357 */
358#define NHASH 31
359
360/*
361 * The current set of multi-line macros we have defined.
362 */
363static MMacro *mmacros[NHASH];
364
365/*
366 * The current set of single-line macros we have defined.
367 */
368static SMacro *smacros[NHASH];
369
370/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000371 * The multi-line macro we are currently defining, or the %rep
372 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000373 */
374static MMacro *defining;
375
376/*
377 * The number of macro parameters to allocate space for at a time.
378 */
379#define PARAM_DELTA 16
380
381/*
382 * The standard macro set: defined as `static char *stdmac[]'. Also
383 * gives our position in the macro set, when we're processing it.
384 */
385#include "macros.c"
386static char **stdmacpos;
387
388/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000389 * The extra standard macros that come from the object format, if
390 * any.
391 */
392static char **extrastdmac = NULL;
393int any_extrastdmac;
394
395/*
396 * Forward declarations.
397 */
H. Peter Anvin4836e332002-04-30 20:56:43 +0000398static Token *expand_mmac_params (Token *tline);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000399static Token *expand_smacro (Token *tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000400static Token *expand_id (Token *tline);
401static Context *get_ctx (char *name, int all_contexts);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000402static void make_tok_num(Token *tok, long val);
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000403static void error (int severity, char *fmt, ...);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000404
405/*
406 * Macros for safe checking of token pointers, avoid *(NULL)
407 */
408#define tok_type_(x,t) ((x) && (x)->type == (t))
409#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
410#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
411#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000412
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000413#ifdef TASM_COMPAT
414/* Handle TASM specific directives, which do not contain a % in
415 * front of them. We do it here because I could not find any other
416 * place to do it for the moment, and it is a hack (ideally it would
417 * be nice to be able to use the NASM pre-processor to do it).
418 */
419static char *check_tasm_directive(char *line)
420{
421 int i, j, k, m, len;
422 char *p = line, *oldline, oldchar;
423
424 /* Skip whitespace */
425 while (isspace(*p) && *p != 0)
426 p++;
427
428 /* Binary search for the directive name */
429 i = -1;
430 j = sizeof(tasm_directives) / sizeof(*tasm_directives);
431 len = 0;
432 while (!isspace(p [len]) && p [len] != 0)
433 len++;
434 if (len) {
435 oldchar = p [len];
436 p [len] = 0;
437 while (j - i > 1) {
438 k = (j + i) / 2;
439 m = nasm_stricmp(p, tasm_directives [k]);
440 if (m == 0) {
441 /* We have found a directive, so jam a % in front of it
442 * so that NASM will then recognise it as one if it's own.
443 */
444 p [len] = oldchar;
445 len = strlen(p);
446 oldline = line;
447 line = nasm_malloc(len + 2);
448 line [0] = '%';
449 if (k == TM_IFDIFI) {
450 /* NASM does not recognise IFDIFI, so we convert it to
451 * %ifdef BOGUS. This is not used in NASM comaptible
452 * code, but does need to parse for the TASM macro
453 * package.
454 */
455 strcpy(line + 1,"ifdef BOGUS");
456 } else {
457 memcpy(line + 1, p, len + 1);
458 }
459 nasm_free(oldline);
460 return line;
461 } else if (m < 0) {
462 j = k;
463 } else
464 i = k;
465 }
466 p [len] = oldchar;
467 }
468 return line;
469}
470#endif
471
H. Peter Anvin76690a12002-04-30 20:52:49 +0000472/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000473 * The pre-preprocessing stage... This function translates line
474 * number indications as they emerge from GNU cpp (`# lineno "file"
475 * flags') into NASM preprocessor line number indications (`%line
476 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000477 */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000478static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000479{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000480 int lineno, fnlen;
481 char *fname, *oldline;
482
483 if (line[0] == '#' && line[1] == ' ') {
484 oldline = line;
485 fname = oldline+2;
486 lineno = atoi(fname);
487 fname += strspn(fname, "0123456789 ");
488 if (*fname == '"')
489 fname++;
490 fnlen = strcspn(fname, "\"");
491 line = nasm_malloc(20+fnlen);
492 sprintf(line, "%%line %d %.*s", lineno, fnlen, fname);
493 nasm_free (oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000494 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000495#ifdef TASM_COMPAT
496 if (tasm_compatible_mode)
497 return check_tasm_directive(line);
498#endif
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000499 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000500}
501
502/*
503 * The hash function for macro lookups. Note that due to some
504 * macros having case-insensitive names, the hash function must be
505 * invariant under case changes. We implement this by applying a
506 * perfectly normal hash function to the uppercase of the string.
507 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000508static int hash(char *s)
509{
510 unsigned int h = 0;
511 int i = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000512 /*
513 * Powers of three, mod 31.
514 */
515 static const int multipliers[] = {
516 1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10,
517 30, 28, 22, 4, 12, 5, 15, 14, 11, 2, 6, 18, 23, 7, 21
518 };
H. Peter Anvineba20a72002-04-30 20:53:55 +0000519
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000520
521 while (*s) {
522 h += multipliers[i] * (unsigned char) (toupper(*s));
523 s++;
524 if (++i >= sizeof(multipliers)/sizeof(*multipliers))
525 i = 0;
526 }
527 h %= NHASH;
528 return h;
529}
530
531/*
532 * Free a linked list of tokens.
533 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000534static void free_tlist (Token *list)
535{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000536 Token *t;
537 while (list) {
538 t = list;
539 list = list->next;
540 nasm_free (t->text);
541 nasm_free (t);
542 }
543}
544
545/*
546 * Free a linked list of lines.
547 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000548static void free_llist (Line *list)
549{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000550 Line *l;
551 while (list) {
552 l = list;
553 list = list->next;
554 free_tlist (l->first);
555 nasm_free (l);
556 }
557}
558
559/*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000560 * Free an MMacro
561 */
562static void free_mmacro (MMacro *m)
563{
564 nasm_free (m->name);
565 free_tlist (m->dlist);
566 nasm_free (m->defaults);
567 free_llist (m->expansion);
568 nasm_free (m);
569}
570
571/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000572 * Pop the context stack.
573 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000574static void ctx_pop (void)
575{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000576 Context *c = cstk;
577 SMacro *smac, *s;
578
579 cstk = cstk->next;
580 smac = c->localmac;
581 while (smac) {
582 s = smac;
583 smac = smac->next;
584 nasm_free (s->name);
585 free_tlist (s->expansion);
586 nasm_free (s);
587 }
588 nasm_free (c->name);
589 nasm_free (c);
590}
591
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000592#define BUF_DELTA 512
593/*
594 * Read a line from the top file in istk, handling multiple CR/LFs
595 * at the end of the line read, and handling spurious ^Zs. Will
596 * return lines from the standard macro set if this has not already
597 * been done.
598 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000599static char *read_line (void)
600{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000601 char *buffer, *p, *q;
602 int bufsize;
603
604 if (stdmacpos) {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000605 if (*stdmacpos) {
606 char *ret = nasm_strdup(*stdmacpos++);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000607 if (!*stdmacpos && any_extrastdmac)
608 {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000609 stdmacpos = extrastdmac;
610 any_extrastdmac = FALSE;
611 return ret;
612 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000613 /*
614 * Nasty hack: here we push the contents of `predef' on
615 * to the top-level expansion stack, since this is the
616 * most convenient way to implement the pre-include and
617 * pre-define features.
618 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000619 if (!*stdmacpos)
620 {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000621 Line *pd, *l;
622 Token *head, **tail, *t, *tt;
623
624 for (pd = predef; pd; pd = pd->next) {
625 head = NULL;
626 tail = &head;
627 for (t = pd->first; t; t = t->next) {
628 tt = *tail = nasm_malloc(sizeof(Token));
629 tt->next = NULL;
630 tail = &tt->next;
631 tt->type = t->type;
632 tt->text = nasm_strdup(t->text);
633 tt->mac = t->mac; /* always NULL here, in fact */
634 }
635 l = nasm_malloc(sizeof(Line));
636 l->next = istk->expansion;
637 l->first = head;
638 l->finishes = FALSE;
639 istk->expansion = l;
640 }
641 }
642 return ret;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000643 }
644 else {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000645 stdmacpos = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000646 }
647 }
648
649 bufsize = BUF_DELTA;
650 buffer = nasm_malloc(BUF_DELTA);
651 p = buffer;
652 while (1) {
653 q = fgets(p, bufsize-(p-buffer), istk->fp);
654 if (!q)
655 break;
656 p += strlen(p);
657 if (p > buffer && p[-1] == '\n') {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000658 break;
659 }
660 if (p-buffer > bufsize-10) {
H. Peter Anvineba20a72002-04-30 20:53:55 +0000661 long offset = p-buffer;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000662 bufsize += BUF_DELTA;
663 buffer = nasm_realloc(buffer, bufsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000664 p = buffer+offset; /* prevent stale-pointer problems */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000665 }
666 }
667
668 if (!q && p == buffer) {
669 nasm_free (buffer);
670 return NULL;
671 }
672
H. Peter Anvineba20a72002-04-30 20:53:55 +0000673 src_set_linnum(src_get_linnum() + istk->lineinc);
674
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000675 /*
676 * Play safe: remove CRs as well as LFs, if any of either are
677 * present at the end of the line.
678 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000679 while (--p >= buffer && (*p == '\n' || *p == '\r'))
680 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000681
682 /*
683 * Handle spurious ^Z, which may be inserted into source files
684 * by some file transfer utilities.
685 */
686 buffer[strcspn(buffer, "\032")] = '\0';
687
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000688 list->line (LIST_READ, buffer);
689
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000690 return buffer;
691}
692
693/*
694 * Tokenise a line of text. This is a very simple process since we
695 * don't need to parse the value out of e.g. numeric tokens: we
696 * simply split one string into many.
697 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000698static Token *tokenise (char *line)
699{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000700 char *p = line;
701 int type;
702 Token *list = NULL;
703 Token *t, **tail = &list;
704
705 while (*line) {
706 p = line;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000707 if (*p == '%' &&
708 (isdigit(p[1]) ||
709 ((p[1] == '-' || p[1] == '+') && isdigit(p[2])) ||
710 ((p[1] == '+') && (isspace (p[2]) || !p[2]))))
H. Peter Anvineba20a72002-04-30 20:53:55 +0000711 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000712 p++;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000713 do {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000714 p++;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000715 } while (isdigit(*p));
716 type = TOK_PREPROC_ID;
717 }
718 else if (*p == '%' && p[1] == '{') {
719 p += 2;
720 while (*p && *p != '}') {
721 p[-1] = *p;
722 p++;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000723 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000724 p[-1] = '\0';
725 if (*p) p++;
726 type = TOK_PREPROC_ID;
727 }
728 else if (*p == '%' && (isidchar(p[1]) ||
729 ((p[1] == '!' || p[1] == '%' || p[1] == '$') &&
730 isidchar(p[2]))))
731 {
732 p++;
733 do {
734 p++;
735 } while (isidchar(*p));
736 type = TOK_PREPROC_ID;
737 }
738 else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000739 type = TOK_ID;
740 p++;
741 while (*p && isidchar(*p))
742 p++;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000743 }
744 else if (*p == '\'' || *p == '"') {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000745 /*
746 * A string token.
747 */
748 char c = *p;
749 p++;
750 type = TOK_STRING;
751 while (*p && *p != c)
752 p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000753 if (*p) {
754 p++;
755 }
756 else {
757 error(ERR_WARNING, "unterminated string");
758 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000759 }
760 else if (isnumstart(*p)) {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000761 /*
762 * A number token.
763 */
764 type = TOK_NUMBER;
765 p++;
766 while (*p && isnumchar(*p))
767 p++;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000768 }
769 else if (isspace(*p)) {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000770 type = TOK_WHITESPACE;
771 p++;
772 while (*p && isspace(*p))
773 p++;
774 /*
775 * Whitespace just before end-of-line is discarded by
776 * pretending it's a comment; whitespace just before a
777 * comment gets lumped into the comment.
778 */
779 if (!*p || *p == ';') {
780 type = TOK_COMMENT;
781 while (*p) p++;
782 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000783 }
784 else if (*p == ';') {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000785 type = TOK_COMMENT;
786 while (*p) p++;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000787 }
788 else {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000789 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000790 * Anything else is an operator of some kind. We check
791 * for all the double-character operators (>>, <<, //,
792 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
793 * else is a single-character operator.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000794 */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000795 type = TOK_OTHER;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000796 if ((p[0] == '>' && p[1] == '>') ||
797 (p[0] == '<' && p[1] == '<') ||
798 (p[0] == '/' && p[1] == '/') ||
799 (p[0] == '%' && p[1] == '%') ||
800 (p[0] == '<' && p[1] == '=') ||
801 (p[0] == '>' && p[1] == '=') ||
802 (p[0] == '=' && p[1] == '=') ||
803 (p[0] == '!' && p[1] == '=') ||
804 (p[0] == '<' && p[1] == '>') ||
805 (p[0] == '&' && p[1] == '&') ||
806 (p[0] == '|' && p[1] == '|') ||
807 (p[0] == '^' && p[1] == '^'))
H. Peter Anvineba20a72002-04-30 20:53:55 +0000808 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000809 p++;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000810 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000811 p++;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000812 }
813 if (type != TOK_COMMENT) {
814 *tail = t = nasm_malloc (sizeof(Token));
815 tail = &t->next;
816 t->next = NULL;
817 t->type = type;
818 t->text = nasm_malloc(1+p-line);
819 strncpy(t->text, line, p-line);
820 t->text[p-line] = '\0';
821 }
822 line = p;
823 }
824
825 return list;
826}
827
828/*
829 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000830 * If expand_locals is not zero, identifiers of the form "%$*xxx"
831 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000832 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000833static char *detoken (Token *tlist, int expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000834{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000835 Token *t;
836 int len;
837 char *line, *p;
838
839 len = 0;
840 for (t = tlist; t; t = t->next) {
841 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
842 char *p = getenv(t->text+2);
843 nasm_free (t->text);
844 if (p)
845 t->text = nasm_strdup(p);
846 else
847 t->text = NULL;
848 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000849 /* Expand local macros here and not during preprocessing */
850 if (expand_locals &&
851 t->type == TOK_PREPROC_ID && t->text &&
852 t->text[0] == '%' && t->text [1] == '$') {
853 Context *ctx = get_ctx (t->text, FALSE);
854 if (ctx) {
855 char buffer [40];
856 char *p, *q = t->text + 2;
857
858 q += strspn (q, "$");
859 sprintf (buffer, "..@%lu.", ctx->number);
860 p = nasm_malloc (strlen(buffer)+strlen(q)+1);
861 strcpy (p, buffer);
862 strcat (p, q);
863 nasm_free (t->text);
864 t->text = p;
865 }
866 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000867 if (t->text)
868 len += strlen(t->text);
869 }
870 p = line = nasm_malloc(len+1);
871 for (t = tlist; t; t = t->next) {
872 if (t->text) {
873 strcpy (p, t->text);
874 p += strlen(p);
875 }
876 }
877 *p = '\0';
878 return line;
879}
880
881/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000882 * A scanner, suitable for use by the expression evaluator, which
883 * operates on a line of Tokens. Expects a pointer to a pointer to
884 * the first token in the line to be passed in as its private_data
885 * field.
886 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000887static int ppscan(void *private_data, struct tokenval *tokval)
888{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000889 Token **tlineptr = private_data;
890 Token *tline;
891
892 do {
893 tline = *tlineptr;
894 *tlineptr = tline ? tline->next : NULL;
895 } while (tline && (tline->type == TOK_WHITESPACE ||
896 tline->type == TOK_COMMENT));
897
898 if (!tline)
899 return tokval->t_type = TOKEN_EOS;
900
901 if (tline->text[0] == '$' && !tline->text[1])
902 return tokval->t_type = TOKEN_HERE;
903 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[1])
904 return tokval->t_type = TOKEN_BASE;
905
906 if (tline->type == TOK_ID) {
907 tokval->t_charptr = tline->text;
908 if (tline->text[0] == '$') {
909 tokval->t_charptr++;
910 return tokval->t_type = TOKEN_ID;
911 }
912
913 /*
914 * This is the only special case we actually need to worry
915 * about in this restricted context.
916 */
917 if (!nasm_stricmp(tline->text, "seg"))
918 return tokval->t_type = TOKEN_SEG;
919
920 return tokval->t_type = TOKEN_ID;
921 }
922
923 if (tline->type == TOK_NUMBER) {
924 int rn_error;
925
926 tokval->t_integer = readnum(tline->text, &rn_error);
927 if (rn_error)
928 return tokval->t_type = TOKEN_ERRNUM;
929 tokval->t_charptr = NULL;
930 return tokval->t_type = TOKEN_NUM;
931 }
932
H. Peter Anvineba20a72002-04-30 20:53:55 +0000933 if (tline->type == TOK_STRING) {
934 int rn_warn;
935 char q, *r;
936 int l;
937
938 r = tline->text;
939 q = *r++;
940 l = strlen(r);
941
942 if (l == 0 || r[l-1] != q)
943 return tokval->t_type = TOKEN_ERRNUM;
944 tokval->t_integer = readstrnum(r, l-1, &rn_warn);
945 if (rn_warn)
946 error(ERR_WARNING|ERR_PASS1,
947 "character constant too long");
948 tokval->t_charptr = NULL;
949 return tokval->t_type = TOKEN_NUM;
950 }
951
H. Peter Anvin76690a12002-04-30 20:52:49 +0000952 if (tline->type == TOK_OTHER) {
953 if (!strcmp(tline->text, "<<")) return tokval->t_type = TOKEN_SHL;
954 if (!strcmp(tline->text, ">>")) return tokval->t_type = TOKEN_SHR;
955 if (!strcmp(tline->text, "//")) return tokval->t_type = TOKEN_SDIV;
956 if (!strcmp(tline->text, "%%")) return tokval->t_type = TOKEN_SMOD;
957 if (!strcmp(tline->text, "==")) return tokval->t_type = TOKEN_EQ;
958 if (!strcmp(tline->text, "<>")) return tokval->t_type = TOKEN_NE;
959 if (!strcmp(tline->text, "!=")) return tokval->t_type = TOKEN_NE;
960 if (!strcmp(tline->text, "<=")) return tokval->t_type = TOKEN_LE;
961 if (!strcmp(tline->text, ">=")) return tokval->t_type = TOKEN_GE;
962 if (!strcmp(tline->text, "&&")) return tokval->t_type = TOKEN_DBL_AND;
963 if (!strcmp(tline->text, "^^")) return tokval->t_type = TOKEN_DBL_XOR;
964 if (!strcmp(tline->text, "||")) return tokval->t_type = TOKEN_DBL_OR;
965 }
966
967 /*
968 * We have no other options: just return the first character of
969 * the token text.
970 */
971 return tokval->t_type = tline->text[0];
972}
973
974/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000975 * Compare a string to the name of an existing macro; this is a
976 * simple wrapper which calls either strcmp or nasm_stricmp
977 * depending on the value of the `casesense' parameter.
978 */
979static int mstrcmp(char *p, char *q, int casesense)
980{
981 return casesense ? strcmp(p,q) : nasm_stricmp(p,q);
982}
983
984/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000985 * Return the Context structure associated with a %$ token. Return
986 * NULL, having _already_ reported an error condition, if the
987 * context stack isn't deep enough for the supplied number of $
988 * signs.
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000989 * If all_contexts == TRUE, contexts that enclose current are
990 * also scanned for such smacro, until it is found; if not -
991 * only the context that directly results from the number of $'s
992 * in variable's name.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000993 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000994static Context *get_ctx (char *name, int all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000995{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000996 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000997 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000998 int i;
999
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001000 if (!name || name[0] != '%' || name[1] != '$')
1001 return NULL;
1002
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001003 if (!cstk) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00001004 error (ERR_NONFATAL, "`%s': context stack is empty", name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001005 return NULL;
1006 }
1007
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001008 for (i = strspn (name+2, "$"), ctx = cstk; (i > 0) && ctx; i--) {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001009 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001010 i--;
1011 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001012 if (!ctx) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00001013 error (ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001014 " %d level%s deep", name, i-1, (i==2 ? "" : "s"));
1015 return NULL;
1016 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001017 if (!all_contexts)
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001018 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001019
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001020 do {
1021 /* Search for this smacro in found context */
1022 m = ctx->localmac;
1023 while (m) {
1024 if (!mstrcmp(m->name, name, m->casesense))
1025 return ctx;
1026 m = m->next;
1027 }
1028 ctx = ctx->next;
1029 } while (ctx);
1030 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001031}
1032
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001033#ifdef TASM_COMPAT
1034/* Add a slash to the end of a path if it is missing. We use the
1035 * forward slash to make it compatible with Unix systems.
1036 */
1037static void backslash(char *s)
1038{
1039 int pos = strlen(s);
1040 if (s[pos-1] != '\\' && s[pos-1] != '/') {
1041 s[pos] = '/';
1042 s[pos+1] = '\0';
1043 }
1044}
1045#endif
1046
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001047/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001048 * Open an include file. This routine must always return a valid
1049 * file pointer if it returns - it's responsible for throwing an
1050 * ERR_FATAL and bombing out completely if not. It should also try
1051 * the include path one by one until it finds the file or reaches
1052 * the end of the path.
1053 */
H. Peter Anvineba20a72002-04-30 20:53:55 +00001054static FILE *inc_fopen(char *file)
1055{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001056 FILE *fp;
1057 char *prefix = "", *combine;
1058 IncPath *ip = ipath;
H. Peter Anvin620515a2002-04-30 20:57:38 +00001059 static int namelen = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001060#ifdef TASM_COMPAT
1061 int len = strlen(file);
1062#endif
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001063
H. Peter Anvineba20a72002-04-30 20:53:55 +00001064 while (1) {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001065#ifdef TASM_COMPAT
1066 combine = nasm_malloc(strlen(prefix)+1+len+1);
1067 strcpy(combine, prefix);
1068 if (prefix[0] != 0)
1069 backslash(combine);
1070 strcat(combine, file);
1071#else
H. Peter Anvineba20a72002-04-30 20:53:55 +00001072 combine = nasm_strcat(prefix,file);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001073#endif
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001074 fp = fopen(combine, "r");
H. Peter Anvin620515a2002-04-30 20:57:38 +00001075 if (pass == 0 && fp)
1076 {
1077 namelen += strlen(combine) + 1;
1078 if (namelen > 62)
1079 {
1080 printf(" \\\n ");
1081 namelen = 2;
1082 }
1083 printf(" %s", combine);
1084 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001085 nasm_free (combine);
1086 if (fp)
1087 return fp;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001088 if (!ip)
1089 break;
1090 prefix = ip->path;
1091 ip = ip->next;
1092 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001093
H. Peter Anvineba20a72002-04-30 20:53:55 +00001094 error (ERR_FATAL,
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001095 "unable to open include file `%s'", file);
1096 return NULL; /* never reached - placate compilers */
1097}
1098
1099/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001100 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001101 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001102 * return TRUE if _any_ single-line macro of that name is defined.
1103 * Otherwise, will return TRUE if a single-line macro with either
1104 * `nparam' or no parameters is defined.
1105 *
1106 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001107 * defined, or nparam is -1, the address of the definition structure
1108 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001109 * is NULL, no action will be taken regarding its contents, and no
1110 * error will occur.
1111 *
1112 * Note that this is also called with nparam zero to resolve
1113 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001114 *
1115 * If you already know which context macro belongs to, you can pass
1116 * the context pointer as first parameter; if you won't but name begins
1117 * with %$ the context will be automatically computed. If all_contexts
1118 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001119 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001120static int smacro_defined (Context *ctx, char *name, int nparam, SMacro **defn,
1121 int nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001122{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001123 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001124
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001125 if (ctx)
1126 m = ctx->localmac;
1127 else if (name[0] == '%' && name[1] == '$') {
1128 if (cstk)
1129 ctx = get_ctx (name, FALSE);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001130 if (!ctx)
1131 return FALSE; /* got to return _something_ */
1132 m = ctx->localmac;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001133 } else
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001134 m = smacros[hash(name)];
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001135
1136 while (m) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001137 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001138 (nparam <= 0 || m->nparam == 0 || nparam == m->nparam)) {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001139 if (defn) {
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001140 if (nparam == m->nparam || nparam == -1)
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001141 *defn = m;
1142 else
1143 *defn = NULL;
1144 }
1145 return TRUE;
1146 }
1147 m = m->next;
1148 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001149
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001150 return FALSE;
1151}
1152
1153/*
1154 * Count and mark off the parameters in a multi-line macro call.
1155 * This is called both from within the multi-line macro expansion
1156 * code, and also to mark off the default parameters when provided
1157 * in a %macro definition line.
1158 */
H. Peter Anvineba20a72002-04-30 20:53:55 +00001159static void count_mmac_params (Token *t, int *nparam, Token ***params)
1160{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001161 int paramsize, brace;
1162
1163 *nparam = paramsize = 0;
1164 *params = NULL;
1165 while (t) {
1166 if (*nparam >= paramsize) {
1167 paramsize += PARAM_DELTA;
1168 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1169 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001170 skip_white_(t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001171 brace = FALSE;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001172 if (tok_is_(t, "{"))
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001173 brace = TRUE;
1174 (*params)[(*nparam)++] = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001175 while (tok_isnt_(t, brace ? "}" : ","))
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001176 t = t->next;
1177 if (t) { /* got a comma/brace */
1178 t = t->next;
1179 if (brace) {
1180 /*
1181 * Now we've found the closing brace, look further
1182 * for the comma.
1183 */
H. Peter Anvineba20a72002-04-30 20:53:55 +00001184 skip_white_(t);
1185 if (tok_isnt_(t, ",")) {
1186 error (ERR_NONFATAL,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001187 "braces do not enclose all of macro parameter");
H. Peter Anvineba20a72002-04-30 20:53:55 +00001188 while (tok_isnt_(t, ","))
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001189 t = t->next;
1190 }
1191 if (t)
1192 t = t->next; /* eat the comma */
1193 }
1194 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001195 }
1196}
1197
1198/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001199 * Determine whether one of the various `if' conditions is true or
1200 * not.
1201 *
1202 * We must free the tline we get passed.
1203 */
H. Peter Anvineba20a72002-04-30 20:53:55 +00001204static int if_condition (Token *tline, int i)
1205{
1206 int j, casesense;
1207 Token * t, * tt, ** tptr, * origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001208 struct tokenval tokval;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001209 expr * evalresult;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001210
1211 origline = tline;
1212
1213 switch (i) {
1214 case PP_IFCTX: case PP_ELIFCTX:
1215 case PP_IFNCTX: case PP_ELIFNCTX:
1216 j = FALSE; /* have we matched yet? */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001217 while (cstk && tline) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00001218 skip_white_(tline);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001219 if (!tline || tline->type != TOK_ID) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00001220 error(ERR_NONFATAL,
H. Peter Anvin76690a12002-04-30 20:52:49 +00001221 "`%s' expects context identifiers", directives[i]);
1222 free_tlist (origline);
1223 return -1;
1224 }
1225 if (!nasm_stricmp(tline->text, cstk->name))
1226 j = TRUE;
1227 tline = tline->next;
1228 }
1229 if (i == PP_IFNCTX || i == PP_ELIFNCTX)
1230 j = !j;
1231 free_tlist (origline);
1232 return j;
1233
1234 case PP_IFDEF: case PP_ELIFDEF:
1235 case PP_IFNDEF: case PP_ELIFNDEF:
1236 j = FALSE; /* have we matched yet? */
1237 while (tline) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00001238 skip_white_(tline);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001239 if (!tline || (tline->type != TOK_ID &&
1240 (tline->type != TOK_PREPROC_ID ||
1241 tline->text[1] != '$'))) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00001242 error(ERR_NONFATAL,
H. Peter Anvin76690a12002-04-30 20:52:49 +00001243 "`%%if%sdef' expects macro identifiers",
1244 (i==PP_ELIFNDEF ? "n" : ""));
1245 free_tlist (origline);
1246 return -1;
1247 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001248 if (smacro_defined (NULL, tline->text, 0, NULL, 1))
H. Peter Anvin76690a12002-04-30 20:52:49 +00001249 j = TRUE;
1250 tline = tline->next;
1251 }
1252 if (i == PP_IFNDEF || i == PP_ELIFNDEF)
1253 j = !j;
1254 free_tlist (origline);
1255 return j;
1256
1257 case PP_IFIDN: case PP_ELIFIDN: case PP_IFNIDN: case PP_ELIFNIDN:
1258 case PP_IFIDNI: case PP_ELIFIDNI: case PP_IFNIDNI: case PP_ELIFNIDNI:
1259 tline = expand_smacro(tline);
1260 t = tt = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001261 while (tok_isnt_(tt, ","))
H. Peter Anvin76690a12002-04-30 20:52:49 +00001262 tt = tt->next;
1263 if (!tt) {
H. Peter Anvin620515a2002-04-30 20:57:38 +00001264 error(ERR_NONFATAL, "`%s' expects two comma-separated arguments",
1265 directives[i]);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001266 free_tlist (tline);
1267 return -1;
1268 }
1269 tt = tt->next;
1270 casesense = (i == PP_IFIDN || i == PP_ELIFIDN ||
1271 i == PP_IFNIDN || i == PP_ELIFNIDN);
1272 j = TRUE; /* assume equality unless proved not */
1273 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1274 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1275 error(ERR_NONFATAL, "`%s': more than one comma on line",
1276 directives[i]);
1277 free_tlist (tline);
1278 return -1;
1279 }
1280 if (t->type == TOK_WHITESPACE) {
1281 t = t->next;
1282 continue;
1283 } else if (tt->type == TOK_WHITESPACE) {
1284 tt = tt->next;
1285 continue;
1286 } else if (tt->type != t->type ||
1287 (casesense ? strcmp(tt->text, t->text) :
1288 nasm_stricmp(tt->text, t->text))) {
1289 j = FALSE; /* found mismatching tokens */
1290 break;
1291 } else {
1292 t = t->next;
1293 tt = tt->next;
1294 continue;
1295 }
1296 }
1297 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
1298 j = FALSE; /* trailing gunk on one end or other */
H. Peter Anvineba20a72002-04-30 20:53:55 +00001299 if (i == PP_IFNIDN || i == PP_ELIFNIDN ||
1300 i == PP_IFNIDNI || i == PP_ELIFNIDNI)
H. Peter Anvin76690a12002-04-30 20:52:49 +00001301 j = !j;
1302 free_tlist (tline);
1303 return j;
1304
1305 case PP_IFID: case PP_ELIFID: case PP_IFNID: case PP_ELIFNID:
1306 case PP_IFNUM: case PP_ELIFNUM: case PP_IFNNUM: case PP_ELIFNNUM:
1307 case PP_IFSTR: case PP_ELIFSTR: case PP_IFNSTR: case PP_ELIFNSTR:
1308 tline = expand_smacro(tline);
1309 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001310 while (tok_type_(t, TOK_WHITESPACE))
H. Peter Anvin76690a12002-04-30 20:52:49 +00001311 t = t->next;
1312 j = FALSE; /* placate optimiser */
H. Peter Anvineba20a72002-04-30 20:53:55 +00001313 if (t) switch (i) {
H. Peter Anvin76690a12002-04-30 20:52:49 +00001314 case PP_IFID: case PP_ELIFID: case PP_IFNID: case PP_ELIFNID:
1315 j = (t->type == TOK_ID);
1316 break;
1317 case PP_IFNUM: case PP_ELIFNUM: case PP_IFNNUM: case PP_ELIFNNUM:
1318 j = (t->type == TOK_NUMBER);
1319 break;
1320 case PP_IFSTR: case PP_ELIFSTR: case PP_IFNSTR: case PP_ELIFNSTR:
1321 j = (t->type == TOK_STRING);
1322 break;
1323 }
1324 if (i == PP_IFNID || i == PP_ELIFNID ||
1325 i == PP_IFNNUM || i == PP_ELIFNNUM ||
1326 i == PP_IFNSTR || i == PP_ELIFNSTR)
1327 j = !j;
1328 free_tlist (tline);
1329 return j;
1330
1331 case PP_IF: case PP_ELIF:
1332 t = tline = expand_smacro(tline);
1333 tptr = &t;
1334 tokval.t_type = TOKEN_INVALID;
1335 evalresult = evaluate (ppscan, tptr, &tokval,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001336 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001337 free_tlist (tline);
1338 if (!evalresult)
1339 return -1;
1340 if (tokval.t_type)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001341 error(ERR_WARNING,
H. Peter Anvin76690a12002-04-30 20:52:49 +00001342 "trailing garbage after expression ignored");
1343 if (!is_simple(evalresult)) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00001344 error(ERR_NONFATAL,
H. Peter Anvin76690a12002-04-30 20:52:49 +00001345 "non-constant value given to `%s'", directives[i]);
1346 return -1;
1347 }
1348 return reloc_value(evalresult) != 0;
1349
1350 default:
H. Peter Anvineba20a72002-04-30 20:53:55 +00001351 error(ERR_FATAL,
H. Peter Anvin76690a12002-04-30 20:52:49 +00001352 "preprocessor directive `%s' not yet implemented",
1353 directives[i]);
1354 free_tlist (origline);
1355 return -1; /* yeah, right */
1356 }
1357}
1358
1359/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001360 * Expand macros in a string. Used in %error and %include directives.
1361 * First tokenise the string, apply "expand_smacro" and then de-tokenise back.
1362 * The returned variable should ALWAYS be freed after usage.
1363 */
1364void expand_macros_in_string (char **p)
1365{
1366 Token *line = tokenise (*p);
1367 line = expand_smacro (line);
1368 *p = detoken (line, FALSE);
1369}
1370
1371/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001372 * Find out if a line contains a preprocessor directive, and deal
1373 * with it if so.
1374 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00001375 * If a directive _is_ found, we are expected to free_tlist() the
1376 * line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001377 *
1378 * Return values go like this:
1379 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00001380 * bit 0 is set if a directive was found (so the line gets freed)
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001381 */
H. Peter Anvineba20a72002-04-30 20:53:55 +00001382static int do_directive (Token *tline)
1383{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001384 int i, j, k, m, nparam, nolist;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001385#ifdef TASM_COMPAT
1386 int offset;
1387#endif
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001388 char *p, *mname;
1389 Include *inc;
1390 Context *ctx;
1391 Cond *cond;
1392 SMacro *smac, **smhead;
1393 MMacro *mmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001394 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1395 Line *l;
1396 struct tokenval tokval;
1397 expr *evalresult;
H. Peter Anvinb64535f2002-04-30 20:55:37 +00001398 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001399
1400 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001401
H. Peter Anvineba20a72002-04-30 20:53:55 +00001402 skip_white_(tline);
1403 if (!tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001404 (tline->text[1]=='%' || tline->text[1]=='$' || tline->text[1]=='!'))
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001405 return 0;
1406
1407 i = -1;
1408 j = sizeof(directives)/sizeof(*directives);
1409 while (j-i > 1) {
1410 k = (j+i) / 2;
1411 m = nasm_stricmp(tline->text, directives[k]);
1412 if (m == 0) {
1413 i = k;
1414 j = -2;
1415 break;
1416 } else if (m < 0) {
1417 j = k;
1418 } else
1419 i = k;
1420 }
1421
1422 /*
1423 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00001424 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001425 * we should ignore all directives except for condition
1426 * directives.
1427 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001428 if (((istk->conds && !emitting(istk->conds->state)) ||
1429 (istk->mstk && !istk->mstk->in_progress)) &&
1430 i != PP_IF && i != PP_ELIF &&
1431 i != PP_IFCTX && i != PP_ELIFCTX &&
1432 i != PP_IFDEF && i != PP_ELIFDEF &&
1433 i != PP_IFID && i != PP_ELIFID &&
1434 i != PP_IFIDN && i != PP_ELIFIDN &&
1435 i != PP_IFIDNI && i != PP_ELIFIDNI &&
1436 i != PP_IFNCTX && i != PP_ELIFNCTX &&
1437 i != PP_IFNDEF && i != PP_ELIFNDEF &&
1438 i != PP_IFNID && i != PP_ELIFNID &&
1439 i != PP_IFNIDN && i != PP_ELIFNIDN &&
1440 i != PP_IFNIDNI && i != PP_ELIFNIDNI &&
1441 i != PP_IFNNUM && i != PP_ELIFNNUM &&
1442 i != PP_IFNSTR && i != PP_ELIFNSTR &&
1443 i != PP_IFNUM && i != PP_ELIFNUM &&
1444 i != PP_IFSTR && i != PP_ELIFSTR &&
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001445 i != PP_ELSE && i != PP_ENDIF)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001446 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001447 return 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001448 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001449
1450 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001451 * If we're defining a macro or reading a %rep block, we should
1452 * ignore all directives except for %macro/%imacro (which
1453 * generate an error), %endm/%endmacro, and (only if we're in a
H. Peter Anvineba20a72002-04-30 20:53:55 +00001454 * %rep block) %endrep. If we're in a %rep block, another %rep
1455 * causes an error, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001456 */
1457 if (defining && i != PP_MACRO && i != PP_IMACRO &&
H. Peter Anvin76690a12002-04-30 20:52:49 +00001458 i != PP_ENDMACRO && i != PP_ENDM &&
H. Peter Anvineba20a72002-04-30 20:53:55 +00001459 (defining->name || (i != PP_ENDREP && i != PP_REP)))
1460 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001461 return 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001462 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001463
1464 if (j != -2) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00001465 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001466 tline->text);
1467 return 0; /* didn't get it */
1468 }
1469
1470 switch (i) {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001471#ifdef TASM_COMPAT
1472 case PP_STACKSIZE:
1473 /* Directive to tell NASM what the default stack size is. The
1474 * default is for a 16-bit stack, and this can be overriden with
1475 * %stacksize large.
1476 * the following form:
1477 *
1478 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1479 */
1480 tline = tline->next;
1481 if (tline && tline->type == TOK_WHITESPACE)
1482 tline = tline->next;
1483 if (!tline || tline->type != TOK_ID) {
1484 error (ERR_NONFATAL,"`%%stacksize' missing size parameter");
1485 free_tlist (origline);
1486 return 3;
1487 }
1488 if (nasm_stricmp(tline->text,"flat") == 0) {
1489 /* All subsequent ARG directives are for a 32-bit stack */
1490 StackSize = 4;
1491 StackPointer = "ebp";
1492 ArgOffset = 8;
1493 LocalOffset = 4;
1494 } else if (nasm_stricmp(tline->text,"large") == 0) {
1495 /* All subsequent ARG directives are for a 16-bit stack,
1496 * far function call.
1497 */
1498 StackSize = 2;
1499 StackPointer = "bp";
1500 ArgOffset = 4;
1501 LocalOffset = 2;
1502 } else if (nasm_stricmp(tline->text,"small") == 0) {
1503 /* All subsequent ARG directives are for a 16-bit stack,
1504 * far function call. We don't support near functions.
1505 */
1506 StackSize = 2;
1507 StackPointer = "bp";
1508 ArgOffset = 6;
1509 LocalOffset = 2;
1510 } else {
1511 error (ERR_NONFATAL,"`%%stacksize' invalid size type");
1512 free_tlist (origline);
1513 return 3;
1514 }
1515 free_tlist(origline);
1516 return 3;
1517
1518 case PP_ARG:
1519 /* TASM like ARG directive to define arguments to functions, in
1520 * the following form:
1521 *
1522 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1523 */
1524 offset = ArgOffset;
1525 do {
1526 char *arg,directive[256];
1527 int size = StackSize;
1528
1529 /* Find the argument name */
1530 tline = tline->next;
1531 if (tline && tline->type == TOK_WHITESPACE)
1532 tline = tline->next;
1533 if (!tline || tline->type != TOK_ID) {
1534 error (ERR_NONFATAL,"`%%arg' missing argument parameter");
1535 free_tlist (origline);
1536 return 3;
1537 }
1538 arg = tline->text;
1539
1540 /* Find the argument size type */
1541 tline = tline->next;
1542 if (!tline || tline->type != TOK_OTHER || tline->text[0] != ':') {
1543 error (ERR_NONFATAL,"Syntax error processing `%%arg' directive");
1544 free_tlist (origline);
1545 return 3;
1546 }
1547 tline = tline->next;
1548 if (!tline || tline->type != TOK_ID) {
1549 error (ERR_NONFATAL,"`%%arg' missing size type parameter");
1550 free_tlist (origline);
1551 return 3;
1552 }
1553
1554 /* Allow macro expansion of type parameter */
1555 tt = tokenise(tline->text);
1556 tt = expand_smacro(tt);
1557 if (nasm_stricmp(tt->text,"byte") == 0) {
1558 size = MAX(StackSize,1);
1559 } else if (nasm_stricmp(tt->text,"word") == 0) {
1560 size = MAX(StackSize,2);
1561 } else if (nasm_stricmp(tt->text,"dword") == 0) {
1562 size = MAX(StackSize,4);
1563 } else if (nasm_stricmp(tt->text,"qword") == 0) {
1564 size = MAX(StackSize,8);
1565 } else if (nasm_stricmp(tt->text,"tword") == 0) {
1566 size = MAX(StackSize,10);
1567 } else {
1568 error (ERR_NONFATAL,"Invalid size type for `%%arg' missing directive");
1569 free_tlist (tt);
1570 free_tlist (origline);
1571 return 3;
1572 }
1573 free_tlist (tt);
1574
1575 /* Now define the macro for the argument */
1576 sprintf(directive,"%%define %s (%s+%d)", arg, StackPointer, offset);
1577 do_directive(tokenise(directive));
1578 offset += size;
1579
1580 /* Move to the next argument in the list */
1581 tline = tline->next;
1582 if (tline && tline->type == TOK_WHITESPACE)
1583 tline = tline->next;
1584 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1585 free_tlist (origline);
1586 return 3;
1587
1588 case PP_LOCAL:
1589 /* TASM like LOCAL directive to define local variables for a
1590 * function, in the following form:
1591 *
1592 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
1593 *
1594 * The '= LocalSize' at the end is ignored by NASM, but is
1595 * required by TASM to define the local parameter size (and used
1596 * by the TASM macro package).
1597 */
1598 offset = LocalOffset;
1599 do {
1600 char *local,directive[256];
1601 int size = StackSize;
1602
1603 /* Find the argument name */
1604 tline = tline->next;
1605 if (tline && tline->type == TOK_WHITESPACE)
1606 tline = tline->next;
1607 if (!tline || tline->type != TOK_ID) {
1608 error (ERR_NONFATAL,"`%%local' missing argument parameter");
1609 free_tlist (origline);
1610 return 3;
1611 }
1612 local = tline->text;
1613
1614 /* Find the argument size type */
1615 tline = tline->next;
1616 if (!tline || tline->type != TOK_OTHER || tline->text[0] != ':') {
1617 error (ERR_NONFATAL,"Syntax error processing `%%local' directive");
1618 free_tlist (origline);
1619 return 3;
1620 }
1621 tline = tline->next;
1622 if (!tline || tline->type != TOK_ID) {
1623 error (ERR_NONFATAL,"`%%local' missing size type parameter");
1624 free_tlist (origline);
1625 return 3;
1626 }
1627
1628 /* Allow macro expansion of type parameter */
1629 tt = tokenise(tline->text);
1630 tt = expand_smacro(tt);
1631 if (nasm_stricmp(tt->text,"byte") == 0) {
1632 size = MAX(StackSize,1);
1633 } else if (nasm_stricmp(tt->text,"word") == 0) {
1634 size = MAX(StackSize,2);
1635 } else if (nasm_stricmp(tt->text,"dword") == 0) {
1636 size = MAX(StackSize,4);
1637 } else if (nasm_stricmp(tt->text,"qword") == 0) {
1638 size = MAX(StackSize,8);
1639 } else if (nasm_stricmp(tt->text,"tword") == 0) {
1640 size = MAX(StackSize,10);
1641 } else {
1642 error (ERR_NONFATAL,"Invalid size type for `%%local' missing directive");
1643 free_tlist (tt);
1644 free_tlist (origline);
1645 return 3;
1646 }
1647 free_tlist (tt);
1648
1649 /* Now define the macro for the argument */
1650 sprintf(directive,"%%define %s (%s-%d)", local, StackPointer, offset);
1651 do_directive(tokenise(directive));
1652 offset += size;
1653
1654 /* Now define the assign to setup the enter_c macro correctly */
1655 sprintf(directive,"%%assign %%$localsize %%$localsize+%d", size);
1656 do_directive(tokenise(directive));
1657
1658 /* Move to the next argument in the list */
1659 tline = tline->next;
1660 if (tline && tline->type == TOK_WHITESPACE)
1661 tline = tline->next;
1662 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1663 free_tlist (origline);
1664 return 3;
1665#endif
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001666
1667 case PP_CLEAR:
1668 if (tline->next)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001669 error(ERR_WARNING,
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001670 "trailing garbage after `%%clear' ignored");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001671 for (j=0; j<NHASH; j++) {
1672 while (mmacros[j]) {
1673 MMacro *m = mmacros[j];
H. Peter Anvineba20a72002-04-30 20:53:55 +00001674 mmacros[j] = m->next;
1675 free_mmacro(m);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001676 }
1677 while (smacros[j]) {
1678 SMacro *s = smacros[j];
1679 smacros[j] = smacros[j]->next;
1680 nasm_free (s->name);
1681 free_tlist (s->expansion);
1682 nasm_free (s);
1683 }
1684 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001685 free_tlist (origline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001686 return 3;
1687
1688 case PP_INCLUDE:
1689 tline = tline->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001690 skip_white_(tline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001691 if (!tline || (tline->type != TOK_STRING &&
H. Peter Anvineba20a72002-04-30 20:53:55 +00001692 tline->type != TOK_INTERNAL_STRING))
1693 {
1694 error(ERR_NONFATAL, "`%%include' expects a file name");
H. Peter Anvin76690a12002-04-30 20:52:49 +00001695 free_tlist (origline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001696 return 3; /* but we did _something_ */
1697 }
1698 if (tline->next)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001699 error(ERR_WARNING,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001700 "trailing garbage after `%%include' ignored");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001701 if (tline->type != TOK_INTERNAL_STRING) {
1702 p = tline->text+1; /* point past the quote to the name */
1703 p[strlen(p)-1] = '\0'; /* remove the trailing quote */
1704 } else
1705 p = tline->text; /* internal_string is easier */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001706 expand_macros_in_string (&p);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001707 inc = nasm_malloc(sizeof(Include));
1708 inc->next = istk;
1709 inc->conds = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001710 inc->fp = inc_fopen(p);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001711 inc->fname = src_set_fname (p);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001712 inc->lineno = src_set_linnum(0);
1713 inc->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001714 inc->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001715 inc->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001716 istk = inc;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001717 list->uplevel (LIST_INCLUDE);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001718 free_tlist (origline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001719 return 5;
1720
1721 case PP_PUSH:
1722 tline = tline->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001723 skip_white_(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001724 tline = expand_id (tline);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001725 if (!tok_type_(tline, TOK_ID)) {
1726 error(ERR_NONFATAL,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001727 "`%%push' expects a context identifier");
H. Peter Anvin76690a12002-04-30 20:52:49 +00001728 free_tlist (origline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001729 return 3; /* but we did _something_ */
1730 }
1731 if (tline->next)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001732 error(ERR_WARNING,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001733 "trailing garbage after `%%push' ignored");
1734 ctx = nasm_malloc(sizeof(Context));
1735 ctx->next = cstk;
1736 ctx->localmac = NULL;
1737 ctx->name = nasm_strdup(tline->text);
1738 ctx->number = unique++;
1739 cstk = ctx;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001740 free_tlist (origline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001741 break;
1742
1743 case PP_REPL:
1744 tline = tline->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001745 skip_white_(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001746 tline = expand_id (tline);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001747 if (!tok_type_(tline, TOK_ID)) {
1748 error(ERR_NONFATAL,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001749 "`%%repl' expects a context identifier");
H. Peter Anvin76690a12002-04-30 20:52:49 +00001750 free_tlist (origline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001751 return 3; /* but we did _something_ */
1752 }
1753 if (tline->next)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001754 error(ERR_WARNING,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001755 "trailing garbage after `%%repl' ignored");
1756 if (!cstk)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001757 error(ERR_NONFATAL,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001758 "`%%repl': context stack is empty");
1759 else {
1760 nasm_free (cstk->name);
1761 cstk->name = nasm_strdup(tline->text);
1762 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001763 free_tlist (origline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001764 break;
1765
1766 case PP_POP:
1767 if (tline->next)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001768 error(ERR_WARNING,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001769 "trailing garbage after `%%pop' ignored");
1770 if (!cstk)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001771 error(ERR_NONFATAL,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001772 "`%%pop': context stack is already empty");
1773 else
1774 ctx_pop();
H. Peter Anvin76690a12002-04-30 20:52:49 +00001775 free_tlist (origline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001776 break;
1777
1778 case PP_ERROR:
H. Peter Anvineba20a72002-04-30 20:53:55 +00001779 tline->next = expand_smacro (tline->next);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001780 tline = tline->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001781 skip_white_(tline);
1782 if (tok_type_(tline, TOK_STRING)) {
1783 p = tline->text+1; /* point past the quote to the name */
1784 p[strlen(p)-1] = '\0'; /* remove the trailing quote */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001785 expand_macros_in_string (&p);
1786 error (ERR_NONFATAL, "%s", p);
1787 nasm_free (p);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001788 } else {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001789 p = detoken(tline, FALSE);
1790 error (ERR_WARNING, "%s", p);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001791 nasm_free(p);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001792 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001793 free_tlist (origline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001794 break;
1795
H. Peter Anvin76690a12002-04-30 20:52:49 +00001796 case PP_IF:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001797 case PP_IFCTX:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001798 case PP_IFDEF:
H. Peter Anvin76690a12002-04-30 20:52:49 +00001799 case PP_IFID:
1800 case PP_IFIDN:
1801 case PP_IFIDNI:
1802 case PP_IFNCTX:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001803 case PP_IFNDEF:
H. Peter Anvin76690a12002-04-30 20:52:49 +00001804 case PP_IFNID:
1805 case PP_IFNIDN:
1806 case PP_IFNIDNI:
1807 case PP_IFNNUM:
1808 case PP_IFNSTR:
1809 case PP_IFNUM:
1810 case PP_IFSTR:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001811 if (istk->conds && !emitting(istk->conds->state))
1812 j = COND_NEVER;
1813 else {
H. Peter Anvin76690a12002-04-30 20:52:49 +00001814 j = if_condition(tline->next, i);
1815 tline->next = NULL; /* it got freed */
1816 free_tlist (origline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001817 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001818 }
1819 cond = nasm_malloc(sizeof(Cond));
1820 cond->next = istk->conds;
1821 cond->state = j;
1822 istk->conds = cond;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001823 return (j == COND_IF_TRUE ? 3 : 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001824
H. Peter Anvin76690a12002-04-30 20:52:49 +00001825 case PP_ELIF:
1826 case PP_ELIFCTX:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001827 case PP_ELIFDEF:
H. Peter Anvin76690a12002-04-30 20:52:49 +00001828 case PP_ELIFID:
1829 case PP_ELIFIDN:
1830 case PP_ELIFIDNI:
1831 case PP_ELIFNCTX:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001832 case PP_ELIFNDEF:
H. Peter Anvin76690a12002-04-30 20:52:49 +00001833 case PP_ELIFNID:
1834 case PP_ELIFNIDN:
1835 case PP_ELIFNIDNI:
1836 case PP_ELIFNNUM:
1837 case PP_ELIFNSTR:
1838 case PP_ELIFNUM:
1839 case PP_ELIFSTR:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001840 if (!istk->conds)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001841 error(ERR_FATAL, "`%s': no matching `%%if'",
H. Peter Anvin76690a12002-04-30 20:52:49 +00001842 directives[i]);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001843 if (emitting(istk->conds->state) || istk->conds->state == COND_NEVER)
1844 istk->conds->state = COND_NEVER;
1845 else {
H. Peter Anvin4836e332002-04-30 20:56:43 +00001846 j = if_condition(expand_mmac_params(tline->next), i);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001847 tline->next = NULL; /* it got freed */
1848 free_tlist (origline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001849 istk->conds->state = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001850 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001851 return (istk->conds->state == COND_IF_TRUE ? 5 : 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001852
1853 case PP_ELSE:
1854 if (tline->next)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001855 error(ERR_WARNING,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001856 "trailing garbage after `%%else' ignored");
1857 if (!istk->conds)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001858 error(ERR_FATAL,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001859 "`%%else': no matching `%%if'");
1860 if (emitting(istk->conds->state) || istk->conds->state == COND_NEVER)
1861 istk->conds->state = COND_ELSE_FALSE;
1862 else
1863 istk->conds->state = COND_ELSE_TRUE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001864 free_tlist (origline);
1865 return 5;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001866
1867 case PP_ENDIF:
1868 if (tline->next)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001869 error(ERR_WARNING,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001870 "trailing garbage after `%%endif' ignored");
1871 if (!istk->conds)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001872 error(ERR_FATAL,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001873 "`%%endif': no matching `%%if'");
1874 cond = istk->conds;
1875 istk->conds = cond->next;
1876 nasm_free (cond);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001877 free_tlist (origline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001878 return 5;
1879
1880 case PP_MACRO:
1881 case PP_IMACRO:
1882 if (defining)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001883 error (ERR_FATAL,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001884 "`%%%smacro': already defining a macro",
1885 (i == PP_IMACRO ? "i" : ""));
1886 tline = tline->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001887 skip_white_(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001888 tline = expand_id (tline);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001889 if (!tok_type_(tline, TOK_ID)) {
1890 error (ERR_NONFATAL,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001891 "`%%%smacro' expects a macro name",
1892 (i == PP_IMACRO ? "i" : ""));
1893 return 3;
1894 }
1895 defining = nasm_malloc(sizeof(MMacro));
1896 defining->name = nasm_strdup(tline->text);
1897 defining->casesense = (i == PP_MACRO);
1898 defining->plus = FALSE;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001899 defining->nolist = FALSE;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001900 defining->in_progress = FALSE;
H. Peter Anvinb64535f2002-04-30 20:55:37 +00001901 defining->rep_nest = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001902 tline = expand_smacro (tline->next);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001903 skip_white_(tline);
1904 if (!tok_type_(tline, TOK_NUMBER)) {
1905 error (ERR_NONFATAL,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001906 "`%%%smacro' expects a parameter count",
1907 (i == PP_IMACRO ? "i" : ""));
1908 defining->nparam_min = defining->nparam_max = 0;
1909 } else {
1910 defining->nparam_min = defining->nparam_max =
1911 readnum(tline->text, &j);
1912 if (j)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001913 error (ERR_NONFATAL,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001914 "unable to parse parameter count `%s'", tline->text);
1915 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001916 if (tline && tok_is_(tline->next, "-")) {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001917 tline = tline->next->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001918 if (tok_is_(tline, "*"))
H. Peter Anvin76690a12002-04-30 20:52:49 +00001919 defining->nparam_max = INT_MAX;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001920 else if (!tok_type_(tline, TOK_NUMBER))
1921 error (ERR_NONFATAL,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001922 "`%%%smacro' expects a parameter count after `-'",
1923 (i == PP_IMACRO ? "i" : ""));
1924 else {
1925 defining->nparam_max = readnum(tline->text, &j);
1926 if (j)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001927 error (ERR_NONFATAL,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001928 "unable to parse parameter count `%s'",
1929 tline->text);
1930 if (defining->nparam_min > defining->nparam_max)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001931 error (ERR_NONFATAL,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001932 "minimum parameter count exceeds maximum");
1933 }
1934 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001935 if (tline && tok_is_(tline->next, "+")) {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001936 tline = tline->next;
1937 defining->plus = TRUE;
1938 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001939 if (tline && tok_type_(tline->next, TOK_ID) &&
1940 !nasm_stricmp(tline->next->text, ".nolist"))
1941 {
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001942 tline = tline->next;
1943 defining->nolist = TRUE;
1944 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001945 mmac = mmacros[hash(defining->name)];
1946 while (mmac) {
1947 if (!strcmp(mmac->name, defining->name) &&
1948 (mmac->nparam_min<=defining->nparam_max || defining->plus) &&
H. Peter Anvineba20a72002-04-30 20:53:55 +00001949 (defining->nparam_min<=mmac->nparam_max || mmac->plus))
1950 {
1951 error (ERR_WARNING,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001952 "redefining multi-line macro `%s'", defining->name);
1953 break;
1954 }
1955 mmac = mmac->next;
1956 }
1957 /*
1958 * Handle default parameters.
1959 */
1960 if (tline && tline->next) {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001961 defining->dlist = tline->next;
1962 tline->next = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001963 count_mmac_params (defining->dlist, &defining->ndefs,
1964 &defining->defaults);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001965 } else {
1966 defining->dlist = NULL;
1967 defining->defaults = NULL;
1968 }
1969 defining->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001970 free_tlist (origline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001971 return 1;
1972
1973 case PP_ENDM:
1974 case PP_ENDMACRO:
1975 if (!defining) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00001976 error (ERR_NONFATAL, "`%s': not defining a macro",
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001977 tline->text);
1978 return 3;
1979 }
1980 k = hash(defining->name);
1981 defining->next = mmacros[k];
1982 mmacros[k] = defining;
1983 defining = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001984 free_tlist (origline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001985 return 5;
1986
H. Peter Anvin76690a12002-04-30 20:52:49 +00001987 case PP_ROTATE:
1988 if (tline->next && tline->next->type == TOK_WHITESPACE)
1989 tline = tline->next;
1990 t = expand_smacro(tline->next);
1991 tline->next = NULL;
1992 free_tlist (origline);
1993 tline = t;
1994 tptr = &t;
1995 tokval.t_type = TOKEN_INVALID;
1996 evalresult = evaluate (ppscan, tptr, &tokval, NULL, pass, error, NULL);
1997 free_tlist (tline);
1998 if (!evalresult)
1999 return 3;
2000 if (tokval.t_type)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002001 error(ERR_WARNING,
H. Peter Anvin76690a12002-04-30 20:52:49 +00002002 "trailing garbage after expression ignored");
2003 if (!is_simple(evalresult)) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00002004 error(ERR_NONFATAL,
H. Peter Anvin76690a12002-04-30 20:52:49 +00002005 "non-constant value given to `%%rotate'");
2006 return 3;
2007 }
2008 mmac = istk->mstk;
2009 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2010 mmac = mmac->next_active;
2011 if (!mmac)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002012 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
H. Peter Anvin76690a12002-04-30 20:52:49 +00002013 mmac->rotate = mmac->rotate + reloc_value(evalresult);
2014 if (mmac->rotate < 0)
2015 mmac->rotate = mmac->nparam - (-mmac->rotate) % mmac->nparam;
2016 mmac->rotate %= mmac->nparam;
2017 return 1;
2018
2019 case PP_REP:
2020 nolist = FALSE;
2021 tline = tline->next;
2022 if (tline->next && tline->next->type == TOK_WHITESPACE)
2023 tline = tline->next;
2024 if (tline->next && tline->next->type == TOK_ID &&
2025 !nasm_stricmp(tline->next->text, ".nolist")) {
2026 tline = tline->next;
2027 nolist = TRUE;
2028 }
2029 t = expand_smacro(tline->next);
2030 tline->next = NULL;
2031 free_tlist (origline);
2032 tline = t;
2033 tptr = &t;
2034 tokval.t_type = TOKEN_INVALID;
2035 evalresult = evaluate (ppscan, tptr, &tokval, NULL, pass, error, NULL);
2036 free_tlist (tline);
2037 if (!evalresult)
2038 return 3;
2039 if (tokval.t_type)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002040 error(ERR_WARNING,
H. Peter Anvin76690a12002-04-30 20:52:49 +00002041 "trailing garbage after expression ignored");
2042 if (!is_simple(evalresult)) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00002043 error(ERR_NONFATAL,
H. Peter Anvin76690a12002-04-30 20:52:49 +00002044 "non-constant value given to `%%rep'");
2045 return 3;
2046 }
H. Peter Anvinb64535f2002-04-30 20:55:37 +00002047 tmp_defining = defining;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002048 defining = nasm_malloc(sizeof(MMacro));
2049 defining->name = NULL; /* flags this macro as a %rep block */
2050 defining->casesense = 0;
2051 defining->plus = FALSE;
2052 defining->nolist = nolist;
2053 defining->in_progress = reloc_value(evalresult) + 1;
2054 defining->nparam_min = defining->nparam_max = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002055 defining->defaults = NULL;
2056 defining->dlist = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002057 defining->expansion = NULL;
2058 defining->next_active = istk->mstk;
H. Peter Anvinb64535f2002-04-30 20:55:37 +00002059 defining->rep_nest = tmp_defining;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002060 return 1;
2061
2062 case PP_ENDREP:
H. Peter Anvineba20a72002-04-30 20:53:55 +00002063 if (!defining || defining->name) {
2064 error (ERR_NONFATAL,
H. Peter Anvin76690a12002-04-30 20:52:49 +00002065 "`%%endrep': no matching `%%rep'");
2066 return 3;
2067 }
2068
2069 /*
2070 * Now we have a "macro" defined - although it has no name
2071 * and we won't be entering it in the hash tables - we must
2072 * push a macro-end marker for it on to istk->expansion.
2073 * After that, it will take care of propagating itself (a
2074 * macro-end marker line for a macro which is really a %rep
2075 * block will cause the macro to be re-expanded, complete
2076 * with another macro-end marker to ensure the process
2077 * continues) until the whole expansion is forcibly removed
2078 * from istk->expansion by a %exitrep.
2079 */
2080 l = nasm_malloc(sizeof(Line));
2081 l->next = istk->expansion;
2082 l->finishes = defining;
2083 l->first = NULL;
2084 istk->expansion = l;
2085
2086 istk->mstk = defining;
2087
2088 list->uplevel (defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvinb64535f2002-04-30 20:55:37 +00002089 tmp_defining = defining;
2090 defining = defining->rep_nest;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002091 free_tlist (origline);
H. Peter Anvineba20a72002-04-30 20:53:55 +00002092 return 1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002093
2094 case PP_EXITREP:
2095 /*
2096 * We must search along istk->expansion until we hit a
2097 * macro-end marker for a macro with no name. Then we set
2098 * its `in_progress' flag to 0.
2099 */
2100 for (l = istk->expansion; l; l = l->next)
2101 if (l->finishes && !l->finishes->name)
2102 break;
2103
H. Peter Anvineba20a72002-04-30 20:53:55 +00002104 if (l)
H. Peter Anvin76690a12002-04-30 20:52:49 +00002105 l->finishes->in_progress = 0;
2106 else
2107 error (ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2108 free_tlist (origline);
H. Peter Anvineba20a72002-04-30 20:53:55 +00002109 return 1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002110
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002111 case PP_XDEFINE:
2112 case PP_IXDEFINE:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002113 case PP_DEFINE:
2114 case PP_IDEFINE:
2115 tline = tline->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002116 skip_white_(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002117 tline = expand_id (tline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002118 if (!tline || (tline->type != TOK_ID &&
2119 (tline->type != TOK_PREPROC_ID ||
2120 tline->text[1] != '$'))) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00002121 error (ERR_NONFATAL,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002122 "`%%%s%sdefine' expects a macro identifier",
2123 ((i == PP_IDEFINE || i == PP_IXDEFINE) ? "i" : ""),
2124 ((i == PP_XDEFINE || i == PP_IXDEFINE) ? "x" : ""));
H. Peter Anvin76690a12002-04-30 20:52:49 +00002125 free_tlist (origline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002126 return 3;
2127 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002128
2129 ctx = get_ctx (tline->text, FALSE);
2130 if (!ctx)
2131 smhead = &smacros[hash(tline->text)];
2132 else
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002133 smhead = &ctx->localmac;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002134 mname = tline->text;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002135 last = tline;
2136 param_start = tline = tline->next;
2137 nparam = 0;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002138
2139 /* Expand the macro definition now for %xdefine and %ixdefine */
2140 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2141 tline = expand_smacro (tline);
2142
H. Peter Anvineba20a72002-04-30 20:53:55 +00002143 if (tok_is_(tline, "(")) {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002144 /*
2145 * This macro has parameters.
2146 */
2147
2148 tline = tline->next;
2149 while (1) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00002150 skip_white_(tline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002151 if (!tline) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00002152 error (ERR_NONFATAL,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002153 "parameter identifier expected");
H. Peter Anvin76690a12002-04-30 20:52:49 +00002154 free_tlist (origline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002155 return 3;
2156 }
2157 if (tline->type != TOK_ID) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00002158 error (ERR_NONFATAL,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002159 "`%s': parameter identifier expected",
2160 tline->text);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002161 free_tlist (origline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002162 return 3;
2163 }
2164 tline->type = TOK_SMAC_PARAM + nparam++;
2165 tline = tline->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002166 skip_white_(tline);
2167 if (tok_is_(tline, ",")) {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002168 tline = tline->next;
2169 continue;
2170 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00002171 if (!tok_is_(tline, ")")) {
2172 error (ERR_NONFATAL,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002173 "`)' expected to terminate macro template");
H. Peter Anvin76690a12002-04-30 20:52:49 +00002174 free_tlist (origline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002175 return 3;
2176 }
2177 break;
2178 }
2179 last = tline;
2180 tline = tline->next;
2181 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00002182 if (tok_type_(tline, TOK_WHITESPACE))
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002183 last = tline, tline = tline->next;
2184 macro_start = NULL;
2185 last->next = NULL;
2186 t = tline;
2187 while (t) {
2188 if (t->type == TOK_ID) {
2189 for (tt = param_start; tt; tt = tt->next)
2190 if (tt->type >= TOK_SMAC_PARAM &&
2191 !strcmp(tt->text, t->text))
2192 t->type = tt->type;
2193 }
2194 tt = t->next;
2195 t->next = macro_start;
2196 macro_start = t;
2197 t = tt;
2198 }
2199 /*
2200 * Good. We now have a macro name, a parameter count, and a
2201 * token list (in reverse order) for an expansion. We ought
2202 * to be OK just to create an SMacro, store it, and let
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002203 * free_tlist have the rest of the line (which we have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002204 * carefully re-terminated after chopping off the expansion
2205 * from the end).
2206 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002207 if (smacro_defined (ctx, mname, nparam, &smac, i == PP_DEFINE)) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00002208 if (!smac) {
2209 error (ERR_WARNING,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002210 "single-line macro `%s' defined both with and"
2211 " without parameters", mname);
H. Peter Anvineba20a72002-04-30 20:53:55 +00002212 free_tlist (origline);
2213 free_tlist (macro_start);
2214 return 3;
2215 } else {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002216 /*
2217 * We're redefining, so we have to take over an
2218 * existing SMacro structure. This means freeing
2219 * what was already in it.
2220 */
2221 nasm_free (smac->name);
2222 free_tlist (smac->expansion);
2223 }
2224 } else {
2225 smac = nasm_malloc(sizeof(SMacro));
2226 smac->next = *smhead;
2227 *smhead = smac;
2228 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002229 smac->name = nasm_strdup(mname);
2230 smac->casesense = ((i == PP_DEFINE) || (i == PP_XDEFINE));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002231 smac->nparam = nparam;
2232 smac->expansion = macro_start;
2233 smac->in_progress = FALSE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002234 free_tlist (origline);
2235 return 3;
2236
H. Peter Anvin620515a2002-04-30 20:57:38 +00002237 case PP_UNDEF:
2238 tline = tline->next;
2239 skip_white_(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002240 tline = expand_id (tline);
H. Peter Anvin620515a2002-04-30 20:57:38 +00002241 if (!tline || (tline->type != TOK_ID &&
2242 (tline->type != TOK_PREPROC_ID ||
2243 tline->text[1] != '$'))) {
2244 error (ERR_NONFATAL,
2245 "`%%undef' expects a macro identifier");
2246 free_tlist (origline);
2247 return 3;
2248 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002249 if (tline->next) {
2250 error (ERR_WARNING,
2251 "trailing garbage after macro name ignored");
H. Peter Anvin620515a2002-04-30 20:57:38 +00002252 }
H. Peter Anvin620515a2002-04-30 20:57:38 +00002253
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002254 /* Find the context that symbol belongs to */
2255 ctx = get_ctx (tline->text, FALSE);
2256 if (!ctx)
2257 smhead = &smacros[hash(tline->text)];
2258 else
2259 smhead = &ctx->localmac;
2260
2261 mname = tline->text;
2262 last = tline;
2263 last->next = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00002264
2265 /*
2266 * We now have a macro name... go hunt for it.
2267 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002268 while (smacro_defined (ctx, mname, -1, &smac, 1)) {
H. Peter Anvin620515a2002-04-30 20:57:38 +00002269 /* Defined, so we need to find its predecessor and nuke it */
2270 SMacro **s;
2271 for ( s = smhead ; *s && *s != smac ; s = &(*s)->next );
2272 if ( *s ) {
2273 *s = smac->next;
2274 nasm_free(smac->name);
2275 free_tlist(smac->expansion);
2276 nasm_free(smac);
2277 }
2278 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002279 free_tlist (origline);
H. Peter Anvin620515a2002-04-30 20:57:38 +00002280 return 3;
2281
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002282 case PP_STRLEN:
2283 tline = tline->next;
2284 skip_white_(tline);
2285 tline = expand_id (tline);
2286 if (!tline || (tline->type != TOK_ID &&
2287 (tline->type != TOK_PREPROC_ID ||
2288 tline->text[1] != '$'))) {
2289 error (ERR_NONFATAL,
2290 "`%%strlen' expects a macro identifier as first parameter");
2291 free_tlist (origline);
2292 return 3;
2293 }
2294 ctx = get_ctx (tline->text, FALSE);
2295 if (!ctx)
2296 smhead = &smacros[hash(tline->text)];
2297 else
2298 smhead = &ctx->localmac;
2299 mname = tline->text;
2300 last = tline;
2301 tline = expand_smacro (tline->next);
2302 last->next = NULL;
2303
2304 t = tline;
2305 while (tok_type_(t, TOK_WHITESPACE))
2306 t = t->next;
2307 /* t should now point to the string */
2308 if (t->type != TOK_STRING) {
2309 error(ERR_NONFATAL,
2310 "`%%strlen` requires string as second parameter");
2311 free_tlist(tline);
2312 free_tlist(origline);
2313 return 3;
2314 }
2315
2316 macro_start = nasm_malloc(sizeof(*macro_start));
2317 macro_start->next = NULL;
2318 make_tok_num(macro_start, strlen(t->text)-2);
2319 macro_start->mac = NULL;
2320
2321 /*
2322 * We now have a macro name, an implicit parameter count of
2323 * zero, and a numeric token to use as an expansion. Create
2324 * and store an SMacro.
2325 */
2326 if (smacro_defined (ctx, mname, 0, &smac, i == PP_STRLEN)) {
2327 if (!smac)
2328 error (ERR_WARNING,
2329 "single-line macro `%s' defined both with and"
2330 " without parameters", mname);
2331 else {
2332 /*
2333 * We're redefining, so we have to take over an
2334 * existing SMacro structure. This means freeing
2335 * what was already in it.
2336 */
2337 nasm_free (smac->name);
2338 free_tlist (smac->expansion);
2339 }
2340 }
2341 else {
2342 smac = nasm_malloc(sizeof(SMacro));
2343 smac->next = *smhead;
2344 *smhead = smac;
2345 }
2346 smac->name = nasm_strdup(mname);
2347 smac->casesense = (i == PP_STRLEN);
2348 smac->nparam = 0;
2349 smac->expansion = macro_start;
2350 smac->in_progress = FALSE;
2351 free_tlist (tline);
2352 free_tlist (origline);
2353 return 3;
2354
2355 case PP_SUBSTR:
2356 tline = tline->next;
2357 skip_white_(tline);
2358 tline = expand_id (tline);
2359 if (!tline || (tline->type != TOK_ID &&
2360 (tline->type != TOK_PREPROC_ID ||
2361 tline->text[1] != '$'))) {
2362 error (ERR_NONFATAL,
2363 "`%%substr' expects a macro identifier as first parameter");
2364 free_tlist (origline);
2365 return 3;
2366 }
2367 ctx = get_ctx (tline->text, FALSE);
2368 if (!ctx)
2369 smhead = &smacros[hash(tline->text)];
2370 else
2371 smhead = &ctx->localmac;
2372 mname = tline->text;
2373 last = tline;
2374 tline = expand_smacro (tline->next);
2375 last->next = NULL;
2376
2377 t = tline->next;
2378 while (tok_type_(t, TOK_WHITESPACE))
2379 t = t->next;
2380
2381 /* t should now point to the string */
2382 if (t->type != TOK_STRING) {
2383 error(ERR_NONFATAL,
2384 "`%%substr` requires string as second parameter");
2385 free_tlist(tline);
2386 free_tlist(origline);
2387 return 3;
2388 }
2389
2390 tt = t->next;
2391 tptr = &tt;
2392 tokval.t_type = TOKEN_INVALID;
2393 evalresult = evaluate (ppscan, tptr, &tokval, NULL, pass, error, NULL);
2394 if (!evalresult) {
2395 free_tlist(tline);
2396 free_tlist(origline);
2397 return 3;
2398 }
2399 if (!is_simple(evalresult)) {
2400 error(ERR_NONFATAL,
2401 "non-constant value given to `%%substr`");
2402 free_tlist(tline);
2403 free_tlist(origline);
2404 return 3;
2405 }
2406
2407 macro_start = nasm_malloc(sizeof(*macro_start));
2408 macro_start->next = NULL;
2409 macro_start->text = nasm_strdup("'''");
2410 if (evalresult->value > 0 && evalresult->value < strlen(t->text)-1) {
2411 macro_start->text[1] = t->text[evalresult->value];
2412 }
2413 else {
2414 macro_start->text[2] = '\0';
2415 }
2416 macro_start->type = TOK_STRING;
2417 macro_start->mac = NULL;
2418
2419 /*
2420 * We now have a macro name, an implicit parameter count of
2421 * zero, and a numeric token to use as an expansion. Create
2422 * and store an SMacro.
2423 */
2424 if (smacro_defined (ctx, mname, 0, &smac, i == PP_SUBSTR)) {
2425 if (!smac)
2426 error (ERR_WARNING,
2427 "single-line macro `%s' defined both with and"
2428 " without parameters", mname);
2429 else {
2430 /*
2431 * We're redefining, so we have to take over an
2432 * existing SMacro structure. This means freeing
2433 * what was already in it.
2434 */
2435 nasm_free (smac->name);
2436 free_tlist (smac->expansion);
2437 }
2438 }
2439 else {
2440 smac = nasm_malloc(sizeof(SMacro));
2441 smac->next = *smhead;
2442 *smhead = smac;
2443 }
2444 smac->name = nasm_strdup(mname);
2445 smac->casesense = (i == PP_SUBSTR);
2446 smac->nparam = 0;
2447 smac->expansion = macro_start;
2448 smac->in_progress = FALSE;
2449 free_tlist (tline);
2450 free_tlist (origline);
2451 return 3;
2452
2453
H. Peter Anvin76690a12002-04-30 20:52:49 +00002454 case PP_ASSIGN:
2455 case PP_IASSIGN:
2456 tline = tline->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002457 skip_white_(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002458 tline = expand_id (tline);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002459 if (!tline || (tline->type != TOK_ID &&
2460 (tline->type != TOK_PREPROC_ID ||
2461 tline->text[1] != '$'))) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00002462 error (ERR_NONFATAL,
H. Peter Anvin76690a12002-04-30 20:52:49 +00002463 "`%%%sassign' expects a macro identifier",
2464 (i == PP_IASSIGN ? "i" : ""));
2465 free_tlist (origline);
2466 return 3;
2467 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002468 ctx = get_ctx (tline->text, FALSE);
2469 if (!ctx)
2470 smhead = &smacros[hash(tline->text)];
2471 else
H. Peter Anvin76690a12002-04-30 20:52:49 +00002472 smhead = &ctx->localmac;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002473 mname = tline->text;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002474 last = tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002475 tline = expand_smacro (tline->next);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002476 last->next = NULL;
2477
H. Peter Anvin76690a12002-04-30 20:52:49 +00002478 t = tline;
2479 tptr = &t;
2480 tokval.t_type = TOKEN_INVALID;
2481 evalresult = evaluate (ppscan, tptr, &tokval, NULL, pass, error, NULL);
2482 free_tlist (tline);
2483 if (!evalresult) {
2484 free_tlist (origline);
2485 return 3;
2486 }
2487
2488 if (tokval.t_type)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002489 error(ERR_WARNING,
H. Peter Anvin76690a12002-04-30 20:52:49 +00002490 "trailing garbage after expression ignored");
2491
2492 if (!is_simple(evalresult)) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00002493 error(ERR_NONFATAL,
H. Peter Anvin76690a12002-04-30 20:52:49 +00002494 "non-constant value given to `%%%sassign'",
2495 (i == PP_IASSIGN ? "i" : ""));
2496 free_tlist (origline);
2497 return 3;
2498 }
2499
2500 macro_start = nasm_malloc(sizeof(*macro_start));
2501 macro_start->next = NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002502 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvin76690a12002-04-30 20:52:49 +00002503 macro_start->mac = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002504
2505 /*
2506 * We now have a macro name, an implicit parameter count of
2507 * zero, and a numeric token to use as an expansion. Create
2508 * and store an SMacro.
2509 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002510 if (smacro_defined (ctx, mname, 0, &smac, i == PP_ASSIGN)) {
H. Peter Anvin76690a12002-04-30 20:52:49 +00002511 if (!smac)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002512 error (ERR_WARNING,
H. Peter Anvin76690a12002-04-30 20:52:49 +00002513 "single-line macro `%s' defined both with and"
2514 " without parameters", mname);
2515 else {
2516 /*
2517 * We're redefining, so we have to take over an
2518 * existing SMacro structure. This means freeing
2519 * what was already in it.
2520 */
2521 nasm_free (smac->name);
2522 free_tlist (smac->expansion);
2523 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00002524 }
2525 else {
H. Peter Anvin76690a12002-04-30 20:52:49 +00002526 smac = nasm_malloc(sizeof(SMacro));
2527 smac->next = *smhead;
2528 *smhead = smac;
2529 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002530 smac->name = nasm_strdup(mname);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002531 smac->casesense = (i == PP_ASSIGN);
2532 smac->nparam = 0;
2533 smac->expansion = macro_start;
2534 smac->in_progress = FALSE;
2535 free_tlist (origline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002536 return 3;
2537
2538 case PP_LINE:
2539 /*
2540 * Syntax is `%line nnn[+mmm] [filename]'
2541 */
2542 tline = tline->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002543 skip_white_(tline);
2544 if (!tok_type_(tline, TOK_NUMBER)) {
2545 error (ERR_NONFATAL, "`%%line' expects line number");
H. Peter Anvin76690a12002-04-30 20:52:49 +00002546 free_tlist (origline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002547 return 3;
2548 }
2549 k = readnum(tline->text, &j);
2550 m = 1;
2551 tline = tline->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002552 if (tok_is_(tline, "+")) {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002553 tline = tline->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002554 if (!tok_type_(tline, TOK_NUMBER)) {
2555 error (ERR_NONFATAL,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002556 "`%%line' expects line increment");
H. Peter Anvin76690a12002-04-30 20:52:49 +00002557 free_tlist (origline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002558 return 3;
2559 }
2560 m = readnum(tline->text, &j);
2561 tline = tline->next;
2562 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00002563 skip_white_(tline);
2564 src_set_linnum(k);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002565 istk->lineinc = m;
2566 if (tline) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002567 nasm_free (src_set_fname (detoken (tline, FALSE)));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002568 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002569 free_tlist (origline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002570 return 5;
2571
2572 default:
H. Peter Anvineba20a72002-04-30 20:53:55 +00002573 error(ERR_FATAL,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002574 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin76690a12002-04-30 20:52:49 +00002575 directives[i]);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002576 break;
2577 }
2578 return 3;
2579}
2580
2581/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002582 * Ensure that a macro parameter contains a condition code and
2583 * nothing else. Return the condition code index if so, or -1
2584 * otherwise.
2585 */
H. Peter Anvineba20a72002-04-30 20:53:55 +00002586static int find_cc (Token *t)
2587{
H. Peter Anvin76690a12002-04-30 20:52:49 +00002588 Token *tt;
2589 int i, j, k, m;
2590
H. Peter Anvineba20a72002-04-30 20:53:55 +00002591 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002592 if (t->type != TOK_ID)
2593 return -1;
2594 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002595 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002596 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
2597 return -1;
2598
2599 i = -1;
2600 j = sizeof(conditions)/sizeof(*conditions);
2601 while (j-i > 1) {
2602 k = (j+i) / 2;
2603 m = nasm_stricmp(t->text, conditions[k]);
2604 if (m == 0) {
2605 i = k;
2606 j = -2;
2607 break;
2608 } else if (m < 0) {
2609 j = k;
2610 } else
2611 i = k;
2612 }
2613 if (j != -2)
2614 return -1;
2615 return i;
2616}
2617
2618/*
2619 * Expand MMacro-local things: parameter references (%0, %n, %+n,
2620 * %-n) and MMacro-local identifiers (%%foo).
2621 */
H. Peter Anvineba20a72002-04-30 20:53:55 +00002622static Token *expand_mmac_params (Token *tline)
2623{
H. Peter Anvin76690a12002-04-30 20:52:49 +00002624 Token *t, *tt, *ttt, **tail, *thead;
2625
2626 tail = &thead;
2627 thead = NULL;
2628
2629 while (tline) {
2630 if (tline->type == TOK_PREPROC_ID &&
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002631 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text [2]) ||
H. Peter Anvin76690a12002-04-30 20:52:49 +00002632 tline->text[1] == '%' ||
2633 (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
2634 char *text = NULL;
2635 int type = 0, cc; /* type = 0 to placate optimisers */
2636 char tmpbuf[30];
2637 int n, i;
2638 MMacro *mac;
2639
2640 t = tline;
2641 tline = tline->next;
2642
2643 mac = istk->mstk;
2644 while (mac && !mac->name) /* avoid mistaking %reps for macros */
2645 mac = mac->next_active;
2646 if (!mac)
2647 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
2648 else switch (t->text[1]) {
2649 /*
2650 * We have to make a substitution of one of the
2651 * forms %1, %-1, %+1, %%foo, %0.
2652 */
2653 case '0':
2654 type = TOK_NUMBER;
2655 sprintf(tmpbuf, "%d", mac->nparam);
2656 text = nasm_strdup(tmpbuf);
2657 break;
2658 case '%':
2659 type = TOK_ID;
2660 sprintf(tmpbuf, "..@%lu.", mac->unique);
H. Peter Anvineba20a72002-04-30 20:53:55 +00002661 text = nasm_strcat(tmpbuf, t->text+2);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002662 break;
2663 case '-':
2664 n = atoi(t->text+2)-1;
2665 if (n >= mac->nparam)
2666 tt = NULL;
2667 else {
2668 if (mac->nparam > 1)
2669 n = (n + mac->rotate) % mac->nparam;
2670 tt = mac->params[n];
2671 }
2672 cc = find_cc (tt);
2673 if (cc == -1) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00002674 error (ERR_NONFATAL,
H. Peter Anvin76690a12002-04-30 20:52:49 +00002675 "macro parameter %d is not a condition code",
2676 n+1);
2677 text = NULL;
2678 } else {
2679 type = TOK_ID;
2680 if (inverse_ccs[cc] == -1) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00002681 error (ERR_NONFATAL,
H. Peter Anvin76690a12002-04-30 20:52:49 +00002682 "condition code `%s' is not invertible",
2683 conditions[cc]);
2684 text = NULL;
2685 } else
2686 text = nasm_strdup(conditions[inverse_ccs[cc]]);
2687 }
2688 break;
2689 case '+':
2690 n = atoi(t->text+2)-1;
2691 if (n >= mac->nparam)
2692 tt = NULL;
2693 else {
2694 if (mac->nparam > 1)
2695 n = (n + mac->rotate) % mac->nparam;
2696 tt = mac->params[n];
2697 }
2698 cc = find_cc (tt);
2699 if (cc == -1) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00002700 error (ERR_NONFATAL,
H. Peter Anvin76690a12002-04-30 20:52:49 +00002701 "macro parameter %d is not a condition code",
2702 n+1);
2703 text = NULL;
2704 } else {
2705 type = TOK_ID;
2706 text = nasm_strdup(conditions[cc]);
2707 }
2708 break;
2709 default:
2710 n = atoi(t->text+1)-1;
2711 if (n >= mac->nparam)
2712 tt = NULL;
2713 else {
2714 if (mac->nparam > 1)
2715 n = (n + mac->rotate) % mac->nparam;
2716 tt = mac->params[n];
2717 }
2718 if (tt) {
2719 for (i=0; i<mac->paramlen[n]; i++) {
2720 ttt = *tail = nasm_malloc(sizeof(Token));
H. Peter Anvin76690a12002-04-30 20:52:49 +00002721 tail = &ttt->next;
2722 ttt->type = tt->type;
2723 ttt->text = nasm_strdup(tt->text);
2724 ttt->mac = NULL;
2725 tt = tt->next;
2726 }
2727 }
2728 text = NULL; /* we've done it here */
2729 break;
2730 }
2731 nasm_free (t->text);
H. Peter Anvineba20a72002-04-30 20:53:55 +00002732 if (!text) {
2733 nasm_free (t);
2734 } else {
2735 *tail = t;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002736 tail = &t->next;
2737 t->type = type;
2738 t->text = text;
2739 t->mac = NULL;
2740 }
2741 continue;
2742 } else {
2743 t = *tail = tline;
2744 tline = tline->next;
2745 t->mac = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002746 tail = &t->next;
2747 }
2748 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00002749 *tail = NULL;
2750 t = thead;
2751 for (; t && (tt=t->next)!=NULL ; t = t->next)
2752 switch (t->type) {
2753 case TOK_WHITESPACE:
2754 if (tt->type == TOK_WHITESPACE) {
2755 t->next = tt->next;
2756 nasm_free(tt->text);
2757 nasm_free(tt);
2758 }
2759 break;
2760 case TOK_ID:
2761 if (tt->type == TOK_ID || tt->type == TOK_NUMBER) {
2762 char *tmp = nasm_strcat(t->text, tt->text);
2763 nasm_free(t->text);
2764 t->text = tmp;
2765 t->next = tt->next;
2766 nasm_free(tt->text);
2767 nasm_free(tt);
2768 }
2769 break;
2770 case TOK_NUMBER:
2771 if (tt->type == TOK_NUMBER) {
2772 char *tmp = nasm_strcat(t->text, tt->text);
2773 nasm_free(t->text);
2774 t->text = tmp;
2775 t->next = tt->next;
2776 nasm_free(tt->text);
2777 nasm_free(tt);
2778 }
2779 break;
2780 }
2781
H. Peter Anvin76690a12002-04-30 20:52:49 +00002782 return thead;
2783}
2784
2785/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002786 * Expand all single-line macro calls made in the given line.
2787 * Return the expanded version of the line. The original is deemed
2788 * to be destroyed in the process. (In reality we'll just move
2789 * Tokens from input to output a lot of the time, rather than
2790 * actually bothering to destroy and replicate.)
2791 */
H. Peter Anvineba20a72002-04-30 20:53:55 +00002792static Token *expand_smacro (Token *tline)
2793{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002794 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002795 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002796 Token **params;
2797 int *paramsize;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002798 int nparam, sparam, brackets, rescan;
2799 Token *org_tline = tline;
2800 Context *ctx;
2801 char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002802
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002803 /*
2804 * Trick: we should avoid changing the start token pointer since it can
2805 * be contained in "next" field of other token. Because of this
2806 * we allocate a copy of first token and work with it; at the end of
2807 * routine we copy it back
2808 */
2809 if (org_tline)
2810 {
2811 tline = nasm_malloc (sizeof (Token));
2812 *tline = *org_tline;
2813 }
2814
2815again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002816 tail = &thead;
2817 thead = NULL;
2818
H. Peter Anvineba20a72002-04-30 20:53:55 +00002819 while (tline) { /* main token loop */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002820 if ((mname = tline->text)) {
2821 /* if this token is a local macro, look in local context */
2822 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID)
2823 ctx = get_ctx (mname, TRUE);
2824 else
2825 ctx = NULL;
2826 if (!ctx)
2827 head = smacros[hash(mname)];
2828 else
H. Peter Anvineba20a72002-04-30 20:53:55 +00002829 head = ctx->localmac;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002830 /*
2831 * We've hit an identifier. As in is_mmacro below, we first
2832 * check whether the identifier is a single-line macro at
2833 * all, then think about checking for parameters if
2834 * necessary.
2835 */
H. Peter Anvineba20a72002-04-30 20:53:55 +00002836 for (m = head; m; m = m->next)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002837 if (!mstrcmp(m->name, mname, m->casesense))
H. Peter Anvineba20a72002-04-30 20:53:55 +00002838 break;
2839 if (m) {
2840 mstart = tline;
2841 params = NULL;
2842 paramsize = NULL;
2843 if (m->nparam == 0) {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002844 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00002845 * Simple case: the macro is parameterless. Discard the
2846 * one token that the macro call took, and push the
2847 * expansion back on the to-do stack.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002848 */
H. Peter Anvineba20a72002-04-30 20:53:55 +00002849 if (!m->expansion)
2850 {
2851 if (!strcmp("__FILE__", m->name)) {
2852 long num=0;
2853 src_get(&num, &(tline->text));
2854 nasm_quote(&(tline->text));
2855 tline->type = TOK_STRING;
2856 continue;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002857 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00002858 if (!strcmp("__LINE__", m->name)) {
2859 nasm_free(tline->text);
2860 make_tok_num(tline, src_get_linnum());
2861 continue;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002862 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00002863 t = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002864 tline = tline->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002865 nasm_free (t->text);
2866 nasm_free (t);
2867 continue;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002868 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002869 } else {
H. Peter Anvineba20a72002-04-30 20:53:55 +00002870 /*
2871 * Complicated case: at least one macro with this name
2872 * exists and takes parameters. We must find the
2873 * parameters in the call, count them, find the SMacro
2874 * that corresponds to that form of the macro call, and
2875 * substitute for the parameters when we expand. What a
2876 * pain.
2877 */
2878 tline = tline->next;
2879 skip_white_(tline);
2880 if (!tok_is_(tline, "(")) {
2881 /*
2882 * This macro wasn't called with parameters: ignore
2883 * the call. (Behaviour borrowed from gnu cpp.)
2884 */
2885 tline = mstart;
2886 m = NULL;
2887 }
2888 else {
2889 int paren = 0;
2890 int white = 0;
2891 brackets = 0;
2892 nparam = 0;
2893 tline = tline->next;
2894 sparam = PARAM_DELTA;
2895 params = nasm_malloc (sparam*sizeof(Token *));
2896 params[0] = tline;
2897 paramsize = nasm_malloc (sparam*sizeof(int));
2898 paramsize[0] = 0;
2899 for (;;tline = tline->next) { /* parameter loop */
2900 if (!tline) {
2901 error(ERR_NONFATAL,
2902 "macro call expects terminating `)'");
2903 break;
2904 }
2905 if (tline->type == TOK_WHITESPACE && brackets<=0) {
2906 if (paramsize[nparam])
2907 white++;
2908 else
2909 params[nparam] = tline->next;
2910 continue; /* parameter loop */
2911 }
2912 if (tline->type == TOK_OTHER && tline->text[1]==0) {
2913 char ch = tline->text[0];
2914 if (ch == ',' && !paren && brackets<=0) {
2915 if (++nparam >= sparam) {
2916 sparam += PARAM_DELTA;
2917 params = nasm_realloc (params,
2918 sparam*sizeof(Token *));
2919 paramsize = nasm_realloc (paramsize,
2920 sparam*sizeof(int));
2921 }
2922 params[nparam] = tline->next;
2923 paramsize[nparam] = 0;
2924 white = 0;
2925 continue; /* parameter loop */
2926 }
H. Peter Anvin4836e332002-04-30 20:56:43 +00002927 if (ch == '{' &&
H. Peter Anvineba20a72002-04-30 20:53:55 +00002928 (brackets>0 || (brackets==0 &&
2929 !paramsize[nparam])))
2930 {
2931 if (!(brackets++))
2932 {
2933 params[nparam] = tline->next;
2934 continue; /* parameter loop */
2935 }
2936 }
H. Peter Anvin4836e332002-04-30 20:56:43 +00002937 if (ch == '}' && brackets>0)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002938 if (--brackets == 0) {
2939 brackets = -1;
2940 continue; /* parameter loop */
2941 }
2942 if (ch == '(' && !brackets)
2943 paren++;
2944 if (ch == ')' && brackets<=0)
2945 if (--paren < 0)
2946 break;
2947 }
2948 if (brackets<0) {
2949 brackets = 0;
2950 error (ERR_NONFATAL, "braces do not "
2951 "enclose all of macro parameter");
2952 }
2953 paramsize[nparam] += white+1;
2954 white = 0;
2955 } /* parameter loop */
2956 nparam++;
2957 while (m && (m->nparam != nparam ||
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002958 mstrcmp(m->name, mname, m->casesense)))
H. Peter Anvineba20a72002-04-30 20:53:55 +00002959 m = m->next;
2960 if (!m)
2961 error (ERR_WARNING|ERR_WARN_MNP,
2962 "macro `%s' exists, "
2963 "but not taking %d parameters",
2964 mstart->text, nparam);
2965 }
2966 }
2967 if (m && m->in_progress)
2968 m = NULL;
2969 if (!m) /* in progess or didn't find '(' or wrong nparam */
2970 {
2971 /*
2972 * Design question: should we handle !tline, which
2973 * indicates missing ')' here, or expand those
2974 * macros anyway, which requires the (t) test a few
2975 * lines down?
2976 */
2977 nasm_free (params);
2978 nasm_free (paramsize);
2979 tline = mstart;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002980 } else {
H. Peter Anvineba20a72002-04-30 20:53:55 +00002981 /*
2982 * Expand the macro: we are placed on the last token of the
2983 * call, so that we can easily split the call from the
2984 * following tokens. We also start by pushing an SMAC_END
2985 * token for the cycle removal.
2986 */
2987 t = tline;
2988 if (t) {
2989 tline = t->next;
2990 t->next = NULL;
2991 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002992 tt = nasm_malloc(sizeof(Token));
H. Peter Anvineba20a72002-04-30 20:53:55 +00002993 tt->type = TOK_SMAC_END;
2994 tt->text = NULL;
2995 tt->mac = m;
2996 m->in_progress = TRUE;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002997 tt->next = tline;
2998 tline = tt;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002999 for (t = m->expansion; t; t = t->next) {
3000 if (t->type >= TOK_SMAC_PARAM) {
3001 Token *pcopy = tline, **ptail = &pcopy;
3002 Token *ttt, *pt;
3003 int i;
3004
3005 ttt = params[t->type - TOK_SMAC_PARAM];
3006 for (i=paramsize[t->type-TOK_SMAC_PARAM]; --i>=0;) {
3007 pt = *ptail = nasm_malloc(sizeof(Token));
3008 pt->next = tline;
3009 ptail = &pt->next;
3010 pt->text = nasm_strdup(ttt->text);
3011 pt->type = ttt->type;
3012 pt->mac = NULL;
3013 ttt = ttt->next;
3014 }
3015 tline = pcopy;
3016 } else {
3017 tt = nasm_malloc(sizeof(Token));
3018 tt->type = t->type;
3019 tt->text = nasm_strdup(t->text);
3020 tt->mac = NULL;
3021 tt->next = tline;
3022 tline = tt;
3023 }
3024 }
3025
3026 /*
3027 * Having done that, get rid of the macro call, and clean
3028 * up the parameters.
3029 */
3030 nasm_free (params);
3031 nasm_free (paramsize);
3032 free_tlist (mstart);
3033 continue; /* main token loop */
3034 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003035 }
3036 }
3037
H. Peter Anvineba20a72002-04-30 20:53:55 +00003038 if (tline->type == TOK_SMAC_END) {
3039 tline->mac->in_progress = FALSE;
3040 t = tline;
3041 tline = tline->next;
3042 nasm_free (t);
3043 } else {
3044 t = *tail = tline;
3045 tline = tline->next;
3046 t->mac = NULL;
3047 t->next = NULL;
3048 tail = &t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003049 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003050 }
3051
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003052 /*
3053 * Now scan the entire line and look for successive TOK_IDs that resulted
3054 * after expansion (they can't be produced by tokenise()). The successive
3055 * TOK_IDs should be concatenated.
3056 * Also we look for %+ tokens and concatenate the tokens before and after
3057 * them (without white spaces in between).
3058 */
3059 t = thead;
3060 rescan = 0;
3061 while (t) {
3062 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3063 t = t->next;
3064 if (!t || !t->next)
3065 break;
3066 if (t->next->type == TOK_ID ||
3067 t->next->type == TOK_PREPROC_ID ||
3068 t->next->type == TOK_NUMBER) {
3069 Token *next = t->next->next;
3070 char *p = nasm_malloc (strlen (t->text) + strlen (t->next->text) + 1);
3071 strcpy (p, t->text);
3072 strcat (p, t->next->text);
3073 nasm_free (t->text);
3074 nasm_free (t->next->text);
3075 nasm_free (t->next);
3076 t->next = next;
3077 t->text = p;
3078 rescan = 1;
3079 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3080 t->next->next->type == TOK_PREPROC_ID &&
3081 strcmp (t->next->next->text, "%+") == 0) {
3082 /* free the next whitespace, the %+ token and next whitespace */
3083 int i;
3084 for (i = 1; i <= 3; i++)
3085 {
3086 Token *next;
3087 if (!t->next || (i != 2 && t->next->type != TOK_WHITESPACE))
3088 break;
3089 next = t->next->next;
3090 nasm_free (t->next->text);
3091 nasm_free (t->next);
3092 t->next = next;
3093 } /* endfor */
3094 } else
3095 t = t->next;
3096 }
3097 /* If we concatenaded something, re-scan the line for macros */
3098 if (rescan) {
3099 tline = thead;
3100 goto again;
3101 }
3102
3103 if (org_tline)
3104 {
3105 if (thead) {
3106 *org_tline = *thead;
3107 nasm_free (thead);
3108 } else
3109 {
3110 /* the expression expanded to empty line;
3111 we can't return NULL for some reasons
3112 we just set the line to a single WHITESPACE token. */
3113 memset (org_tline, 0, sizeof (*org_tline));
3114 org_tline->text = nasm_strdup (" ");
3115 org_tline->type = TOK_WHITESPACE;
3116 }
3117 thead = org_tline;
3118 }
3119
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003120 return thead;
3121}
3122
3123/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003124 * Similar to expand_smacro but used exclusively with macro identifiers
3125 * right before they are fetched in. The reason is that there can be
3126 * identifiers consisting of several subparts. We consider that if there
3127 * are more than one element forming the name, user wants a expansion,
3128 * otherwise it will be left as-is. Example:
3129 *
3130 * %define %$abc cde
3131 *
3132 * the identifier %$abc will be left as-is so that the handler for %define
3133 * will suck it and define the corresponding value. Other case:
3134 *
3135 * %define _%$abc cde
3136 *
3137 * In this case user wants name to be expanded *before* %define starts
3138 * working, so we'll expand %$abc into something (if it has a value;
3139 * otherwise it will be left as-is) then concatenate all successive
3140 * PP_IDs into one.
3141 */
3142static Token *expand_id (Token *tline)
3143{
3144 Token *cur, *oldnext = NULL;
3145
3146 if (!tline ||
3147 !tline->next)
3148 return tline;
3149
3150 cur = tline;
3151 while (cur->next &&
3152 (cur->next->type == TOK_ID ||
3153 cur->next->type == TOK_PREPROC_ID ||
3154 cur->next->type == TOK_NUMBER))
3155 cur = cur->next;
3156
3157 /* If identifier consists of just one token, don't expand */
3158 if (cur == tline)
3159 return tline;
3160
3161 if (cur) {
3162 oldnext = cur->next; /* Detach the tail past identifier */
3163 cur->next = NULL; /* so that expand_smacro stops here */
3164 }
3165
3166 tline = expand_smacro (tline);
3167
3168 if (cur) {
3169 /* expand_smacro possibly changhed tline; re-scan for EOL */
3170 cur = tline;
3171 while (cur && cur->next)
3172 cur = cur->next;
3173 if (cur)
3174 cur->next = oldnext;
3175 }
3176
3177 return tline;
3178}
3179
3180/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003181 * Determine whether the given line constitutes a multi-line macro
3182 * call, and return the MMacro structure called if so. Doesn't have
3183 * to check for an initial label - that's taken care of in
3184 * expand_mmacro - but must check numbers of parameters. Guaranteed
3185 * to be called with tline->type == TOK_ID, so the putative macro
3186 * name is easy to find.
3187 */
H. Peter Anvineba20a72002-04-30 20:53:55 +00003188static MMacro *is_mmacro (Token *tline, Token ***params_array)
3189{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003190 MMacro *head, *m;
3191 Token **params;
3192 int nparam;
3193
3194 head = mmacros[hash(tline->text)];
3195
3196 /*
3197 * Efficiency: first we see if any macro exists with the given
3198 * name. If not, we can return NULL immediately. _Then_ we
3199 * count the parameters, and then we look further along the
3200 * list if necessary to find the proper MMacro.
3201 */
3202 for (m = head; m; m = m->next)
3203 if (!mstrcmp(m->name, tline->text, m->casesense))
3204 break;
3205 if (!m)
3206 return NULL;
3207
3208 /*
3209 * OK, we have a potential macro. Count and demarcate the
3210 * parameters.
3211 */
3212 count_mmac_params (tline->next, &nparam, &params);
3213
3214 /*
3215 * So we know how many parameters we've got. Find the MMacro
3216 * structure that handles this number.
3217 */
3218 while (m) {
3219 if (m->nparam_min <= nparam && (m->plus || nparam <= m->nparam_max)) {
3220 /*
3221 * This one is right. Just check if cycle removal
3222 * prohibits us using it before we actually celebrate...
3223 */
3224 if (m->in_progress) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003225#if 0
3226 error (ERR_NONFATAL,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003227 "self-reference in multi-line macro `%s'",
3228 m->name);
H. Peter Anvineba20a72002-04-30 20:53:55 +00003229#endif
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003230 nasm_free (params);
3231 return NULL;
3232 }
3233 /*
3234 * It's right, and we can use it. Add its default
3235 * parameters to the end of our list if necessary.
3236 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00003237 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
3238 params = nasm_realloc (params, ((m->nparam_min+m->ndefs+1) *
3239 sizeof(*params)));
3240 while (nparam < m->nparam_min + m->ndefs) {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003241 params[nparam] = m->defaults[nparam - m->nparam_min];
3242 nparam++;
3243 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003244 }
3245 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003246 * If we've gone over the maximum parameter count (and
3247 * we're in Plus mode), ignore parameters beyond
3248 * nparam_max.
3249 */
3250 if (m->plus && nparam > m->nparam_max)
3251 nparam = m->nparam_max;
3252 /*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003253 * Then terminate the parameter list, and leave.
3254 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00003255 if (!params) { /* need this special case */
3256 params = nasm_malloc(sizeof(*params));
3257 nparam = 0;
3258 }
3259 params[nparam] = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003260 *params_array = params;
3261 return m;
3262 }
3263 /*
3264 * This one wasn't right: look for the next one with the
3265 * same name.
3266 */
3267 for (m = m->next; m; m = m->next)
3268 if (!mstrcmp(m->name, tline->text, m->casesense))
3269 break;
3270 }
3271
3272 /*
3273 * After all that, we didn't find one with the right number of
3274 * parameters. Issue a warning, and fail to expand the macro.
3275 */
H. Peter Anvineba20a72002-04-30 20:53:55 +00003276 error (ERR_WARNING|ERR_WARN_MNP,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003277 "macro `%s' exists, but not taking %d parameters",
3278 tline->text, nparam);
3279 nasm_free (params);
3280 return NULL;
3281}
3282
3283/*
3284 * Expand the multi-line macro call made by the given line, if
3285 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00003286 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003287 */
H. Peter Anvineba20a72002-04-30 20:53:55 +00003288static int expand_mmacro (Token *tline)
3289{
3290 Token *startline = tline;
3291 Token *label = NULL;
3292 int dont_prepend = 0;
3293 Token **params, *t, *tt;
3294 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003295 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003296 int i, nparam, *paramlen;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003297
3298 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003299 skip_white_(t);
3300 if (!tok_type_(t, TOK_ID))
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003301 return 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003302 m = is_mmacro (t, &params);
3303 if (!m) {
3304 Token *last;
3305 /*
3306 * We have an id which isn't a macro call. We'll assume
3307 * it might be a label; we'll also check to see if a
3308 * colon follows it. Then, if there's another id after
3309 * that lot, we'll check it again for macro-hood.
3310 */
3311 label = last = t;
3312 t = t->next;
3313 if (tok_type_(t, TOK_WHITESPACE))
3314 last = t, t = t->next;
3315 if (tok_is_(t, ":")) {
3316 dont_prepend = 1;
3317 last = t, t = t->next;
3318 if (tok_type_(t, TOK_WHITESPACE))
3319 last = t, t = t->next;
3320 }
3321 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
3322 return 0;
3323 last->next = NULL;
3324 tline = t;
3325 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003326
3327 /*
3328 * Fix up the parameters: this involves stripping leading and
3329 * trailing whitespace, then stripping braces if they are
3330 * present.
3331 */
H. Peter Anvineba20a72002-04-30 20:53:55 +00003332 for (nparam = 0; params[nparam]; nparam++)
3333 ;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003334 paramlen = nparam ? nasm_malloc(nparam*sizeof(*paramlen)) : NULL;
3335
3336 for (i = 0; params[i]; i++) {
3337 int brace = FALSE;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003338 int comma = (!m->plus || i < nparam-1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003339
3340 t = params[i];
H. Peter Anvineba20a72002-04-30 20:53:55 +00003341 skip_white_(t);
3342 if (tok_is_(t, "{"))
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003343 t = t->next, brace = TRUE, comma = FALSE;
3344 params[i] = t;
3345 paramlen[i] = 0;
3346 while (t) {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003347 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003348 break; /* ... because we have hit a comma */
H. Peter Anvineba20a72002-04-30 20:53:55 +00003349 if (comma && t->type == TOK_WHITESPACE && tok_is_(t->next, ","))
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003350 break; /* ... or a space then a comma */
3351 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003352 break; /* ... or a brace */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003353 t = t->next;
3354 paramlen[i]++;
3355 }
3356 }
3357
3358 /*
3359 * OK, we have a MMacro structure together with a set of
3360 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00003361 * copies of each Line on to istk->expansion. Substitution of
3362 * parameter tokens and macro-local tokens doesn't get done
3363 * until the single-line macro substitution process; this is
3364 * because delaying them allows us to change the semantics
3365 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003366 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00003367 * First, push an end marker on to istk->expansion, mark this
3368 * macro as in progress, and set up its invocation-specific
3369 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003370 */
3371 ll = nasm_malloc(sizeof(Line));
3372 ll->next = istk->expansion;
3373 ll->finishes = m;
3374 ll->first = NULL;
3375 istk->expansion = ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003376
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003377 m->in_progress = TRUE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003378 m->params = params;
3379 m->iline = tline;
3380 m->nparam = nparam;
3381 m->rotate = 0;
3382 m->paramlen = paramlen;
3383 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003384 m->lineno = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003385
3386 m->next_active = istk->mstk;
3387 istk->mstk = m;
3388
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003389 for (l = m->expansion; l; l = l->next) {
3390 Token **tail;
3391
3392 ll = nasm_malloc(sizeof(Line));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003393 ll->finishes = NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003394 ll->next = istk->expansion;
3395 istk->expansion = ll;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003396 tail = &ll->first;
3397
3398 for (t = l->first; t; t = t->next) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003399 Token *x = t;
3400 if (t->type == TOK_PREPROC_ID &&
3401 t->text[1]=='0' && t->text[2]=='0')
3402 {
3403 dont_prepend = -1;
3404 x = label;
3405 if (!x)
3406 continue;
3407 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003408 tt = *tail = nasm_malloc(sizeof(Token));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003409 tail = &tt->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003410 tt->type = x->type;
3411 tt->text = nasm_strdup(x->text);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003412 tt->mac = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003413 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00003414 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003415 }
3416
3417 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00003418 * If we had a label, push it on as the first line of
3419 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003420 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003421 if (label) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003422 if (dont_prepend<0)
3423 free_tlist(startline);
3424 else {
3425 ll = nasm_malloc(sizeof(Line));
3426 ll->finishes = NULL;
3427 ll->next = istk->expansion;
3428 istk->expansion = ll;
3429 ll->first = startline;
3430 if (!dont_prepend) {
3431 while (label->next)
3432 label = label->next;
3433 label->next = tt = nasm_malloc(sizeof(Token));
3434 tt->next = NULL;
3435 tt->mac = NULL;
3436 tt->type = TOK_OTHER;
3437 tt->text = nasm_strdup(":");
3438 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00003439 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003440 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003441
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003442 list->uplevel (m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
3443
H. Peter Anvineba20a72002-04-30 20:53:55 +00003444 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003445}
3446
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003447/*
3448 * Since preprocessor always operate only on the line that didn't
3449 * arrived yet, we should always use ERR_OFFBY1. Also since user
3450 * won't want to see same error twice (preprocessing is done once
3451 * per pass) we will want to show errors only during pass one.
3452 */
3453static void error (int severity, char *fmt, ...)
3454{
3455 va_list arg;
3456 char buff [1024];
3457
3458 /* If we're in a dead branch of IF or something like it, ignore the error */
3459 if (istk->conds && !emitting(istk->conds->state))
3460 return;
3461
3462 va_start (arg, fmt);
3463 vsprintf (buff, fmt, arg);
3464 va_end (arg);
3465
3466 if (istk->mstk && istk->mstk->name)
3467 __error (severity|ERR_PASS1, "(%s:%d) %s", istk->mstk->name,
3468 istk->mstk->lineno, buff);
3469 else
3470 __error (severity|ERR_PASS1, "%s", buff);
3471}
3472
H. Peter Anvin76690a12002-04-30 20:52:49 +00003473static void pp_reset (char *file, int apass, efunc errfunc, evalfunc eval,
H. Peter Anvineba20a72002-04-30 20:53:55 +00003474 ListGen *listgen)
3475{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003476 int h;
3477
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003478 __error = errfunc;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003479 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003480 istk = nasm_malloc(sizeof(Include));
3481 istk->next = NULL;
3482 istk->conds = NULL;
3483 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003484 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003485 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00003486 istk->fname = NULL;
3487 src_set_fname(nasm_strdup(file));
3488 src_set_linnum(0);
3489 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003490 if (!istk->fp)
3491 error (ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'", file);
3492 defining = NULL;
3493 for (h=0; h<NHASH; h++) {
3494 mmacros[h] = NULL;
3495 smacros[h] = NULL;
3496 }
3497 unique = 0;
3498 stdmacpos = stdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003499 any_extrastdmac = (extrastdmac != NULL);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003500 list = listgen;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003501 evaluate = eval;
3502 pass = apass;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003503}
3504
H. Peter Anvineba20a72002-04-30 20:53:55 +00003505static char *pp_getline (void)
3506{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003507 char *line;
3508 Token *tline;
3509 int ret;
3510
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003511 while (1) {
3512 /*
3513 * Fetch a tokenised line, either from the macro-expansion
3514 * buffer or from the input file.
3515 */
3516 tline = NULL;
3517 while (istk->expansion && istk->expansion->finishes) {
3518 Line *l = istk->expansion;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003519 if (!l->finishes->name && l->finishes->in_progress > 1) {
3520 Line *ll;
3521
3522 /*
3523 * This is a macro-end marker for a macro with no
3524 * name, which means it's not really a macro at all
3525 * but a %rep block, and the `in_progress' field is
3526 * more than 1, meaning that we still need to
3527 * repeat. (1 means the natural last repetition; 0
3528 * means termination by %exitrep.) We have
3529 * therefore expanded up to the %endrep, and must
3530 * push the whole block on to the expansion buffer
3531 * again. We don't bother to remove the macro-end
3532 * marker: we'd only have to generate another one
3533 * if we did.
3534 */
3535 l->finishes->in_progress--;
3536 for (l = l->finishes->expansion; l; l = l->next) {
3537 Token *t, *tt, **tail;
3538
3539 ll = nasm_malloc(sizeof(Line));
3540 ll->next = istk->expansion;
3541 ll->finishes = NULL;
3542 ll->first = NULL;
3543 tail = &ll->first;
3544
3545 for (t = l->first; t; t = t->next) {
3546 if (t->text) {
3547 tt = *tail = nasm_malloc(sizeof(Token));
3548 tt->next = NULL;
3549 tail = &tt->next;
3550 tt->type = t->type;
3551 tt->text = nasm_strdup(t->text);
3552 tt->mac = NULL;
3553 }
3554 }
3555
3556 istk->expansion = ll;
3557 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003558 } else {
H. Peter Anvin87bc6192002-04-30 20:53:16 +00003559 /*
3560 * Check whether a `%rep' was started and not ended
3561 * within this macro expansion. This can happen and
3562 * should be detected. It's a fatal error because
3563 * I'm too confused to work out how to recover
3564 * sensibly from it.
3565 */
3566 if (defining) {
3567 if (defining->name)
3568 error (ERR_PANIC,
3569 "defining with name in expansion");
H. Peter Anvinb64535f2002-04-30 20:55:37 +00003570 else if (istk->mstk->name)
H. Peter Anvin87bc6192002-04-30 20:53:16 +00003571 error (ERR_FATAL, "`%%rep' without `%%endrep' within"
3572 " expansion of macro `%s'", istk->mstk->name);
3573 }
3574
H. Peter Anvineba20a72002-04-30 20:53:55 +00003575 /*
3576 * FIXME: investigate the relationship at this point between
3577 * istk->mstk and l->finishes
3578 */
3579 {
3580 MMacro *m = istk->mstk;
3581 istk->mstk = m->next_active;
3582 if (m->name) {
3583 /*
3584 * This was a real macro call, not a %rep, and
3585 * therefore the parameter information needs to
3586 * be freed.
3587 */
3588 nasm_free(m->params);
3589 free_tlist(m->iline);
3590 nasm_free(m->paramlen);
3591 l->finishes->in_progress = FALSE;
3592 }
3593 else
3594 free_mmacro(m);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003595 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003596 istk->expansion = l->next;
3597 nasm_free (l);
3598 list->downlevel (LIST_MACRO);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003599 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003600 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00003601 while (1) { /* until we get a line we can use */
3602
3603 if (istk->expansion) { /* from a macro expansion */
3604 char *p;
3605 Line *l = istk->expansion;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003606 if (istk->mstk)
3607 istk->mstk->lineno++;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003608 tline = l->first;
3609 istk->expansion = l->next;
3610 nasm_free (l);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003611 p = detoken (tline, FALSE);
H. Peter Anvineba20a72002-04-30 20:53:55 +00003612 list->line (LIST_MACRO, p);
3613 nasm_free(p);
3614 break;
3615 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003616 line = read_line();
H. Peter Anvineba20a72002-04-30 20:53:55 +00003617 if (line) { /* from the current input file */
3618 line = prepreproc(line);
3619 tline = tokenise(line);
3620 nasm_free (line);
3621 break;
3622 }
3623 /*
3624 * The current file has ended; work down the istk
3625 */
3626 {
3627 Include *i = istk;
3628 fclose(i->fp);
3629 if (i->conds)
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003630 error(ERR_FATAL, "expected `%%endif' before end of file");
H. Peter Anvineba20a72002-04-30 20:53:55 +00003631 istk = i->next;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003632 list->downlevel (LIST_INCLUDE);
H. Peter Anvineba20a72002-04-30 20:53:55 +00003633 src_set_linnum(i->lineno);
3634 nasm_free ( src_set_fname(i->fname) );
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003635 nasm_free (i);
3636 if (!istk)
3637 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003638 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003639 }
3640
3641 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003642 * We must expand MMacro parameters and MMacro-local labels
3643 * _before_ we plunge into directive processing, to cope
3644 * with things like `%define something %1' such as STRUC
3645 * uses. Unless we're _defining_ a MMacro, in which case
3646 * those tokens should be left alone to go into the
H. Peter Anvineba20a72002-04-30 20:53:55 +00003647 * definition; and unless we're in a non-emitting
3648 * condition, in which case we don't want to meddle with
3649 * anything.
H. Peter Anvin76690a12002-04-30 20:52:49 +00003650 */
H. Peter Anvineba20a72002-04-30 20:53:55 +00003651 if (!defining && !(istk->conds && !emitting(istk->conds->state)))
H. Peter Anvin76690a12002-04-30 20:52:49 +00003652 tline = expand_mmac_params(tline);
3653
3654 /*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003655 * Check the line to see if it's a preprocessor directive.
3656 */
3657 ret = do_directive(tline);
3658 if (ret & 1) {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003659 continue;
3660 } else if (defining) {
3661 /*
3662 * We're defining a multi-line macro. We emit nothing
H. Peter Anvineba20a72002-04-30 20:53:55 +00003663 * at all, and just
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003664 * shove the tokenised line on to the macro definition.
3665 */
3666 Line *l = nasm_malloc(sizeof(Line));
3667 l->next = defining->expansion;
3668 l->first = tline;
3669 l->finishes = FALSE;
3670 defining->expansion = l;
3671 continue;
3672 } else if (istk->conds && !emitting(istk->conds->state)) {
3673 /*
3674 * We're in a non-emitting branch of a condition block.
3675 * Emit nothing at all, not even a blank line: when we
3676 * emerge from the condition we'll give a line-number
3677 * directive so we keep our place correctly.
3678 */
3679 free_tlist(tline);
3680 continue;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003681 } else if (istk->mstk && !istk->mstk->in_progress) {
3682 /*
3683 * We're in a %rep block which has been terminated, so
3684 * we're walking through to the %endrep without
3685 * emitting anything. Emit nothing at all, not even a
3686 * blank line: when we emerge from the %rep block we'll
3687 * give a line-number directive so we keep our place
3688 * correctly.
3689 */
3690 free_tlist(tline);
3691 continue;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003692 } else {
3693 tline = expand_smacro(tline);
3694 ret = expand_mmacro(tline);
3695 if (!ret) {
3696 /*
3697 * De-tokenise the line again, and emit it.
3698 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003699 line = detoken(tline, TRUE);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003700 free_tlist (tline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003701 break;
3702 } else {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003703 continue; /* expand_mmacro calls free_tlist */
3704 }
3705 }
3706 }
3707
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003708 return line;
3709}
3710
H. Peter Anvineba20a72002-04-30 20:53:55 +00003711static void pp_cleanup (void)
3712{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003713 int h;
3714
3715 if (defining) {
3716 error (ERR_NONFATAL, "end of file while still defining macro `%s'",
3717 defining->name);
H. Peter Anvineba20a72002-04-30 20:53:55 +00003718 free_mmacro (defining);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003719 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003720 while (cstk)
3721 ctx_pop();
3722 for (h=0; h<NHASH; h++) {
3723 while (mmacros[h]) {
3724 MMacro *m = mmacros[h];
3725 mmacros[h] = mmacros[h]->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003726 free_mmacro(m);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003727 }
3728 while (smacros[h]) {
3729 SMacro *s = smacros[h];
3730 smacros[h] = smacros[h]->next;
3731 nasm_free (s->name);
3732 free_tlist (s->expansion);
3733 nasm_free (s);
3734 }
3735 }
3736 while (istk) {
3737 Include *i = istk;
3738 istk = istk->next;
3739 fclose(i->fp);
3740 nasm_free (i->fname);
3741 nasm_free (i);
3742 }
3743 while (cstk)
3744 ctx_pop();
3745}
3746
H. Peter Anvineba20a72002-04-30 20:53:55 +00003747void pp_include_path (char *path)
3748{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003749 IncPath *i;
3750
3751 i = nasm_malloc(sizeof(IncPath));
3752 i->path = nasm_strdup(path);
3753 i->next = ipath;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003754 ipath = i;
3755}
3756
H. Peter Anvineba20a72002-04-30 20:53:55 +00003757void pp_pre_include (char *fname)
3758{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003759 Token *inc, *space, *name;
3760 Line *l;
3761
3762 inc = nasm_malloc(sizeof(Token));
3763 inc->next = space = nasm_malloc(sizeof(Token));
3764 space->next = name = nasm_malloc(sizeof(Token));
3765 name->next = NULL;
3766
3767 inc->type = TOK_PREPROC_ID;
3768 inc->text = nasm_strdup("%include");
3769 space->type = TOK_WHITESPACE;
3770 space->text = nasm_strdup(" ");
3771 name->type = TOK_INTERNAL_STRING;
3772 name->text = nasm_strdup(fname);
3773
3774 inc->mac = space->mac = name->mac = NULL;
3775
3776 l = nasm_malloc(sizeof(Line));
3777 l->next = predef;
3778 l->first = inc;
3779 l->finishes = FALSE;
3780 predef = l;
3781}
3782
H. Peter Anvineba20a72002-04-30 20:53:55 +00003783void pp_pre_define (char *definition)
3784{
3785 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003786 Line *l;
3787 char *equals;
3788
3789 equals = strchr(definition, '=');
3790
3791 def = nasm_malloc(sizeof(Token));
3792 def->next = space = nasm_malloc(sizeof(Token));
3793 if (equals)
3794 *equals = ' ';
H. Peter Anvineba20a72002-04-30 20:53:55 +00003795 space->next = tokenise(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003796 if (equals)
3797 *equals = '=';
3798
3799 def->type = TOK_PREPROC_ID;
3800 def->text = nasm_strdup("%define");
3801 space->type = TOK_WHITESPACE;
3802 space->text = nasm_strdup(" ");
3803
3804 def->mac = space->mac = NULL;
3805
3806 l = nasm_malloc(sizeof(Line));
3807 l->next = predef;
3808 l->first = def;
3809 l->finishes = FALSE;
3810 predef = l;
3811}
3812
H. Peter Anvin620515a2002-04-30 20:57:38 +00003813void pp_pre_undefine (char *definition)
3814{
3815 Token *def, *space;
3816 Line *l;
3817
3818 def = nasm_malloc(sizeof(Token));
3819 def->next = space = nasm_malloc(sizeof(Token));
3820 space->next = tokenise(definition);
3821
3822 def->type = TOK_PREPROC_ID;
3823 def->text = nasm_strdup("%undef");
3824 space->type = TOK_WHITESPACE;
3825 space->text = nasm_strdup(" ");
3826
3827 def->mac = space->mac = NULL;
3828
3829 l = nasm_malloc(sizeof(Line));
3830 l->next = predef;
3831 l->first = def;
3832 l->finishes = FALSE;
3833 predef = l;
3834}
3835
H. Peter Anvineba20a72002-04-30 20:53:55 +00003836void pp_extra_stdmac (char **macros)
3837{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003838 extrastdmac = macros;
3839}
3840
H. Peter Anvineba20a72002-04-30 20:53:55 +00003841static void make_tok_num(Token *tok, long val)
3842{
3843 char numbuf[20];
3844 sprintf(numbuf, "%ld", val);
3845 tok->text = nasm_strdup(numbuf);
3846 tok->type = TOK_NUMBER;
3847}
3848
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003849Preproc nasmpp = {
3850 pp_reset,
3851 pp_getline,
3852 pp_cleanup
3853};