blob: 267885da0cadbb64e394597b017aec3c04f57e52 [file] [log] [blame]
H. Peter Anvin65747262002-05-07 00:10:05 +00001/* -*- mode: c; c-file-style: "bsd" -*- */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002/* preproc.c macro preprocessor for the Netwide Assembler
3 *
4 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
5 * Julian Hall. All rights reserved. The software is
6 * redistributable under the licence given in the file "Licence"
7 * distributed in the NASM archive.
8 *
9 * initial version 18/iii/97 by Simon Tatham
10 */
11
H. Peter Anvin4836e332002-04-30 20:56:43 +000012/* Typical flow of text through preproc
13 *
14 * pp_getline gets tokenised lines, either
15 *
16 * from a macro expansion
17 *
18 * or
19 * {
20 * read_line gets raw text from stdmacpos, or predef, or current input file
21 * tokenise converts to tokens
22 * }
23 *
24 * expand_mmac_params is used to expand %1 etc., unless a macro is being
25 * defined or a false conditional is being processed
26 * (%0, %1, %+1, %-1, %%foo
27 *
28 * do_directive checks for directives
29 *
30 * expand_smacro is used to expand single line macros
31 *
32 * expand_mmacro is used to expand multi-line macros
33 *
34 * detoken is used to convert the line back to text
35 */
H. Peter Anvineba20a72002-04-30 20:53:55 +000036
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000037#include <stdio.h>
H. Peter Anvinaf535c12002-04-30 20:59:21 +000038#include <stdarg.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000039#include <stdlib.h>
40#include <stddef.h>
41#include <string.h>
42#include <ctype.h>
H. Peter Anvin76690a12002-04-30 20:52:49 +000043#include <limits.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000044
45#include "nasm.h"
46#include "nasmlib.h"
47
48typedef struct SMacro SMacro;
49typedef struct MMacro MMacro;
50typedef struct Context Context;
51typedef struct Token Token;
H. Peter Anvince616072002-04-30 21:02:23 +000052typedef struct Blocks Blocks;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000053typedef struct Line Line;
54typedef struct Include Include;
55typedef struct Cond Cond;
H. Peter Anvin6768eb72002-04-30 20:52:26 +000056typedef struct IncPath IncPath;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000057
58/*
59 * Store the definition of a single-line macro.
60 */
H. Peter Anvin734b1882002-04-30 21:01:08 +000061struct SMacro
62{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000063 SMacro *next;
64 char *name;
65 int casesense;
66 int nparam;
67 int in_progress;
68 Token *expansion;
69};
70
71/*
H. Peter Anvin76690a12002-04-30 20:52:49 +000072 * Store the definition of a multi-line macro. This is also used to
73 * store the interiors of `%rep...%endrep' blocks, which are
74 * effectively self-re-invoking multi-line macros which simply
75 * don't have a name or bother to appear in the hash tables. %rep
76 * blocks are signified by having a NULL `name' field.
77 *
78 * In a MMacro describing a `%rep' block, the `in_progress' field
79 * isn't merely boolean, but gives the number of repeats left to
80 * run.
81 *
82 * The `next' field is used for storing MMacros in hash tables; the
83 * `next_active' field is for stacking them on istk entries.
84 *
85 * When a MMacro is being expanded, `params', `iline', `nparam',
86 * `paramlen', `rotate' and `unique' are local to the invocation.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000087 */
H. Peter Anvin734b1882002-04-30 21:01:08 +000088struct MMacro
89{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000090 MMacro *next;
91 char *name;
92 int casesense;
93 int nparam_min, nparam_max;
H. Peter Anvin734b1882002-04-30 21:01:08 +000094 int plus; /* is the last parameter greedy? */
95 int nolist; /* is this macro listing-inhibited? */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000096 int in_progress;
H. Peter Anvin734b1882002-04-30 21:01:08 +000097 Token *dlist; /* All defaults as one list */
98 Token **defaults; /* Parameter default pointers */
99 int ndefs; /* number of default parameters */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000100 Line *expansion;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000101
102 MMacro *next_active;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000103 MMacro *rep_nest; /* used for nesting %rep */
104 Token **params; /* actual parameters */
105 Token *iline; /* invocation line */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000106 int nparam, rotate, *paramlen;
107 unsigned long unique;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000108 int lineno; /* Current line number on expansion */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000109};
110
111/*
112 * The context stack is composed of a linked list of these.
113 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000114struct Context
115{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000116 Context *next;
117 SMacro *localmac;
118 char *name;
119 unsigned long number;
120};
121
122/*
123 * This is the internal form which we break input lines up into.
124 * Typically stored in linked lists.
125 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000126 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
127 * necessarily used as-is, but is intended to denote the number of
128 * the substituted parameter. So in the definition
129 *
130 * %define a(x,y) ( (x) & ~(y) )
131 *
132 * the token representing `x' will have its type changed to
133 * TOK_SMAC_PARAM, but the one representing `y' will be
134 * TOK_SMAC_PARAM+1.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000135 *
136 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
137 * which doesn't need quotes around it. Used in the pre-include
138 * mechanism as an alternative to trying to find a sensible type of
139 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000140 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000141struct Token
142{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000143 Token *next;
144 char *text;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000145 SMacro *mac; /* associated macro for TOK_SMAC_END */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000146 int type;
147};
H. Peter Anvin734b1882002-04-30 21:01:08 +0000148enum
149{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000150 TOK_WHITESPACE = 1, TOK_COMMENT, TOK_ID, TOK_PREPROC_ID, TOK_STRING,
H. Peter Anvineba20a72002-04-30 20:53:55 +0000151 TOK_NUMBER, TOK_SMAC_END, TOK_OTHER, TOK_SMAC_PARAM,
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000152 TOK_INTERNAL_STRING
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000153};
154
155/*
156 * Multi-line macro definitions are stored as a linked list of
157 * these, which is essentially a container to allow several linked
158 * lists of Tokens.
159 *
160 * Note that in this module, linked lists are treated as stacks
161 * wherever possible. For this reason, Lines are _pushed_ on to the
162 * `expansion' field in MMacro structures, so that the linked list,
163 * if walked, would give the macro lines in reverse order; this
164 * means that we can walk the list when expanding a macro, and thus
165 * push the lines on to the `expansion' field in _istk_ in reverse
166 * order (so that when popped back off they are in the right
167 * order). It may seem cockeyed, and it relies on my design having
168 * an even number of steps in, but it works...
169 *
170 * Some of these structures, rather than being actual lines, are
171 * markers delimiting the end of the expansion of a given macro.
H. Peter Anvin76690a12002-04-30 20:52:49 +0000172 * This is for use in the cycle-tracking and %rep-handling code.
173 * Such structures have `finishes' non-NULL, and `first' NULL. All
174 * others have `finishes' NULL, but `first' may still be NULL if
175 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000176 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000177struct Line
178{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000179 Line *next;
180 MMacro *finishes;
181 Token *first;
182};
183
184/*
185 * To handle an arbitrary level of file inclusion, we maintain a
186 * stack (ie linked list) of these things.
187 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000188struct Include
189{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000190 Include *next;
191 FILE *fp;
192 Cond *conds;
193 Line *expansion;
194 char *fname;
195 int lineno, lineinc;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000196 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000197};
198
199/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000200 * Include search path. This is simply a list of strings which get
201 * prepended, in turn, to the name of an include file, in an
202 * attempt to find the file if it's not in the current directory.
203 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000204struct IncPath
205{
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000206 IncPath *next;
207 char *path;
208};
209
210/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000211 * Conditional assembly: we maintain a separate stack of these for
212 * each level of file inclusion. (The only reason we keep the
213 * stacks separate is to ensure that a stray `%endif' in a file
214 * included from within the true branch of a `%if' won't terminate
215 * it and cause confusion: instead, rightly, it'll cause an error.)
216 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000217struct Cond
218{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000219 Cond *next;
220 int state;
221};
H. Peter Anvin734b1882002-04-30 21:01:08 +0000222enum
223{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000224 /*
225 * These states are for use just after %if or %elif: IF_TRUE
226 * means the condition has evaluated to truth so we are
227 * currently emitting, whereas IF_FALSE means we are not
228 * currently emitting but will start doing so if a %else comes
229 * up. In these states, all directives are admissible: %elif,
230 * %else and %endif. (And of course %if.)
231 */
232 COND_IF_TRUE, COND_IF_FALSE,
233 /*
234 * These states come up after a %else: ELSE_TRUE means we're
235 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
236 * any %elif or %else will cause an error.
237 */
238 COND_ELSE_TRUE, COND_ELSE_FALSE,
239 /*
240 * This state means that we're not emitting now, and also that
241 * nothing until %endif will be emitted at all. It's for use in
242 * two circumstances: (i) when we've had our moment of emission
243 * and have now started seeing %elifs, and (ii) when the
244 * condition construct in question is contained within a
245 * non-emitting branch of a larger condition construct.
246 */
247 COND_NEVER
248};
249#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
250
251/*
252 * Condition codes. Note that we use c_ prefix not C_ because C_ is
253 * used in nasm.h for the "real" condition codes. At _this_ level,
254 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
255 * ones, so we need a different enum...
256 */
H. Peter Anvin0a7a3b42002-05-14 23:54:46 +0000257static const char *conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000258 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
259 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
260 "np", "ns", "nz", "o", "p", "pe", "po", "s", "z"
261};
H. Peter Anvin734b1882002-04-30 21:01:08 +0000262enum
263{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000264 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
265 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
266 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_S, c_Z
267};
268static int inverse_ccs[] = {
269 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
270 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,
271 c_Z, c_NO, c_NP, c_PO, c_PE, c_NS, c_NZ
272};
273
H. Peter Anvin76690a12002-04-30 20:52:49 +0000274/*
275 * Directive names.
276 */
H. Peter Anvin0a7a3b42002-05-14 23:54:46 +0000277static const char *directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000278 "%arg",
H. Peter Anvin76690a12002-04-30 20:52:49 +0000279 "%assign", "%clear", "%define", "%elif", "%elifctx", "%elifdef",
H. Peter Anvin65747262002-05-07 00:10:05 +0000280 "%elifid", "%elifidn", "%elifidni", "%elifmacro", "%elifnctx", "%elifndef",
281 "%elifnid", "%elifnidn", "%elifnidni", "%elifnmacro", "%elifnnum", "%elifnstr",
H. Peter Anvin76690a12002-04-30 20:52:49 +0000282 "%elifnum", "%elifstr", "%else", "%endif", "%endm", "%endmacro",
283 "%endrep", "%error", "%exitrep", "%iassign", "%idefine", "%if",
H. Peter Anvin65747262002-05-07 00:10:05 +0000284 "%ifctx", "%ifdef", "%ifid", "%ifidn", "%ifidni", "%ifmacro", "%ifnctx",
285 "%ifndef", "%ifnid", "%ifnidn", "%ifnidni", "%ifnmacro", "%ifnnum",
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000286 "%ifnstr", "%ifnum", "%ifstr", "%imacro", "%include",
287 "%ixdefine", "%line",
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000288 "%local",
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000289 "%macro", "%pop", "%push", "%rep", "%repl", "%rotate",
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000290 "%stacksize",
H. Peter Anvin734b1882002-04-30 21:01:08 +0000291 "%strlen", "%substr", "%undef", "%xdefine"
H. Peter Anvin76690a12002-04-30 20:52:49 +0000292};
H. Peter Anvin734b1882002-04-30 21:01:08 +0000293enum
294{
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000295 PP_ARG,
H. Peter Anvin76690a12002-04-30 20:52:49 +0000296 PP_ASSIGN, PP_CLEAR, PP_DEFINE, PP_ELIF, PP_ELIFCTX, PP_ELIFDEF,
H. Peter Anvin65747262002-05-07 00:10:05 +0000297 PP_ELIFID, PP_ELIFIDN, PP_ELIFIDNI, PP_ELIFMACRO, PP_ELIFNCTX, PP_ELIFNDEF,
298 PP_ELIFNID, PP_ELIFNIDN, PP_ELIFNIDNI, PP_ELIFNMACRO, PP_ELIFNNUM, PP_ELIFNSTR,
H. Peter Anvin76690a12002-04-30 20:52:49 +0000299 PP_ELIFNUM, PP_ELIFSTR, PP_ELSE, PP_ENDIF, PP_ENDM, PP_ENDMACRO,
300 PP_ENDREP, PP_ERROR, PP_EXITREP, PP_IASSIGN, PP_IDEFINE, PP_IF,
H. Peter Anvin65747262002-05-07 00:10:05 +0000301 PP_IFCTX, PP_IFDEF, PP_IFID, PP_IFIDN, PP_IFIDNI, PP_IFMACRO, PP_IFNCTX,
302 PP_IFNDEF, PP_IFNID, PP_IFNIDN, PP_IFNIDNI, PP_IFNMACRO, PP_IFNNUM,
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000303 PP_IFNSTR, PP_IFNUM, PP_IFSTR, PP_IMACRO, PP_INCLUDE,
304 PP_IXDEFINE, PP_LINE,
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000305 PP_LOCAL,
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000306 PP_MACRO, PP_POP, PP_PUSH, PP_REP, PP_REPL, PP_ROTATE,
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000307 PP_STACKSIZE,
H. Peter Anvin734b1882002-04-30 21:01:08 +0000308 PP_STRLEN, PP_SUBSTR, PP_UNDEF, PP_XDEFINE
H. Peter Anvin76690a12002-04-30 20:52:49 +0000309};
310
H. Peter Anvin65747262002-05-07 00:10:05 +0000311/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
312static int is_condition(int arg)
313{
314 return ((arg >= PP_ELIF) && (arg <= PP_ENDIF)) ||
315 ((arg >= PP_IF) && (arg <= PP_IFSTR));
316}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000317
318/* For TASM compatibility we need to be able to recognise TASM compatible
319 * conditional compilation directives. Using the NASM pre-processor does
320 * not work, so we look for them specifically from the following list and
321 * then jam in the equivalent NASM directive into the input stream.
322 */
323
324#ifndef MAX
325# define MAX(a,b) ( ((a) > (b)) ? (a) : (b))
326#endif
327
H. Peter Anvin734b1882002-04-30 21:01:08 +0000328enum
329{
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000330 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
331 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
332};
333
H. Peter Anvin0a7a3b42002-05-14 23:54:46 +0000334static const char *tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000335 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
336 "ifndef", "include", "local"
337};
338
339static int StackSize = 4;
340static char *StackPointer = "ebp";
341static int ArgOffset = 8;
342static int LocalOffset = 4;
343
H. Peter Anvin76690a12002-04-30 20:52:49 +0000344
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000345static Context *cstk;
346static Include *istk;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000347static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000348
H. Peter Anvin99941bf2002-05-14 17:44:03 +0000349static efunc _error; /* Pointer to client-provided error reporting function */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000350static evalfunc evaluate;
351
H. Peter Anvin620515a2002-04-30 20:57:38 +0000352static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000353
H. Peter Anvin734b1882002-04-30 21:01:08 +0000354static unsigned long unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000355
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000356static Line *predef = NULL;
357
358static ListGen *list;
359
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000360/*
361 * The number of hash values we use for the macro lookup tables.
H. Peter Anvineba20a72002-04-30 20:53:55 +0000362 * FIXME: We should *really* be able to configure this at run time,
363 * or even have the hash table automatically expanding when necessary.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000364 */
365#define NHASH 31
366
367/*
368 * The current set of multi-line macros we have defined.
369 */
370static MMacro *mmacros[NHASH];
371
372/*
373 * The current set of single-line macros we have defined.
374 */
375static SMacro *smacros[NHASH];
376
377/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000378 * The multi-line macro we are currently defining, or the %rep
379 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000380 */
381static MMacro *defining;
382
383/*
384 * The number of macro parameters to allocate space for at a time.
385 */
386#define PARAM_DELTA 16
387
388/*
389 * The standard macro set: defined as `static char *stdmac[]'. Also
390 * gives our position in the macro set, when we're processing it.
391 */
392#include "macros.c"
Ed Beroset168c9c02002-05-17 03:10:13 +0000393static const char **stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000394
395/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000396 * The extra standard macros that come from the object format, if
397 * any.
398 */
399static char **extrastdmac = NULL;
400int any_extrastdmac;
401
402/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000403 * Tokens are allocated in blocks to improve speed
404 */
405#define TOKEN_BLOCKSIZE 4096
406static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000407struct Blocks {
408 Blocks *next;
409 void *chunk;
410};
411
412static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000413
414/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000415 * Forward declarations.
416 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000417static Token *expand_mmac_params(Token * tline);
418static Token *expand_smacro(Token * tline);
419static Token *expand_id(Token * tline);
420static Context *get_ctx(char *name, int all_contexts);
421static void make_tok_num(Token * tok, long val);
Ed Beroset168c9c02002-05-17 03:10:13 +0000422static void error(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000423static void *new_Block(size_t size);
424static void delete_Blocks(void);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000425static Token *new_Token(Token * next, int type, char *text, int txtlen);
426static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000427
428/*
429 * Macros for safe checking of token pointers, avoid *(NULL)
430 */
431#define tok_type_(x,t) ((x) && (x)->type == (t))
432#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
433#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
434#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000435
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000436/* Handle TASM specific directives, which do not contain a % in
437 * front of them. We do it here because I could not find any other
438 * place to do it for the moment, and it is a hack (ideally it would
439 * be nice to be able to use the NASM pre-processor to do it).
440 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000441static char *
442check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000443{
444 int i, j, k, m, len;
445 char *p = line, *oldline, oldchar;
446
447 /* Skip whitespace */
448 while (isspace(*p) && *p != 0)
449 p++;
450
451 /* Binary search for the directive name */
452 i = -1;
453 j = sizeof(tasm_directives) / sizeof(*tasm_directives);
454 len = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000455 while (!isspace(p[len]) && p[len] != 0)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000456 len++;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000457 if (len)
458 {
459 oldchar = p[len];
460 p[len] = 0;
461 while (j - i > 1)
462 {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000463 k = (j + i) / 2;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000464 m = nasm_stricmp(p, tasm_directives[k]);
465 if (m == 0)
466 {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000467 /* We have found a directive, so jam a % in front of it
468 * so that NASM will then recognise it as one if it's own.
469 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000470 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000471 len = strlen(p);
472 oldline = line;
473 line = nasm_malloc(len + 2);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000474 line[0] = '%';
475 if (k == TM_IFDIFI)
476 {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000477 /* NASM does not recognise IFDIFI, so we convert it to
478 * %ifdef BOGUS. This is not used in NASM comaptible
479 * code, but does need to parse for the TASM macro
480 * package.
481 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000482 strcpy(line + 1, "ifdef BOGUS");
483 }
484 else
485 {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000486 memcpy(line + 1, p, len + 1);
487 }
488 nasm_free(oldline);
489 return line;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000490 }
491 else if (m < 0)
492 {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000493 j = k;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000494 }
495 else
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000496 i = k;
497 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000498 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000499 }
500 return line;
501}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000502
H. Peter Anvin76690a12002-04-30 20:52:49 +0000503/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000504 * The pre-preprocessing stage... This function translates line
505 * number indications as they emerge from GNU cpp (`# lineno "file"
506 * flags') into NASM preprocessor line number indications (`%line
507 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000508 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000509static char *
510prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000511{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000512 int lineno, fnlen;
513 char *fname, *oldline;
514
H. Peter Anvin734b1882002-04-30 21:01:08 +0000515 if (line[0] == '#' && line[1] == ' ')
516 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000517 oldline = line;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000518 fname = oldline + 2;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000519 lineno = atoi(fname);
520 fname += strspn(fname, "0123456789 ");
521 if (*fname == '"')
522 fname++;
523 fnlen = strcspn(fname, "\"");
H. Peter Anvin734b1882002-04-30 21:01:08 +0000524 line = nasm_malloc(20 + fnlen);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000525 sprintf(line, "%%line %d %.*s", lineno, fnlen, fname);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000526 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000527 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000528 if (tasm_compatible_mode)
529 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000530 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000531}
532
533/*
534 * The hash function for macro lookups. Note that due to some
535 * macros having case-insensitive names, the hash function must be
536 * invariant under case changes. We implement this by applying a
537 * perfectly normal hash function to the uppercase of the string.
538 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000539static int
540hash(char *s)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000541{
542 unsigned int h = 0;
543 int i = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000544 /*
545 * Powers of three, mod 31.
546 */
547 static const int multipliers[] = {
548 1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10,
549 30, 28, 22, 4, 12, 5, 15, 14, 11, 2, 6, 18, 23, 7, 21
550 };
H. Peter Anvineba20a72002-04-30 20:53:55 +0000551
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000552
H. Peter Anvin734b1882002-04-30 21:01:08 +0000553 while (*s)
554 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000555 h += multipliers[i] * (unsigned char) (toupper(*s));
556 s++;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000557 if (++i >= sizeof(multipliers) / sizeof(*multipliers))
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000558 i = 0;
559 }
560 h %= NHASH;
561 return h;
562}
563
564/*
565 * Free a linked list of tokens.
566 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000567static void
568free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000569{
H. Peter Anvin734b1882002-04-30 21:01:08 +0000570 while (list)
571 {
572 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000573 }
574}
575
576/*
577 * Free a linked list of lines.
578 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000579static void
580free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000581{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000582 Line *l;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000583 while (list)
584 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000585 l = list;
586 list = list->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000587 free_tlist(l->first);
588 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000589 }
590}
591
592/*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000593 * Free an MMacro
594 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000595static void
596free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000597{
H. Peter Anvin734b1882002-04-30 21:01:08 +0000598 nasm_free(m->name);
599 free_tlist(m->dlist);
600 nasm_free(m->defaults);
601 free_llist(m->expansion);
602 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000603}
604
605/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000606 * Pop the context stack.
607 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000608static void
609ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000610{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000611 Context *c = cstk;
612 SMacro *smac, *s;
613
614 cstk = cstk->next;
615 smac = c->localmac;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000616 while (smac)
617 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000618 s = smac;
619 smac = smac->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000620 nasm_free(s->name);
621 free_tlist(s->expansion);
622 nasm_free(s);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000623 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000624 nasm_free(c->name);
625 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000626}
627
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000628#define BUF_DELTA 512
629/*
630 * Read a line from the top file in istk, handling multiple CR/LFs
631 * at the end of the line read, and handling spurious ^Zs. Will
632 * return lines from the standard macro set if this has not already
633 * been done.
634 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000635static char *
636read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000637{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000638 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000639 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000640
H. Peter Anvin734b1882002-04-30 21:01:08 +0000641 if (stdmacpos)
642 {
643 if (*stdmacpos)
644 {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000645 char *ret = nasm_strdup(*stdmacpos++);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000646 if (!*stdmacpos && any_extrastdmac)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000647 {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000648 stdmacpos = extrastdmac;
649 any_extrastdmac = FALSE;
650 return ret;
651 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000652 /*
653 * Nasty hack: here we push the contents of `predef' on
654 * to the top-level expansion stack, since this is the
655 * most convenient way to implement the pre-include and
656 * pre-define features.
657 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000658 if (!*stdmacpos)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000659 {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000660 Line *pd, *l;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000661 Token *head, **tail, *t;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000662
H. Peter Anvin734b1882002-04-30 21:01:08 +0000663 for (pd = predef; pd; pd = pd->next)
664 {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000665 head = NULL;
666 tail = &head;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000667 for (t = pd->first; t; t = t->next)
668 {
669 *tail = new_Token(NULL, t->type, t->text, 0);
670 tail = &(*tail)->next;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000671 }
672 l = nasm_malloc(sizeof(Line));
673 l->next = istk->expansion;
674 l->first = head;
675 l->finishes = FALSE;
676 istk->expansion = l;
677 }
678 }
679 return ret;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000680 }
681 else
682 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000683 stdmacpos = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000684 }
685 }
686
687 bufsize = BUF_DELTA;
688 buffer = nasm_malloc(BUF_DELTA);
689 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000690 continued_count = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000691 while (1)
692 {
693 q = fgets(p, bufsize - (p - buffer), istk->fp);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000694 if (!q)
695 break;
696 p += strlen(p);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000697 if (p > buffer && p[-1] == '\n')
698 {
H. Peter Anvin9f394642002-04-30 21:07:51 +0000699 /* Convert backslash-CRLF line continuation sequences into
700 nothing at all (for DOS and Windows) */
701 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
702 p -= 3;
703 *p = 0;
704 continued_count++;
705 }
706 /* Also convert backslash-LF line continuation sequences into
707 nothing at all (for Unix) */
708 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
709 p -= 2;
710 *p = 0;
711 continued_count++;
712 }
713 else {
714 break;
715 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000716 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000717 if (p - buffer > bufsize - 10)
718 {
719 long offset = p - buffer;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000720 bufsize += BUF_DELTA;
721 buffer = nasm_realloc(buffer, bufsize);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000722 p = buffer + offset; /* prevent stale-pointer problems */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000723 }
724 }
725
H. Peter Anvin734b1882002-04-30 21:01:08 +0000726 if (!q && p == buffer)
727 {
728 nasm_free(buffer);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000729 return NULL;
730 }
731
H. Peter Anvin9a633fa2002-04-30 21:08:11 +0000732 src_set_linnum(src_get_linnum() + istk->lineinc + (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000733
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000734 /*
735 * Play safe: remove CRs as well as LFs, if any of either are
736 * present at the end of the line.
737 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000738 while (--p >= buffer && (*p == '\n' || *p == '\r'))
739 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000740
741 /*
742 * Handle spurious ^Z, which may be inserted into source files
743 * by some file transfer utilities.
744 */
745 buffer[strcspn(buffer, "\032")] = '\0';
746
H. Peter Anvin734b1882002-04-30 21:01:08 +0000747 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000748
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000749 return buffer;
750}
751
752/*
753 * Tokenise a line of text. This is a very simple process since we
754 * don't need to parse the value out of e.g. numeric tokens: we
755 * simply split one string into many.
756 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000757static Token *
758tokenise(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000759{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000760 char *p = line;
761 int type;
762 Token *list = NULL;
763 Token *t, **tail = &list;
764
H. Peter Anvin734b1882002-04-30 21:01:08 +0000765 while (*line)
766 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000767 p = line;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000768 if (*p == '%')
H. Peter Anvineba20a72002-04-30 20:53:55 +0000769 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000770 p++;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000771 if ( isdigit(*p) ||
772 ((*p == '-' || *p == '+') && isdigit(p[1])) ||
773 ((*p == '+') && (isspace(p[1]) || !p[1])))
774 {
775 do
776 {
777 p++;
778 }
779 while (isdigit(*p));
780 type = TOK_PREPROC_ID;
781 }
782 else if (*p == '{')
783 {
784 p++;
785 while (*p && *p != '}')
786 {
787 p[-1] = *p;
788 p++;
789 }
790 p[-1] = '\0';
791 if (*p)
792 p++;
793 type = TOK_PREPROC_ID;
794 }
795 else if (isidchar(*p) ||
796 ((*p == '!' || *p == '%' || *p == '$') &&
797 isidchar(p[1])))
798 {
799 do
800 {
801 p++;
802 }
803 while (isidchar(*p));
804 type = TOK_PREPROC_ID;
805 }
806 else
807 {
808 type = TOK_OTHER;
809 if (*p == '%')
810 p++;
811 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000812 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000813 else if (isidstart(*p) || (*p == '$' && isidstart(p[1])))
H. Peter Anvineba20a72002-04-30 20:53:55 +0000814 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000815 type = TOK_ID;
816 p++;
817 while (*p && isidchar(*p))
818 p++;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000819 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000820 else if (*p == '\'' || *p == '"')
821 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000822 /*
823 * A string token.
824 */
825 char c = *p;
826 p++;
827 type = TOK_STRING;
828 while (*p && *p != c)
829 p++;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000830 if (*p)
831 {
832 p++;
833 }
834 else
835 {
836 error(ERR_WARNING, "unterminated string");
837 }
838 }
839 else if (isnumstart(*p))
840 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000841 /*
842 * A number token.
843 */
844 type = TOK_NUMBER;
845 p++;
846 while (*p && isnumchar(*p))
847 p++;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000848 }
849 else if (isspace(*p))
850 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000851 type = TOK_WHITESPACE;
852 p++;
853 while (*p && isspace(*p))
854 p++;
855 /*
856 * Whitespace just before end-of-line is discarded by
857 * pretending it's a comment; whitespace just before a
858 * comment gets lumped into the comment.
859 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000860 if (!*p || *p == ';')
861 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000862 type = TOK_COMMENT;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000863 while (*p)
864 p++;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000865 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000866 }
867 else if (*p == ';')
868 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000869 type = TOK_COMMENT;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000870 while (*p)
871 p++;
872 }
873 else
874 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000875 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000876 * Anything else is an operator of some kind. We check
877 * for all the double-character operators (>>, <<, //,
878 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
879 * else is a single-character operator.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000880 */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000881 type = TOK_OTHER;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000882 if ((p[0] == '>' && p[1] == '>') ||
H. Peter Anvin734b1882002-04-30 21:01:08 +0000883 (p[0] == '<' && p[1] == '<') ||
884 (p[0] == '/' && p[1] == '/') ||
885 (p[0] == '<' && p[1] == '=') ||
886 (p[0] == '>' && p[1] == '=') ||
887 (p[0] == '=' && p[1] == '=') ||
888 (p[0] == '!' && p[1] == '=') ||
889 (p[0] == '<' && p[1] == '>') ||
890 (p[0] == '&' && p[1] == '&') ||
891 (p[0] == '|' && p[1] == '|') ||
892 (p[0] == '^' && p[1] == '^'))
H. Peter Anvineba20a72002-04-30 20:53:55 +0000893 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000894 p++;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000895 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000896 p++;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000897 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000898 if (type != TOK_COMMENT)
899 {
900 *tail = t = new_Token(NULL, type, line, p - line);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000901 tail = &t->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000902 }
903 line = p;
904 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000905 return list;
906}
907
H. Peter Anvince616072002-04-30 21:02:23 +0000908/*
909 * this function allocates a new managed block of memory and
910 * returns a pointer to the block. The managed blocks are
911 * deleted only all at once by the delete_Blocks function.
912 */
913static void *
914new_Block(size_t size)
915{
916 Blocks *b = &blocks;
917
918 /* first, get to the end of the linked list */
919 while (b->next)
920 b = b->next;
921 /* now allocate the requested chunk */
922 b->chunk = nasm_malloc(size);
923
924 /* now allocate a new block for the next request */
925 b->next = nasm_malloc(sizeof(Blocks));
926 /* and initialize the contents of the new block */
927 b->next->next = NULL;
928 b->next->chunk = NULL;
929 return b->chunk;
930}
931
932/*
933 * this function deletes all managed blocks of memory
934 */
935static void
936delete_Blocks(void)
937{
938 Blocks *a,*b = &blocks;
939
940 /*
941 * keep in mind that the first block, pointed to by blocks
942 * is a static and not dynamically allocated, so we don't
943 * free it.
944 */
945 while (b)
946 {
947 if (b->chunk)
948 nasm_free(b->chunk);
949 a = b;
950 b = b->next;
H. Peter Anvin9eb185b2002-04-30 21:02:47 +0000951 if (a != &blocks)
H. Peter Anvince616072002-04-30 21:02:23 +0000952 nasm_free(a);
953 }
954}
H. Peter Anvin734b1882002-04-30 21:01:08 +0000955
956/*
957 * this function creates a new Token and passes a pointer to it
958 * back to the caller. It sets the type and text elements, and
959 * also the mac and next elements to NULL.
960 */
961static Token *
962new_Token(Token * next, int type, char *text, int txtlen)
963{
964 Token *t;
965 int i;
966
967 if (freeTokens == NULL)
968 {
H. Peter Anvince616072002-04-30 21:02:23 +0000969 freeTokens = (Token *)new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
H. Peter Anvin734b1882002-04-30 21:01:08 +0000970 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
971 freeTokens[i].next = &freeTokens[i + 1];
972 freeTokens[i].next = NULL;
973 }
974 t = freeTokens;
975 freeTokens = t->next;
976 t->next = next;
977 t->mac = NULL;
978 t->type = type;
979 if (type == TOK_WHITESPACE || text == NULL)
980 {
981 t->text = NULL;
982 }
983 else
984 {
985 if (txtlen == 0)
986 txtlen = strlen(text);
987 t->text = nasm_malloc(1 + txtlen);
988 strncpy(t->text, text, txtlen);
989 t->text[txtlen] = '\0';
990 }
991 return t;
992}
993
994static Token *
995delete_Token(Token * t)
996{
997 Token *next = t->next;
998 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +0000999 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001000 freeTokens = t;
1001 return next;
1002}
1003
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001004/*
1005 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001006 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1007 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001008 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001009static char *
1010detoken(Token * tlist, int expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001011{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001012 Token *t;
1013 int len;
1014 char *line, *p;
1015
1016 len = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001017 for (t = tlist; t; t = t->next)
1018 {
1019 if (t->type == TOK_PREPROC_ID && t->text[1] == '!')
1020 {
1021 char *p = getenv(t->text + 2);
1022 nasm_free(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001023 if (p)
1024 t->text = nasm_strdup(p);
1025 else
1026 t->text = NULL;
1027 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001028 /* Expand local macros here and not during preprocessing */
1029 if (expand_locals &&
H. Peter Anvin734b1882002-04-30 21:01:08 +00001030 t->type == TOK_PREPROC_ID && t->text &&
1031 t->text[0] == '%' && t->text[1] == '$')
1032 {
1033 Context *ctx = get_ctx(t->text, FALSE);
1034 if (ctx)
1035 {
1036 char buffer[40];
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001037 char *p, *q = t->text + 2;
1038
H. Peter Anvin734b1882002-04-30 21:01:08 +00001039 q += strspn(q, "$");
1040 sprintf(buffer, "..@%lu.", ctx->number);
1041 p = nasm_strcat(buffer, q);
1042 nasm_free(t->text);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001043 t->text = p;
1044 }
1045 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001046 if (t->type == TOK_WHITESPACE)
1047 {
1048 len++;
1049 }
1050 else if (t->text)
1051 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001052 len += strlen(t->text);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001053 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001054 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001055 p = line = nasm_malloc(len + 1);
1056 for (t = tlist; t; t = t->next)
1057 {
1058 if (t->type == TOK_WHITESPACE)
1059 {
1060 *p = ' ';
1061 p++;
1062 *p = '\0';
1063 }
1064 else if (t->text)
1065 {
1066 strcpy(p, t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001067 p += strlen(p);
1068 }
1069 }
1070 *p = '\0';
1071 return line;
1072}
1073
1074/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001075 * A scanner, suitable for use by the expression evaluator, which
1076 * operates on a line of Tokens. Expects a pointer to a pointer to
1077 * the first token in the line to be passed in as its private_data
1078 * field.
1079 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001080static int
1081ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001082{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001083 Token **tlineptr = private_data;
1084 Token *tline;
1085
H. Peter Anvin734b1882002-04-30 21:01:08 +00001086 do
1087 {
H. Peter Anvin76690a12002-04-30 20:52:49 +00001088 tline = *tlineptr;
1089 *tlineptr = tline ? tline->next : NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001090 }
1091 while (tline && (tline->type == TOK_WHITESPACE ||
1092 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001093
1094 if (!tline)
1095 return tokval->t_type = TOKEN_EOS;
1096
1097 if (tline->text[0] == '$' && !tline->text[1])
1098 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001099 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvin76690a12002-04-30 20:52:49 +00001100 return tokval->t_type = TOKEN_BASE;
1101
H. Peter Anvin734b1882002-04-30 21:01:08 +00001102 if (tline->type == TOK_ID)
1103 {
H. Peter Anvin76690a12002-04-30 20:52:49 +00001104 tokval->t_charptr = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001105 if (tline->text[0] == '$')
1106 {
H. Peter Anvin76690a12002-04-30 20:52:49 +00001107 tokval->t_charptr++;
1108 return tokval->t_type = TOKEN_ID;
1109 }
1110
1111 /*
1112 * This is the only special case we actually need to worry
1113 * about in this restricted context.
1114 */
1115 if (!nasm_stricmp(tline->text, "seg"))
1116 return tokval->t_type = TOKEN_SEG;
1117
1118 return tokval->t_type = TOKEN_ID;
1119 }
1120
H. Peter Anvin734b1882002-04-30 21:01:08 +00001121 if (tline->type == TOK_NUMBER)
1122 {
H. Peter Anvin76690a12002-04-30 20:52:49 +00001123 int rn_error;
1124
1125 tokval->t_integer = readnum(tline->text, &rn_error);
1126 if (rn_error)
1127 return tokval->t_type = TOKEN_ERRNUM;
1128 tokval->t_charptr = NULL;
1129 return tokval->t_type = TOKEN_NUM;
1130 }
1131
H. Peter Anvin734b1882002-04-30 21:01:08 +00001132 if (tline->type == TOK_STRING)
1133 {
H. Peter Anvineba20a72002-04-30 20:53:55 +00001134 int rn_warn;
1135 char q, *r;
1136 int l;
1137
1138 r = tline->text;
1139 q = *r++;
1140 l = strlen(r);
1141
H. Peter Anvin734b1882002-04-30 21:01:08 +00001142 if (l == 0 || r[l - 1] != q)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001143 return tokval->t_type = TOKEN_ERRNUM;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001144 tokval->t_integer = readstrnum(r, l - 1, &rn_warn);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001145 if (rn_warn)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001146 error(ERR_WARNING | ERR_PASS1, "character constant too long");
H. Peter Anvineba20a72002-04-30 20:53:55 +00001147 tokval->t_charptr = NULL;
1148 return tokval->t_type = TOKEN_NUM;
1149 }
1150
H. Peter Anvin734b1882002-04-30 21:01:08 +00001151 if (tline->type == TOK_OTHER)
1152 {
1153 if (!strcmp(tline->text, "<<"))
1154 return tokval->t_type = TOKEN_SHL;
1155 if (!strcmp(tline->text, ">>"))
1156 return tokval->t_type = TOKEN_SHR;
1157 if (!strcmp(tline->text, "//"))
1158 return tokval->t_type = TOKEN_SDIV;
1159 if (!strcmp(tline->text, "%%"))
1160 return tokval->t_type = TOKEN_SMOD;
1161 if (!strcmp(tline->text, "=="))
1162 return tokval->t_type = TOKEN_EQ;
1163 if (!strcmp(tline->text, "<>"))
1164 return tokval->t_type = TOKEN_NE;
1165 if (!strcmp(tline->text, "!="))
1166 return tokval->t_type = TOKEN_NE;
1167 if (!strcmp(tline->text, "<="))
1168 return tokval->t_type = TOKEN_LE;
1169 if (!strcmp(tline->text, ">="))
1170 return tokval->t_type = TOKEN_GE;
1171 if (!strcmp(tline->text, "&&"))
1172 return tokval->t_type = TOKEN_DBL_AND;
1173 if (!strcmp(tline->text, "^^"))
1174 return tokval->t_type = TOKEN_DBL_XOR;
1175 if (!strcmp(tline->text, "||"))
1176 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001177 }
1178
1179 /*
1180 * We have no other options: just return the first character of
1181 * the token text.
1182 */
1183 return tokval->t_type = tline->text[0];
1184}
1185
1186/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001187 * Compare a string to the name of an existing macro; this is a
1188 * simple wrapper which calls either strcmp or nasm_stricmp
1189 * depending on the value of the `casesense' parameter.
1190 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001191static int
1192mstrcmp(char *p, char *q, int casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001193{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001194 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001195}
1196
1197/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001198 * Return the Context structure associated with a %$ token. Return
1199 * NULL, having _already_ reported an error condition, if the
1200 * context stack isn't deep enough for the supplied number of $
1201 * signs.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001202 * If all_contexts == TRUE, contexts that enclose current are
1203 * also scanned for such smacro, until it is found; if not -
1204 * only the context that directly results from the number of $'s
1205 * in variable's name.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001206 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001207static Context *
1208get_ctx(char *name, int all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001209{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001210 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001211 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001212 int i;
1213
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001214 if (!name || name[0] != '%' || name[1] != '$')
1215 return NULL;
1216
H. Peter Anvin734b1882002-04-30 21:01:08 +00001217 if (!cstk)
1218 {
1219 error(ERR_NONFATAL, "`%s': context stack is empty", name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001220 return NULL;
1221 }
1222
H. Peter Anvin734b1882002-04-30 21:01:08 +00001223 for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--)
1224 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001225 ctx = ctx->next;
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00001226/* i--; Lino - 02/25/02 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001227 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001228 if (!ctx)
1229 {
1230 error(ERR_NONFATAL, "`%s': context stack is only"
1231 " %d level%s deep", name, i - 1, (i == 2 ? "" : "s"));
1232 return NULL;
1233 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001234 if (!all_contexts)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001235 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001236
H. Peter Anvin734b1882002-04-30 21:01:08 +00001237 do
1238 {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001239 /* Search for this smacro in found context */
1240 m = ctx->localmac;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001241 while (m)
1242 {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001243 if (!mstrcmp(m->name, name, m->casesense))
1244 return ctx;
1245 m = m->next;
1246 }
1247 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001248 }
1249 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001250 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001251}
1252
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001253/* Add a slash to the end of a path if it is missing. We use the
1254 * forward slash to make it compatible with Unix systems.
1255 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001256static void
1257backslash(char *s)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001258{
1259 int pos = strlen(s);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001260 if (s[pos - 1] != '\\' && s[pos - 1] != '/')
1261 {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001262 s[pos] = '/';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001263 s[pos + 1] = '\0';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001264 }
1265}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001266
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001267/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001268 * Open an include file. This routine must always return a valid
1269 * file pointer if it returns - it's responsible for throwing an
1270 * ERR_FATAL and bombing out completely if not. It should also try
1271 * the include path one by one until it finds the file or reaches
1272 * the end of the path.
1273 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001274static FILE *
1275inc_fopen(char *file)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001276{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001277 FILE *fp;
1278 char *prefix = "", *combine;
1279 IncPath *ip = ipath;
H. Peter Anvin620515a2002-04-30 20:57:38 +00001280 static int namelen = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001281 int len = strlen(file);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001282
H. Peter Anvin734b1882002-04-30 21:01:08 +00001283 while (1)
1284 {
1285 combine = nasm_malloc(strlen(prefix) + 1 + len + 1);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001286 strcpy(combine, prefix);
1287 if (prefix[0] != 0)
1288 backslash(combine);
1289 strcat(combine, file);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001290 fp = fopen(combine, "r");
H. Peter Anvin620515a2002-04-30 20:57:38 +00001291 if (pass == 0 && fp)
1292 {
H. Peter Anvin734b1882002-04-30 21:01:08 +00001293 namelen += strlen(combine) + 1;
1294 if (namelen > 62)
1295 {
1296 printf(" \\\n ");
1297 namelen = 2;
1298 }
1299 printf(" %s", combine);
H. Peter Anvin620515a2002-04-30 20:57:38 +00001300 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001301 nasm_free(combine);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001302 if (fp)
1303 return fp;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001304 if (!ip)
1305 break;
1306 prefix = ip->path;
1307 ip = ip->next;
1308 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001309
H. Peter Anvin734b1882002-04-30 21:01:08 +00001310 error(ERR_FATAL, "unable to open include file `%s'", file);
1311 return NULL; /* never reached - placate compilers */
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001312}
1313
1314/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001315 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001316 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001317 * return TRUE if _any_ single-line macro of that name is defined.
1318 * Otherwise, will return TRUE if a single-line macro with either
1319 * `nparam' or no parameters is defined.
1320 *
1321 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001322 * defined, or nparam is -1, the address of the definition structure
1323 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001324 * is NULL, no action will be taken regarding its contents, and no
1325 * error will occur.
1326 *
1327 * Note that this is also called with nparam zero to resolve
1328 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001329 *
1330 * If you already know which context macro belongs to, you can pass
1331 * the context pointer as first parameter; if you won't but name begins
1332 * with %$ the context will be automatically computed. If all_contexts
1333 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001334 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001335static int
1336smacro_defined(Context * ctx, char *name, int nparam, SMacro ** defn,
1337 int nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001338{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001339 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001340
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001341 if (ctx)
1342 m = ctx->localmac;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001343 else if (name[0] == '%' && name[1] == '$')
1344 {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001345 if (cstk)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001346 ctx = get_ctx(name, FALSE);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001347 if (!ctx)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001348 return FALSE; /* got to return _something_ */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001349 m = ctx->localmac;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001350 }
1351 else
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001352 m = smacros[hash(name)];
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001353
H. Peter Anvin734b1882002-04-30 21:01:08 +00001354 while (m)
1355 {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001356 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
H. Peter Anvin734b1882002-04-30 21:01:08 +00001357 (nparam <= 0 || m->nparam == 0 || nparam == m->nparam))
1358 {
1359 if (defn)
1360 {
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001361 if (nparam == m->nparam || nparam == -1)
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001362 *defn = m;
1363 else
1364 *defn = NULL;
1365 }
1366 return TRUE;
1367 }
1368 m = m->next;
1369 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001370
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001371 return FALSE;
1372}
1373
1374/*
1375 * Count and mark off the parameters in a multi-line macro call.
1376 * This is called both from within the multi-line macro expansion
1377 * code, and also to mark off the default parameters when provided
1378 * in a %macro definition line.
1379 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001380static void
1381count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001382{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001383 int paramsize, brace;
1384
1385 *nparam = paramsize = 0;
1386 *params = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001387 while (t)
1388 {
1389 if (*nparam >= paramsize)
1390 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001391 paramsize += PARAM_DELTA;
1392 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1393 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001394 skip_white_(t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001395 brace = FALSE;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001396 if (tok_is_(t, "{"))
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001397 brace = TRUE;
1398 (*params)[(*nparam)++] = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001399 while (tok_isnt_(t, brace ? "}" : ","))
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001400 t = t->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001401 if (t)
1402 { /* got a comma/brace */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001403 t = t->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001404 if (brace)
1405 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001406 /*
1407 * Now we've found the closing brace, look further
1408 * for the comma.
1409 */
H. Peter Anvineba20a72002-04-30 20:53:55 +00001410 skip_white_(t);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001411 if (tok_isnt_(t, ","))
1412 {
1413 error(ERR_NONFATAL,
1414 "braces do not enclose all of macro parameter");
H. Peter Anvineba20a72002-04-30 20:53:55 +00001415 while (tok_isnt_(t, ","))
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001416 t = t->next;
1417 }
1418 if (t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001419 t = t->next; /* eat the comma */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001420 }
1421 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001422 }
1423}
1424
1425/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001426 * Determine whether one of the various `if' conditions is true or
1427 * not.
1428 *
1429 * We must free the tline we get passed.
1430 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001431static int
1432if_condition(Token * tline, int i)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001433{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001434 int j, casesense;
1435 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001436 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001437 expr *evalresult;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001438
1439 origline = tline;
1440
H. Peter Anvin734b1882002-04-30 21:01:08 +00001441 switch (i)
1442 {
1443 case PP_IFCTX:
1444 case PP_ELIFCTX:
1445 case PP_IFNCTX:
1446 case PP_ELIFNCTX:
1447 j = FALSE; /* have we matched yet? */
1448 while (cstk && tline)
1449 {
1450 skip_white_(tline);
1451 if (!tline || tline->type != TOK_ID)
1452 {
1453 error(ERR_NONFATAL,
1454 "`%s' expects context identifiers",
1455 directives[i]);
1456 free_tlist(origline);
1457 return -1;
1458 }
1459 if (!nasm_stricmp(tline->text, cstk->name))
1460 j = TRUE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001461 tline = tline->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001462 }
1463 if (i == PP_IFNCTX || i == PP_ELIFNCTX)
1464 j = !j;
1465 free_tlist(origline);
1466 return j;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001467
H. Peter Anvin734b1882002-04-30 21:01:08 +00001468 case PP_IFDEF:
1469 case PP_ELIFDEF:
1470 case PP_IFNDEF:
1471 case PP_ELIFNDEF:
1472 j = FALSE; /* have we matched yet? */
1473 while (tline)
1474 {
1475 skip_white_(tline);
1476 if (!tline || (tline->type != TOK_ID &&
1477 (tline->type != TOK_PREPROC_ID ||
1478 tline->text[1] != '$')))
1479 {
1480 error(ERR_NONFATAL,
H. Peter Anvin65747262002-05-07 00:10:05 +00001481 "`%s' expects macro identifiers",
1482 directives[i]);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001483 free_tlist(origline);
1484 return -1;
1485 }
1486 if (smacro_defined(NULL, tline->text, 0, NULL, 1))
1487 j = TRUE;
1488 tline = tline->next;
1489 }
1490 if (i == PP_IFNDEF || i == PP_ELIFNDEF)
1491 j = !j;
1492 free_tlist(origline);
1493 return j;
1494
1495 case PP_IFIDN:
1496 case PP_ELIFIDN:
1497 case PP_IFNIDN:
1498 case PP_ELIFNIDN:
1499 case PP_IFIDNI:
1500 case PP_ELIFIDNI:
1501 case PP_IFNIDNI:
1502 case PP_ELIFNIDNI:
1503 tline = expand_smacro(tline);
1504 t = tt = tline;
1505 while (tok_isnt_(tt, ","))
1506 tt = tt->next;
1507 if (!tt)
1508 {
1509 error(ERR_NONFATAL,
1510 "`%s' expects two comma-separated arguments",
1511 directives[i]);
1512 free_tlist(tline);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001513 return -1;
1514 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001515 tt = tt->next;
1516 casesense = (i == PP_IFIDN || i == PP_ELIFIDN ||
1517 i == PP_IFNIDN || i == PP_ELIFNIDN);
1518 j = TRUE; /* assume equality unless proved not */
1519 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt)
1520 {
1521 if (tt->type == TOK_OTHER && !strcmp(tt->text, ","))
1522 {
1523 error(ERR_NONFATAL, "`%s': more than one comma on line",
1524 directives[i]);
1525 free_tlist(tline);
1526 return -1;
1527 }
1528 if (t->type == TOK_WHITESPACE)
1529 {
1530 t = t->next;
1531 continue;
1532 }
1533 else if (tt->type == TOK_WHITESPACE)
1534 {
1535 tt = tt->next;
1536 continue;
1537 }
1538 else if (tt->type != t->type ||
1539 mstrcmp(tt->text, t->text, casesense))
1540 {
1541 j = FALSE; /* found mismatching tokens */
1542 break;
1543 }
1544 else
1545 {
1546 t = t->next;
1547 tt = tt->next;
1548 continue;
1549 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001550 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001551 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
1552 j = FALSE; /* trailing gunk on one end or other */
1553 if (i == PP_IFNIDN || i == PP_ELIFNIDN ||
1554 i == PP_IFNIDNI || i == PP_ELIFNIDNI)
1555 j = !j;
1556 free_tlist(tline);
1557 return j;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001558
H. Peter Anvin65747262002-05-07 00:10:05 +00001559 case PP_IFMACRO:
1560 case PP_ELIFMACRO:
1561 case PP_IFNMACRO:
1562 case PP_ELIFNMACRO:
1563 {
1564 int found = 0;
1565 MMacro searching, *mmac;
1566
1567 tline = tline->next;
1568 skip_white_(tline);
1569 tline = expand_id(tline);
1570 if (!tok_type_(tline, TOK_ID))
1571 {
1572 error(ERR_NONFATAL,
1573 "`%s' expects a macro name",
1574 directives[i]);
1575 return -1;
1576 }
1577 searching.name = nasm_strdup(tline->text);
1578 searching.casesense = (i == PP_MACRO);
1579 searching.plus = FALSE;
1580 searching.nolist = FALSE;
1581 searching.in_progress = FALSE;
1582 searching.rep_nest = NULL;
1583 searching.nparam_min = 0;
1584 searching.nparam_max = INT_MAX;
1585 tline = expand_smacro(tline->next);
1586 skip_white_(tline);
1587 if (!tline)
1588 {
1589 } else if (!tok_type_(tline, TOK_NUMBER))
1590 {
1591 error(ERR_NONFATAL,
1592 "`%s' expects a parameter count or nothing",
1593 directives[i]);
1594 }
1595 else
1596 {
1597 searching.nparam_min = searching.nparam_max =
1598 readnum(tline->text, &j);
1599 if (j)
1600 error(ERR_NONFATAL,
1601 "unable to parse parameter count `%s'",
1602 tline->text);
1603 }
1604 if (tline && tok_is_(tline->next, "-"))
1605 {
1606 tline = tline->next->next;
1607 if (tok_is_(tline, "*"))
1608 searching.nparam_max = INT_MAX;
1609 else if (!tok_type_(tline, TOK_NUMBER))
1610 error(ERR_NONFATAL,
1611 "`%s' expects a parameter count after `-'",
1612 directives[i]);
1613 else
1614 {
1615 searching.nparam_max = readnum(tline->text, &j);
1616 if (j)
1617 error(ERR_NONFATAL,
1618 "unable to parse parameter count `%s'",
1619 tline->text);
1620 if (searching.nparam_min > searching.nparam_max)
1621 error(ERR_NONFATAL,
1622 "minimum parameter count exceeds maximum");
1623 }
1624 }
1625 if (tline && tok_is_(tline->next, "+"))
1626 {
1627 tline = tline->next;
1628 searching.plus = TRUE;
1629 }
1630 mmac = mmacros[hash(searching.name)];
1631 while (mmac)
1632 {
1633 if (!strcmp(mmac->name, searching.name) &&
1634 (mmac->nparam_min <= searching.nparam_max
1635 || searching.plus)
1636 && (searching.nparam_min <= mmac->nparam_max
1637 || mmac->plus))
1638 {
1639 found = TRUE;
1640 break;
1641 }
1642 mmac = mmac->next;
1643 }
1644 nasm_free(searching.name);
1645 free_tlist(origline);
1646 if (i == PP_IFNMACRO || i == PP_ELIFNMACRO)
1647 found = !found;
1648 return found;
1649 }
1650
H. Peter Anvin734b1882002-04-30 21:01:08 +00001651 case PP_IFID:
1652 case PP_ELIFID:
1653 case PP_IFNID:
1654 case PP_ELIFNID:
1655 case PP_IFNUM:
1656 case PP_ELIFNUM:
1657 case PP_IFNNUM:
1658 case PP_ELIFNNUM:
1659 case PP_IFSTR:
1660 case PP_ELIFSTR:
1661 case PP_IFNSTR:
1662 case PP_ELIFNSTR:
1663 tline = expand_smacro(tline);
1664 t = tline;
1665 while (tok_type_(t, TOK_WHITESPACE))
1666 t = t->next;
1667 j = FALSE; /* placate optimiser */
1668 if (t)
1669 switch (i)
1670 {
1671 case PP_IFID:
1672 case PP_ELIFID:
1673 case PP_IFNID:
1674 case PP_ELIFNID:
1675 j = (t->type == TOK_ID);
1676 break;
1677 case PP_IFNUM:
1678 case PP_ELIFNUM:
1679 case PP_IFNNUM:
1680 case PP_ELIFNNUM:
1681 j = (t->type == TOK_NUMBER);
1682 break;
1683 case PP_IFSTR:
1684 case PP_ELIFSTR:
1685 case PP_IFNSTR:
1686 case PP_ELIFNSTR:
1687 j = (t->type == TOK_STRING);
1688 break;
1689 }
1690 if (i == PP_IFNID || i == PP_ELIFNID ||
1691 i == PP_IFNNUM || i == PP_ELIFNNUM ||
1692 i == PP_IFNSTR || i == PP_ELIFNSTR)
1693 j = !j;
1694 free_tlist(tline);
1695 return j;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001696
H. Peter Anvin734b1882002-04-30 21:01:08 +00001697 case PP_IF:
1698 case PP_ELIF:
1699 t = tline = expand_smacro(tline);
1700 tptr = &t;
1701 tokval.t_type = TOKEN_INVALID;
1702 evalresult = evaluate(ppscan, tptr, &tokval,
1703 NULL, pass | CRITICAL, error, NULL);
1704 free_tlist(tline);
1705 if (!evalresult)
1706 return -1;
1707 if (tokval.t_type)
1708 error(ERR_WARNING,
1709 "trailing garbage after expression ignored");
1710 if (!is_simple(evalresult))
1711 {
1712 error(ERR_NONFATAL,
1713 "non-constant value given to `%s'", directives[i]);
1714 return -1;
1715 }
1716 return reloc_value(evalresult) != 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001717
H. Peter Anvin734b1882002-04-30 21:01:08 +00001718 default:
1719 error(ERR_FATAL,
1720 "preprocessor directive `%s' not yet implemented",
1721 directives[i]);
1722 free_tlist(origline);
1723 return -1; /* yeah, right */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001724 }
1725}
1726
1727/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001728 * Expand macros in a string. Used in %error and %include directives.
1729 * First tokenise the string, apply "expand_smacro" and then de-tokenise back.
1730 * The returned variable should ALWAYS be freed after usage.
1731 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001732void
1733expand_macros_in_string(char **p)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001734{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001735 Token *line = tokenise(*p);
1736 line = expand_smacro(line);
1737 *p = detoken(line, FALSE);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001738}
1739
1740/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001741 * Find out if a line contains a preprocessor directive, and deal
1742 * with it if so.
1743 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00001744 * If a directive _is_ found, we are expected to free_tlist() the
1745 * line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001746 *
1747 * Return values go like this:
1748 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00001749 * bit 0 is set if a directive was found (so the line gets freed)
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001750 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001751static int
1752do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001753{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001754 int i, j, k, m, nparam, nolist;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001755 int offset;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001756 char *p, *mname;
1757 Include *inc;
1758 Context *ctx;
1759 Cond *cond;
1760 SMacro *smac, **smhead;
1761 MMacro *mmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001762 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1763 Line *l;
1764 struct tokenval tokval;
1765 expr *evalresult;
H. Peter Anvinb64535f2002-04-30 20:55:37 +00001766 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001767
1768 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001769
H. Peter Anvineba20a72002-04-30 20:53:55 +00001770 skip_white_(tline);
1771 if (!tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvin734b1882002-04-30 21:01:08 +00001772 (tline->text[1] == '%' || tline->text[1] == '$'
1773 || tline->text[1] == '!'))
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001774 return 0;
1775
1776 i = -1;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001777 j = sizeof(directives) / sizeof(*directives);
1778 while (j - i > 1)
1779 {
1780 k = (j + i) / 2;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001781 m = nasm_stricmp(tline->text, directives[k]);
1782 if (m == 0) {
H. Peter Anvin734b1882002-04-30 21:01:08 +00001783 if (tasm_compatible_mode) {
1784 i = k;
1785 j = -2;
1786 } else if (k != PP_ARG && k != PP_LOCAL && k != PP_STACKSIZE) {
1787 i = k;
1788 j = -2;
1789 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001790 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001791 }
1792 else if (m < 0) {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001793 j = k;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001794 }
1795 else
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001796 i = k;
1797 }
1798
1799 /*
1800 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00001801 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001802 * we should ignore all directives except for condition
1803 * directives.
1804 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001805 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvin65747262002-05-07 00:10:05 +00001806 (istk->mstk && !istk->mstk->in_progress)) &&
1807 !is_condition(i))
H. Peter Anvineba20a72002-04-30 20:53:55 +00001808 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001809 return 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001810 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001811
1812 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001813 * If we're defining a macro or reading a %rep block, we should
1814 * ignore all directives except for %macro/%imacro (which
1815 * generate an error), %endm/%endmacro, and (only if we're in a
H. Peter Anvineba20a72002-04-30 20:53:55 +00001816 * %rep block) %endrep. If we're in a %rep block, another %rep
1817 * causes an error, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001818 */
1819 if (defining && i != PP_MACRO && i != PP_IMACRO &&
H. Peter Anvin734b1882002-04-30 21:01:08 +00001820 i != PP_ENDMACRO && i != PP_ENDM &&
1821 (defining->name || (i != PP_ENDREP && i != PP_REP)))
H. Peter Anvineba20a72002-04-30 20:53:55 +00001822 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001823 return 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001824 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001825
H. Peter Anvin734b1882002-04-30 21:01:08 +00001826 if (j != -2)
1827 {
H. Peter Anvineba20a72002-04-30 20:53:55 +00001828 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
H. Peter Anvin734b1882002-04-30 21:01:08 +00001829 tline->text);
1830 return 0; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001831 }
1832
H. Peter Anvin734b1882002-04-30 21:01:08 +00001833 switch (i)
1834 {
1835 case PP_STACKSIZE:
1836 /* Directive to tell NASM what the default stack size is. The
1837 * default is for a 16-bit stack, and this can be overriden with
1838 * %stacksize large.
1839 * the following form:
1840 *
1841 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001842 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001843 tline = tline->next;
1844 if (tline && tline->type == TOK_WHITESPACE)
1845 tline = tline->next;
1846 if (!tline || tline->type != TOK_ID)
1847 {
1848 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
1849 free_tlist(origline);
1850 return 3;
1851 }
1852 if (nasm_stricmp(tline->text, "flat") == 0)
1853 {
1854 /* All subsequent ARG directives are for a 32-bit stack */
1855 StackSize = 4;
1856 StackPointer = "ebp";
1857 ArgOffset = 8;
1858 LocalOffset = 4;
1859 }
1860 else if (nasm_stricmp(tline->text, "large") == 0)
1861 {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001862 /* All subsequent ARG directives are for a 16-bit stack,
H. Peter Anvin734b1882002-04-30 21:01:08 +00001863 * far function call.
1864 */
1865 StackSize = 2;
1866 StackPointer = "bp";
1867 ArgOffset = 4;
1868 LocalOffset = 2;
1869 }
1870 else if (nasm_stricmp(tline->text, "small") == 0)
1871 {
1872 /* All subsequent ARG directives are for a 16-bit stack,
1873 * far function call. We don't support near functions.
1874 */
1875 StackSize = 2;
1876 StackPointer = "bp";
1877 ArgOffset = 6;
1878 LocalOffset = 2;
1879 }
1880 else
1881 {
1882 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
1883 free_tlist(origline);
1884 return 3;
1885 }
1886 free_tlist(origline);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001887 return 3;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001888
H. Peter Anvin734b1882002-04-30 21:01:08 +00001889 case PP_ARG:
1890 /* TASM like ARG directive to define arguments to functions, in
1891 * the following form:
1892 *
1893 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1894 */
1895 offset = ArgOffset;
1896 do
1897 {
1898 char *arg, directive[256];
1899 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001900
H. Peter Anvin734b1882002-04-30 21:01:08 +00001901 /* Find the argument name */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001902 tline = tline->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001903 if (tline && tline->type == TOK_WHITESPACE)
1904 tline = tline->next;
1905 if (!tline || tline->type != TOK_ID)
1906 {
1907 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
1908 free_tlist(origline);
1909 return 3;
1910 }
1911 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001912
H. Peter Anvin734b1882002-04-30 21:01:08 +00001913 /* Find the argument size type */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001914 tline = tline->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001915 if (!tline || tline->type != TOK_OTHER
1916 || tline->text[0] != ':')
1917 {
1918 error(ERR_NONFATAL,
1919 "Syntax error processing `%%arg' directive");
1920 free_tlist(origline);
1921 return 3;
1922 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001923 tline = tline->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001924 if (!tline || tline->type != TOK_ID)
1925 {
1926 error(ERR_NONFATAL,
1927 "`%%arg' missing size type parameter");
1928 free_tlist(origline);
1929 return 3;
1930 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001931
H. Peter Anvin734b1882002-04-30 21:01:08 +00001932 /* Allow macro expansion of type parameter */
1933 tt = tokenise(tline->text);
1934 tt = expand_smacro(tt);
1935 if (nasm_stricmp(tt->text, "byte") == 0)
1936 {
1937 size = MAX(StackSize, 1);
1938 }
1939 else if (nasm_stricmp(tt->text, "word") == 0)
1940 {
1941 size = MAX(StackSize, 2);
1942 }
1943 else if (nasm_stricmp(tt->text, "dword") == 0)
1944 {
1945 size = MAX(StackSize, 4);
1946 }
1947 else if (nasm_stricmp(tt->text, "qword") == 0)
1948 {
1949 size = MAX(StackSize, 8);
1950 }
1951 else if (nasm_stricmp(tt->text, "tword") == 0)
1952 {
1953 size = MAX(StackSize, 10);
1954 }
1955 else
1956 {
1957 error(ERR_NONFATAL,
1958 "Invalid size type for `%%arg' missing directive");
1959 free_tlist(tt);
1960 free_tlist(origline);
1961 return 3;
1962 }
1963 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001964
H. Peter Anvin734b1882002-04-30 21:01:08 +00001965 /* Now define the macro for the argument */
1966 sprintf(directive, "%%define %s (%s+%d)", arg, StackPointer,
1967 offset);
1968 do_directive(tokenise(directive));
1969 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001970
H. Peter Anvin734b1882002-04-30 21:01:08 +00001971 /* Move to the next argument in the list */
1972 tline = tline->next;
1973 if (tline && tline->type == TOK_WHITESPACE)
1974 tline = tline->next;
1975 }
1976 while (tline && tline->type == TOK_OTHER
1977 && tline->text[0] == ',');
1978 free_tlist(origline);
1979 return 3;
1980
1981 case PP_LOCAL:
1982 /* TASM like LOCAL directive to define local variables for a
1983 * function, in the following form:
1984 *
1985 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
1986 *
1987 * The '= LocalSize' at the end is ignored by NASM, but is
1988 * required by TASM to define the local parameter size (and used
1989 * by the TASM macro package).
1990 */
1991 offset = LocalOffset;
1992 do
1993 {
1994 char *local, directive[256];
1995 int size = StackSize;
1996
1997 /* Find the argument name */
1998 tline = tline->next;
1999 if (tline && tline->type == TOK_WHITESPACE)
2000 tline = tline->next;
2001 if (!tline || tline->type != TOK_ID)
2002 {
2003 error(ERR_NONFATAL,
2004 "`%%local' missing argument parameter");
2005 free_tlist(origline);
2006 return 3;
2007 }
2008 local = tline->text;
2009
2010 /* Find the argument size type */
2011 tline = tline->next;
2012 if (!tline || tline->type != TOK_OTHER
2013 || tline->text[0] != ':')
2014 {
2015 error(ERR_NONFATAL,
2016 "Syntax error processing `%%local' directive");
2017 free_tlist(origline);
2018 return 3;
2019 }
2020 tline = tline->next;
2021 if (!tline || tline->type != TOK_ID)
2022 {
2023 error(ERR_NONFATAL,
2024 "`%%local' missing size type parameter");
2025 free_tlist(origline);
2026 return 3;
2027 }
2028
2029 /* Allow macro expansion of type parameter */
2030 tt = tokenise(tline->text);
2031 tt = expand_smacro(tt);
2032 if (nasm_stricmp(tt->text, "byte") == 0)
2033 {
2034 size = MAX(StackSize, 1);
2035 }
2036 else if (nasm_stricmp(tt->text, "word") == 0)
2037 {
2038 size = MAX(StackSize, 2);
2039 }
2040 else if (nasm_stricmp(tt->text, "dword") == 0)
2041 {
2042 size = MAX(StackSize, 4);
2043 }
2044 else if (nasm_stricmp(tt->text, "qword") == 0)
2045 {
2046 size = MAX(StackSize, 8);
2047 }
2048 else if (nasm_stricmp(tt->text, "tword") == 0)
2049 {
2050 size = MAX(StackSize, 10);
2051 }
2052 else
2053 {
2054 error(ERR_NONFATAL,
2055 "Invalid size type for `%%local' missing directive");
2056 free_tlist(tt);
2057 free_tlist(origline);
2058 return 3;
2059 }
2060 free_tlist(tt);
2061
2062 /* Now define the macro for the argument */
2063 sprintf(directive, "%%define %s (%s-%d)", local, StackPointer,
2064 offset);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002065 do_directive(tokenise(directive));
2066 offset += size;
2067
2068 /* Now define the assign to setup the enter_c macro correctly */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002069 sprintf(directive, "%%assign %%$localsize %%$localsize+%d",
2070 size);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002071 do_directive(tokenise(directive));
2072
H. Peter Anvin734b1882002-04-30 21:01:08 +00002073 /* Move to the next argument in the list */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002074 tline = tline->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002075 if (tline && tline->type == TOK_WHITESPACE)
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002076 tline = tline->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002077 }
2078 while (tline && tline->type == TOK_OTHER
2079 && tline->text[0] == ',');
2080 free_tlist(origline);
2081 return 3;
2082
2083 case PP_CLEAR:
2084 if (tline->next)
2085 error(ERR_WARNING,
2086 "trailing garbage after `%%clear' ignored");
2087 for (j = 0; j < NHASH; j++)
2088 {
2089 while (mmacros[j])
2090 {
2091 MMacro *m = mmacros[j];
2092 mmacros[j] = m->next;
2093 free_mmacro(m);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002094 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002095 while (smacros[j])
2096 {
2097 SMacro *s = smacros[j];
2098 smacros[j] = smacros[j]->next;
2099 nasm_free(s->name);
2100 free_tlist(s->expansion);
2101 nasm_free(s);
2102 }
2103 }
2104 free_tlist(origline);
2105 return 3;
2106
2107 case PP_INCLUDE:
2108 tline = tline->next;
2109 skip_white_(tline);
2110 if (!tline || (tline->type != TOK_STRING &&
2111 tline->type != TOK_INTERNAL_STRING))
2112 {
2113 error(ERR_NONFATAL, "`%%include' expects a file name");
2114 free_tlist(origline);
2115 return 3; /* but we did _something_ */
2116 }
2117 if (tline->next)
2118 error(ERR_WARNING,
2119 "trailing garbage after `%%include' ignored");
2120 if (tline->type != TOK_INTERNAL_STRING)
2121 {
2122 p = tline->text + 1; /* point past the quote to the name */
2123 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
2124 }
2125 else
2126 p = tline->text; /* internal_string is easier */
2127 expand_macros_in_string(&p);
2128 inc = nasm_malloc(sizeof(Include));
2129 inc->next = istk;
2130 inc->conds = NULL;
2131 inc->fp = inc_fopen(p);
2132 inc->fname = src_set_fname(p);
2133 inc->lineno = src_set_linnum(0);
2134 inc->lineinc = 1;
2135 inc->expansion = NULL;
2136 inc->mstk = NULL;
2137 istk = inc;
2138 list->uplevel(LIST_INCLUDE);
2139 free_tlist(origline);
2140 return 5;
2141
2142 case PP_PUSH:
2143 tline = tline->next;
2144 skip_white_(tline);
2145 tline = expand_id(tline);
2146 if (!tok_type_(tline, TOK_ID))
2147 {
2148 error(ERR_NONFATAL, "`%%push' expects a context identifier");
2149 free_tlist(origline);
2150 return 3; /* but we did _something_ */
2151 }
2152 if (tline->next)
2153 error(ERR_WARNING, "trailing garbage after `%%push' ignored");
2154 ctx = nasm_malloc(sizeof(Context));
2155 ctx->next = cstk;
2156 ctx->localmac = NULL;
2157 ctx->name = nasm_strdup(tline->text);
2158 ctx->number = unique++;
2159 cstk = ctx;
2160 free_tlist(origline);
2161 break;
2162
2163 case PP_REPL:
2164 tline = tline->next;
2165 skip_white_(tline);
2166 tline = expand_id(tline);
2167 if (!tok_type_(tline, TOK_ID))
2168 {
2169 error(ERR_NONFATAL, "`%%repl' expects a context identifier");
2170 free_tlist(origline);
2171 return 3; /* but we did _something_ */
2172 }
2173 if (tline->next)
2174 error(ERR_WARNING, "trailing garbage after `%%repl' ignored");
2175 if (!cstk)
2176 error(ERR_NONFATAL, "`%%repl': context stack is empty");
2177 else
2178 {
2179 nasm_free(cstk->name);
2180 cstk->name = nasm_strdup(tline->text);
2181 }
2182 free_tlist(origline);
2183 break;
2184
2185 case PP_POP:
2186 if (tline->next)
2187 error(ERR_WARNING, "trailing garbage after `%%pop' ignored");
2188 if (!cstk)
2189 error(ERR_NONFATAL,
2190 "`%%pop': context stack is already empty");
2191 else
2192 ctx_pop();
2193 free_tlist(origline);
2194 break;
2195
2196 case PP_ERROR:
2197 tline->next = expand_smacro(tline->next);
2198 tline = tline->next;
2199 skip_white_(tline);
2200 if (tok_type_(tline, TOK_STRING))
2201 {
2202 p = tline->text + 1; /* point past the quote to the name */
2203 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
2204 expand_macros_in_string(&p);
2205 error(ERR_NONFATAL, "%s", p);
2206 nasm_free(p);
2207 }
2208 else
2209 {
2210 p = detoken(tline, FALSE);
2211 error(ERR_WARNING, "%s", p);
2212 nasm_free(p);
2213 }
2214 free_tlist(origline);
2215 break;
2216
2217 case PP_IF:
2218 case PP_IFCTX:
2219 case PP_IFDEF:
2220 case PP_IFID:
2221 case PP_IFIDN:
2222 case PP_IFIDNI:
H. Peter Anvin65747262002-05-07 00:10:05 +00002223 case PP_IFMACRO:
H. Peter Anvin734b1882002-04-30 21:01:08 +00002224 case PP_IFNCTX:
2225 case PP_IFNDEF:
2226 case PP_IFNID:
2227 case PP_IFNIDN:
2228 case PP_IFNIDNI:
H. Peter Anvin65747262002-05-07 00:10:05 +00002229 case PP_IFNMACRO:
H. Peter Anvin734b1882002-04-30 21:01:08 +00002230 case PP_IFNNUM:
2231 case PP_IFNSTR:
2232 case PP_IFNUM:
2233 case PP_IFSTR:
2234 if (istk->conds && !emitting(istk->conds->state))
2235 j = COND_NEVER;
2236 else
2237 {
2238 j = if_condition(tline->next, i);
2239 tline->next = NULL; /* it got freed */
2240 free_tlist(origline);
2241 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2242 }
2243 cond = nasm_malloc(sizeof(Cond));
2244 cond->next = istk->conds;
2245 cond->state = j;
2246 istk->conds = cond;
2247 return (j == COND_IF_TRUE ? 3 : 1);
2248
2249 case PP_ELIF:
2250 case PP_ELIFCTX:
2251 case PP_ELIFDEF:
2252 case PP_ELIFID:
2253 case PP_ELIFIDN:
2254 case PP_ELIFIDNI:
H. Peter Anvin65747262002-05-07 00:10:05 +00002255 case PP_ELIFMACRO:
H. Peter Anvin734b1882002-04-30 21:01:08 +00002256 case PP_ELIFNCTX:
2257 case PP_ELIFNDEF:
2258 case PP_ELIFNID:
2259 case PP_ELIFNIDN:
2260 case PP_ELIFNIDNI:
H. Peter Anvin65747262002-05-07 00:10:05 +00002261 case PP_ELIFNMACRO:
H. Peter Anvin734b1882002-04-30 21:01:08 +00002262 case PP_ELIFNNUM:
2263 case PP_ELIFNSTR:
2264 case PP_ELIFNUM:
2265 case PP_ELIFSTR:
2266 if (!istk->conds)
2267 error(ERR_FATAL, "`%s': no matching `%%if'", directives[i]);
2268 if (emitting(istk->conds->state)
2269 || istk->conds->state == COND_NEVER)
2270 istk->conds->state = COND_NEVER;
2271 else
2272 {
H. Peter Anvin0c608152002-05-22 22:59:40 +00002273 /*
2274 * IMPORTANT: In the case of %if, we will already have
2275 * called expand_mmac_params(); however, if we're
2276 * processing an %elif we must have been in a
2277 * non-emitting mode, which would have inhibited
2278 * the normal invocation of expand_mmac_params(). Therefore,
2279 * we have to do it explicitly here.
2280 */
2281 j = if_condition(expand_mmac_params(tline->next), i);
2282 tline->next = NULL; /* it got freed */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002283 free_tlist(origline);
2284 istk->conds->state =
2285 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2286 }
2287 return (istk->conds->state == COND_IF_TRUE ? 5 : 1);
2288
2289 case PP_ELSE:
2290 if (tline->next)
2291 error(ERR_WARNING, "trailing garbage after `%%else' ignored");
2292 if (!istk->conds)
2293 error(ERR_FATAL, "`%%else': no matching `%%if'");
2294 if (emitting(istk->conds->state)
2295 || istk->conds->state == COND_NEVER)
2296 istk->conds->state = COND_ELSE_FALSE;
2297 else
2298 istk->conds->state = COND_ELSE_TRUE;
2299 free_tlist(origline);
2300 return 5;
2301
2302 case PP_ENDIF:
2303 if (tline->next)
2304 error(ERR_WARNING,
2305 "trailing garbage after `%%endif' ignored");
2306 if (!istk->conds)
2307 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2308 cond = istk->conds;
2309 istk->conds = cond->next;
2310 nasm_free(cond);
2311 free_tlist(origline);
2312 return 5;
2313
2314 case PP_MACRO:
2315 case PP_IMACRO:
2316 if (defining)
2317 error(ERR_FATAL,
2318 "`%%%smacro': already defining a macro",
2319 (i == PP_IMACRO ? "i" : ""));
2320 tline = tline->next;
2321 skip_white_(tline);
2322 tline = expand_id(tline);
2323 if (!tok_type_(tline, TOK_ID))
2324 {
2325 error(ERR_NONFATAL,
2326 "`%%%smacro' expects a macro name",
2327 (i == PP_IMACRO ? "i" : ""));
2328 return 3;
2329 }
2330 defining = nasm_malloc(sizeof(MMacro));
2331 defining->name = nasm_strdup(tline->text);
2332 defining->casesense = (i == PP_MACRO);
2333 defining->plus = FALSE;
2334 defining->nolist = FALSE;
2335 defining->in_progress = FALSE;
2336 defining->rep_nest = NULL;
2337 tline = expand_smacro(tline->next);
2338 skip_white_(tline);
2339 if (!tok_type_(tline, TOK_NUMBER))
2340 {
2341 error(ERR_NONFATAL,
2342 "`%%%smacro' expects a parameter count",
2343 (i == PP_IMACRO ? "i" : ""));
2344 defining->nparam_min = defining->nparam_max = 0;
2345 }
2346 else
2347 {
2348 defining->nparam_min = defining->nparam_max =
2349 readnum(tline->text, &j);
2350 if (j)
2351 error(ERR_NONFATAL,
2352 "unable to parse parameter count `%s'",
2353 tline->text);
2354 }
2355 if (tline && tok_is_(tline->next, "-"))
2356 {
2357 tline = tline->next->next;
2358 if (tok_is_(tline, "*"))
2359 defining->nparam_max = INT_MAX;
2360 else if (!tok_type_(tline, TOK_NUMBER))
2361 error(ERR_NONFATAL,
2362 "`%%%smacro' expects a parameter count after `-'",
2363 (i == PP_IMACRO ? "i" : ""));
2364 else
2365 {
2366 defining->nparam_max = readnum(tline->text, &j);
2367 if (j)
2368 error(ERR_NONFATAL,
2369 "unable to parse parameter count `%s'",
2370 tline->text);
2371 if (defining->nparam_min > defining->nparam_max)
2372 error(ERR_NONFATAL,
2373 "minimum parameter count exceeds maximum");
2374 }
2375 }
2376 if (tline && tok_is_(tline->next, "+"))
2377 {
2378 tline = tline->next;
2379 defining->plus = TRUE;
2380 }
2381 if (tline && tok_type_(tline->next, TOK_ID) &&
2382 !nasm_stricmp(tline->next->text, ".nolist"))
2383 {
2384 tline = tline->next;
2385 defining->nolist = TRUE;
2386 }
2387 mmac = mmacros[hash(defining->name)];
2388 while (mmac)
2389 {
2390 if (!strcmp(mmac->name, defining->name) &&
2391 (mmac->nparam_min <= defining->nparam_max
2392 || defining->plus)
2393 && (defining->nparam_min <= mmac->nparam_max
2394 || mmac->plus))
2395 {
2396 error(ERR_WARNING,
2397 "redefining multi-line macro `%s'",
2398 defining->name);
2399 break;
2400 }
2401 mmac = mmac->next;
2402 }
2403 /*
2404 * Handle default parameters.
2405 */
2406 if (tline && tline->next)
2407 {
2408 defining->dlist = tline->next;
2409 tline->next = NULL;
2410 count_mmac_params(defining->dlist, &defining->ndefs,
2411 &defining->defaults);
2412 }
2413 else
2414 {
2415 defining->dlist = NULL;
2416 defining->defaults = NULL;
2417 }
2418 defining->expansion = NULL;
2419 free_tlist(origline);
2420 return 1;
2421
2422 case PP_ENDM:
2423 case PP_ENDMACRO:
2424 if (!defining)
2425 {
2426 error(ERR_NONFATAL, "`%s': not defining a macro",
2427 tline->text);
2428 return 3;
2429 }
2430 k = hash(defining->name);
2431 defining->next = mmacros[k];
2432 mmacros[k] = defining;
2433 defining = NULL;
2434 free_tlist(origline);
2435 return 5;
2436
2437 case PP_ROTATE:
2438 if (tline->next && tline->next->type == TOK_WHITESPACE)
2439 tline = tline->next;
2440 t = expand_smacro(tline->next);
2441 tline->next = NULL;
2442 free_tlist(origline);
2443 tline = t;
2444 tptr = &t;
2445 tokval.t_type = TOKEN_INVALID;
2446 evalresult =
2447 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2448 free_tlist(tline);
2449 if (!evalresult)
2450 return 3;
2451 if (tokval.t_type)
2452 error(ERR_WARNING,
2453 "trailing garbage after expression ignored");
2454 if (!is_simple(evalresult))
2455 {
2456 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2457 return 3;
2458 }
2459 mmac = istk->mstk;
2460 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2461 mmac = mmac->next_active;
2462 if (!mmac)
2463 error(ERR_NONFATAL,
2464 "`%%rotate' invoked outside a macro call");
2465 mmac->rotate = mmac->rotate + reloc_value(evalresult);
2466 if (mmac->rotate < 0)
2467 mmac->rotate = mmac->nparam - (-mmac->rotate) % mmac->nparam;
2468 mmac->rotate %= mmac->nparam;
2469 return 1;
2470
2471 case PP_REP:
2472 nolist = FALSE;
2473 tline = tline->next;
2474 if (tline->next && tline->next->type == TOK_WHITESPACE)
2475 tline = tline->next;
2476 if (tline->next && tline->next->type == TOK_ID &&
2477 !nasm_stricmp(tline->next->text, ".nolist"))
2478 {
2479 tline = tline->next;
2480 nolist = TRUE;
2481 }
2482 t = expand_smacro(tline->next);
2483 tline->next = NULL;
2484 free_tlist(origline);
2485 tline = t;
2486 tptr = &t;
2487 tokval.t_type = TOKEN_INVALID;
2488 evalresult =
2489 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2490 free_tlist(tline);
2491 if (!evalresult)
2492 return 3;
2493 if (tokval.t_type)
2494 error(ERR_WARNING,
2495 "trailing garbage after expression ignored");
2496 if (!is_simple(evalresult))
2497 {
2498 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2499 return 3;
2500 }
2501 tmp_defining = defining;
2502 defining = nasm_malloc(sizeof(MMacro));
2503 defining->name = NULL; /* flags this macro as a %rep block */
2504 defining->casesense = 0;
2505 defining->plus = FALSE;
2506 defining->nolist = nolist;
2507 defining->in_progress = reloc_value(evalresult) + 1;
2508 defining->nparam_min = defining->nparam_max = 0;
2509 defining->defaults = NULL;
2510 defining->dlist = NULL;
2511 defining->expansion = NULL;
2512 defining->next_active = istk->mstk;
2513 defining->rep_nest = tmp_defining;
2514 return 1;
2515
2516 case PP_ENDREP:
2517 if (!defining || defining->name)
2518 {
2519 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2520 return 3;
2521 }
2522
2523 /*
2524 * Now we have a "macro" defined - although it has no name
2525 * and we won't be entering it in the hash tables - we must
2526 * push a macro-end marker for it on to istk->expansion.
2527 * After that, it will take care of propagating itself (a
2528 * macro-end marker line for a macro which is really a %rep
2529 * block will cause the macro to be re-expanded, complete
2530 * with another macro-end marker to ensure the process
2531 * continues) until the whole expansion is forcibly removed
2532 * from istk->expansion by a %exitrep.
2533 */
2534 l = nasm_malloc(sizeof(Line));
2535 l->next = istk->expansion;
2536 l->finishes = defining;
2537 l->first = NULL;
2538 istk->expansion = l;
2539
2540 istk->mstk = defining;
2541
2542 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2543 tmp_defining = defining;
2544 defining = defining->rep_nest;
2545 free_tlist(origline);
2546 return 1;
2547
2548 case PP_EXITREP:
2549 /*
2550 * We must search along istk->expansion until we hit a
2551 * macro-end marker for a macro with no name. Then we set
2552 * its `in_progress' flag to 0.
2553 */
2554 for (l = istk->expansion; l; l = l->next)
2555 if (l->finishes && !l->finishes->name)
2556 break;
2557
2558 if (l)
2559 l->finishes->in_progress = 0;
2560 else
2561 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2562 free_tlist(origline);
2563 return 1;
2564
2565 case PP_XDEFINE:
2566 case PP_IXDEFINE:
2567 case PP_DEFINE:
2568 case PP_IDEFINE:
2569 tline = tline->next;
2570 skip_white_(tline);
2571 tline = expand_id(tline);
2572 if (!tline || (tline->type != TOK_ID &&
2573 (tline->type != TOK_PREPROC_ID ||
2574 tline->text[1] != '$')))
2575 {
2576 error(ERR_NONFATAL,
2577 "`%%%s%sdefine' expects a macro identifier",
2578 ((i == PP_IDEFINE || i == PP_IXDEFINE) ? "i" : ""),
2579 ((i == PP_XDEFINE || i == PP_IXDEFINE) ? "x" : ""));
2580 free_tlist(origline);
2581 return 3;
2582 }
2583
2584 ctx = get_ctx(tline->text, FALSE);
2585 if (!ctx)
2586 smhead = &smacros[hash(tline->text)];
2587 else
2588 smhead = &ctx->localmac;
2589 mname = tline->text;
2590 last = tline;
2591 param_start = tline = tline->next;
2592 nparam = 0;
2593
2594 /* Expand the macro definition now for %xdefine and %ixdefine */
2595 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2596 tline = expand_smacro(tline);
2597
2598 if (tok_is_(tline, "("))
2599 {
2600 /*
2601 * This macro has parameters.
2602 */
2603
2604 tline = tline->next;
2605 while (1)
2606 {
2607 skip_white_(tline);
2608 if (!tline)
2609 {
2610 error(ERR_NONFATAL, "parameter identifier expected");
2611 free_tlist(origline);
2612 return 3;
2613 }
2614 if (tline->type != TOK_ID)
2615 {
2616 error(ERR_NONFATAL,
2617 "`%s': parameter identifier expected",
2618 tline->text);
2619 free_tlist(origline);
2620 return 3;
2621 }
2622 tline->type = TOK_SMAC_PARAM + nparam++;
2623 tline = tline->next;
2624 skip_white_(tline);
2625 if (tok_is_(tline, ","))
2626 {
2627 tline = tline->next;
2628 continue;
2629 }
2630 if (!tok_is_(tline, ")"))
2631 {
2632 error(ERR_NONFATAL,
2633 "`)' expected to terminate macro template");
2634 free_tlist(origline);
2635 return 3;
2636 }
2637 break;
2638 }
2639 last = tline;
2640 tline = tline->next;
2641 }
2642 if (tok_type_(tline, TOK_WHITESPACE))
2643 last = tline, tline = tline->next;
2644 macro_start = NULL;
2645 last->next = NULL;
2646 t = tline;
2647 while (t)
2648 {
2649 if (t->type == TOK_ID)
2650 {
2651 for (tt = param_start; tt; tt = tt->next)
2652 if (tt->type >= TOK_SMAC_PARAM &&
2653 !strcmp(tt->text, t->text))
2654 t->type = tt->type;
2655 }
2656 tt = t->next;
2657 t->next = macro_start;
2658 macro_start = t;
2659 t = tt;
2660 }
2661 /*
2662 * Good. We now have a macro name, a parameter count, and a
2663 * token list (in reverse order) for an expansion. We ought
2664 * to be OK just to create an SMacro, store it, and let
2665 * free_tlist have the rest of the line (which we have
2666 * carefully re-terminated after chopping off the expansion
2667 * from the end).
2668 */
2669 if (smacro_defined(ctx, mname, nparam, &smac, i == PP_DEFINE))
2670 {
2671 if (!smac)
2672 {
2673 error(ERR_WARNING,
2674 "single-line macro `%s' defined both with and"
2675 " without parameters", mname);
2676 free_tlist(origline);
2677 free_tlist(macro_start);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002678 return 3;
2679 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002680 else
2681 {
2682 /*
2683 * We're redefining, so we have to take over an
2684 * existing SMacro structure. This means freeing
2685 * what was already in it.
2686 */
2687 nasm_free(smac->name);
2688 free_tlist(smac->expansion);
2689 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002690 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002691 else
2692 {
2693 smac = nasm_malloc(sizeof(SMacro));
2694 smac->next = *smhead;
2695 *smhead = smac;
2696 }
2697 smac->name = nasm_strdup(mname);
2698 smac->casesense = ((i == PP_DEFINE) || (i == PP_XDEFINE));
2699 smac->nparam = nparam;
2700 smac->expansion = macro_start;
2701 smac->in_progress = FALSE;
2702 free_tlist(origline);
2703 return 3;
2704
2705 case PP_UNDEF:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002706 tline = tline->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002707 skip_white_(tline);
2708 tline = expand_id(tline);
2709 if (!tline || (tline->type != TOK_ID &&
2710 (tline->type != TOK_PREPROC_ID ||
2711 tline->text[1] != '$')))
2712 {
2713 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2714 free_tlist(origline);
H. Peter Anvineba20a72002-04-30 20:53:55 +00002715 return 3;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002716 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002717 if (tline->next)
2718 {
2719 error(ERR_WARNING,
2720 "trailing garbage after macro name ignored");
2721 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002722
H. Peter Anvin734b1882002-04-30 21:01:08 +00002723 /* Find the context that symbol belongs to */
2724 ctx = get_ctx(tline->text, FALSE);
2725 if (!ctx)
2726 smhead = &smacros[hash(tline->text)];
2727 else
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002728 smhead = &ctx->localmac;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002729
H. Peter Anvin734b1882002-04-30 21:01:08 +00002730 mname = tline->text;
2731 last = tline;
2732 last->next = NULL;
2733
2734 /*
2735 * We now have a macro name... go hunt for it.
2736 */
2737 while (smacro_defined(ctx, mname, -1, &smac, 1))
2738 {
2739 /* Defined, so we need to find its predecessor and nuke it */
2740 SMacro **s;
2741 for (s = smhead; *s && *s != smac; s = &(*s)->next);
2742 if (*s)
2743 {
2744 *s = smac->next;
2745 nasm_free(smac->name);
2746 free_tlist(smac->expansion);
2747 nasm_free(smac);
2748 }
2749 }
2750 free_tlist(origline);
2751 return 3;
2752
2753 case PP_STRLEN:
2754 tline = tline->next;
2755 skip_white_(tline);
2756 tline = expand_id(tline);
2757 if (!tline || (tline->type != TOK_ID &&
2758 (tline->type != TOK_PREPROC_ID ||
2759 tline->text[1] != '$')))
2760 {
2761 error(ERR_NONFATAL,
2762 "`%%strlen' expects a macro identifier as first parameter");
2763 free_tlist(origline);
2764 return 3;
2765 }
2766 ctx = get_ctx(tline->text, FALSE);
2767 if (!ctx)
2768 smhead = &smacros[hash(tline->text)];
2769 else
2770 smhead = &ctx->localmac;
2771 mname = tline->text;
2772 last = tline;
2773 tline = expand_smacro(tline->next);
2774 last->next = NULL;
2775
2776 t = tline;
2777 while (tok_type_(t, TOK_WHITESPACE))
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002778 t = t->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002779 /* t should now point to the string */
2780 if (t->type != TOK_STRING)
2781 {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002782 error(ERR_NONFATAL,
2783 "`%%strlen` requires string as second parameter");
2784 free_tlist(tline);
2785 free_tlist(origline);
2786 return 3;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002787 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002788
H. Peter Anvin734b1882002-04-30 21:01:08 +00002789 macro_start = nasm_malloc(sizeof(*macro_start));
2790 macro_start->next = NULL;
2791 make_tok_num(macro_start, strlen(t->text) - 2);
2792 macro_start->mac = NULL;
2793
2794 /*
2795 * We now have a macro name, an implicit parameter count of
2796 * zero, and a numeric token to use as an expansion. Create
2797 * and store an SMacro.
2798 */
2799 if (smacro_defined(ctx, mname, 0, &smac, i == PP_STRLEN))
2800 {
2801 if (!smac)
2802 error(ERR_WARNING,
2803 "single-line macro `%s' defined both with and"
2804 " without parameters", mname);
2805 else
2806 {
2807 /*
2808 * We're redefining, so we have to take over an
2809 * existing SMacro structure. This means freeing
2810 * what was already in it.
2811 */
2812 nasm_free(smac->name);
2813 free_tlist(smac->expansion);
2814 }
2815 }
2816 else
2817 {
2818 smac = nasm_malloc(sizeof(SMacro));
2819 smac->next = *smhead;
2820 *smhead = smac;
2821 }
2822 smac->name = nasm_strdup(mname);
2823 smac->casesense = (i == PP_STRLEN);
2824 smac->nparam = 0;
2825 smac->expansion = macro_start;
2826 smac->in_progress = FALSE;
2827 free_tlist(tline);
2828 free_tlist(origline);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002829 return 3;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002830
H. Peter Anvin734b1882002-04-30 21:01:08 +00002831 case PP_SUBSTR:
2832 tline = tline->next;
2833 skip_white_(tline);
2834 tline = expand_id(tline);
2835 if (!tline || (tline->type != TOK_ID &&
2836 (tline->type != TOK_PREPROC_ID ||
2837 tline->text[1] != '$')))
2838 {
2839 error(ERR_NONFATAL,
2840 "`%%substr' expects a macro identifier as first parameter");
2841 free_tlist(origline);
2842 return 3;
2843 }
2844 ctx = get_ctx(tline->text, FALSE);
2845 if (!ctx)
2846 smhead = &smacros[hash(tline->text)];
2847 else
2848 smhead = &ctx->localmac;
2849 mname = tline->text;
2850 last = tline;
2851 tline = expand_smacro(tline->next);
2852 last->next = NULL;
2853
2854 t = tline->next;
2855 while (tok_type_(t, TOK_WHITESPACE))
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002856 t = t->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002857
2858 /* t should now point to the string */
2859 if (t->type != TOK_STRING)
2860 {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002861 error(ERR_NONFATAL,
2862 "`%%substr` requires string as second parameter");
2863 free_tlist(tline);
2864 free_tlist(origline);
2865 return 3;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002866 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002867
H. Peter Anvin734b1882002-04-30 21:01:08 +00002868 tt = t->next;
2869 tptr = &tt;
2870 tokval.t_type = TOKEN_INVALID;
2871 evalresult =
2872 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2873 if (!evalresult)
2874 {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002875 free_tlist(tline);
2876 free_tlist(origline);
2877 return 3;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002878 }
2879 if (!is_simple(evalresult))
2880 {
2881 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002882 free_tlist(tline);
2883 free_tlist(origline);
2884 return 3;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002885 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002886
H. Peter Anvin734b1882002-04-30 21:01:08 +00002887 macro_start = nasm_malloc(sizeof(*macro_start));
2888 macro_start->next = NULL;
2889 macro_start->text = nasm_strdup("'''");
2890 if (evalresult->value > 0
2891 && evalresult->value < strlen(t->text) - 1)
2892 {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002893 macro_start->text[1] = t->text[evalresult->value];
H. Peter Anvin734b1882002-04-30 21:01:08 +00002894 }
2895 else
2896 {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002897 macro_start->text[2] = '\0';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002898 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002899 macro_start->type = TOK_STRING;
2900 macro_start->mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002901
H. Peter Anvin734b1882002-04-30 21:01:08 +00002902 /*
2903 * We now have a macro name, an implicit parameter count of
2904 * zero, and a numeric token to use as an expansion. Create
2905 * and store an SMacro.
2906 */
2907 if (smacro_defined(ctx, mname, 0, &smac, i == PP_SUBSTR))
2908 {
2909 if (!smac)
2910 error(ERR_WARNING,
2911 "single-line macro `%s' defined both with and"
2912 " without parameters", mname);
2913 else
2914 {
2915 /*
2916 * We're redefining, so we have to take over an
2917 * existing SMacro structure. This means freeing
2918 * what was already in it.
2919 */
2920 nasm_free(smac->name);
2921 free_tlist(smac->expansion);
2922 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002923 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002924 else
2925 {
2926 smac = nasm_malloc(sizeof(SMacro));
2927 smac->next = *smhead;
2928 *smhead = smac;
2929 }
2930 smac->name = nasm_strdup(mname);
2931 smac->casesense = (i == PP_SUBSTR);
2932 smac->nparam = 0;
2933 smac->expansion = macro_start;
2934 smac->in_progress = FALSE;
2935 free_tlist(tline);
2936 free_tlist(origline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002937 return 3;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002938
2939
2940 case PP_ASSIGN:
2941 case PP_IASSIGN:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002942 tline = tline->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002943 skip_white_(tline);
2944 tline = expand_id(tline);
2945 if (!tline || (tline->type != TOK_ID &&
2946 (tline->type != TOK_PREPROC_ID ||
2947 tline->text[1] != '$')))
2948 {
2949 error(ERR_NONFATAL,
2950 "`%%%sassign' expects a macro identifier",
2951 (i == PP_IASSIGN ? "i" : ""));
2952 free_tlist(origline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002953 return 3;
2954 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002955 ctx = get_ctx(tline->text, FALSE);
2956 if (!ctx)
2957 smhead = &smacros[hash(tline->text)];
2958 else
2959 smhead = &ctx->localmac;
2960 mname = tline->text;
2961 last = tline;
2962 tline = expand_smacro(tline->next);
2963 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002964
H. Peter Anvin734b1882002-04-30 21:01:08 +00002965 t = tline;
2966 tptr = &t;
2967 tokval.t_type = TOKEN_INVALID;
2968 evalresult =
2969 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2970 free_tlist(tline);
2971 if (!evalresult)
2972 {
2973 free_tlist(origline);
2974 return 3;
2975 }
2976
2977 if (tokval.t_type)
2978 error(ERR_WARNING,
2979 "trailing garbage after expression ignored");
2980
2981 if (!is_simple(evalresult))
2982 {
2983 error(ERR_NONFATAL,
2984 "non-constant value given to `%%%sassign'",
2985 (i == PP_IASSIGN ? "i" : ""));
2986 free_tlist(origline);
2987 return 3;
2988 }
2989
2990 macro_start = nasm_malloc(sizeof(*macro_start));
2991 macro_start->next = NULL;
2992 make_tok_num(macro_start, reloc_value(evalresult));
2993 macro_start->mac = NULL;
2994
2995 /*
2996 * We now have a macro name, an implicit parameter count of
2997 * zero, and a numeric token to use as an expansion. Create
2998 * and store an SMacro.
2999 */
3000 if (smacro_defined(ctx, mname, 0, &smac, i == PP_ASSIGN))
3001 {
3002 if (!smac)
3003 error(ERR_WARNING,
3004 "single-line macro `%s' defined both with and"
3005 " without parameters", mname);
3006 else
3007 {
3008 /*
3009 * We're redefining, so we have to take over an
3010 * existing SMacro structure. This means freeing
3011 * what was already in it.
3012 */
3013 nasm_free(smac->name);
3014 free_tlist(smac->expansion);
3015 }
3016 }
3017 else
3018 {
3019 smac = nasm_malloc(sizeof(SMacro));
3020 smac->next = *smhead;
3021 *smhead = smac;
3022 }
3023 smac->name = nasm_strdup(mname);
3024 smac->casesense = (i == PP_ASSIGN);
3025 smac->nparam = 0;
3026 smac->expansion = macro_start;
3027 smac->in_progress = FALSE;
3028 free_tlist(origline);
3029 return 3;
3030
3031 case PP_LINE:
3032 /*
3033 * Syntax is `%line nnn[+mmm] [filename]'
3034 */
3035 tline = tline->next;
3036 skip_white_(tline);
3037 if (!tok_type_(tline, TOK_NUMBER))
3038 {
3039 error(ERR_NONFATAL, "`%%line' expects line number");
3040 free_tlist(origline);
3041 return 3;
3042 }
3043 k = readnum(tline->text, &j);
3044 m = 1;
3045 tline = tline->next;
3046 if (tok_is_(tline, "+"))
3047 {
3048 tline = tline->next;
3049 if (!tok_type_(tline, TOK_NUMBER))
3050 {
3051 error(ERR_NONFATAL, "`%%line' expects line increment");
3052 free_tlist(origline);
3053 return 3;
3054 }
3055 m = readnum(tline->text, &j);
3056 tline = tline->next;
3057 }
3058 skip_white_(tline);
3059 src_set_linnum(k);
3060 istk->lineinc = m;
3061 if (tline)
3062 {
3063 nasm_free(src_set_fname(detoken(tline, FALSE)));
3064 }
3065 free_tlist(origline);
3066 return 5;
3067
3068 default:
3069 error(ERR_FATAL,
3070 "preprocessor directive `%s' not yet implemented",
3071 directives[i]);
3072 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003073 }
3074 return 3;
3075}
3076
3077/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003078 * Ensure that a macro parameter contains a condition code and
3079 * nothing else. Return the condition code index if so, or -1
3080 * otherwise.
3081 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003082static int
3083find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003084{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003085 Token *tt;
3086 int i, j, k, m;
3087
H. Peter Anvineba20a72002-04-30 20:53:55 +00003088 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003089 if (t->type != TOK_ID)
3090 return -1;
3091 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003092 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003093 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
3094 return -1;
3095
3096 i = -1;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003097 j = sizeof(conditions) / sizeof(*conditions);
3098 while (j - i > 1)
3099 {
3100 k = (j + i) / 2;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003101 m = nasm_stricmp(t->text, conditions[k]);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003102 if (m == 0)
3103 {
H. Peter Anvin76690a12002-04-30 20:52:49 +00003104 i = k;
3105 j = -2;
3106 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003107 }
3108 else if (m < 0)
3109 {
H. Peter Anvin76690a12002-04-30 20:52:49 +00003110 j = k;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003111 }
3112 else
H. Peter Anvin76690a12002-04-30 20:52:49 +00003113 i = k;
3114 }
3115 if (j != -2)
3116 return -1;
3117 return i;
3118}
3119
3120/*
3121 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3122 * %-n) and MMacro-local identifiers (%%foo).
3123 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003124static Token *
3125expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003126{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003127 Token *t, *tt, **tail, *thead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003128
3129 tail = &thead;
3130 thead = NULL;
3131
H. Peter Anvin734b1882002-04-30 21:01:08 +00003132 while (tline)
3133 {
H. Peter Anvin76690a12002-04-30 20:52:49 +00003134 if (tline->type == TOK_PREPROC_ID &&
H. Peter Anvin734b1882002-04-30 21:01:08 +00003135 (((tline->text[1] == '+' || tline->text[1] == '-')
3136 && tline->text[2]) || tline->text[1] == '%'
3137 || (tline->text[1] >= '0' && tline->text[1] <= '9')))
3138 {
H. Peter Anvin76690a12002-04-30 20:52:49 +00003139 char *text = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003140 int type = 0, cc; /* type = 0 to placate optimisers */
H. Peter Anvin76690a12002-04-30 20:52:49 +00003141 char tmpbuf[30];
3142 int n, i;
3143 MMacro *mac;
3144
3145 t = tline;
3146 tline = tline->next;
3147
3148 mac = istk->mstk;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003149 while (mac && !mac->name) /* avoid mistaking %reps for macros */
H. Peter Anvin76690a12002-04-30 20:52:49 +00003150 mac = mac->next_active;
3151 if (!mac)
3152 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003153 else
3154 switch (t->text[1])
3155 {
3156 /*
3157 * We have to make a substitution of one of the
3158 * forms %1, %-1, %+1, %%foo, %0.
3159 */
3160 case '0':
3161 type = TOK_NUMBER;
3162 sprintf(tmpbuf, "%d", mac->nparam);
3163 text = nasm_strdup(tmpbuf);
3164 break;
3165 case '%':
3166 type = TOK_ID;
3167 sprintf(tmpbuf, "..@%lu.", mac->unique);
3168 text = nasm_strcat(tmpbuf, t->text + 2);
3169 break;
3170 case '-':
3171 n = atoi(t->text + 2) - 1;
3172 if (n >= mac->nparam)
3173 tt = NULL;
3174 else
3175 {
3176 if (mac->nparam > 1)
3177 n = (n + mac->rotate) % mac->nparam;
3178 tt = mac->params[n];
3179 }
3180 cc = find_cc(tt);
3181 if (cc == -1)
3182 {
3183 error(ERR_NONFATAL,
3184 "macro parameter %d is not a condition code",
3185 n + 1);
3186 text = NULL;
3187 }
3188 else
3189 {
3190 type = TOK_ID;
3191 if (inverse_ccs[cc] == -1)
3192 {
3193 error(ERR_NONFATAL,
3194 "condition code `%s' is not invertible",
3195 conditions[cc]);
3196 text = NULL;
3197 }
3198 else
3199 text =
3200 nasm_strdup(conditions[inverse_ccs
3201 [cc]]);
3202 }
3203 break;
3204 case '+':
3205 n = atoi(t->text + 2) - 1;
3206 if (n >= mac->nparam)
3207 tt = NULL;
3208 else
3209 {
3210 if (mac->nparam > 1)
3211 n = (n + mac->rotate) % mac->nparam;
3212 tt = mac->params[n];
3213 }
3214 cc = find_cc(tt);
3215 if (cc == -1)
3216 {
3217 error(ERR_NONFATAL,
3218 "macro parameter %d is not a condition code",
3219 n + 1);
3220 text = NULL;
3221 }
3222 else
3223 {
3224 type = TOK_ID;
3225 text = nasm_strdup(conditions[cc]);
3226 }
3227 break;
3228 default:
3229 n = atoi(t->text + 1) - 1;
3230 if (n >= mac->nparam)
3231 tt = NULL;
3232 else
3233 {
3234 if (mac->nparam > 1)
3235 n = (n + mac->rotate) % mac->nparam;
3236 tt = mac->params[n];
3237 }
3238 if (tt)
3239 {
3240 for (i = 0; i < mac->paramlen[n]; i++)
3241 {
3242 *tail =
3243 new_Token(NULL, tt->type, tt->text,
3244 0);
3245 tail = &(*tail)->next;
3246 tt = tt->next;
3247 }
3248 }
3249 text = NULL; /* we've done it here */
3250 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003251 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003252 if (!text)
3253 {
3254 delete_Token(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003255 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003256 else
3257 {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003258 *tail = t;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003259 tail = &t->next;
3260 t->type = type;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003261 nasm_free(t->text);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003262 t->text = text;
3263 t->mac = NULL;
3264 }
3265 continue;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003266 }
3267 else
3268 {
H. Peter Anvin76690a12002-04-30 20:52:49 +00003269 t = *tail = tline;
3270 tline = tline->next;
3271 t->mac = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003272 tail = &t->next;
3273 }
3274 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00003275 *tail = NULL;
3276 t = thead;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003277 for (; t && (tt = t->next) != NULL; t = t->next)
3278 switch (t->type)
3279 {
3280 case TOK_WHITESPACE:
3281 if (tt->type == TOK_WHITESPACE)
3282 {
3283 t->next = delete_Token(tt);
3284 }
3285 break;
3286 case TOK_ID:
3287 if (tt->type == TOK_ID || tt->type == TOK_NUMBER)
3288 {
3289 char *tmp = nasm_strcat(t->text, tt->text);
3290 nasm_free(t->text);
3291 t->text = tmp;
3292 t->next = delete_Token(tt);
3293 }
3294 break;
3295 case TOK_NUMBER:
3296 if (tt->type == TOK_NUMBER)
3297 {
3298 char *tmp = nasm_strcat(t->text, tt->text);
3299 nasm_free(t->text);
3300 t->text = tmp;
3301 t->next = delete_Token(tt);
3302 }
3303 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003304 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003305
H. Peter Anvin76690a12002-04-30 20:52:49 +00003306 return thead;
3307}
3308
3309/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003310 * Expand all single-line macro calls made in the given line.
3311 * Return the expanded version of the line. The original is deemed
3312 * to be destroyed in the process. (In reality we'll just move
3313 * Tokens from input to output a lot of the time, rather than
3314 * actually bothering to destroy and replicate.)
3315 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003316static Token *
3317expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003318{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003319 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003320 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003321 Token **params;
3322 int *paramsize;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003323 int nparam, sparam, brackets, rescan;
3324 Token *org_tline = tline;
3325 Context *ctx;
3326 char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003327
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003328 /*
3329 * Trick: we should avoid changing the start token pointer since it can
3330 * be contained in "next" field of other token. Because of this
3331 * we allocate a copy of first token and work with it; at the end of
3332 * routine we copy it back
3333 */
3334 if (org_tline)
3335 {
H. Peter Anvin734b1882002-04-30 21:01:08 +00003336 tline =
3337 new_Token(org_tline->next, org_tline->type, org_tline->text,
3338 0);
3339 tline->mac = org_tline->mac;
H. Peter Anvince616072002-04-30 21:02:23 +00003340 nasm_free(org_tline->text);
3341 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003342 }
3343
H. Peter Anvin734b1882002-04-30 21:01:08 +00003344 again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003345 tail = &thead;
3346 thead = NULL;
3347
H. Peter Anvin734b1882002-04-30 21:01:08 +00003348 while (tline)
3349 { /* main token loop */
3350 if ((mname = tline->text))
3351 {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003352 /* if this token is a local macro, look in local context */
3353 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID)
H. Peter Anvin734b1882002-04-30 21:01:08 +00003354 ctx = get_ctx(mname, TRUE);
3355 else
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003356 ctx = NULL;
3357 if (!ctx)
3358 head = smacros[hash(mname)];
3359 else
H. Peter Anvineba20a72002-04-30 20:53:55 +00003360 head = ctx->localmac;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003361 /*
3362 * We've hit an identifier. As in is_mmacro below, we first
3363 * check whether the identifier is a single-line macro at
3364 * all, then think about checking for parameters if
3365 * necessary.
3366 */
H. Peter Anvineba20a72002-04-30 20:53:55 +00003367 for (m = head; m; m = m->next)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003368 if (!mstrcmp(m->name, mname, m->casesense))
H. Peter Anvineba20a72002-04-30 20:53:55 +00003369 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003370 if (m)
3371 {
3372 mstart = tline;
3373 params = NULL;
3374 paramsize = NULL;
3375 if (m->nparam == 0)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003376 {
H. Peter Anvin734b1882002-04-30 21:01:08 +00003377 /*
3378 * Simple case: the macro is parameterless. Discard the
3379 * one token that the macro call took, and push the
3380 * expansion back on the to-do stack.
3381 */
3382 if (!m->expansion)
3383 {
3384 if (!strcmp("__FILE__", m->name))
3385 {
3386 long num = 0;
3387 src_get(&num, &(tline->text));
3388 nasm_quote(&(tline->text));
3389 tline->type = TOK_STRING;
3390 continue;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003391 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003392 if (!strcmp("__LINE__", m->name))
3393 {
3394 nasm_free(tline->text);
3395 make_tok_num(tline, src_get_linnum());
3396 continue;
3397 }
3398 tline = delete_Token(tline);
3399 continue;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003400 }
3401 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003402 else
3403 {
3404 /*
3405 * Complicated case: at least one macro with this name
3406 * exists and takes parameters. We must find the
3407 * parameters in the call, count them, find the SMacro
3408 * that corresponds to that form of the macro call, and
3409 * substitute for the parameters when we expand. What a
3410 * pain.
3411 */
3412 tline = tline->next;
3413 skip_white_(tline);
3414 if (!tok_is_(tline, "("))
3415 {
3416 /*
3417 * This macro wasn't called with parameters: ignore
3418 * the call. (Behaviour borrowed from gnu cpp.)
3419 */
3420 tline = mstart;
3421 m = NULL;
3422 }
3423 else
3424 {
3425 int paren = 0;
3426 int white = 0;
3427 brackets = 0;
3428 nparam = 0;
3429 tline = tline->next;
3430 sparam = PARAM_DELTA;
3431 params = nasm_malloc(sparam * sizeof(Token *));
3432 params[0] = tline;
3433 paramsize = nasm_malloc(sparam * sizeof(int));
3434 paramsize[0] = 0;
3435 for (;; tline = tline->next)
3436 { /* parameter loop */
3437 if (!tline)
3438 {
3439 error(ERR_NONFATAL,
3440 "macro call expects terminating `)'");
3441 break;
3442 }
3443 if (tline->type == TOK_WHITESPACE
3444 && brackets <= 0)
3445 {
3446 if (paramsize[nparam])
3447 white++;
3448 else
3449 params[nparam] = tline->next;
3450 continue; /* parameter loop */
3451 }
3452 if (tline->type == TOK_OTHER
3453 && tline->text[1] == 0)
3454 {
3455 char ch = tline->text[0];
3456 if (ch == ',' && !paren && brackets <= 0)
3457 {
3458 if (++nparam >= sparam)
3459 {
3460 sparam += PARAM_DELTA;
3461 params = nasm_realloc(params,
3462 sparam * sizeof(Token *));
3463 paramsize = nasm_realloc(paramsize,
3464 sparam * sizeof(int));
3465 }
3466 params[nparam] = tline->next;
3467 paramsize[nparam] = 0;
3468 white = 0;
3469 continue; /* parameter loop */
3470 }
3471 if (ch == '{' &&
3472 (brackets > 0 || (brackets == 0 &&
3473 !paramsize[nparam])))
3474 {
3475 if (!(brackets++))
3476 {
3477 params[nparam] = tline->next;
3478 continue; /* parameter loop */
3479 }
3480 }
3481 if (ch == '}' && brackets > 0)
3482 if (--brackets == 0)
3483 {
3484 brackets = -1;
3485 continue; /* parameter loop */
3486 }
3487 if (ch == '(' && !brackets)
3488 paren++;
3489 if (ch == ')' && brackets <= 0)
3490 if (--paren < 0)
3491 break;
3492 }
3493 if (brackets < 0)
3494 {
3495 brackets = 0;
3496 error(ERR_NONFATAL, "braces do not "
3497 "enclose all of macro parameter");
3498 }
3499 paramsize[nparam] += white + 1;
3500 white = 0;
3501 } /* parameter loop */
3502 nparam++;
3503 while (m && (m->nparam != nparam ||
3504 mstrcmp(m->name, mname,
3505 m->casesense)))
3506 m = m->next;
3507 if (!m)
3508 error(ERR_WARNING | ERR_WARN_MNP,
3509 "macro `%s' exists, "
3510 "but not taking %d parameters",
3511 mstart->text, nparam);
3512 }
3513 }
3514 if (m && m->in_progress)
3515 m = NULL;
3516 if (!m) /* in progess or didn't find '(' or wrong nparam */
3517 {
3518 /*
3519 * Design question: should we handle !tline, which
3520 * indicates missing ')' here, or expand those
3521 * macros anyway, which requires the (t) test a few
3522 * lines down?
3523 */
3524 nasm_free(params);
3525 nasm_free(paramsize);
3526 tline = mstart;
3527 }
3528 else
3529 {
3530 /*
3531 * Expand the macro: we are placed on the last token of the
3532 * call, so that we can easily split the call from the
3533 * following tokens. We also start by pushing an SMAC_END
3534 * token for the cycle removal.
3535 */
3536 t = tline;
3537 if (t)
3538 {
3539 tline = t->next;
3540 t->next = NULL;
3541 }
3542 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
3543 tt->mac = m;
3544 m->in_progress = TRUE;
3545 tline = tt;
3546 for (t = m->expansion; t; t = t->next)
3547 {
3548 if (t->type >= TOK_SMAC_PARAM)
3549 {
3550 Token *pcopy = tline, **ptail = &pcopy;
3551 Token *ttt, *pt;
3552 int i;
3553
3554 ttt = params[t->type - TOK_SMAC_PARAM];
3555 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3556 --i >= 0;)
3557 {
3558 pt = *ptail =
3559 new_Token(tline, ttt->type, ttt->text,
3560 0);
3561 ptail = &pt->next;
3562 ttt = ttt->next;
3563 }
3564 tline = pcopy;
3565 }
3566 else
3567 {
3568 tt = new_Token(tline, t->type, t->text, 0);
3569 tline = tt;
3570 }
3571 }
3572
3573 /*
3574 * Having done that, get rid of the macro call, and clean
3575 * up the parameters.
3576 */
3577 nasm_free(params);
3578 nasm_free(paramsize);
3579 free_tlist(mstart);
3580 continue; /* main token loop */
3581 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003582 }
3583 }
3584
H. Peter Anvin734b1882002-04-30 21:01:08 +00003585 if (tline->type == TOK_SMAC_END)
3586 {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003587 tline->mac->in_progress = FALSE;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003588 tline = delete_Token(tline);
3589 }
3590 else
3591 {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003592 t = *tail = tline;
3593 tline = tline->next;
3594 t->mac = NULL;
3595 t->next = NULL;
3596 tail = &t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003597 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003598 }
3599
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003600 /*
3601 * Now scan the entire line and look for successive TOK_IDs that resulted
3602 * after expansion (they can't be produced by tokenise()). The successive
3603 * TOK_IDs should be concatenated.
3604 * Also we look for %+ tokens and concatenate the tokens before and after
3605 * them (without white spaces in between).
3606 */
3607 t = thead;
3608 rescan = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003609 while (t)
3610 {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003611 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3612 t = t->next;
3613 if (!t || !t->next)
3614 break;
3615 if (t->next->type == TOK_ID ||
H. Peter Anvin734b1882002-04-30 21:01:08 +00003616 t->next->type == TOK_PREPROC_ID ||
3617 t->next->type == TOK_NUMBER)
3618 {
3619 char *p = nasm_strcat(t->text, t->next->text);
3620 nasm_free(t->text);
3621 t->next = delete_Token(t->next);
3622 t->text = p;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003623 rescan = 1;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003624 }
3625 else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3626 t->next->next->type == TOK_PREPROC_ID &&
3627 strcmp(t->next->next->text, "%+") == 0)
3628 {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003629 /* free the next whitespace, the %+ token and next whitespace */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003630 int i;
3631 for (i = 1; i <= 3; i++)
3632 {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003633 if (!t->next || (i != 2 && t->next->type != TOK_WHITESPACE))
3634 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003635 t->next = delete_Token(t->next);
3636 } /* endfor */
3637 }
3638 else
3639 t = t->next;
3640 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003641 /* If we concatenaded something, re-scan the line for macros */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003642 if (rescan)
3643 {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003644 tline = thead;
3645 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003646 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003647
3648 if (org_tline)
3649 {
H. Peter Anvin734b1882002-04-30 21:01:08 +00003650 if (thead)
3651 {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003652 *org_tline = *thead;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003653 /* since we just gave text to org_line, don't free it */
3654 thead->text = NULL;
3655 delete_Token(thead);
3656 }
3657 else
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003658 {
3659 /* the expression expanded to empty line;
3660 we can't return NULL for some reasons
3661 we just set the line to a single WHITESPACE token. */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003662 memset(org_tline, 0, sizeof(*org_tline));
3663 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003664 org_tline->type = TOK_WHITESPACE;
3665 }
3666 thead = org_tline;
3667 }
3668
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003669 return thead;
3670}
3671
3672/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003673 * Similar to expand_smacro but used exclusively with macro identifiers
3674 * right before they are fetched in. The reason is that there can be
3675 * identifiers consisting of several subparts. We consider that if there
3676 * are more than one element forming the name, user wants a expansion,
3677 * otherwise it will be left as-is. Example:
3678 *
3679 * %define %$abc cde
3680 *
3681 * the identifier %$abc will be left as-is so that the handler for %define
3682 * will suck it and define the corresponding value. Other case:
3683 *
3684 * %define _%$abc cde
3685 *
3686 * In this case user wants name to be expanded *before* %define starts
3687 * working, so we'll expand %$abc into something (if it has a value;
3688 * otherwise it will be left as-is) then concatenate all successive
3689 * PP_IDs into one.
3690 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003691static Token *
3692expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003693{
3694 Token *cur, *oldnext = NULL;
3695
H. Peter Anvin734b1882002-04-30 21:01:08 +00003696 if (!tline || !tline->next)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003697 return tline;
3698
3699 cur = tline;
3700 while (cur->next &&
H. Peter Anvin734b1882002-04-30 21:01:08 +00003701 (cur->next->type == TOK_ID ||
3702 cur->next->type == TOK_PREPROC_ID || cur->next->type == TOK_NUMBER))
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003703 cur = cur->next;
3704
3705 /* If identifier consists of just one token, don't expand */
3706 if (cur == tline)
3707 return tline;
3708
H. Peter Anvin734b1882002-04-30 21:01:08 +00003709 if (cur)
3710 {
3711 oldnext = cur->next; /* Detach the tail past identifier */
3712 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003713 }
3714
H. Peter Anvin734b1882002-04-30 21:01:08 +00003715 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003716
H. Peter Anvin734b1882002-04-30 21:01:08 +00003717 if (cur)
3718 {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003719 /* expand_smacro possibly changhed tline; re-scan for EOL */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003720 cur = tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003721 while (cur && cur->next)
3722 cur = cur->next;
3723 if (cur)
3724 cur->next = oldnext;
3725 }
3726
3727 return tline;
3728}
3729
3730/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003731 * Determine whether the given line constitutes a multi-line macro
3732 * call, and return the MMacro structure called if so. Doesn't have
3733 * to check for an initial label - that's taken care of in
3734 * expand_mmacro - but must check numbers of parameters. Guaranteed
3735 * to be called with tline->type == TOK_ID, so the putative macro
3736 * name is easy to find.
3737 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003738static MMacro *
3739is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003740{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003741 MMacro *head, *m;
3742 Token **params;
3743 int nparam;
3744
3745 head = mmacros[hash(tline->text)];
3746
3747 /*
3748 * Efficiency: first we see if any macro exists with the given
3749 * name. If not, we can return NULL immediately. _Then_ we
3750 * count the parameters, and then we look further along the
3751 * list if necessary to find the proper MMacro.
3752 */
3753 for (m = head; m; m = m->next)
3754 if (!mstrcmp(m->name, tline->text, m->casesense))
3755 break;
3756 if (!m)
3757 return NULL;
3758
3759 /*
3760 * OK, we have a potential macro. Count and demarcate the
3761 * parameters.
3762 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003763 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003764
3765 /*
3766 * So we know how many parameters we've got. Find the MMacro
3767 * structure that handles this number.
3768 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003769 while (m)
3770 {
3771 if (m->nparam_min <= nparam && (m->plus || nparam <= m->nparam_max))
3772 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003773 /*
3774 * This one is right. Just check if cycle removal
3775 * prohibits us using it before we actually celebrate...
3776 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003777 if (m->in_progress)
3778 {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003779#if 0
H. Peter Anvin734b1882002-04-30 21:01:08 +00003780 error(ERR_NONFATAL,
3781 "self-reference in multi-line macro `%s'", m->name);
H. Peter Anvineba20a72002-04-30 20:53:55 +00003782#endif
H. Peter Anvin734b1882002-04-30 21:01:08 +00003783 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003784 return NULL;
3785 }
3786 /*
3787 * It's right, and we can use it. Add its default
3788 * parameters to the end of our list if necessary.
3789 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003790 if (m->defaults && nparam < m->nparam_min + m->ndefs)
3791 {
3792 params =
3793 nasm_realloc(params,
3794 ((m->nparam_min + m->ndefs + 1) * sizeof(*params)));
3795 while (nparam < m->nparam_min + m->ndefs)
3796 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003797 params[nparam] = m->defaults[nparam - m->nparam_min];
3798 nparam++;
3799 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003800 }
3801 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003802 * If we've gone over the maximum parameter count (and
3803 * we're in Plus mode), ignore parameters beyond
3804 * nparam_max.
3805 */
3806 if (m->plus && nparam > m->nparam_max)
3807 nparam = m->nparam_max;
3808 /*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003809 * Then terminate the parameter list, and leave.
3810 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003811 if (!params)
3812 { /* need this special case */
H. Peter Anvin76690a12002-04-30 20:52:49 +00003813 params = nasm_malloc(sizeof(*params));
3814 nparam = 0;
3815 }
3816 params[nparam] = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003817 *params_array = params;
3818 return m;
3819 }
3820 /*
3821 * This one wasn't right: look for the next one with the
3822 * same name.
3823 */
3824 for (m = m->next; m; m = m->next)
3825 if (!mstrcmp(m->name, tline->text, m->casesense))
3826 break;
3827 }
3828
3829 /*
3830 * After all that, we didn't find one with the right number of
3831 * parameters. Issue a warning, and fail to expand the macro.
3832 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003833 error(ERR_WARNING | ERR_WARN_MNP,
3834 "macro `%s' exists, but not taking %d parameters",
3835 tline->text, nparam);
3836 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003837 return NULL;
3838}
3839
3840/*
3841 * Expand the multi-line macro call made by the given line, if
3842 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00003843 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003844 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003845static int
3846expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003847{
3848 Token *startline = tline;
3849 Token *label = NULL;
3850 int dont_prepend = 0;
3851 Token **params, *t, *tt;
3852 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003853 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003854 int i, nparam, *paramlen;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003855
3856 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003857 skip_white_(t);
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00003858/* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
3859 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003860 return 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003861 m = is_mmacro(t, &params);
3862 if (!m)
3863 {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003864 Token *last;
3865 /*
3866 * We have an id which isn't a macro call. We'll assume
3867 * it might be a label; we'll also check to see if a
3868 * colon follows it. Then, if there's another id after
3869 * that lot, we'll check it again for macro-hood.
3870 */
3871 label = last = t;
3872 t = t->next;
3873 if (tok_type_(t, TOK_WHITESPACE))
3874 last = t, t = t->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003875 if (tok_is_(t, ":"))
3876 {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003877 dont_prepend = 1;
3878 last = t, t = t->next;
3879 if (tok_type_(t, TOK_WHITESPACE))
3880 last = t, t = t->next;
3881 }
3882 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
3883 return 0;
3884 last->next = NULL;
3885 tline = t;
3886 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003887
3888 /*
3889 * Fix up the parameters: this involves stripping leading and
3890 * trailing whitespace, then stripping braces if they are
3891 * present.
3892 */
H. Peter Anvineba20a72002-04-30 20:53:55 +00003893 for (nparam = 0; params[nparam]; nparam++)
3894 ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003895 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003896
H. Peter Anvin734b1882002-04-30 21:01:08 +00003897 for (i = 0; params[i]; i++)
3898 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003899 int brace = FALSE;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003900 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003901
3902 t = params[i];
H. Peter Anvineba20a72002-04-30 20:53:55 +00003903 skip_white_(t);
3904 if (tok_is_(t, "{"))
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003905 t = t->next, brace = TRUE, comma = FALSE;
3906 params[i] = t;
3907 paramlen[i] = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003908 while (t)
3909 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003910 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
H. Peter Anvin734b1882002-04-30 21:01:08 +00003911 break; /* ... because we have hit a comma */
H. Peter Anvineba20a72002-04-30 20:53:55 +00003912 if (comma && t->type == TOK_WHITESPACE && tok_is_(t->next, ","))
H. Peter Anvin734b1882002-04-30 21:01:08 +00003913 break; /* ... or a space then a comma */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003914 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
H. Peter Anvin734b1882002-04-30 21:01:08 +00003915 break; /* ... or a brace */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003916 t = t->next;
3917 paramlen[i]++;
3918 }
3919 }
3920
3921 /*
3922 * OK, we have a MMacro structure together with a set of
3923 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00003924 * copies of each Line on to istk->expansion. Substitution of
3925 * parameter tokens and macro-local tokens doesn't get done
3926 * until the single-line macro substitution process; this is
3927 * because delaying them allows us to change the semantics
3928 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003929 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00003930 * First, push an end marker on to istk->expansion, mark this
3931 * macro as in progress, and set up its invocation-specific
3932 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003933 */
3934 ll = nasm_malloc(sizeof(Line));
3935 ll->next = istk->expansion;
3936 ll->finishes = m;
3937 ll->first = NULL;
3938 istk->expansion = ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003939
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003940 m->in_progress = TRUE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003941 m->params = params;
3942 m->iline = tline;
3943 m->nparam = nparam;
3944 m->rotate = 0;
3945 m->paramlen = paramlen;
3946 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003947 m->lineno = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003948
3949 m->next_active = istk->mstk;
3950 istk->mstk = m;
3951
H. Peter Anvin734b1882002-04-30 21:01:08 +00003952 for (l = m->expansion; l; l = l->next)
3953 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003954 Token **tail;
3955
3956 ll = nasm_malloc(sizeof(Line));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003957 ll->finishes = NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003958 ll->next = istk->expansion;
3959 istk->expansion = ll;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003960 tail = &ll->first;
3961
H. Peter Anvin734b1882002-04-30 21:01:08 +00003962 for (t = l->first; t; t = t->next)
3963 {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003964 Token *x = t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003965 if (t->type == TOK_PREPROC_ID &&
3966 t->text[1] == '0' && t->text[2] == '0')
H. Peter Anvineba20a72002-04-30 20:53:55 +00003967 {
3968 dont_prepend = -1;
3969 x = label;
3970 if (!x)
3971 continue;
3972 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003973 tt = *tail = new_Token(NULL, x->type, x->text, 0);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003974 tail = &tt->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003975 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00003976 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003977 }
3978
3979 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00003980 * If we had a label, push it on as the first line of
3981 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003982 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003983 if (label)
3984 {
3985 if (dont_prepend < 0)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003986 free_tlist(startline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003987 else
3988 {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003989 ll = nasm_malloc(sizeof(Line));
3990 ll->finishes = NULL;
3991 ll->next = istk->expansion;
3992 istk->expansion = ll;
3993 ll->first = startline;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003994 if (!dont_prepend)
3995 {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003996 while (label->next)
3997 label = label->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003998 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
H. Peter Anvineba20a72002-04-30 20:53:55 +00003999 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00004000 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004001 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004002
H. Peter Anvin734b1882002-04-30 21:01:08 +00004003 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004004
H. Peter Anvineba20a72002-04-30 20:53:55 +00004005 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004006}
4007
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004008/*
4009 * Since preprocessor always operate only on the line that didn't
4010 * arrived yet, we should always use ERR_OFFBY1. Also since user
4011 * won't want to see same error twice (preprocessing is done once
4012 * per pass) we will want to show errors only during pass one.
4013 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004014static void
Ed Beroset168c9c02002-05-17 03:10:13 +00004015error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004016{
4017 va_list arg;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004018 char buff[1024];
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004019
4020 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00004021 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004022 return;
4023
H. Peter Anvin734b1882002-04-30 21:01:08 +00004024 va_start(arg, fmt);
4025 vsprintf(buff, fmt, arg);
4026 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004027
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00004028 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvin99941bf2002-05-14 17:44:03 +00004029 _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name,
H. Peter Anvin734b1882002-04-30 21:01:08 +00004030 istk->mstk->lineno, buff);
Ed Beroset168c9c02002-05-17 03:10:13 +00004031 else
H. Peter Anvin99941bf2002-05-14 17:44:03 +00004032 _error(severity | ERR_PASS1, "%s", buff);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004033}
4034
H. Peter Anvin734b1882002-04-30 21:01:08 +00004035static void
4036pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
4037 ListGen * listgen)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004038{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004039 int h;
4040
H. Peter Anvin99941bf2002-05-14 17:44:03 +00004041 _error = errfunc;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004042 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004043 istk = nasm_malloc(sizeof(Include));
4044 istk->next = NULL;
4045 istk->conds = NULL;
4046 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004047 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004048 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00004049 istk->fname = NULL;
4050 src_set_fname(nasm_strdup(file));
4051 src_set_linnum(0);
4052 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004053 if (!istk->fp)
H. Peter Anvin734b1882002-04-30 21:01:08 +00004054 error(ERR_FATAL | ERR_NOFILE, "unable to open input file `%s'", file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004055 defining = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004056 for (h = 0; h < NHASH; h++)
4057 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004058 mmacros[h] = NULL;
4059 smacros[h] = NULL;
4060 }
4061 unique = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004062 if (tasm_compatible_mode) {
4063 stdmacpos = stdmac;
4064 } else {
4065 stdmacpos = &stdmac[TASM_MACRO_COUNT];
4066 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004067 any_extrastdmac = (extrastdmac != NULL);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004068 list = listgen;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004069 evaluate = eval;
4070 pass = apass;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004071}
4072
H. Peter Anvin734b1882002-04-30 21:01:08 +00004073static char *
4074pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004075{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004076 char *line;
4077 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004078
H. Peter Anvin734b1882002-04-30 21:01:08 +00004079 while (1)
4080 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004081 /*
4082 * Fetch a tokenised line, either from the macro-expansion
4083 * buffer or from the input file.
4084 */
4085 tline = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004086 while (istk->expansion && istk->expansion->finishes)
4087 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004088 Line *l = istk->expansion;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004089 if (!l->finishes->name && l->finishes->in_progress > 1)
4090 {
H. Peter Anvin76690a12002-04-30 20:52:49 +00004091 Line *ll;
4092
4093 /*
4094 * This is a macro-end marker for a macro with no
4095 * name, which means it's not really a macro at all
4096 * but a %rep block, and the `in_progress' field is
4097 * more than 1, meaning that we still need to
4098 * repeat. (1 means the natural last repetition; 0
4099 * means termination by %exitrep.) We have
4100 * therefore expanded up to the %endrep, and must
4101 * push the whole block on to the expansion buffer
4102 * again. We don't bother to remove the macro-end
4103 * marker: we'd only have to generate another one
4104 * if we did.
4105 */
4106 l->finishes->in_progress--;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004107 for (l = l->finishes->expansion; l; l = l->next)
4108 {
H. Peter Anvin76690a12002-04-30 20:52:49 +00004109 Token *t, *tt, **tail;
4110
4111 ll = nasm_malloc(sizeof(Line));
4112 ll->next = istk->expansion;
4113 ll->finishes = NULL;
4114 ll->first = NULL;
4115 tail = &ll->first;
4116
H. Peter Anvin734b1882002-04-30 21:01:08 +00004117 for (t = l->first; t; t = t->next)
4118 {
4119 if (t->text || t->type == TOK_WHITESPACE)
4120 {
4121 tt = *tail = new_Token(NULL, t->type, t->text, 0);
H. Peter Anvin76690a12002-04-30 20:52:49 +00004122 tail = &tt->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004123 }
4124 }
4125
4126 istk->expansion = ll;
4127 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004128 }
4129 else
4130 {
H. Peter Anvin87bc6192002-04-30 20:53:16 +00004131 /*
4132 * Check whether a `%rep' was started and not ended
4133 * within this macro expansion. This can happen and
4134 * should be detected. It's a fatal error because
4135 * I'm too confused to work out how to recover
4136 * sensibly from it.
4137 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004138 if (defining)
4139 {
H. Peter Anvin87bc6192002-04-30 20:53:16 +00004140 if (defining->name)
H. Peter Anvin734b1882002-04-30 21:01:08 +00004141 error(ERR_PANIC, "defining with name in expansion");
H. Peter Anvinb64535f2002-04-30 20:55:37 +00004142 else if (istk->mstk->name)
H. Peter Anvin734b1882002-04-30 21:01:08 +00004143 error(ERR_FATAL, "`%%rep' without `%%endrep' within"
4144 " expansion of macro `%s'", istk->mstk->name);
H. Peter Anvin87bc6192002-04-30 20:53:16 +00004145 }
4146
H. Peter Anvin734b1882002-04-30 21:01:08 +00004147 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004148 * FIXME: investigate the relationship at this point between
4149 * istk->mstk and l->finishes
4150 */
4151 {
4152 MMacro *m = istk->mstk;
4153 istk->mstk = m->next_active;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004154 if (m->name)
4155 {
H. Peter Anvineba20a72002-04-30 20:53:55 +00004156 /*
4157 * This was a real macro call, not a %rep, and
4158 * therefore the parameter information needs to
4159 * be freed.
4160 */
4161 nasm_free(m->params);
4162 free_tlist(m->iline);
4163 nasm_free(m->paramlen);
4164 l->finishes->in_progress = FALSE;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004165 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004166 else
4167 free_mmacro(m);
H. Peter Anvin76690a12002-04-30 20:52:49 +00004168 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004169 istk->expansion = l->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004170 nasm_free(l);
4171 list->downlevel(LIST_MACRO);
H. Peter Anvin76690a12002-04-30 20:52:49 +00004172 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004173 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004174 while (1)
4175 { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00004176
H. Peter Anvin734b1882002-04-30 21:01:08 +00004177 if (istk->expansion)
4178 { /* from a macro expansion */
H. Peter Anvineba20a72002-04-30 20:53:55 +00004179 char *p;
4180 Line *l = istk->expansion;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004181 if (istk->mstk)
4182 istk->mstk->lineno++;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004183 tline = l->first;
4184 istk->expansion = l->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004185 nasm_free(l);
4186 p = detoken(tline, FALSE);
4187 list->line(LIST_MACRO, p);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004188 nasm_free(p);
4189 break;
4190 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004191 line = read_line();
H. Peter Anvin734b1882002-04-30 21:01:08 +00004192 if (line)
4193 { /* from the current input file */
H. Peter Anvineba20a72002-04-30 20:53:55 +00004194 line = prepreproc(line);
4195 tline = tokenise(line);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004196 nasm_free(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004197 break;
4198 }
4199 /*
4200 * The current file has ended; work down the istk
4201 */
4202 {
4203 Include *i = istk;
4204 fclose(i->fp);
4205 if (i->conds)
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004206 error(ERR_FATAL, "expected `%%endif' before end of file");
Ed Beroset168c9c02002-05-17 03:10:13 +00004207 /* only set line and file name if there's a next node */
4208 if (i->next)
4209 {
4210 src_set_linnum(i->lineno);
4211 nasm_free(src_set_fname(i->fname));
4212 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004213 istk = i->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004214 list->downlevel(LIST_INCLUDE);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004215 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004216 if (!istk)
4217 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004218 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004219 }
4220
4221 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00004222 * We must expand MMacro parameters and MMacro-local labels
4223 * _before_ we plunge into directive processing, to cope
4224 * with things like `%define something %1' such as STRUC
4225 * uses. Unless we're _defining_ a MMacro, in which case
4226 * those tokens should be left alone to go into the
H. Peter Anvineba20a72002-04-30 20:53:55 +00004227 * definition; and unless we're in a non-emitting
4228 * condition, in which case we don't want to meddle with
4229 * anything.
H. Peter Anvin76690a12002-04-30 20:52:49 +00004230 */
H. Peter Anvineba20a72002-04-30 20:53:55 +00004231 if (!defining && !(istk->conds && !emitting(istk->conds->state)))
H. Peter Anvin76690a12002-04-30 20:52:49 +00004232 tline = expand_mmac_params(tline);
4233
4234 /*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004235 * Check the line to see if it's a preprocessor directive.
4236 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004237 if (do_directive(tline) & 1)
4238 {
4239 continue;
4240 }
4241 else if (defining)
4242 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004243 /*
4244 * We're defining a multi-line macro. We emit nothing
H. Peter Anvineba20a72002-04-30 20:53:55 +00004245 * at all, and just
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004246 * shove the tokenised line on to the macro definition.
4247 */
4248 Line *l = nasm_malloc(sizeof(Line));
4249 l->next = defining->expansion;
4250 l->first = tline;
4251 l->finishes = FALSE;
4252 defining->expansion = l;
4253 continue;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004254 }
4255 else if (istk->conds && !emitting(istk->conds->state))
4256 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004257 /*
4258 * We're in a non-emitting branch of a condition block.
4259 * Emit nothing at all, not even a blank line: when we
4260 * emerge from the condition we'll give a line-number
4261 * directive so we keep our place correctly.
4262 */
4263 free_tlist(tline);
4264 continue;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004265 }
4266 else if (istk->mstk && !istk->mstk->in_progress)
4267 {
H. Peter Anvin76690a12002-04-30 20:52:49 +00004268 /*
4269 * We're in a %rep block which has been terminated, so
4270 * we're walking through to the %endrep without
4271 * emitting anything. Emit nothing at all, not even a
4272 * blank line: when we emerge from the %rep block we'll
4273 * give a line-number directive so we keep our place
4274 * correctly.
4275 */
4276 free_tlist(tline);
4277 continue;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004278 }
4279 else
4280 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004281 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004282 if (!expand_mmacro(tline))
4283 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004284 /*
4285 * De-tokenise the line again, and emit it.
4286 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004287 line = detoken(tline, TRUE);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004288 free_tlist(tline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004289 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004290 }
4291 else
4292 {
4293 continue; /* expand_mmacro calls free_tlist */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004294 }
4295 }
4296 }
4297
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004298 return line;
4299}
4300
H. Peter Anvin734b1882002-04-30 21:01:08 +00004301static void
H. Peter Anvince616072002-04-30 21:02:23 +00004302pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004303{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004304 int h;
4305
H. Peter Anvin734b1882002-04-30 21:01:08 +00004306 if (defining)
4307 {
4308 error(ERR_NONFATAL, "end of file while still defining macro `%s'",
4309 defining->name);
4310 free_mmacro(defining);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004311 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004312 while (cstk)
4313 ctx_pop();
H. Peter Anvin734b1882002-04-30 21:01:08 +00004314 for (h = 0; h < NHASH; h++)
4315 {
4316 while (mmacros[h])
4317 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004318 MMacro *m = mmacros[h];
4319 mmacros[h] = mmacros[h]->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004320 free_mmacro(m);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004321 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004322 while (smacros[h])
4323 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004324 SMacro *s = smacros[h];
4325 smacros[h] = smacros[h]->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004326 nasm_free(s->name);
4327 free_tlist(s->expansion);
4328 nasm_free(s);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004329 }
4330 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004331 while (istk)
4332 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004333 Include *i = istk;
4334 istk = istk->next;
4335 fclose(i->fp);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004336 nasm_free(i->fname);
4337 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004338 }
4339 while (cstk)
4340 ctx_pop();
H. Peter Anvince616072002-04-30 21:02:23 +00004341 if (pass == 0)
4342 {
4343 free_llist(predef);
4344 delete_Blocks();
4345 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004346}
4347
H. Peter Anvin734b1882002-04-30 21:01:08 +00004348void
4349pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004350{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004351 IncPath *i;
4352
4353 i = nasm_malloc(sizeof(IncPath));
4354 i->path = nasm_strdup(path);
4355 i->next = ipath;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004356 ipath = i;
4357}
4358
H. Peter Anvin734b1882002-04-30 21:01:08 +00004359void
4360pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004361{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004362 Token *inc, *space, *name;
4363 Line *l;
4364
H. Peter Anvin734b1882002-04-30 21:01:08 +00004365 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4366 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4367 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004368
4369 l = nasm_malloc(sizeof(Line));
4370 l->next = predef;
4371 l->first = inc;
4372 l->finishes = FALSE;
4373 predef = l;
4374}
4375
H. Peter Anvin734b1882002-04-30 21:01:08 +00004376void
4377pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004378{
4379 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004380 Line *l;
4381 char *equals;
4382
4383 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00004384 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4385 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004386 if (equals)
4387 *equals = ' ';
H. Peter Anvineba20a72002-04-30 20:53:55 +00004388 space->next = tokenise(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004389 if (equals)
4390 *equals = '=';
4391
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004392 l = nasm_malloc(sizeof(Line));
4393 l->next = predef;
4394 l->first = def;
4395 l->finishes = FALSE;
4396 predef = l;
4397}
4398
H. Peter Anvin734b1882002-04-30 21:01:08 +00004399void
4400pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00004401{
4402 Token *def, *space;
4403 Line *l;
4404
H. Peter Anvin734b1882002-04-30 21:01:08 +00004405 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4406 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
H. Peter Anvin620515a2002-04-30 20:57:38 +00004407
4408 l = nasm_malloc(sizeof(Line));
4409 l->next = predef;
4410 l->first = def;
4411 l->finishes = FALSE;
4412 predef = l;
4413}
4414
H. Peter Anvin734b1882002-04-30 21:01:08 +00004415void
4416pp_extra_stdmac(char **macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004417{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004418 extrastdmac = macros;
4419}
4420
H. Peter Anvin734b1882002-04-30 21:01:08 +00004421static void
4422make_tok_num(Token * tok, long val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004423{
4424 char numbuf[20];
4425 sprintf(numbuf, "%ld", val);
4426 tok->text = nasm_strdup(numbuf);
4427 tok->type = TOK_NUMBER;
4428}
4429
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004430Preproc nasmpp = {
4431 pp_reset,
4432 pp_getline,
4433 pp_cleanup
4434};