blob: 2492df6840bbff6c73e647649e98e30aaae0d7a7 [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
Ed Beroset3ab3f412002-06-11 03:31:49 +0000251/*
252 * These defines are used as the possible return values for do_directive
253 */
254#define NO_DIRECTIVE_FOUND 0
255#define DIRECTIVE_FOUND 1
256
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000257/*
258 * Condition codes. Note that we use c_ prefix not C_ because C_ is
259 * used in nasm.h for the "real" condition codes. At _this_ level,
260 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
261 * ones, so we need a different enum...
262 */
H. Peter Anvin0a7a3b42002-05-14 23:54:46 +0000263static const char *conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000264 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
265 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
266 "np", "ns", "nz", "o", "p", "pe", "po", "s", "z"
267};
H. Peter Anvin734b1882002-04-30 21:01:08 +0000268enum
269{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000270 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
271 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
272 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_S, c_Z
273};
274static int inverse_ccs[] = {
275 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
276 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,
277 c_Z, c_NO, c_NP, c_PO, c_PE, c_NS, c_NZ
278};
279
H. Peter Anvin76690a12002-04-30 20:52:49 +0000280/*
281 * Directive names.
282 */
H. Peter Anvin0a7a3b42002-05-14 23:54:46 +0000283static const char *directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000284 "%arg",
H. Peter Anvin76690a12002-04-30 20:52:49 +0000285 "%assign", "%clear", "%define", "%elif", "%elifctx", "%elifdef",
H. Peter Anvin65747262002-05-07 00:10:05 +0000286 "%elifid", "%elifidn", "%elifidni", "%elifmacro", "%elifnctx", "%elifndef",
287 "%elifnid", "%elifnidn", "%elifnidni", "%elifnmacro", "%elifnnum", "%elifnstr",
H. Peter Anvin76690a12002-04-30 20:52:49 +0000288 "%elifnum", "%elifstr", "%else", "%endif", "%endm", "%endmacro",
289 "%endrep", "%error", "%exitrep", "%iassign", "%idefine", "%if",
H. Peter Anvin65747262002-05-07 00:10:05 +0000290 "%ifctx", "%ifdef", "%ifid", "%ifidn", "%ifidni", "%ifmacro", "%ifnctx",
291 "%ifndef", "%ifnid", "%ifnidn", "%ifnidni", "%ifnmacro", "%ifnnum",
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000292 "%ifnstr", "%ifnum", "%ifstr", "%imacro", "%include",
293 "%ixdefine", "%line",
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000294 "%local",
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000295 "%macro", "%pop", "%push", "%rep", "%repl", "%rotate",
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000296 "%stacksize",
H. Peter Anvin734b1882002-04-30 21:01:08 +0000297 "%strlen", "%substr", "%undef", "%xdefine"
H. Peter Anvin76690a12002-04-30 20:52:49 +0000298};
H. Peter Anvin734b1882002-04-30 21:01:08 +0000299enum
300{
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000301 PP_ARG,
H. Peter Anvin76690a12002-04-30 20:52:49 +0000302 PP_ASSIGN, PP_CLEAR, PP_DEFINE, PP_ELIF, PP_ELIFCTX, PP_ELIFDEF,
H. Peter Anvin65747262002-05-07 00:10:05 +0000303 PP_ELIFID, PP_ELIFIDN, PP_ELIFIDNI, PP_ELIFMACRO, PP_ELIFNCTX, PP_ELIFNDEF,
304 PP_ELIFNID, PP_ELIFNIDN, PP_ELIFNIDNI, PP_ELIFNMACRO, PP_ELIFNNUM, PP_ELIFNSTR,
H. Peter Anvin76690a12002-04-30 20:52:49 +0000305 PP_ELIFNUM, PP_ELIFSTR, PP_ELSE, PP_ENDIF, PP_ENDM, PP_ENDMACRO,
306 PP_ENDREP, PP_ERROR, PP_EXITREP, PP_IASSIGN, PP_IDEFINE, PP_IF,
H. Peter Anvin65747262002-05-07 00:10:05 +0000307 PP_IFCTX, PP_IFDEF, PP_IFID, PP_IFIDN, PP_IFIDNI, PP_IFMACRO, PP_IFNCTX,
308 PP_IFNDEF, PP_IFNID, PP_IFNIDN, PP_IFNIDNI, PP_IFNMACRO, PP_IFNNUM,
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000309 PP_IFNSTR, PP_IFNUM, PP_IFSTR, PP_IMACRO, PP_INCLUDE,
310 PP_IXDEFINE, PP_LINE,
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000311 PP_LOCAL,
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000312 PP_MACRO, PP_POP, PP_PUSH, PP_REP, PP_REPL, PP_ROTATE,
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000313 PP_STACKSIZE,
H. Peter Anvin734b1882002-04-30 21:01:08 +0000314 PP_STRLEN, PP_SUBSTR, PP_UNDEF, PP_XDEFINE
H. Peter Anvin76690a12002-04-30 20:52:49 +0000315};
316
H. Peter Anvin65747262002-05-07 00:10:05 +0000317/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
318static int is_condition(int arg)
319{
320 return ((arg >= PP_ELIF) && (arg <= PP_ENDIF)) ||
321 ((arg >= PP_IF) && (arg <= PP_IFSTR));
322}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000323
324/* For TASM compatibility we need to be able to recognise TASM compatible
325 * conditional compilation directives. Using the NASM pre-processor does
326 * not work, so we look for them specifically from the following list and
327 * then jam in the equivalent NASM directive into the input stream.
328 */
329
330#ifndef MAX
331# define MAX(a,b) ( ((a) > (b)) ? (a) : (b))
332#endif
333
H. Peter Anvin734b1882002-04-30 21:01:08 +0000334enum
335{
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000336 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
337 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
338};
339
H. Peter Anvin0a7a3b42002-05-14 23:54:46 +0000340static const char *tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000341 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
342 "ifndef", "include", "local"
343};
344
345static int StackSize = 4;
346static char *StackPointer = "ebp";
347static int ArgOffset = 8;
348static int LocalOffset = 4;
349
H. Peter Anvin76690a12002-04-30 20:52:49 +0000350
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000351static Context *cstk;
352static Include *istk;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000353static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000354
H. Peter Anvin99941bf2002-05-14 17:44:03 +0000355static efunc _error; /* Pointer to client-provided error reporting function */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000356static evalfunc evaluate;
357
H. Peter Anvin620515a2002-04-30 20:57:38 +0000358static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000359
H. Peter Anvin734b1882002-04-30 21:01:08 +0000360static unsigned long unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000361
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000362static Line *predef = NULL;
363
364static ListGen *list;
365
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000366/*
367 * The number of hash values we use for the macro lookup tables.
H. Peter Anvineba20a72002-04-30 20:53:55 +0000368 * FIXME: We should *really* be able to configure this at run time,
369 * or even have the hash table automatically expanding when necessary.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000370 */
371#define NHASH 31
372
373/*
374 * The current set of multi-line macros we have defined.
375 */
376static MMacro *mmacros[NHASH];
377
378/*
379 * The current set of single-line macros we have defined.
380 */
381static SMacro *smacros[NHASH];
382
383/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000384 * The multi-line macro we are currently defining, or the %rep
385 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000386 */
387static MMacro *defining;
388
389/*
390 * The number of macro parameters to allocate space for at a time.
391 */
392#define PARAM_DELTA 16
393
394/*
395 * The standard macro set: defined as `static char *stdmac[]'. Also
396 * gives our position in the macro set, when we're processing it.
397 */
398#include "macros.c"
Ed Beroset168c9c02002-05-17 03:10:13 +0000399static const char **stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000400
401/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000402 * The extra standard macros that come from the object format, if
403 * any.
404 */
H. Peter Anvinbfebdb02002-09-12 02:23:54 +0000405static const char **extrastdmac = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000406int any_extrastdmac;
407
408/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000409 * Tokens are allocated in blocks to improve speed
410 */
411#define TOKEN_BLOCKSIZE 4096
412static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000413struct Blocks {
414 Blocks *next;
415 void *chunk;
416};
417
418static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000419
420/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000421 * Forward declarations.
422 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000423static Token *expand_mmac_params(Token * tline);
424static Token *expand_smacro(Token * tline);
425static Token *expand_id(Token * tline);
426static Context *get_ctx(char *name, int all_contexts);
427static void make_tok_num(Token * tok, long val);
Ed Beroset168c9c02002-05-17 03:10:13 +0000428static void error(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000429static void *new_Block(size_t size);
430static void delete_Blocks(void);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000431static Token *new_Token(Token * next, int type, char *text, int txtlen);
432static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000433
434/*
435 * Macros for safe checking of token pointers, avoid *(NULL)
436 */
437#define tok_type_(x,t) ((x) && (x)->type == (t))
438#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
439#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
440#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000441
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000442/* Handle TASM specific directives, which do not contain a % in
443 * front of them. We do it here because I could not find any other
444 * place to do it for the moment, and it is a hack (ideally it would
445 * be nice to be able to use the NASM pre-processor to do it).
446 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000447static char *
448check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000449{
450 int i, j, k, m, len;
451 char *p = line, *oldline, oldchar;
452
453 /* Skip whitespace */
454 while (isspace(*p) && *p != 0)
455 p++;
456
457 /* Binary search for the directive name */
458 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +0000459 j = elements(tasm_directives);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000460 len = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000461 while (!isspace(p[len]) && p[len] != 0)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000462 len++;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000463 if (len)
464 {
465 oldchar = p[len];
466 p[len] = 0;
467 while (j - i > 1)
468 {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000469 k = (j + i) / 2;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000470 m = nasm_stricmp(p, tasm_directives[k]);
471 if (m == 0)
472 {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000473 /* We have found a directive, so jam a % in front of it
474 * so that NASM will then recognise it as one if it's own.
475 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000476 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000477 len = strlen(p);
478 oldline = line;
479 line = nasm_malloc(len + 2);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000480 line[0] = '%';
481 if (k == TM_IFDIFI)
482 {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000483 /* NASM does not recognise IFDIFI, so we convert it to
484 * %ifdef BOGUS. This is not used in NASM comaptible
485 * code, but does need to parse for the TASM macro
486 * package.
487 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000488 strcpy(line + 1, "ifdef BOGUS");
489 }
490 else
491 {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000492 memcpy(line + 1, p, len + 1);
493 }
494 nasm_free(oldline);
495 return line;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000496 }
497 else if (m < 0)
498 {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000499 j = k;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000500 }
501 else
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000502 i = k;
503 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000504 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000505 }
506 return line;
507}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000508
H. Peter Anvin76690a12002-04-30 20:52:49 +0000509/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000510 * The pre-preprocessing stage... This function translates line
511 * number indications as they emerge from GNU cpp (`# lineno "file"
512 * flags') into NASM preprocessor line number indications (`%line
513 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000514 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000515static char *
516prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000517{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000518 int lineno, fnlen;
519 char *fname, *oldline;
520
H. Peter Anvin734b1882002-04-30 21:01:08 +0000521 if (line[0] == '#' && line[1] == ' ')
522 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000523 oldline = line;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000524 fname = oldline + 2;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000525 lineno = atoi(fname);
526 fname += strspn(fname, "0123456789 ");
527 if (*fname == '"')
528 fname++;
529 fnlen = strcspn(fname, "\"");
H. Peter Anvin734b1882002-04-30 21:01:08 +0000530 line = nasm_malloc(20 + fnlen);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000531 sprintf(line, "%%line %d %.*s", lineno, fnlen, fname);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000532 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000533 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000534 if (tasm_compatible_mode)
535 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000536 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000537}
538
539/*
540 * The hash function for macro lookups. Note that due to some
541 * macros having case-insensitive names, the hash function must be
542 * invariant under case changes. We implement this by applying a
543 * perfectly normal hash function to the uppercase of the string.
544 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000545static int
546hash(char *s)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000547{
548 unsigned int h = 0;
549 int i = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000550 /*
551 * Powers of three, mod 31.
552 */
553 static const int multipliers[] = {
554 1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10,
555 30, 28, 22, 4, 12, 5, 15, 14, 11, 2, 6, 18, 23, 7, 21
556 };
H. Peter Anvineba20a72002-04-30 20:53:55 +0000557
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000558
H. Peter Anvin734b1882002-04-30 21:01:08 +0000559 while (*s)
560 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000561 h += multipliers[i] * (unsigned char) (toupper(*s));
562 s++;
Ed Beroset3ab3f412002-06-11 03:31:49 +0000563 if (++i >= elements(multipliers))
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000564 i = 0;
565 }
566 h %= NHASH;
567 return h;
568}
569
570/*
571 * Free a linked list of tokens.
572 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000573static void
574free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000575{
H. Peter Anvin734b1882002-04-30 21:01:08 +0000576 while (list)
577 {
578 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000579 }
580}
581
582/*
583 * Free a linked list of lines.
584 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000585static void
586free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000587{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000588 Line *l;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000589 while (list)
590 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000591 l = list;
592 list = list->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000593 free_tlist(l->first);
594 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000595 }
596}
597
598/*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000599 * Free an MMacro
600 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000601static void
602free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000603{
H. Peter Anvin734b1882002-04-30 21:01:08 +0000604 nasm_free(m->name);
605 free_tlist(m->dlist);
606 nasm_free(m->defaults);
607 free_llist(m->expansion);
608 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000609}
610
611/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000612 * Pop the context stack.
613 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000614static void
615ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000616{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000617 Context *c = cstk;
618 SMacro *smac, *s;
619
620 cstk = cstk->next;
621 smac = c->localmac;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000622 while (smac)
623 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000624 s = smac;
625 smac = smac->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000626 nasm_free(s->name);
627 free_tlist(s->expansion);
628 nasm_free(s);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000629 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000630 nasm_free(c->name);
631 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000632}
633
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000634#define BUF_DELTA 512
635/*
636 * Read a line from the top file in istk, handling multiple CR/LFs
637 * at the end of the line read, and handling spurious ^Zs. Will
638 * return lines from the standard macro set if this has not already
639 * been done.
640 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000641static char *
642read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000643{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000644 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000645 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000646
H. Peter Anvin734b1882002-04-30 21:01:08 +0000647 if (stdmacpos)
648 {
649 if (*stdmacpos)
650 {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000651 char *ret = nasm_strdup(*stdmacpos++);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000652 if (!*stdmacpos && any_extrastdmac)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000653 {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000654 stdmacpos = extrastdmac;
655 any_extrastdmac = FALSE;
656 return ret;
657 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000658 /*
659 * Nasty hack: here we push the contents of `predef' on
660 * to the top-level expansion stack, since this is the
661 * most convenient way to implement the pre-include and
662 * pre-define features.
663 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000664 if (!*stdmacpos)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000665 {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000666 Line *pd, *l;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000667 Token *head, **tail, *t;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000668
H. Peter Anvin734b1882002-04-30 21:01:08 +0000669 for (pd = predef; pd; pd = pd->next)
670 {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000671 head = NULL;
672 tail = &head;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000673 for (t = pd->first; t; t = t->next)
674 {
675 *tail = new_Token(NULL, t->type, t->text, 0);
676 tail = &(*tail)->next;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000677 }
678 l = nasm_malloc(sizeof(Line));
679 l->next = istk->expansion;
680 l->first = head;
681 l->finishes = FALSE;
682 istk->expansion = l;
683 }
684 }
685 return ret;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000686 }
687 else
688 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000689 stdmacpos = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000690 }
691 }
692
693 bufsize = BUF_DELTA;
694 buffer = nasm_malloc(BUF_DELTA);
695 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000696 continued_count = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000697 while (1)
698 {
699 q = fgets(p, bufsize - (p - buffer), istk->fp);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000700 if (!q)
701 break;
702 p += strlen(p);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000703 if (p > buffer && p[-1] == '\n')
704 {
H. Peter Anvin9f394642002-04-30 21:07:51 +0000705 /* Convert backslash-CRLF line continuation sequences into
706 nothing at all (for DOS and Windows) */
707 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
708 p -= 3;
709 *p = 0;
710 continued_count++;
711 }
712 /* Also convert backslash-LF line continuation sequences into
713 nothing at all (for Unix) */
714 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
715 p -= 2;
716 *p = 0;
717 continued_count++;
718 }
719 else {
720 break;
721 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000722 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000723 if (p - buffer > bufsize - 10)
724 {
725 long offset = p - buffer;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000726 bufsize += BUF_DELTA;
727 buffer = nasm_realloc(buffer, bufsize);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000728 p = buffer + offset; /* prevent stale-pointer problems */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000729 }
730 }
731
H. Peter Anvin734b1882002-04-30 21:01:08 +0000732 if (!q && p == buffer)
733 {
734 nasm_free(buffer);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000735 return NULL;
736 }
737
H. Peter Anvin9a633fa2002-04-30 21:08:11 +0000738 src_set_linnum(src_get_linnum() + istk->lineinc + (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000739
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000740 /*
741 * Play safe: remove CRs as well as LFs, if any of either are
742 * present at the end of the line.
743 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000744 while (--p >= buffer && (*p == '\n' || *p == '\r'))
745 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000746
747 /*
748 * Handle spurious ^Z, which may be inserted into source files
749 * by some file transfer utilities.
750 */
751 buffer[strcspn(buffer, "\032")] = '\0';
752
H. Peter Anvin734b1882002-04-30 21:01:08 +0000753 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000754
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000755 return buffer;
756}
757
758/*
759 * Tokenise a line of text. This is a very simple process since we
760 * don't need to parse the value out of e.g. numeric tokens: we
761 * simply split one string into many.
762 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000763static Token *
764tokenise(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000765{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000766 char *p = line;
767 int type;
768 Token *list = NULL;
769 Token *t, **tail = &list;
770
H. Peter Anvin734b1882002-04-30 21:01:08 +0000771 while (*line)
772 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000773 p = line;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000774 if (*p == '%')
H. Peter Anvineba20a72002-04-30 20:53:55 +0000775 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000776 p++;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000777 if ( isdigit(*p) ||
778 ((*p == '-' || *p == '+') && isdigit(p[1])) ||
779 ((*p == '+') && (isspace(p[1]) || !p[1])))
780 {
781 do
782 {
783 p++;
784 }
785 while (isdigit(*p));
786 type = TOK_PREPROC_ID;
787 }
788 else if (*p == '{')
789 {
790 p++;
791 while (*p && *p != '}')
792 {
793 p[-1] = *p;
794 p++;
795 }
796 p[-1] = '\0';
797 if (*p)
798 p++;
799 type = TOK_PREPROC_ID;
800 }
801 else if (isidchar(*p) ||
802 ((*p == '!' || *p == '%' || *p == '$') &&
803 isidchar(p[1])))
804 {
805 do
806 {
807 p++;
808 }
809 while (isidchar(*p));
810 type = TOK_PREPROC_ID;
811 }
812 else
813 {
814 type = TOK_OTHER;
815 if (*p == '%')
816 p++;
817 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000818 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000819 else if (isidstart(*p) || (*p == '$' && isidstart(p[1])))
H. Peter Anvineba20a72002-04-30 20:53:55 +0000820 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000821 type = TOK_ID;
822 p++;
823 while (*p && isidchar(*p))
824 p++;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000825 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000826 else if (*p == '\'' || *p == '"')
827 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000828 /*
829 * A string token.
830 */
831 char c = *p;
832 p++;
833 type = TOK_STRING;
834 while (*p && *p != c)
835 p++;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000836 if (*p)
837 {
838 p++;
839 }
840 else
841 {
842 error(ERR_WARNING, "unterminated string");
843 }
844 }
845 else if (isnumstart(*p))
846 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000847 /*
848 * A number token.
849 */
850 type = TOK_NUMBER;
851 p++;
852 while (*p && isnumchar(*p))
853 p++;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000854 }
855 else if (isspace(*p))
856 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000857 type = TOK_WHITESPACE;
858 p++;
859 while (*p && isspace(*p))
860 p++;
861 /*
862 * Whitespace just before end-of-line is discarded by
863 * pretending it's a comment; whitespace just before a
864 * comment gets lumped into the comment.
865 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000866 if (!*p || *p == ';')
867 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000868 type = TOK_COMMENT;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000869 while (*p)
870 p++;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000871 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000872 }
873 else if (*p == ';')
874 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000875 type = TOK_COMMENT;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000876 while (*p)
877 p++;
878 }
879 else
880 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000881 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000882 * Anything else is an operator of some kind. We check
883 * for all the double-character operators (>>, <<, //,
884 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
885 * else is a single-character operator.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000886 */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000887 type = TOK_OTHER;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000888 if ((p[0] == '>' && p[1] == '>') ||
H. Peter Anvin734b1882002-04-30 21:01:08 +0000889 (p[0] == '<' && p[1] == '<') ||
890 (p[0] == '/' && p[1] == '/') ||
891 (p[0] == '<' && p[1] == '=') ||
892 (p[0] == '>' && p[1] == '=') ||
893 (p[0] == '=' && p[1] == '=') ||
894 (p[0] == '!' && p[1] == '=') ||
895 (p[0] == '<' && p[1] == '>') ||
896 (p[0] == '&' && p[1] == '&') ||
897 (p[0] == '|' && p[1] == '|') ||
898 (p[0] == '^' && p[1] == '^'))
H. Peter Anvineba20a72002-04-30 20:53:55 +0000899 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000900 p++;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000901 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000902 p++;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000903 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000904 if (type != TOK_COMMENT)
905 {
906 *tail = t = new_Token(NULL, type, line, p - line);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000907 tail = &t->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000908 }
909 line = p;
910 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000911 return list;
912}
913
H. Peter Anvince616072002-04-30 21:02:23 +0000914/*
915 * this function allocates a new managed block of memory and
916 * returns a pointer to the block. The managed blocks are
917 * deleted only all at once by the delete_Blocks function.
918 */
919static void *
920new_Block(size_t size)
921{
922 Blocks *b = &blocks;
923
924 /* first, get to the end of the linked list */
925 while (b->next)
926 b = b->next;
927 /* now allocate the requested chunk */
928 b->chunk = nasm_malloc(size);
929
930 /* now allocate a new block for the next request */
931 b->next = nasm_malloc(sizeof(Blocks));
932 /* and initialize the contents of the new block */
933 b->next->next = NULL;
934 b->next->chunk = NULL;
935 return b->chunk;
936}
937
938/*
939 * this function deletes all managed blocks of memory
940 */
941static void
942delete_Blocks(void)
943{
944 Blocks *a,*b = &blocks;
945
946 /*
947 * keep in mind that the first block, pointed to by blocks
948 * is a static and not dynamically allocated, so we don't
949 * free it.
950 */
951 while (b)
952 {
953 if (b->chunk)
954 nasm_free(b->chunk);
955 a = b;
956 b = b->next;
H. Peter Anvin9eb185b2002-04-30 21:02:47 +0000957 if (a != &blocks)
H. Peter Anvince616072002-04-30 21:02:23 +0000958 nasm_free(a);
959 }
960}
H. Peter Anvin734b1882002-04-30 21:01:08 +0000961
962/*
963 * this function creates a new Token and passes a pointer to it
964 * back to the caller. It sets the type and text elements, and
965 * also the mac and next elements to NULL.
966 */
967static Token *
968new_Token(Token * next, int type, char *text, int txtlen)
969{
970 Token *t;
971 int i;
972
973 if (freeTokens == NULL)
974 {
H. Peter Anvince616072002-04-30 21:02:23 +0000975 freeTokens = (Token *)new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
H. Peter Anvin734b1882002-04-30 21:01:08 +0000976 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
977 freeTokens[i].next = &freeTokens[i + 1];
978 freeTokens[i].next = NULL;
979 }
980 t = freeTokens;
981 freeTokens = t->next;
982 t->next = next;
983 t->mac = NULL;
984 t->type = type;
985 if (type == TOK_WHITESPACE || text == NULL)
986 {
987 t->text = NULL;
988 }
989 else
990 {
991 if (txtlen == 0)
992 txtlen = strlen(text);
993 t->text = nasm_malloc(1 + txtlen);
994 strncpy(t->text, text, txtlen);
995 t->text[txtlen] = '\0';
996 }
997 return t;
998}
999
1000static Token *
1001delete_Token(Token * t)
1002{
1003 Token *next = t->next;
1004 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001005 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001006 freeTokens = t;
1007 return next;
1008}
1009
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001010/*
1011 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001012 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1013 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001014 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001015static char *
1016detoken(Token * tlist, int expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001017{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001018 Token *t;
1019 int len;
1020 char *line, *p;
1021
1022 len = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001023 for (t = tlist; t; t = t->next)
1024 {
1025 if (t->type == TOK_PREPROC_ID && t->text[1] == '!')
1026 {
1027 char *p = getenv(t->text + 2);
1028 nasm_free(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001029 if (p)
1030 t->text = nasm_strdup(p);
1031 else
1032 t->text = NULL;
1033 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001034 /* Expand local macros here and not during preprocessing */
1035 if (expand_locals &&
H. Peter Anvin734b1882002-04-30 21:01:08 +00001036 t->type == TOK_PREPROC_ID && t->text &&
1037 t->text[0] == '%' && t->text[1] == '$')
1038 {
1039 Context *ctx = get_ctx(t->text, FALSE);
1040 if (ctx)
1041 {
1042 char buffer[40];
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001043 char *p, *q = t->text + 2;
1044
H. Peter Anvin734b1882002-04-30 21:01:08 +00001045 q += strspn(q, "$");
1046 sprintf(buffer, "..@%lu.", ctx->number);
1047 p = nasm_strcat(buffer, q);
1048 nasm_free(t->text);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001049 t->text = p;
1050 }
1051 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001052 if (t->type == TOK_WHITESPACE)
1053 {
1054 len++;
1055 }
1056 else if (t->text)
1057 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001058 len += strlen(t->text);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001059 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001060 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001061 p = line = nasm_malloc(len + 1);
1062 for (t = tlist; t; t = t->next)
1063 {
1064 if (t->type == TOK_WHITESPACE)
1065 {
1066 *p = ' ';
1067 p++;
1068 *p = '\0';
1069 }
1070 else if (t->text)
1071 {
1072 strcpy(p, t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001073 p += strlen(p);
1074 }
1075 }
1076 *p = '\0';
1077 return line;
1078}
1079
1080/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001081 * A scanner, suitable for use by the expression evaluator, which
1082 * operates on a line of Tokens. Expects a pointer to a pointer to
1083 * the first token in the line to be passed in as its private_data
1084 * field.
1085 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001086static int
1087ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001088{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001089 Token **tlineptr = private_data;
1090 Token *tline;
1091
H. Peter Anvin734b1882002-04-30 21:01:08 +00001092 do
1093 {
H. Peter Anvin76690a12002-04-30 20:52:49 +00001094 tline = *tlineptr;
1095 *tlineptr = tline ? tline->next : NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001096 }
1097 while (tline && (tline->type == TOK_WHITESPACE ||
1098 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001099
1100 if (!tline)
1101 return tokval->t_type = TOKEN_EOS;
1102
1103 if (tline->text[0] == '$' && !tline->text[1])
1104 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001105 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvin76690a12002-04-30 20:52:49 +00001106 return tokval->t_type = TOKEN_BASE;
1107
H. Peter Anvin734b1882002-04-30 21:01:08 +00001108 if (tline->type == TOK_ID)
1109 {
H. Peter Anvin76690a12002-04-30 20:52:49 +00001110 tokval->t_charptr = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001111 if (tline->text[0] == '$')
1112 {
H. Peter Anvin76690a12002-04-30 20:52:49 +00001113 tokval->t_charptr++;
1114 return tokval->t_type = TOKEN_ID;
1115 }
1116
1117 /*
1118 * This is the only special case we actually need to worry
1119 * about in this restricted context.
1120 */
1121 if (!nasm_stricmp(tline->text, "seg"))
1122 return tokval->t_type = TOKEN_SEG;
1123
1124 return tokval->t_type = TOKEN_ID;
1125 }
1126
H. Peter Anvin734b1882002-04-30 21:01:08 +00001127 if (tline->type == TOK_NUMBER)
1128 {
H. Peter Anvin76690a12002-04-30 20:52:49 +00001129 int rn_error;
1130
1131 tokval->t_integer = readnum(tline->text, &rn_error);
1132 if (rn_error)
1133 return tokval->t_type = TOKEN_ERRNUM;
1134 tokval->t_charptr = NULL;
1135 return tokval->t_type = TOKEN_NUM;
1136 }
1137
H. Peter Anvin734b1882002-04-30 21:01:08 +00001138 if (tline->type == TOK_STRING)
1139 {
H. Peter Anvineba20a72002-04-30 20:53:55 +00001140 int rn_warn;
1141 char q, *r;
1142 int l;
1143
1144 r = tline->text;
1145 q = *r++;
1146 l = strlen(r);
1147
H. Peter Anvin734b1882002-04-30 21:01:08 +00001148 if (l == 0 || r[l - 1] != q)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001149 return tokval->t_type = TOKEN_ERRNUM;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001150 tokval->t_integer = readstrnum(r, l - 1, &rn_warn);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001151 if (rn_warn)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001152 error(ERR_WARNING | ERR_PASS1, "character constant too long");
H. Peter Anvineba20a72002-04-30 20:53:55 +00001153 tokval->t_charptr = NULL;
1154 return tokval->t_type = TOKEN_NUM;
1155 }
1156
H. Peter Anvin734b1882002-04-30 21:01:08 +00001157 if (tline->type == TOK_OTHER)
1158 {
1159 if (!strcmp(tline->text, "<<"))
1160 return tokval->t_type = TOKEN_SHL;
1161 if (!strcmp(tline->text, ">>"))
1162 return tokval->t_type = TOKEN_SHR;
1163 if (!strcmp(tline->text, "//"))
1164 return tokval->t_type = TOKEN_SDIV;
1165 if (!strcmp(tline->text, "%%"))
1166 return tokval->t_type = TOKEN_SMOD;
1167 if (!strcmp(tline->text, "=="))
1168 return tokval->t_type = TOKEN_EQ;
1169 if (!strcmp(tline->text, "<>"))
1170 return tokval->t_type = TOKEN_NE;
1171 if (!strcmp(tline->text, "!="))
1172 return tokval->t_type = TOKEN_NE;
1173 if (!strcmp(tline->text, "<="))
1174 return tokval->t_type = TOKEN_LE;
1175 if (!strcmp(tline->text, ">="))
1176 return tokval->t_type = TOKEN_GE;
1177 if (!strcmp(tline->text, "&&"))
1178 return tokval->t_type = TOKEN_DBL_AND;
1179 if (!strcmp(tline->text, "^^"))
1180 return tokval->t_type = TOKEN_DBL_XOR;
1181 if (!strcmp(tline->text, "||"))
1182 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001183 }
1184
1185 /*
1186 * We have no other options: just return the first character of
1187 * the token text.
1188 */
1189 return tokval->t_type = tline->text[0];
1190}
1191
1192/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001193 * Compare a string to the name of an existing macro; this is a
1194 * simple wrapper which calls either strcmp or nasm_stricmp
1195 * depending on the value of the `casesense' parameter.
1196 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001197static int
1198mstrcmp(char *p, char *q, int casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001199{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001200 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001201}
1202
1203/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001204 * Return the Context structure associated with a %$ token. Return
1205 * NULL, having _already_ reported an error condition, if the
1206 * context stack isn't deep enough for the supplied number of $
1207 * signs.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001208 * If all_contexts == TRUE, contexts that enclose current are
1209 * also scanned for such smacro, until it is found; if not -
1210 * only the context that directly results from the number of $'s
1211 * in variable's name.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001212 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001213static Context *
1214get_ctx(char *name, int all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001215{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001216 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001217 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001218 int i;
1219
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001220 if (!name || name[0] != '%' || name[1] != '$')
1221 return NULL;
1222
H. Peter Anvin734b1882002-04-30 21:01:08 +00001223 if (!cstk)
1224 {
1225 error(ERR_NONFATAL, "`%s': context stack is empty", name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001226 return NULL;
1227 }
1228
H. Peter Anvin734b1882002-04-30 21:01:08 +00001229 for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--)
1230 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001231 ctx = ctx->next;
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00001232/* i--; Lino - 02/25/02 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001233 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001234 if (!ctx)
1235 {
1236 error(ERR_NONFATAL, "`%s': context stack is only"
1237 " %d level%s deep", name, i - 1, (i == 2 ? "" : "s"));
1238 return NULL;
1239 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001240 if (!all_contexts)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001241 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001242
H. Peter Anvin734b1882002-04-30 21:01:08 +00001243 do
1244 {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001245 /* Search for this smacro in found context */
1246 m = ctx->localmac;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001247 while (m)
1248 {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001249 if (!mstrcmp(m->name, name, m->casesense))
1250 return ctx;
1251 m = m->next;
1252 }
1253 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001254 }
1255 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001256 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001257}
1258
1259/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001260 * Open an include file. This routine must always return a valid
1261 * file pointer if it returns - it's responsible for throwing an
1262 * ERR_FATAL and bombing out completely if not. It should also try
1263 * the include path one by one until it finds the file or reaches
1264 * the end of the path.
1265 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001266static FILE *
1267inc_fopen(char *file)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001268{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001269 FILE *fp;
1270 char *prefix = "", *combine;
1271 IncPath *ip = ipath;
H. Peter Anvin620515a2002-04-30 20:57:38 +00001272 static int namelen = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001273 int len = strlen(file);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001274
H. Peter Anvin734b1882002-04-30 21:01:08 +00001275 while (1)
1276 {
Frank Kotler7fd4f002003-08-06 07:10:16 +00001277 combine = nasm_malloc(strlen(prefix) + len + 1);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001278 strcpy(combine, prefix);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001279 strcat(combine, file);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001280 fp = fopen(combine, "r");
H. Peter Anvin620515a2002-04-30 20:57:38 +00001281 if (pass == 0 && fp)
1282 {
H. Peter Anvin734b1882002-04-30 21:01:08 +00001283 namelen += strlen(combine) + 1;
1284 if (namelen > 62)
1285 {
1286 printf(" \\\n ");
1287 namelen = 2;
1288 }
1289 printf(" %s", combine);
H. Peter Anvin620515a2002-04-30 20:57:38 +00001290 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001291 nasm_free(combine);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001292 if (fp)
1293 return fp;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001294 if (!ip)
1295 break;
1296 prefix = ip->path;
1297 ip = ip->next;
1298 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001299
H. Peter Anvin734b1882002-04-30 21:01:08 +00001300 error(ERR_FATAL, "unable to open include file `%s'", file);
1301 return NULL; /* never reached - placate compilers */
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001302}
1303
1304/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001305 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001306 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001307 * return TRUE if _any_ single-line macro of that name is defined.
1308 * Otherwise, will return TRUE if a single-line macro with either
1309 * `nparam' or no parameters is defined.
1310 *
1311 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001312 * defined, or nparam is -1, the address of the definition structure
1313 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001314 * is NULL, no action will be taken regarding its contents, and no
1315 * error will occur.
1316 *
1317 * Note that this is also called with nparam zero to resolve
1318 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001319 *
1320 * If you already know which context macro belongs to, you can pass
1321 * the context pointer as first parameter; if you won't but name begins
1322 * with %$ the context will be automatically computed. If all_contexts
1323 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001324 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001325static int
1326smacro_defined(Context * ctx, char *name, int nparam, SMacro ** defn,
1327 int nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001328{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001329 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001330
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001331 if (ctx)
1332 m = ctx->localmac;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001333 else if (name[0] == '%' && name[1] == '$')
1334 {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001335 if (cstk)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001336 ctx = get_ctx(name, FALSE);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001337 if (!ctx)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001338 return FALSE; /* got to return _something_ */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001339 m = ctx->localmac;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001340 }
1341 else
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001342 m = smacros[hash(name)];
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001343
H. Peter Anvin734b1882002-04-30 21:01:08 +00001344 while (m)
1345 {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001346 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
H. Peter Anvin734b1882002-04-30 21:01:08 +00001347 (nparam <= 0 || m->nparam == 0 || nparam == m->nparam))
1348 {
1349 if (defn)
1350 {
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001351 if (nparam == m->nparam || nparam == -1)
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001352 *defn = m;
1353 else
1354 *defn = NULL;
1355 }
1356 return TRUE;
1357 }
1358 m = m->next;
1359 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001360
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001361 return FALSE;
1362}
1363
1364/*
1365 * Count and mark off the parameters in a multi-line macro call.
1366 * This is called both from within the multi-line macro expansion
1367 * code, and also to mark off the default parameters when provided
1368 * in a %macro definition line.
1369 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001370static void
1371count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001372{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001373 int paramsize, brace;
1374
1375 *nparam = paramsize = 0;
1376 *params = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001377 while (t)
1378 {
1379 if (*nparam >= paramsize)
1380 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001381 paramsize += PARAM_DELTA;
1382 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1383 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001384 skip_white_(t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001385 brace = FALSE;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001386 if (tok_is_(t, "{"))
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001387 brace = TRUE;
1388 (*params)[(*nparam)++] = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001389 while (tok_isnt_(t, brace ? "}" : ","))
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001390 t = t->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001391 if (t)
1392 { /* got a comma/brace */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001393 t = t->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001394 if (brace)
1395 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001396 /*
1397 * Now we've found the closing brace, look further
1398 * for the comma.
1399 */
H. Peter Anvineba20a72002-04-30 20:53:55 +00001400 skip_white_(t);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001401 if (tok_isnt_(t, ","))
1402 {
1403 error(ERR_NONFATAL,
1404 "braces do not enclose all of macro parameter");
H. Peter Anvineba20a72002-04-30 20:53:55 +00001405 while (tok_isnt_(t, ","))
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001406 t = t->next;
1407 }
1408 if (t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001409 t = t->next; /* eat the comma */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001410 }
1411 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001412 }
1413}
1414
1415/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001416 * Determine whether one of the various `if' conditions is true or
1417 * not.
1418 *
1419 * We must free the tline we get passed.
1420 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001421static int
1422if_condition(Token * tline, int i)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001423{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001424 int j, casesense;
1425 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001426 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001427 expr *evalresult;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001428
1429 origline = tline;
1430
H. Peter Anvin734b1882002-04-30 21:01:08 +00001431 switch (i)
1432 {
1433 case PP_IFCTX:
1434 case PP_ELIFCTX:
1435 case PP_IFNCTX:
1436 case PP_ELIFNCTX:
1437 j = FALSE; /* have we matched yet? */
1438 while (cstk && tline)
1439 {
1440 skip_white_(tline);
1441 if (!tline || tline->type != TOK_ID)
1442 {
1443 error(ERR_NONFATAL,
1444 "`%s' expects context identifiers",
1445 directives[i]);
1446 free_tlist(origline);
1447 return -1;
1448 }
1449 if (!nasm_stricmp(tline->text, cstk->name))
1450 j = TRUE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001451 tline = tline->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001452 }
1453 if (i == PP_IFNCTX || i == PP_ELIFNCTX)
1454 j = !j;
1455 free_tlist(origline);
1456 return j;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001457
H. Peter Anvin734b1882002-04-30 21:01:08 +00001458 case PP_IFDEF:
1459 case PP_ELIFDEF:
1460 case PP_IFNDEF:
1461 case PP_ELIFNDEF:
1462 j = FALSE; /* have we matched yet? */
1463 while (tline)
1464 {
1465 skip_white_(tline);
1466 if (!tline || (tline->type != TOK_ID &&
1467 (tline->type != TOK_PREPROC_ID ||
1468 tline->text[1] != '$')))
1469 {
1470 error(ERR_NONFATAL,
H. Peter Anvin65747262002-05-07 00:10:05 +00001471 "`%s' expects macro identifiers",
1472 directives[i]);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001473 free_tlist(origline);
1474 return -1;
1475 }
1476 if (smacro_defined(NULL, tline->text, 0, NULL, 1))
1477 j = TRUE;
1478 tline = tline->next;
1479 }
1480 if (i == PP_IFNDEF || i == PP_ELIFNDEF)
1481 j = !j;
1482 free_tlist(origline);
1483 return j;
1484
1485 case PP_IFIDN:
1486 case PP_ELIFIDN:
1487 case PP_IFNIDN:
1488 case PP_ELIFNIDN:
1489 case PP_IFIDNI:
1490 case PP_ELIFIDNI:
1491 case PP_IFNIDNI:
1492 case PP_ELIFNIDNI:
1493 tline = expand_smacro(tline);
1494 t = tt = tline;
1495 while (tok_isnt_(tt, ","))
1496 tt = tt->next;
1497 if (!tt)
1498 {
1499 error(ERR_NONFATAL,
1500 "`%s' expects two comma-separated arguments",
1501 directives[i]);
1502 free_tlist(tline);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001503 return -1;
1504 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001505 tt = tt->next;
1506 casesense = (i == PP_IFIDN || i == PP_ELIFIDN ||
1507 i == PP_IFNIDN || i == PP_ELIFNIDN);
1508 j = TRUE; /* assume equality unless proved not */
1509 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt)
1510 {
1511 if (tt->type == TOK_OTHER && !strcmp(tt->text, ","))
1512 {
1513 error(ERR_NONFATAL, "`%s': more than one comma on line",
1514 directives[i]);
1515 free_tlist(tline);
1516 return -1;
1517 }
1518 if (t->type == TOK_WHITESPACE)
1519 {
1520 t = t->next;
1521 continue;
1522 }
1523 else if (tt->type == TOK_WHITESPACE)
1524 {
1525 tt = tt->next;
1526 continue;
1527 }
1528 else if (tt->type != t->type ||
1529 mstrcmp(tt->text, t->text, casesense))
1530 {
1531 j = FALSE; /* found mismatching tokens */
1532 break;
1533 }
1534 else
1535 {
1536 t = t->next;
1537 tt = tt->next;
1538 continue;
1539 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001540 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001541 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
1542 j = FALSE; /* trailing gunk on one end or other */
1543 if (i == PP_IFNIDN || i == PP_ELIFNIDN ||
1544 i == PP_IFNIDNI || i == PP_ELIFNIDNI)
1545 j = !j;
1546 free_tlist(tline);
1547 return j;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001548
H. Peter Anvin65747262002-05-07 00:10:05 +00001549 case PP_IFMACRO:
1550 case PP_ELIFMACRO:
1551 case PP_IFNMACRO:
1552 case PP_ELIFNMACRO:
1553 {
1554 int found = 0;
1555 MMacro searching, *mmac;
1556
1557 tline = tline->next;
1558 skip_white_(tline);
1559 tline = expand_id(tline);
1560 if (!tok_type_(tline, TOK_ID))
1561 {
1562 error(ERR_NONFATAL,
1563 "`%s' expects a macro name",
1564 directives[i]);
1565 return -1;
1566 }
1567 searching.name = nasm_strdup(tline->text);
1568 searching.casesense = (i == PP_MACRO);
1569 searching.plus = FALSE;
1570 searching.nolist = FALSE;
1571 searching.in_progress = FALSE;
1572 searching.rep_nest = NULL;
1573 searching.nparam_min = 0;
1574 searching.nparam_max = INT_MAX;
1575 tline = expand_smacro(tline->next);
1576 skip_white_(tline);
1577 if (!tline)
1578 {
1579 } else if (!tok_type_(tline, TOK_NUMBER))
1580 {
1581 error(ERR_NONFATAL,
1582 "`%s' expects a parameter count or nothing",
1583 directives[i]);
1584 }
1585 else
1586 {
1587 searching.nparam_min = searching.nparam_max =
1588 readnum(tline->text, &j);
1589 if (j)
1590 error(ERR_NONFATAL,
1591 "unable to parse parameter count `%s'",
1592 tline->text);
1593 }
1594 if (tline && tok_is_(tline->next, "-"))
1595 {
1596 tline = tline->next->next;
1597 if (tok_is_(tline, "*"))
1598 searching.nparam_max = INT_MAX;
1599 else if (!tok_type_(tline, TOK_NUMBER))
1600 error(ERR_NONFATAL,
1601 "`%s' expects a parameter count after `-'",
1602 directives[i]);
1603 else
1604 {
1605 searching.nparam_max = readnum(tline->text, &j);
1606 if (j)
1607 error(ERR_NONFATAL,
1608 "unable to parse parameter count `%s'",
1609 tline->text);
1610 if (searching.nparam_min > searching.nparam_max)
1611 error(ERR_NONFATAL,
1612 "minimum parameter count exceeds maximum");
1613 }
1614 }
1615 if (tline && tok_is_(tline->next, "+"))
1616 {
1617 tline = tline->next;
1618 searching.plus = TRUE;
1619 }
1620 mmac = mmacros[hash(searching.name)];
1621 while (mmac)
1622 {
1623 if (!strcmp(mmac->name, searching.name) &&
1624 (mmac->nparam_min <= searching.nparam_max
1625 || searching.plus)
1626 && (searching.nparam_min <= mmac->nparam_max
1627 || mmac->plus))
1628 {
1629 found = TRUE;
1630 break;
1631 }
1632 mmac = mmac->next;
1633 }
1634 nasm_free(searching.name);
1635 free_tlist(origline);
1636 if (i == PP_IFNMACRO || i == PP_ELIFNMACRO)
1637 found = !found;
1638 return found;
1639 }
1640
H. Peter Anvin734b1882002-04-30 21:01:08 +00001641 case PP_IFID:
1642 case PP_ELIFID:
1643 case PP_IFNID:
1644 case PP_ELIFNID:
1645 case PP_IFNUM:
1646 case PP_ELIFNUM:
1647 case PP_IFNNUM:
1648 case PP_ELIFNNUM:
1649 case PP_IFSTR:
1650 case PP_ELIFSTR:
1651 case PP_IFNSTR:
1652 case PP_ELIFNSTR:
1653 tline = expand_smacro(tline);
1654 t = tline;
1655 while (tok_type_(t, TOK_WHITESPACE))
1656 t = t->next;
1657 j = FALSE; /* placate optimiser */
1658 if (t)
1659 switch (i)
1660 {
1661 case PP_IFID:
1662 case PP_ELIFID:
1663 case PP_IFNID:
1664 case PP_ELIFNID:
1665 j = (t->type == TOK_ID);
1666 break;
1667 case PP_IFNUM:
1668 case PP_ELIFNUM:
1669 case PP_IFNNUM:
1670 case PP_ELIFNNUM:
1671 j = (t->type == TOK_NUMBER);
1672 break;
1673 case PP_IFSTR:
1674 case PP_ELIFSTR:
1675 case PP_IFNSTR:
1676 case PP_ELIFNSTR:
1677 j = (t->type == TOK_STRING);
1678 break;
1679 }
1680 if (i == PP_IFNID || i == PP_ELIFNID ||
1681 i == PP_IFNNUM || i == PP_ELIFNNUM ||
1682 i == PP_IFNSTR || i == PP_ELIFNSTR)
1683 j = !j;
1684 free_tlist(tline);
1685 return j;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001686
H. Peter Anvin734b1882002-04-30 21:01:08 +00001687 case PP_IF:
1688 case PP_ELIF:
1689 t = tline = expand_smacro(tline);
1690 tptr = &t;
1691 tokval.t_type = TOKEN_INVALID;
1692 evalresult = evaluate(ppscan, tptr, &tokval,
1693 NULL, pass | CRITICAL, error, NULL);
1694 free_tlist(tline);
1695 if (!evalresult)
1696 return -1;
1697 if (tokval.t_type)
1698 error(ERR_WARNING,
1699 "trailing garbage after expression ignored");
1700 if (!is_simple(evalresult))
1701 {
1702 error(ERR_NONFATAL,
1703 "non-constant value given to `%s'", directives[i]);
1704 return -1;
1705 }
1706 return reloc_value(evalresult) != 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001707
H. Peter Anvin734b1882002-04-30 21:01:08 +00001708 default:
1709 error(ERR_FATAL,
1710 "preprocessor directive `%s' not yet implemented",
1711 directives[i]);
1712 free_tlist(origline);
1713 return -1; /* yeah, right */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001714 }
1715}
1716
1717/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001718 * Expand macros in a string. Used in %error and %include directives.
1719 * First tokenise the string, apply "expand_smacro" and then de-tokenise back.
1720 * The returned variable should ALWAYS be freed after usage.
1721 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001722void
1723expand_macros_in_string(char **p)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001724{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001725 Token *line = tokenise(*p);
1726 line = expand_smacro(line);
1727 *p = detoken(line, FALSE);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001728}
1729
Ed Beroset3ab3f412002-06-11 03:31:49 +00001730/**
1731 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001732 * Find out if a line contains a preprocessor directive, and deal
1733 * with it if so.
1734 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001735 * If a directive _is_ found, it is the responsibility of this routine
1736 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001737 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001738 * @param tline a pointer to the current tokeninzed line linked list
1739 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001740 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001741 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001742static int
1743do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001744{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001745 int i, j, k, m, nparam, nolist;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001746 int offset;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001747 char *p, *mname;
1748 Include *inc;
1749 Context *ctx;
1750 Cond *cond;
1751 SMacro *smac, **smhead;
1752 MMacro *mmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001753 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1754 Line *l;
1755 struct tokenval tokval;
1756 expr *evalresult;
H. Peter Anvinb64535f2002-04-30 20:55:37 +00001757 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001758
1759 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001760
H. Peter Anvineba20a72002-04-30 20:53:55 +00001761 skip_white_(tline);
1762 if (!tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvin734b1882002-04-30 21:01:08 +00001763 (tline->text[1] == '%' || tline->text[1] == '$'
1764 || tline->text[1] == '!'))
Ed Beroset3ab3f412002-06-11 03:31:49 +00001765 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001766
1767 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +00001768 j = elements(directives);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001769 while (j - i > 1)
1770 {
1771 k = (j + i) / 2;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001772 m = nasm_stricmp(tline->text, directives[k]);
1773 if (m == 0) {
H. Peter Anvin734b1882002-04-30 21:01:08 +00001774 if (tasm_compatible_mode) {
1775 i = k;
1776 j = -2;
1777 } else if (k != PP_ARG && k != PP_LOCAL && k != PP_STACKSIZE) {
1778 i = k;
1779 j = -2;
1780 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001781 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001782 }
1783 else if (m < 0) {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001784 j = k;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001785 }
1786 else
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001787 i = k;
1788 }
1789
1790 /*
1791 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00001792 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001793 * we should ignore all directives except for condition
1794 * directives.
1795 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001796 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvin65747262002-05-07 00:10:05 +00001797 (istk->mstk && !istk->mstk->in_progress)) &&
1798 !is_condition(i))
H. Peter Anvineba20a72002-04-30 20:53:55 +00001799 {
Ed Beroset3ab3f412002-06-11 03:31:49 +00001800 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001801 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001802
1803 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001804 * If we're defining a macro or reading a %rep block, we should
1805 * ignore all directives except for %macro/%imacro (which
1806 * generate an error), %endm/%endmacro, and (only if we're in a
H. Peter Anvineba20a72002-04-30 20:53:55 +00001807 * %rep block) %endrep. If we're in a %rep block, another %rep
1808 * causes an error, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001809 */
1810 if (defining && i != PP_MACRO && i != PP_IMACRO &&
H. Peter Anvin734b1882002-04-30 21:01:08 +00001811 i != PP_ENDMACRO && i != PP_ENDM &&
1812 (defining->name || (i != PP_ENDREP && i != PP_REP)))
H. Peter Anvineba20a72002-04-30 20:53:55 +00001813 {
Ed Beroset3ab3f412002-06-11 03:31:49 +00001814 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001815 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001816
H. Peter Anvin734b1882002-04-30 21:01:08 +00001817 if (j != -2)
1818 {
H. Peter Anvineba20a72002-04-30 20:53:55 +00001819 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
H. Peter Anvin734b1882002-04-30 21:01:08 +00001820 tline->text);
Ed Beroset3ab3f412002-06-11 03:31:49 +00001821 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001822 }
1823
H. Peter Anvin734b1882002-04-30 21:01:08 +00001824 switch (i)
1825 {
1826 case PP_STACKSIZE:
1827 /* Directive to tell NASM what the default stack size is. The
1828 * default is for a 16-bit stack, and this can be overriden with
1829 * %stacksize large.
1830 * the following form:
1831 *
1832 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001833 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001834 tline = tline->next;
1835 if (tline && tline->type == TOK_WHITESPACE)
1836 tline = tline->next;
1837 if (!tline || tline->type != TOK_ID)
1838 {
1839 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
1840 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00001841 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001842 }
1843 if (nasm_stricmp(tline->text, "flat") == 0)
1844 {
1845 /* All subsequent ARG directives are for a 32-bit stack */
1846 StackSize = 4;
1847 StackPointer = "ebp";
1848 ArgOffset = 8;
1849 LocalOffset = 4;
1850 }
1851 else if (nasm_stricmp(tline->text, "large") == 0)
1852 {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001853 /* All subsequent ARG directives are for a 16-bit stack,
H. Peter Anvin734b1882002-04-30 21:01:08 +00001854 * far function call.
1855 */
1856 StackSize = 2;
1857 StackPointer = "bp";
1858 ArgOffset = 4;
1859 LocalOffset = 2;
1860 }
1861 else if (nasm_stricmp(tline->text, "small") == 0)
1862 {
1863 /* All subsequent ARG directives are for a 16-bit stack,
1864 * far function call. We don't support near functions.
1865 */
1866 StackSize = 2;
1867 StackPointer = "bp";
1868 ArgOffset = 6;
1869 LocalOffset = 2;
1870 }
1871 else
1872 {
1873 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
1874 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00001875 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001876 }
1877 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00001878 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001879
H. Peter Anvin734b1882002-04-30 21:01:08 +00001880 case PP_ARG:
1881 /* TASM like ARG directive to define arguments to functions, in
1882 * the following form:
1883 *
1884 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1885 */
1886 offset = ArgOffset;
1887 do
1888 {
1889 char *arg, directive[256];
1890 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001891
H. Peter Anvin734b1882002-04-30 21:01:08 +00001892 /* Find the argument name */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001893 tline = tline->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001894 if (tline && tline->type == TOK_WHITESPACE)
1895 tline = tline->next;
1896 if (!tline || tline->type != TOK_ID)
1897 {
1898 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
1899 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00001900 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001901 }
1902 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001903
H. Peter Anvin734b1882002-04-30 21:01:08 +00001904 /* Find the argument size type */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001905 tline = tline->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001906 if (!tline || tline->type != TOK_OTHER
1907 || tline->text[0] != ':')
1908 {
1909 error(ERR_NONFATAL,
1910 "Syntax error processing `%%arg' directive");
1911 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00001912 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001913 }
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_ID)
1916 {
1917 error(ERR_NONFATAL,
1918 "`%%arg' missing size type parameter");
1919 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00001920 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001921 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001922
H. Peter Anvin734b1882002-04-30 21:01:08 +00001923 /* Allow macro expansion of type parameter */
1924 tt = tokenise(tline->text);
1925 tt = expand_smacro(tt);
1926 if (nasm_stricmp(tt->text, "byte") == 0)
1927 {
1928 size = MAX(StackSize, 1);
1929 }
1930 else if (nasm_stricmp(tt->text, "word") == 0)
1931 {
1932 size = MAX(StackSize, 2);
1933 }
1934 else if (nasm_stricmp(tt->text, "dword") == 0)
1935 {
1936 size = MAX(StackSize, 4);
1937 }
1938 else if (nasm_stricmp(tt->text, "qword") == 0)
1939 {
1940 size = MAX(StackSize, 8);
1941 }
1942 else if (nasm_stricmp(tt->text, "tword") == 0)
1943 {
1944 size = MAX(StackSize, 10);
1945 }
1946 else
1947 {
1948 error(ERR_NONFATAL,
1949 "Invalid size type for `%%arg' missing directive");
1950 free_tlist(tt);
1951 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00001952 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001953 }
1954 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001955
H. Peter Anvin734b1882002-04-30 21:01:08 +00001956 /* Now define the macro for the argument */
1957 sprintf(directive, "%%define %s (%s+%d)", arg, StackPointer,
1958 offset);
1959 do_directive(tokenise(directive));
1960 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001961
H. Peter Anvin734b1882002-04-30 21:01:08 +00001962 /* Move to the next argument in the list */
1963 tline = tline->next;
1964 if (tline && tline->type == TOK_WHITESPACE)
1965 tline = tline->next;
1966 }
1967 while (tline && tline->type == TOK_OTHER
1968 && tline->text[0] == ',');
1969 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00001970 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001971
1972 case PP_LOCAL:
1973 /* TASM like LOCAL directive to define local variables for a
1974 * function, in the following form:
1975 *
1976 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
1977 *
1978 * The '= LocalSize' at the end is ignored by NASM, but is
1979 * required by TASM to define the local parameter size (and used
1980 * by the TASM macro package).
1981 */
1982 offset = LocalOffset;
1983 do
1984 {
1985 char *local, directive[256];
1986 int size = StackSize;
1987
1988 /* Find the argument name */
1989 tline = tline->next;
1990 if (tline && tline->type == TOK_WHITESPACE)
1991 tline = tline->next;
1992 if (!tline || tline->type != TOK_ID)
1993 {
1994 error(ERR_NONFATAL,
1995 "`%%local' missing argument parameter");
1996 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00001997 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001998 }
1999 local = tline->text;
2000
2001 /* Find the argument size type */
2002 tline = tline->next;
2003 if (!tline || tline->type != TOK_OTHER
2004 || tline->text[0] != ':')
2005 {
2006 error(ERR_NONFATAL,
2007 "Syntax error processing `%%local' directive");
2008 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00002009 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002010 }
2011 tline = tline->next;
2012 if (!tline || tline->type != TOK_ID)
2013 {
2014 error(ERR_NONFATAL,
2015 "`%%local' missing size type parameter");
2016 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00002017 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002018 }
2019
2020 /* Allow macro expansion of type parameter */
2021 tt = tokenise(tline->text);
2022 tt = expand_smacro(tt);
2023 if (nasm_stricmp(tt->text, "byte") == 0)
2024 {
2025 size = MAX(StackSize, 1);
2026 }
2027 else if (nasm_stricmp(tt->text, "word") == 0)
2028 {
2029 size = MAX(StackSize, 2);
2030 }
2031 else if (nasm_stricmp(tt->text, "dword") == 0)
2032 {
2033 size = MAX(StackSize, 4);
2034 }
2035 else if (nasm_stricmp(tt->text, "qword") == 0)
2036 {
2037 size = MAX(StackSize, 8);
2038 }
2039 else if (nasm_stricmp(tt->text, "tword") == 0)
2040 {
2041 size = MAX(StackSize, 10);
2042 }
2043 else
2044 {
2045 error(ERR_NONFATAL,
2046 "Invalid size type for `%%local' missing directive");
2047 free_tlist(tt);
2048 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00002049 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002050 }
2051 free_tlist(tt);
2052
2053 /* Now define the macro for the argument */
2054 sprintf(directive, "%%define %s (%s-%d)", local, StackPointer,
2055 offset);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002056 do_directive(tokenise(directive));
2057 offset += size;
2058
2059 /* Now define the assign to setup the enter_c macro correctly */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002060 sprintf(directive, "%%assign %%$localsize %%$localsize+%d",
2061 size);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002062 do_directive(tokenise(directive));
2063
H. Peter Anvin734b1882002-04-30 21:01:08 +00002064 /* Move to the next argument in the list */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002065 tline = tline->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002066 if (tline && tline->type == TOK_WHITESPACE)
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002067 tline = tline->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002068 }
2069 while (tline && tline->type == TOK_OTHER
2070 && tline->text[0] == ',');
2071 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00002072 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002073
2074 case PP_CLEAR:
2075 if (tline->next)
2076 error(ERR_WARNING,
2077 "trailing garbage after `%%clear' ignored");
2078 for (j = 0; j < NHASH; j++)
2079 {
2080 while (mmacros[j])
2081 {
2082 MMacro *m = mmacros[j];
2083 mmacros[j] = m->next;
2084 free_mmacro(m);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002085 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002086 while (smacros[j])
2087 {
2088 SMacro *s = smacros[j];
2089 smacros[j] = smacros[j]->next;
2090 nasm_free(s->name);
2091 free_tlist(s->expansion);
2092 nasm_free(s);
2093 }
2094 }
2095 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00002096 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002097
2098 case PP_INCLUDE:
2099 tline = tline->next;
2100 skip_white_(tline);
2101 if (!tline || (tline->type != TOK_STRING &&
2102 tline->type != TOK_INTERNAL_STRING))
2103 {
2104 error(ERR_NONFATAL, "`%%include' expects a file name");
2105 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00002106 return DIRECTIVE_FOUND; /* but we did _something_ */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002107 }
2108 if (tline->next)
2109 error(ERR_WARNING,
2110 "trailing garbage after `%%include' ignored");
2111 if (tline->type != TOK_INTERNAL_STRING)
2112 {
2113 p = tline->text + 1; /* point past the quote to the name */
2114 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
2115 }
2116 else
2117 p = tline->text; /* internal_string is easier */
2118 expand_macros_in_string(&p);
2119 inc = nasm_malloc(sizeof(Include));
2120 inc->next = istk;
2121 inc->conds = NULL;
2122 inc->fp = inc_fopen(p);
2123 inc->fname = src_set_fname(p);
2124 inc->lineno = src_set_linnum(0);
2125 inc->lineinc = 1;
2126 inc->expansion = NULL;
2127 inc->mstk = NULL;
2128 istk = inc;
2129 list->uplevel(LIST_INCLUDE);
2130 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00002131 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002132
2133 case PP_PUSH:
2134 tline = tline->next;
2135 skip_white_(tline);
2136 tline = expand_id(tline);
2137 if (!tok_type_(tline, TOK_ID))
2138 {
2139 error(ERR_NONFATAL, "`%%push' expects a context identifier");
2140 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00002141 return DIRECTIVE_FOUND; /* but we did _something_ */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002142 }
2143 if (tline->next)
2144 error(ERR_WARNING, "trailing garbage after `%%push' ignored");
2145 ctx = nasm_malloc(sizeof(Context));
2146 ctx->next = cstk;
2147 ctx->localmac = NULL;
2148 ctx->name = nasm_strdup(tline->text);
2149 ctx->number = unique++;
2150 cstk = ctx;
2151 free_tlist(origline);
2152 break;
2153
2154 case PP_REPL:
2155 tline = tline->next;
2156 skip_white_(tline);
2157 tline = expand_id(tline);
2158 if (!tok_type_(tline, TOK_ID))
2159 {
2160 error(ERR_NONFATAL, "`%%repl' expects a context identifier");
2161 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00002162 return DIRECTIVE_FOUND; /* but we did _something_ */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002163 }
2164 if (tline->next)
2165 error(ERR_WARNING, "trailing garbage after `%%repl' ignored");
2166 if (!cstk)
2167 error(ERR_NONFATAL, "`%%repl': context stack is empty");
2168 else
2169 {
2170 nasm_free(cstk->name);
2171 cstk->name = nasm_strdup(tline->text);
2172 }
2173 free_tlist(origline);
2174 break;
2175
2176 case PP_POP:
2177 if (tline->next)
2178 error(ERR_WARNING, "trailing garbage after `%%pop' ignored");
2179 if (!cstk)
2180 error(ERR_NONFATAL,
2181 "`%%pop': context stack is already empty");
2182 else
2183 ctx_pop();
2184 free_tlist(origline);
2185 break;
2186
2187 case PP_ERROR:
2188 tline->next = expand_smacro(tline->next);
2189 tline = tline->next;
2190 skip_white_(tline);
2191 if (tok_type_(tline, TOK_STRING))
2192 {
2193 p = tline->text + 1; /* point past the quote to the name */
2194 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
2195 expand_macros_in_string(&p);
2196 error(ERR_NONFATAL, "%s", p);
2197 nasm_free(p);
2198 }
2199 else
2200 {
2201 p = detoken(tline, FALSE);
2202 error(ERR_WARNING, "%s", p);
2203 nasm_free(p);
2204 }
2205 free_tlist(origline);
2206 break;
2207
2208 case PP_IF:
2209 case PP_IFCTX:
2210 case PP_IFDEF:
2211 case PP_IFID:
2212 case PP_IFIDN:
2213 case PP_IFIDNI:
H. Peter Anvin65747262002-05-07 00:10:05 +00002214 case PP_IFMACRO:
H. Peter Anvin734b1882002-04-30 21:01:08 +00002215 case PP_IFNCTX:
2216 case PP_IFNDEF:
2217 case PP_IFNID:
2218 case PP_IFNIDN:
2219 case PP_IFNIDNI:
H. Peter Anvin65747262002-05-07 00:10:05 +00002220 case PP_IFNMACRO:
H. Peter Anvin734b1882002-04-30 21:01:08 +00002221 case PP_IFNNUM:
2222 case PP_IFNSTR:
2223 case PP_IFNUM:
2224 case PP_IFSTR:
2225 if (istk->conds && !emitting(istk->conds->state))
2226 j = COND_NEVER;
2227 else
2228 {
2229 j = if_condition(tline->next, i);
2230 tline->next = NULL; /* it got freed */
2231 free_tlist(origline);
2232 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2233 }
2234 cond = nasm_malloc(sizeof(Cond));
2235 cond->next = istk->conds;
2236 cond->state = j;
2237 istk->conds = cond;
Ed Beroset3ab3f412002-06-11 03:31:49 +00002238 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002239
2240 case PP_ELIF:
2241 case PP_ELIFCTX:
2242 case PP_ELIFDEF:
2243 case PP_ELIFID:
2244 case PP_ELIFIDN:
2245 case PP_ELIFIDNI:
H. Peter Anvin65747262002-05-07 00:10:05 +00002246 case PP_ELIFMACRO:
H. Peter Anvin734b1882002-04-30 21:01:08 +00002247 case PP_ELIFNCTX:
2248 case PP_ELIFNDEF:
2249 case PP_ELIFNID:
2250 case PP_ELIFNIDN:
2251 case PP_ELIFNIDNI:
H. Peter Anvin65747262002-05-07 00:10:05 +00002252 case PP_ELIFNMACRO:
H. Peter Anvin734b1882002-04-30 21:01:08 +00002253 case PP_ELIFNNUM:
2254 case PP_ELIFNSTR:
2255 case PP_ELIFNUM:
2256 case PP_ELIFSTR:
2257 if (!istk->conds)
2258 error(ERR_FATAL, "`%s': no matching `%%if'", directives[i]);
2259 if (emitting(istk->conds->state)
2260 || istk->conds->state == COND_NEVER)
2261 istk->conds->state = COND_NEVER;
2262 else
2263 {
H. Peter Anvin0c608152002-05-22 22:59:40 +00002264 /*
2265 * IMPORTANT: In the case of %if, we will already have
2266 * called expand_mmac_params(); however, if we're
2267 * processing an %elif we must have been in a
2268 * non-emitting mode, which would have inhibited
2269 * the normal invocation of expand_mmac_params(). Therefore,
2270 * we have to do it explicitly here.
2271 */
2272 j = if_condition(expand_mmac_params(tline->next), i);
2273 tline->next = NULL; /* it got freed */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002274 free_tlist(origline);
2275 istk->conds->state =
2276 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2277 }
Ed Beroset3ab3f412002-06-11 03:31:49 +00002278 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002279
2280 case PP_ELSE:
2281 if (tline->next)
2282 error(ERR_WARNING, "trailing garbage after `%%else' ignored");
2283 if (!istk->conds)
2284 error(ERR_FATAL, "`%%else': no matching `%%if'");
2285 if (emitting(istk->conds->state)
2286 || istk->conds->state == COND_NEVER)
2287 istk->conds->state = COND_ELSE_FALSE;
2288 else
2289 istk->conds->state = COND_ELSE_TRUE;
2290 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00002291 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002292
2293 case PP_ENDIF:
2294 if (tline->next)
2295 error(ERR_WARNING,
2296 "trailing garbage after `%%endif' ignored");
2297 if (!istk->conds)
2298 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2299 cond = istk->conds;
2300 istk->conds = cond->next;
2301 nasm_free(cond);
2302 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00002303 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002304
2305 case PP_MACRO:
2306 case PP_IMACRO:
2307 if (defining)
2308 error(ERR_FATAL,
2309 "`%%%smacro': already defining a macro",
2310 (i == PP_IMACRO ? "i" : ""));
2311 tline = tline->next;
2312 skip_white_(tline);
2313 tline = expand_id(tline);
2314 if (!tok_type_(tline, TOK_ID))
2315 {
2316 error(ERR_NONFATAL,
2317 "`%%%smacro' expects a macro name",
2318 (i == PP_IMACRO ? "i" : ""));
Ed Beroset3ab3f412002-06-11 03:31:49 +00002319 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002320 }
2321 defining = nasm_malloc(sizeof(MMacro));
2322 defining->name = nasm_strdup(tline->text);
2323 defining->casesense = (i == PP_MACRO);
2324 defining->plus = FALSE;
2325 defining->nolist = FALSE;
2326 defining->in_progress = FALSE;
2327 defining->rep_nest = NULL;
2328 tline = expand_smacro(tline->next);
2329 skip_white_(tline);
2330 if (!tok_type_(tline, TOK_NUMBER))
2331 {
2332 error(ERR_NONFATAL,
2333 "`%%%smacro' expects a parameter count",
2334 (i == PP_IMACRO ? "i" : ""));
2335 defining->nparam_min = defining->nparam_max = 0;
2336 }
2337 else
2338 {
2339 defining->nparam_min = defining->nparam_max =
2340 readnum(tline->text, &j);
2341 if (j)
2342 error(ERR_NONFATAL,
2343 "unable to parse parameter count `%s'",
2344 tline->text);
2345 }
2346 if (tline && tok_is_(tline->next, "-"))
2347 {
2348 tline = tline->next->next;
2349 if (tok_is_(tline, "*"))
2350 defining->nparam_max = INT_MAX;
2351 else if (!tok_type_(tline, TOK_NUMBER))
2352 error(ERR_NONFATAL,
2353 "`%%%smacro' expects a parameter count after `-'",
2354 (i == PP_IMACRO ? "i" : ""));
2355 else
2356 {
2357 defining->nparam_max = readnum(tline->text, &j);
2358 if (j)
2359 error(ERR_NONFATAL,
2360 "unable to parse parameter count `%s'",
2361 tline->text);
2362 if (defining->nparam_min > defining->nparam_max)
2363 error(ERR_NONFATAL,
2364 "minimum parameter count exceeds maximum");
2365 }
2366 }
2367 if (tline && tok_is_(tline->next, "+"))
2368 {
2369 tline = tline->next;
2370 defining->plus = TRUE;
2371 }
2372 if (tline && tok_type_(tline->next, TOK_ID) &&
2373 !nasm_stricmp(tline->next->text, ".nolist"))
2374 {
2375 tline = tline->next;
2376 defining->nolist = TRUE;
2377 }
2378 mmac = mmacros[hash(defining->name)];
2379 while (mmac)
2380 {
2381 if (!strcmp(mmac->name, defining->name) &&
2382 (mmac->nparam_min <= defining->nparam_max
2383 || defining->plus)
2384 && (defining->nparam_min <= mmac->nparam_max
2385 || mmac->plus))
2386 {
2387 error(ERR_WARNING,
2388 "redefining multi-line macro `%s'",
2389 defining->name);
2390 break;
2391 }
2392 mmac = mmac->next;
2393 }
2394 /*
2395 * Handle default parameters.
2396 */
2397 if (tline && tline->next)
2398 {
2399 defining->dlist = tline->next;
2400 tline->next = NULL;
2401 count_mmac_params(defining->dlist, &defining->ndefs,
2402 &defining->defaults);
2403 }
2404 else
2405 {
2406 defining->dlist = NULL;
2407 defining->defaults = NULL;
2408 }
2409 defining->expansion = NULL;
2410 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00002411 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002412
2413 case PP_ENDM:
2414 case PP_ENDMACRO:
2415 if (!defining)
2416 {
2417 error(ERR_NONFATAL, "`%s': not defining a macro",
2418 tline->text);
Ed Beroset3ab3f412002-06-11 03:31:49 +00002419 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002420 }
2421 k = hash(defining->name);
2422 defining->next = mmacros[k];
2423 mmacros[k] = defining;
2424 defining = NULL;
2425 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00002426 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002427
2428 case PP_ROTATE:
2429 if (tline->next && tline->next->type == TOK_WHITESPACE)
2430 tline = tline->next;
Ed Beroset3ab3f412002-06-11 03:31:49 +00002431 if (tline->next == NULL)
2432 {
2433 free_tlist(origline);
2434 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2435 return DIRECTIVE_FOUND;
2436 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002437 t = expand_smacro(tline->next);
2438 tline->next = NULL;
2439 free_tlist(origline);
2440 tline = t;
2441 tptr = &t;
2442 tokval.t_type = TOKEN_INVALID;
2443 evalresult =
2444 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2445 free_tlist(tline);
2446 if (!evalresult)
Ed Beroset3ab3f412002-06-11 03:31:49 +00002447 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002448 if (tokval.t_type)
2449 error(ERR_WARNING,
2450 "trailing garbage after expression ignored");
2451 if (!is_simple(evalresult))
2452 {
2453 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
Ed Beroset3ab3f412002-06-11 03:31:49 +00002454 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002455 }
2456 mmac = istk->mstk;
2457 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2458 mmac = mmac->next_active;
2459 if (!mmac)
Ed Beroset3ab3f412002-06-11 03:31:49 +00002460 {
H. Peter Anvin734b1882002-04-30 21:01:08 +00002461 error(ERR_NONFATAL,
2462 "`%%rotate' invoked outside a macro call");
Ed Beroset3ab3f412002-06-11 03:31:49 +00002463 }
2464 else if (mmac->nparam == 0)
2465 {
2466 error(ERR_NONFATAL,
2467 "`%%rotate' invoked within macro without parameters");
2468 }
2469 else
2470 {
2471 mmac->rotate = mmac->rotate + reloc_value(evalresult);
2472
2473 if (mmac->rotate < 0)
2474 mmac->rotate =
2475 mmac->nparam - (-mmac->rotate) % mmac->nparam;
2476 mmac->rotate %= mmac->nparam;
2477 }
2478 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002479
2480 case PP_REP:
2481 nolist = FALSE;
2482 tline = tline->next;
2483 if (tline->next && tline->next->type == TOK_WHITESPACE)
2484 tline = tline->next;
2485 if (tline->next && tline->next->type == TOK_ID &&
2486 !nasm_stricmp(tline->next->text, ".nolist"))
2487 {
2488 tline = tline->next;
2489 nolist = TRUE;
2490 }
2491 t = expand_smacro(tline->next);
2492 tline->next = NULL;
2493 free_tlist(origline);
2494 tline = t;
2495 tptr = &t;
2496 tokval.t_type = TOKEN_INVALID;
2497 evalresult =
2498 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2499 free_tlist(tline);
2500 if (!evalresult)
Ed Beroset3ab3f412002-06-11 03:31:49 +00002501 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002502 if (tokval.t_type)
2503 error(ERR_WARNING,
2504 "trailing garbage after expression ignored");
2505 if (!is_simple(evalresult))
2506 {
2507 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
Ed Beroset3ab3f412002-06-11 03:31:49 +00002508 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002509 }
2510 tmp_defining = defining;
2511 defining = nasm_malloc(sizeof(MMacro));
2512 defining->name = NULL; /* flags this macro as a %rep block */
2513 defining->casesense = 0;
2514 defining->plus = FALSE;
2515 defining->nolist = nolist;
2516 defining->in_progress = reloc_value(evalresult) + 1;
2517 defining->nparam_min = defining->nparam_max = 0;
2518 defining->defaults = NULL;
2519 defining->dlist = NULL;
2520 defining->expansion = NULL;
2521 defining->next_active = istk->mstk;
2522 defining->rep_nest = tmp_defining;
Ed Beroset3ab3f412002-06-11 03:31:49 +00002523 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002524
2525 case PP_ENDREP:
2526 if (!defining || defining->name)
2527 {
2528 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
Ed Beroset3ab3f412002-06-11 03:31:49 +00002529 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002530 }
2531
2532 /*
2533 * Now we have a "macro" defined - although it has no name
2534 * and we won't be entering it in the hash tables - we must
2535 * push a macro-end marker for it on to istk->expansion.
2536 * After that, it will take care of propagating itself (a
2537 * macro-end marker line for a macro which is really a %rep
2538 * block will cause the macro to be re-expanded, complete
2539 * with another macro-end marker to ensure the process
2540 * continues) until the whole expansion is forcibly removed
2541 * from istk->expansion by a %exitrep.
2542 */
2543 l = nasm_malloc(sizeof(Line));
2544 l->next = istk->expansion;
2545 l->finishes = defining;
2546 l->first = NULL;
2547 istk->expansion = l;
2548
2549 istk->mstk = defining;
2550
2551 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2552 tmp_defining = defining;
2553 defining = defining->rep_nest;
2554 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00002555 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002556
2557 case PP_EXITREP:
2558 /*
2559 * We must search along istk->expansion until we hit a
2560 * macro-end marker for a macro with no name. Then we set
2561 * its `in_progress' flag to 0.
2562 */
2563 for (l = istk->expansion; l; l = l->next)
2564 if (l->finishes && !l->finishes->name)
2565 break;
2566
2567 if (l)
2568 l->finishes->in_progress = 0;
2569 else
2570 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2571 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00002572 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002573
2574 case PP_XDEFINE:
2575 case PP_IXDEFINE:
2576 case PP_DEFINE:
2577 case PP_IDEFINE:
2578 tline = tline->next;
2579 skip_white_(tline);
2580 tline = expand_id(tline);
2581 if (!tline || (tline->type != TOK_ID &&
2582 (tline->type != TOK_PREPROC_ID ||
2583 tline->text[1] != '$')))
2584 {
2585 error(ERR_NONFATAL,
2586 "`%%%s%sdefine' expects a macro identifier",
2587 ((i == PP_IDEFINE || i == PP_IXDEFINE) ? "i" : ""),
2588 ((i == PP_XDEFINE || i == PP_IXDEFINE) ? "x" : ""));
2589 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00002590 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002591 }
2592
2593 ctx = get_ctx(tline->text, FALSE);
2594 if (!ctx)
2595 smhead = &smacros[hash(tline->text)];
2596 else
2597 smhead = &ctx->localmac;
2598 mname = tline->text;
2599 last = tline;
2600 param_start = tline = tline->next;
2601 nparam = 0;
2602
2603 /* Expand the macro definition now for %xdefine and %ixdefine */
2604 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2605 tline = expand_smacro(tline);
2606
2607 if (tok_is_(tline, "("))
2608 {
2609 /*
2610 * This macro has parameters.
2611 */
2612
2613 tline = tline->next;
2614 while (1)
2615 {
2616 skip_white_(tline);
2617 if (!tline)
2618 {
2619 error(ERR_NONFATAL, "parameter identifier expected");
2620 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00002621 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002622 }
2623 if (tline->type != TOK_ID)
2624 {
2625 error(ERR_NONFATAL,
2626 "`%s': parameter identifier expected",
2627 tline->text);
2628 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00002629 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002630 }
2631 tline->type = TOK_SMAC_PARAM + nparam++;
2632 tline = tline->next;
2633 skip_white_(tline);
2634 if (tok_is_(tline, ","))
2635 {
2636 tline = tline->next;
2637 continue;
2638 }
2639 if (!tok_is_(tline, ")"))
2640 {
2641 error(ERR_NONFATAL,
2642 "`)' expected to terminate macro template");
2643 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00002644 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002645 }
2646 break;
2647 }
2648 last = tline;
2649 tline = tline->next;
2650 }
2651 if (tok_type_(tline, TOK_WHITESPACE))
2652 last = tline, tline = tline->next;
2653 macro_start = NULL;
2654 last->next = NULL;
2655 t = tline;
2656 while (t)
2657 {
2658 if (t->type == TOK_ID)
2659 {
2660 for (tt = param_start; tt; tt = tt->next)
2661 if (tt->type >= TOK_SMAC_PARAM &&
2662 !strcmp(tt->text, t->text))
2663 t->type = tt->type;
2664 }
2665 tt = t->next;
2666 t->next = macro_start;
2667 macro_start = t;
2668 t = tt;
2669 }
2670 /*
2671 * Good. We now have a macro name, a parameter count, and a
2672 * token list (in reverse order) for an expansion. We ought
2673 * to be OK just to create an SMacro, store it, and let
2674 * free_tlist have the rest of the line (which we have
2675 * carefully re-terminated after chopping off the expansion
2676 * from the end).
2677 */
2678 if (smacro_defined(ctx, mname, nparam, &smac, i == PP_DEFINE))
2679 {
2680 if (!smac)
2681 {
2682 error(ERR_WARNING,
2683 "single-line macro `%s' defined both with and"
2684 " without parameters", mname);
2685 free_tlist(origline);
2686 free_tlist(macro_start);
Ed Beroset3ab3f412002-06-11 03:31:49 +00002687 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002688 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002689 else
2690 {
2691 /*
2692 * We're redefining, so we have to take over an
2693 * existing SMacro structure. This means freeing
2694 * what was already in it.
2695 */
2696 nasm_free(smac->name);
2697 free_tlist(smac->expansion);
2698 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002699 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002700 else
2701 {
2702 smac = nasm_malloc(sizeof(SMacro));
2703 smac->next = *smhead;
2704 *smhead = smac;
2705 }
2706 smac->name = nasm_strdup(mname);
2707 smac->casesense = ((i == PP_DEFINE) || (i == PP_XDEFINE));
2708 smac->nparam = nparam;
2709 smac->expansion = macro_start;
2710 smac->in_progress = FALSE;
2711 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00002712 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002713
2714 case PP_UNDEF:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002715 tline = tline->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002716 skip_white_(tline);
2717 tline = expand_id(tline);
2718 if (!tline || (tline->type != TOK_ID &&
2719 (tline->type != TOK_PREPROC_ID ||
2720 tline->text[1] != '$')))
2721 {
2722 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2723 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00002724 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002725 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002726 if (tline->next)
2727 {
2728 error(ERR_WARNING,
2729 "trailing garbage after macro name ignored");
2730 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002731
H. Peter Anvin734b1882002-04-30 21:01:08 +00002732 /* Find the context that symbol belongs to */
2733 ctx = get_ctx(tline->text, FALSE);
2734 if (!ctx)
2735 smhead = &smacros[hash(tline->text)];
2736 else
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002737 smhead = &ctx->localmac;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002738
H. Peter Anvin734b1882002-04-30 21:01:08 +00002739 mname = tline->text;
2740 last = tline;
2741 last->next = NULL;
2742
2743 /*
2744 * We now have a macro name... go hunt for it.
2745 */
2746 while (smacro_defined(ctx, mname, -1, &smac, 1))
2747 {
2748 /* Defined, so we need to find its predecessor and nuke it */
2749 SMacro **s;
2750 for (s = smhead; *s && *s != smac; s = &(*s)->next);
2751 if (*s)
2752 {
2753 *s = smac->next;
2754 nasm_free(smac->name);
2755 free_tlist(smac->expansion);
2756 nasm_free(smac);
2757 }
2758 }
2759 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00002760 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002761
2762 case PP_STRLEN:
2763 tline = tline->next;
2764 skip_white_(tline);
2765 tline = expand_id(tline);
2766 if (!tline || (tline->type != TOK_ID &&
2767 (tline->type != TOK_PREPROC_ID ||
2768 tline->text[1] != '$')))
2769 {
2770 error(ERR_NONFATAL,
2771 "`%%strlen' expects a macro identifier as first parameter");
2772 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00002773 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002774 }
2775 ctx = get_ctx(tline->text, FALSE);
2776 if (!ctx)
2777 smhead = &smacros[hash(tline->text)];
2778 else
2779 smhead = &ctx->localmac;
2780 mname = tline->text;
2781 last = tline;
2782 tline = expand_smacro(tline->next);
2783 last->next = NULL;
2784
2785 t = tline;
2786 while (tok_type_(t, TOK_WHITESPACE))
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002787 t = t->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002788 /* t should now point to the string */
2789 if (t->type != TOK_STRING)
2790 {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002791 error(ERR_NONFATAL,
2792 "`%%strlen` requires string as second parameter");
2793 free_tlist(tline);
2794 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00002795 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002796 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002797
H. Peter Anvin734b1882002-04-30 21:01:08 +00002798 macro_start = nasm_malloc(sizeof(*macro_start));
2799 macro_start->next = NULL;
2800 make_tok_num(macro_start, strlen(t->text) - 2);
2801 macro_start->mac = NULL;
2802
2803 /*
2804 * We now have a macro name, an implicit parameter count of
2805 * zero, and a numeric token to use as an expansion. Create
2806 * and store an SMacro.
2807 */
2808 if (smacro_defined(ctx, mname, 0, &smac, i == PP_STRLEN))
2809 {
2810 if (!smac)
2811 error(ERR_WARNING,
2812 "single-line macro `%s' defined both with and"
2813 " without parameters", mname);
2814 else
2815 {
2816 /*
2817 * We're redefining, so we have to take over an
2818 * existing SMacro structure. This means freeing
2819 * what was already in it.
2820 */
2821 nasm_free(smac->name);
2822 free_tlist(smac->expansion);
2823 }
2824 }
2825 else
2826 {
2827 smac = nasm_malloc(sizeof(SMacro));
2828 smac->next = *smhead;
2829 *smhead = smac;
2830 }
2831 smac->name = nasm_strdup(mname);
2832 smac->casesense = (i == PP_STRLEN);
2833 smac->nparam = 0;
2834 smac->expansion = macro_start;
2835 smac->in_progress = FALSE;
2836 free_tlist(tline);
2837 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00002838 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002839
H. Peter Anvin734b1882002-04-30 21:01:08 +00002840 case PP_SUBSTR:
2841 tline = tline->next;
2842 skip_white_(tline);
2843 tline = expand_id(tline);
2844 if (!tline || (tline->type != TOK_ID &&
2845 (tline->type != TOK_PREPROC_ID ||
2846 tline->text[1] != '$')))
2847 {
2848 error(ERR_NONFATAL,
2849 "`%%substr' expects a macro identifier as first parameter");
2850 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00002851 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002852 }
2853 ctx = get_ctx(tline->text, FALSE);
2854 if (!ctx)
2855 smhead = &smacros[hash(tline->text)];
2856 else
2857 smhead = &ctx->localmac;
2858 mname = tline->text;
2859 last = tline;
2860 tline = expand_smacro(tline->next);
2861 last->next = NULL;
2862
2863 t = tline->next;
2864 while (tok_type_(t, TOK_WHITESPACE))
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002865 t = t->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002866
2867 /* t should now point to the string */
2868 if (t->type != TOK_STRING)
2869 {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002870 error(ERR_NONFATAL,
2871 "`%%substr` requires string as second parameter");
2872 free_tlist(tline);
2873 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00002874 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002875 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002876
H. Peter Anvin734b1882002-04-30 21:01:08 +00002877 tt = t->next;
2878 tptr = &tt;
2879 tokval.t_type = TOKEN_INVALID;
2880 evalresult =
2881 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2882 if (!evalresult)
2883 {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002884 free_tlist(tline);
2885 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00002886 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002887 }
2888 if (!is_simple(evalresult))
2889 {
2890 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002891 free_tlist(tline);
2892 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00002893 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002894 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002895
H. Peter Anvin734b1882002-04-30 21:01:08 +00002896 macro_start = nasm_malloc(sizeof(*macro_start));
2897 macro_start->next = NULL;
2898 macro_start->text = nasm_strdup("'''");
2899 if (evalresult->value > 0
2900 && evalresult->value < strlen(t->text) - 1)
2901 {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002902 macro_start->text[1] = t->text[evalresult->value];
H. Peter Anvin734b1882002-04-30 21:01:08 +00002903 }
2904 else
2905 {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002906 macro_start->text[2] = '\0';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002907 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002908 macro_start->type = TOK_STRING;
2909 macro_start->mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002910
H. Peter Anvin734b1882002-04-30 21:01:08 +00002911 /*
2912 * We now have a macro name, an implicit parameter count of
2913 * zero, and a numeric token to use as an expansion. Create
2914 * and store an SMacro.
2915 */
2916 if (smacro_defined(ctx, mname, 0, &smac, i == PP_SUBSTR))
2917 {
2918 if (!smac)
2919 error(ERR_WARNING,
2920 "single-line macro `%s' defined both with and"
2921 " without parameters", mname);
2922 else
2923 {
2924 /*
2925 * We're redefining, so we have to take over an
2926 * existing SMacro structure. This means freeing
2927 * what was already in it.
2928 */
2929 nasm_free(smac->name);
2930 free_tlist(smac->expansion);
2931 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002932 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002933 else
2934 {
2935 smac = nasm_malloc(sizeof(SMacro));
2936 smac->next = *smhead;
2937 *smhead = smac;
2938 }
2939 smac->name = nasm_strdup(mname);
2940 smac->casesense = (i == PP_SUBSTR);
2941 smac->nparam = 0;
2942 smac->expansion = macro_start;
2943 smac->in_progress = FALSE;
2944 free_tlist(tline);
2945 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00002946 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002947
2948
2949 case PP_ASSIGN:
2950 case PP_IASSIGN:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002951 tline = tline->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002952 skip_white_(tline);
2953 tline = expand_id(tline);
2954 if (!tline || (tline->type != TOK_ID &&
2955 (tline->type != TOK_PREPROC_ID ||
2956 tline->text[1] != '$')))
2957 {
2958 error(ERR_NONFATAL,
2959 "`%%%sassign' expects a macro identifier",
2960 (i == PP_IASSIGN ? "i" : ""));
2961 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00002962 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002963 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002964 ctx = get_ctx(tline->text, FALSE);
2965 if (!ctx)
2966 smhead = &smacros[hash(tline->text)];
2967 else
2968 smhead = &ctx->localmac;
2969 mname = tline->text;
2970 last = tline;
2971 tline = expand_smacro(tline->next);
2972 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002973
H. Peter Anvin734b1882002-04-30 21:01:08 +00002974 t = tline;
2975 tptr = &t;
2976 tokval.t_type = TOKEN_INVALID;
2977 evalresult =
2978 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2979 free_tlist(tline);
2980 if (!evalresult)
2981 {
2982 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00002983 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002984 }
2985
2986 if (tokval.t_type)
2987 error(ERR_WARNING,
2988 "trailing garbage after expression ignored");
2989
2990 if (!is_simple(evalresult))
2991 {
2992 error(ERR_NONFATAL,
2993 "non-constant value given to `%%%sassign'",
2994 (i == PP_IASSIGN ? "i" : ""));
2995 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00002996 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002997 }
2998
2999 macro_start = nasm_malloc(sizeof(*macro_start));
3000 macro_start->next = NULL;
3001 make_tok_num(macro_start, reloc_value(evalresult));
3002 macro_start->mac = NULL;
3003
3004 /*
3005 * We now have a macro name, an implicit parameter count of
3006 * zero, and a numeric token to use as an expansion. Create
3007 * and store an SMacro.
3008 */
3009 if (smacro_defined(ctx, mname, 0, &smac, i == PP_ASSIGN))
3010 {
3011 if (!smac)
3012 error(ERR_WARNING,
3013 "single-line macro `%s' defined both with and"
3014 " without parameters", mname);
3015 else
3016 {
3017 /*
3018 * We're redefining, so we have to take over an
3019 * existing SMacro structure. This means freeing
3020 * what was already in it.
3021 */
3022 nasm_free(smac->name);
3023 free_tlist(smac->expansion);
3024 }
3025 }
3026 else
3027 {
3028 smac = nasm_malloc(sizeof(SMacro));
3029 smac->next = *smhead;
3030 *smhead = smac;
3031 }
3032 smac->name = nasm_strdup(mname);
3033 smac->casesense = (i == PP_ASSIGN);
3034 smac->nparam = 0;
3035 smac->expansion = macro_start;
3036 smac->in_progress = FALSE;
3037 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00003038 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003039
3040 case PP_LINE:
3041 /*
3042 * Syntax is `%line nnn[+mmm] [filename]'
3043 */
3044 tline = tline->next;
3045 skip_white_(tline);
3046 if (!tok_type_(tline, TOK_NUMBER))
3047 {
3048 error(ERR_NONFATAL, "`%%line' expects line number");
3049 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00003050 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003051 }
3052 k = readnum(tline->text, &j);
3053 m = 1;
3054 tline = tline->next;
3055 if (tok_is_(tline, "+"))
3056 {
3057 tline = tline->next;
3058 if (!tok_type_(tline, TOK_NUMBER))
3059 {
3060 error(ERR_NONFATAL, "`%%line' expects line increment");
3061 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00003062 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003063 }
3064 m = readnum(tline->text, &j);
3065 tline = tline->next;
3066 }
3067 skip_white_(tline);
3068 src_set_linnum(k);
3069 istk->lineinc = m;
3070 if (tline)
3071 {
3072 nasm_free(src_set_fname(detoken(tline, FALSE)));
3073 }
3074 free_tlist(origline);
Ed Beroset3ab3f412002-06-11 03:31:49 +00003075 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003076
3077 default:
3078 error(ERR_FATAL,
3079 "preprocessor directive `%s' not yet implemented",
3080 directives[i]);
3081 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003082 }
Ed Beroset3ab3f412002-06-11 03:31:49 +00003083 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003084}
3085
3086/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003087 * Ensure that a macro parameter contains a condition code and
3088 * nothing else. Return the condition code index if so, or -1
3089 * otherwise.
3090 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003091static int
3092find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003093{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003094 Token *tt;
3095 int i, j, k, m;
3096
H. Peter Anvineba20a72002-04-30 20:53:55 +00003097 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003098 if (t->type != TOK_ID)
3099 return -1;
3100 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003101 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003102 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
3103 return -1;
3104
3105 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +00003106 j = elements(conditions);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003107 while (j - i > 1)
3108 {
3109 k = (j + i) / 2;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003110 m = nasm_stricmp(t->text, conditions[k]);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003111 if (m == 0)
3112 {
H. Peter Anvin76690a12002-04-30 20:52:49 +00003113 i = k;
3114 j = -2;
3115 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003116 }
3117 else if (m < 0)
3118 {
H. Peter Anvin76690a12002-04-30 20:52:49 +00003119 j = k;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003120 }
3121 else
H. Peter Anvin76690a12002-04-30 20:52:49 +00003122 i = k;
3123 }
3124 if (j != -2)
3125 return -1;
3126 return i;
3127}
3128
3129/*
3130 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3131 * %-n) and MMacro-local identifiers (%%foo).
3132 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003133static Token *
3134expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003135{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003136 Token *t, *tt, **tail, *thead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003137
3138 tail = &thead;
3139 thead = NULL;
3140
H. Peter Anvin734b1882002-04-30 21:01:08 +00003141 while (tline)
3142 {
H. Peter Anvin76690a12002-04-30 20:52:49 +00003143 if (tline->type == TOK_PREPROC_ID &&
H. Peter Anvin734b1882002-04-30 21:01:08 +00003144 (((tline->text[1] == '+' || tline->text[1] == '-')
3145 && tline->text[2]) || tline->text[1] == '%'
3146 || (tline->text[1] >= '0' && tline->text[1] <= '9')))
3147 {
H. Peter Anvin76690a12002-04-30 20:52:49 +00003148 char *text = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003149 int type = 0, cc; /* type = 0 to placate optimisers */
H. Peter Anvin76690a12002-04-30 20:52:49 +00003150 char tmpbuf[30];
3151 int n, i;
3152 MMacro *mac;
3153
3154 t = tline;
3155 tline = tline->next;
3156
3157 mac = istk->mstk;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003158 while (mac && !mac->name) /* avoid mistaking %reps for macros */
H. Peter Anvin76690a12002-04-30 20:52:49 +00003159 mac = mac->next_active;
3160 if (!mac)
3161 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003162 else
3163 switch (t->text[1])
3164 {
3165 /*
3166 * We have to make a substitution of one of the
3167 * forms %1, %-1, %+1, %%foo, %0.
3168 */
3169 case '0':
3170 type = TOK_NUMBER;
3171 sprintf(tmpbuf, "%d", mac->nparam);
3172 text = nasm_strdup(tmpbuf);
3173 break;
3174 case '%':
3175 type = TOK_ID;
3176 sprintf(tmpbuf, "..@%lu.", mac->unique);
3177 text = nasm_strcat(tmpbuf, t->text + 2);
3178 break;
3179 case '-':
3180 n = atoi(t->text + 2) - 1;
3181 if (n >= mac->nparam)
3182 tt = NULL;
3183 else
3184 {
3185 if (mac->nparam > 1)
3186 n = (n + mac->rotate) % mac->nparam;
3187 tt = mac->params[n];
3188 }
3189 cc = find_cc(tt);
3190 if (cc == -1)
3191 {
3192 error(ERR_NONFATAL,
3193 "macro parameter %d is not a condition code",
3194 n + 1);
3195 text = NULL;
3196 }
3197 else
3198 {
3199 type = TOK_ID;
3200 if (inverse_ccs[cc] == -1)
3201 {
3202 error(ERR_NONFATAL,
3203 "condition code `%s' is not invertible",
3204 conditions[cc]);
3205 text = NULL;
3206 }
3207 else
3208 text =
3209 nasm_strdup(conditions[inverse_ccs
3210 [cc]]);
3211 }
3212 break;
3213 case '+':
3214 n = atoi(t->text + 2) - 1;
3215 if (n >= mac->nparam)
3216 tt = NULL;
3217 else
3218 {
3219 if (mac->nparam > 1)
3220 n = (n + mac->rotate) % mac->nparam;
3221 tt = mac->params[n];
3222 }
3223 cc = find_cc(tt);
3224 if (cc == -1)
3225 {
3226 error(ERR_NONFATAL,
3227 "macro parameter %d is not a condition code",
3228 n + 1);
3229 text = NULL;
3230 }
3231 else
3232 {
3233 type = TOK_ID;
3234 text = nasm_strdup(conditions[cc]);
3235 }
3236 break;
3237 default:
3238 n = atoi(t->text + 1) - 1;
3239 if (n >= mac->nparam)
3240 tt = NULL;
3241 else
3242 {
3243 if (mac->nparam > 1)
3244 n = (n + mac->rotate) % mac->nparam;
3245 tt = mac->params[n];
3246 }
3247 if (tt)
3248 {
3249 for (i = 0; i < mac->paramlen[n]; i++)
3250 {
3251 *tail =
3252 new_Token(NULL, tt->type, tt->text,
3253 0);
3254 tail = &(*tail)->next;
3255 tt = tt->next;
3256 }
3257 }
3258 text = NULL; /* we've done it here */
3259 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003260 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003261 if (!text)
3262 {
3263 delete_Token(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003264 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003265 else
3266 {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003267 *tail = t;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003268 tail = &t->next;
3269 t->type = type;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003270 nasm_free(t->text);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003271 t->text = text;
3272 t->mac = NULL;
3273 }
3274 continue;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003275 }
3276 else
3277 {
H. Peter Anvin76690a12002-04-30 20:52:49 +00003278 t = *tail = tline;
3279 tline = tline->next;
3280 t->mac = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003281 tail = &t->next;
3282 }
3283 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00003284 *tail = NULL;
3285 t = thead;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003286 for (; t && (tt = t->next) != NULL; t = t->next)
3287 switch (t->type)
3288 {
3289 case TOK_WHITESPACE:
3290 if (tt->type == TOK_WHITESPACE)
3291 {
3292 t->next = delete_Token(tt);
3293 }
3294 break;
3295 case TOK_ID:
3296 if (tt->type == TOK_ID || 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;
3304 case TOK_NUMBER:
3305 if (tt->type == TOK_NUMBER)
3306 {
3307 char *tmp = nasm_strcat(t->text, tt->text);
3308 nasm_free(t->text);
3309 t->text = tmp;
3310 t->next = delete_Token(tt);
3311 }
3312 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003313 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003314
H. Peter Anvin76690a12002-04-30 20:52:49 +00003315 return thead;
3316}
3317
3318/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003319 * Expand all single-line macro calls made in the given line.
3320 * Return the expanded version of the line. The original is deemed
3321 * to be destroyed in the process. (In reality we'll just move
3322 * Tokens from input to output a lot of the time, rather than
3323 * actually bothering to destroy and replicate.)
3324 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003325static Token *
3326expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003327{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003328 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003329 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003330 Token **params;
3331 int *paramsize;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003332 int nparam, sparam, brackets, rescan;
3333 Token *org_tline = tline;
3334 Context *ctx;
3335 char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003336
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003337 /*
3338 * Trick: we should avoid changing the start token pointer since it can
3339 * be contained in "next" field of other token. Because of this
3340 * we allocate a copy of first token and work with it; at the end of
3341 * routine we copy it back
3342 */
3343 if (org_tline)
3344 {
H. Peter Anvin734b1882002-04-30 21:01:08 +00003345 tline =
3346 new_Token(org_tline->next, org_tline->type, org_tline->text,
3347 0);
3348 tline->mac = org_tline->mac;
H. Peter Anvince616072002-04-30 21:02:23 +00003349 nasm_free(org_tline->text);
3350 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003351 }
3352
H. Peter Anvin734b1882002-04-30 21:01:08 +00003353 again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003354 tail = &thead;
3355 thead = NULL;
3356
H. Peter Anvin734b1882002-04-30 21:01:08 +00003357 while (tline)
3358 { /* main token loop */
3359 if ((mname = tline->text))
3360 {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003361 /* if this token is a local macro, look in local context */
3362 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID)
H. Peter Anvin734b1882002-04-30 21:01:08 +00003363 ctx = get_ctx(mname, TRUE);
3364 else
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003365 ctx = NULL;
3366 if (!ctx)
3367 head = smacros[hash(mname)];
3368 else
H. Peter Anvineba20a72002-04-30 20:53:55 +00003369 head = ctx->localmac;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003370 /*
3371 * We've hit an identifier. As in is_mmacro below, we first
3372 * check whether the identifier is a single-line macro at
3373 * all, then think about checking for parameters if
3374 * necessary.
3375 */
H. Peter Anvineba20a72002-04-30 20:53:55 +00003376 for (m = head; m; m = m->next)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003377 if (!mstrcmp(m->name, mname, m->casesense))
H. Peter Anvineba20a72002-04-30 20:53:55 +00003378 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003379 if (m)
3380 {
3381 mstart = tline;
3382 params = NULL;
3383 paramsize = NULL;
3384 if (m->nparam == 0)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003385 {
H. Peter Anvin734b1882002-04-30 21:01:08 +00003386 /*
3387 * Simple case: the macro is parameterless. Discard the
3388 * one token that the macro call took, and push the
3389 * expansion back on the to-do stack.
3390 */
3391 if (!m->expansion)
3392 {
3393 if (!strcmp("__FILE__", m->name))
3394 {
3395 long num = 0;
3396 src_get(&num, &(tline->text));
3397 nasm_quote(&(tline->text));
3398 tline->type = TOK_STRING;
3399 continue;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003400 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003401 if (!strcmp("__LINE__", m->name))
3402 {
3403 nasm_free(tline->text);
3404 make_tok_num(tline, src_get_linnum());
3405 continue;
3406 }
3407 tline = delete_Token(tline);
3408 continue;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003409 }
3410 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003411 else
3412 {
3413 /*
3414 * Complicated case: at least one macro with this name
3415 * exists and takes parameters. We must find the
3416 * parameters in the call, count them, find the SMacro
3417 * that corresponds to that form of the macro call, and
3418 * substitute for the parameters when we expand. What a
3419 * pain.
3420 */
3421 tline = tline->next;
3422 skip_white_(tline);
3423 if (!tok_is_(tline, "("))
3424 {
3425 /*
3426 * This macro wasn't called with parameters: ignore
3427 * the call. (Behaviour borrowed from gnu cpp.)
3428 */
3429 tline = mstart;
3430 m = NULL;
3431 }
3432 else
3433 {
3434 int paren = 0;
3435 int white = 0;
3436 brackets = 0;
3437 nparam = 0;
3438 tline = tline->next;
3439 sparam = PARAM_DELTA;
3440 params = nasm_malloc(sparam * sizeof(Token *));
3441 params[0] = tline;
3442 paramsize = nasm_malloc(sparam * sizeof(int));
3443 paramsize[0] = 0;
3444 for (;; tline = tline->next)
3445 { /* parameter loop */
3446 if (!tline)
3447 {
3448 error(ERR_NONFATAL,
3449 "macro call expects terminating `)'");
3450 break;
3451 }
3452 if (tline->type == TOK_WHITESPACE
3453 && brackets <= 0)
3454 {
3455 if (paramsize[nparam])
3456 white++;
3457 else
3458 params[nparam] = tline->next;
3459 continue; /* parameter loop */
3460 }
3461 if (tline->type == TOK_OTHER
3462 && tline->text[1] == 0)
3463 {
3464 char ch = tline->text[0];
3465 if (ch == ',' && !paren && brackets <= 0)
3466 {
3467 if (++nparam >= sparam)
3468 {
3469 sparam += PARAM_DELTA;
3470 params = nasm_realloc(params,
3471 sparam * sizeof(Token *));
3472 paramsize = nasm_realloc(paramsize,
3473 sparam * sizeof(int));
3474 }
3475 params[nparam] = tline->next;
3476 paramsize[nparam] = 0;
3477 white = 0;
3478 continue; /* parameter loop */
3479 }
3480 if (ch == '{' &&
3481 (brackets > 0 || (brackets == 0 &&
3482 !paramsize[nparam])))
3483 {
3484 if (!(brackets++))
3485 {
3486 params[nparam] = tline->next;
3487 continue; /* parameter loop */
3488 }
3489 }
3490 if (ch == '}' && brackets > 0)
3491 if (--brackets == 0)
3492 {
3493 brackets = -1;
3494 continue; /* parameter loop */
3495 }
3496 if (ch == '(' && !brackets)
3497 paren++;
3498 if (ch == ')' && brackets <= 0)
3499 if (--paren < 0)
3500 break;
3501 }
3502 if (brackets < 0)
3503 {
3504 brackets = 0;
3505 error(ERR_NONFATAL, "braces do not "
3506 "enclose all of macro parameter");
3507 }
3508 paramsize[nparam] += white + 1;
3509 white = 0;
3510 } /* parameter loop */
3511 nparam++;
3512 while (m && (m->nparam != nparam ||
3513 mstrcmp(m->name, mname,
3514 m->casesense)))
3515 m = m->next;
3516 if (!m)
3517 error(ERR_WARNING | ERR_WARN_MNP,
3518 "macro `%s' exists, "
3519 "but not taking %d parameters",
3520 mstart->text, nparam);
3521 }
3522 }
3523 if (m && m->in_progress)
3524 m = NULL;
3525 if (!m) /* in progess or didn't find '(' or wrong nparam */
3526 {
3527 /*
3528 * Design question: should we handle !tline, which
3529 * indicates missing ')' here, or expand those
3530 * macros anyway, which requires the (t) test a few
3531 * lines down?
3532 */
3533 nasm_free(params);
3534 nasm_free(paramsize);
3535 tline = mstart;
3536 }
3537 else
3538 {
3539 /*
3540 * Expand the macro: we are placed on the last token of the
3541 * call, so that we can easily split the call from the
3542 * following tokens. We also start by pushing an SMAC_END
3543 * token for the cycle removal.
3544 */
3545 t = tline;
3546 if (t)
3547 {
3548 tline = t->next;
3549 t->next = NULL;
3550 }
3551 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
3552 tt->mac = m;
3553 m->in_progress = TRUE;
3554 tline = tt;
3555 for (t = m->expansion; t; t = t->next)
3556 {
3557 if (t->type >= TOK_SMAC_PARAM)
3558 {
3559 Token *pcopy = tline, **ptail = &pcopy;
3560 Token *ttt, *pt;
3561 int i;
3562
3563 ttt = params[t->type - TOK_SMAC_PARAM];
3564 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3565 --i >= 0;)
3566 {
3567 pt = *ptail =
3568 new_Token(tline, ttt->type, ttt->text,
3569 0);
3570 ptail = &pt->next;
3571 ttt = ttt->next;
3572 }
3573 tline = pcopy;
3574 }
3575 else
3576 {
3577 tt = new_Token(tline, t->type, t->text, 0);
3578 tline = tt;
3579 }
3580 }
3581
3582 /*
3583 * Having done that, get rid of the macro call, and clean
3584 * up the parameters.
3585 */
3586 nasm_free(params);
3587 nasm_free(paramsize);
3588 free_tlist(mstart);
3589 continue; /* main token loop */
3590 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003591 }
3592 }
3593
H. Peter Anvin734b1882002-04-30 21:01:08 +00003594 if (tline->type == TOK_SMAC_END)
3595 {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003596 tline->mac->in_progress = FALSE;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003597 tline = delete_Token(tline);
3598 }
3599 else
3600 {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003601 t = *tail = tline;
3602 tline = tline->next;
3603 t->mac = NULL;
3604 t->next = NULL;
3605 tail = &t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003606 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003607 }
3608
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003609 /*
3610 * Now scan the entire line and look for successive TOK_IDs that resulted
3611 * after expansion (they can't be produced by tokenise()). The successive
3612 * TOK_IDs should be concatenated.
3613 * Also we look for %+ tokens and concatenate the tokens before and after
3614 * them (without white spaces in between).
3615 */
3616 t = thead;
3617 rescan = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003618 while (t)
3619 {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003620 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3621 t = t->next;
3622 if (!t || !t->next)
3623 break;
3624 if (t->next->type == TOK_ID ||
H. Peter Anvin734b1882002-04-30 21:01:08 +00003625 t->next->type == TOK_PREPROC_ID ||
3626 t->next->type == TOK_NUMBER)
3627 {
3628 char *p = nasm_strcat(t->text, t->next->text);
3629 nasm_free(t->text);
3630 t->next = delete_Token(t->next);
3631 t->text = p;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003632 rescan = 1;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003633 }
3634 else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3635 t->next->next->type == TOK_PREPROC_ID &&
3636 strcmp(t->next->next->text, "%+") == 0)
3637 {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003638 /* free the next whitespace, the %+ token and next whitespace */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003639 int i;
3640 for (i = 1; i <= 3; i++)
3641 {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003642 if (!t->next || (i != 2 && t->next->type != TOK_WHITESPACE))
3643 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003644 t->next = delete_Token(t->next);
3645 } /* endfor */
3646 }
3647 else
3648 t = t->next;
3649 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003650 /* If we concatenaded something, re-scan the line for macros */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003651 if (rescan)
3652 {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003653 tline = thead;
3654 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003655 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003656
3657 if (org_tline)
3658 {
H. Peter Anvin734b1882002-04-30 21:01:08 +00003659 if (thead)
3660 {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003661 *org_tline = *thead;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003662 /* since we just gave text to org_line, don't free it */
3663 thead->text = NULL;
3664 delete_Token(thead);
3665 }
3666 else
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003667 {
3668 /* the expression expanded to empty line;
3669 we can't return NULL for some reasons
3670 we just set the line to a single WHITESPACE token. */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003671 memset(org_tline, 0, sizeof(*org_tline));
3672 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003673 org_tline->type = TOK_WHITESPACE;
3674 }
3675 thead = org_tline;
3676 }
3677
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003678 return thead;
3679}
3680
3681/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003682 * Similar to expand_smacro but used exclusively with macro identifiers
3683 * right before they are fetched in. The reason is that there can be
3684 * identifiers consisting of several subparts. We consider that if there
3685 * are more than one element forming the name, user wants a expansion,
3686 * otherwise it will be left as-is. Example:
3687 *
3688 * %define %$abc cde
3689 *
3690 * the identifier %$abc will be left as-is so that the handler for %define
3691 * will suck it and define the corresponding value. Other case:
3692 *
3693 * %define _%$abc cde
3694 *
3695 * In this case user wants name to be expanded *before* %define starts
3696 * working, so we'll expand %$abc into something (if it has a value;
3697 * otherwise it will be left as-is) then concatenate all successive
3698 * PP_IDs into one.
3699 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003700static Token *
3701expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003702{
3703 Token *cur, *oldnext = NULL;
3704
H. Peter Anvin734b1882002-04-30 21:01:08 +00003705 if (!tline || !tline->next)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003706 return tline;
3707
3708 cur = tline;
3709 while (cur->next &&
H. Peter Anvin734b1882002-04-30 21:01:08 +00003710 (cur->next->type == TOK_ID ||
3711 cur->next->type == TOK_PREPROC_ID || cur->next->type == TOK_NUMBER))
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003712 cur = cur->next;
3713
3714 /* If identifier consists of just one token, don't expand */
3715 if (cur == tline)
3716 return tline;
3717
H. Peter Anvin734b1882002-04-30 21:01:08 +00003718 if (cur)
3719 {
3720 oldnext = cur->next; /* Detach the tail past identifier */
3721 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003722 }
3723
H. Peter Anvin734b1882002-04-30 21:01:08 +00003724 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003725
H. Peter Anvin734b1882002-04-30 21:01:08 +00003726 if (cur)
3727 {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003728 /* expand_smacro possibly changhed tline; re-scan for EOL */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003729 cur = tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003730 while (cur && cur->next)
3731 cur = cur->next;
3732 if (cur)
3733 cur->next = oldnext;
3734 }
3735
3736 return tline;
3737}
3738
3739/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003740 * Determine whether the given line constitutes a multi-line macro
3741 * call, and return the MMacro structure called if so. Doesn't have
3742 * to check for an initial label - that's taken care of in
3743 * expand_mmacro - but must check numbers of parameters. Guaranteed
3744 * to be called with tline->type == TOK_ID, so the putative macro
3745 * name is easy to find.
3746 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003747static MMacro *
3748is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003749{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003750 MMacro *head, *m;
3751 Token **params;
3752 int nparam;
3753
3754 head = mmacros[hash(tline->text)];
3755
3756 /*
3757 * Efficiency: first we see if any macro exists with the given
3758 * name. If not, we can return NULL immediately. _Then_ we
3759 * count the parameters, and then we look further along the
3760 * list if necessary to find the proper MMacro.
3761 */
3762 for (m = head; m; m = m->next)
3763 if (!mstrcmp(m->name, tline->text, m->casesense))
3764 break;
3765 if (!m)
3766 return NULL;
3767
3768 /*
3769 * OK, we have a potential macro. Count and demarcate the
3770 * parameters.
3771 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003772 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003773
3774 /*
3775 * So we know how many parameters we've got. Find the MMacro
3776 * structure that handles this number.
3777 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003778 while (m)
3779 {
3780 if (m->nparam_min <= nparam && (m->plus || nparam <= m->nparam_max))
3781 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003782 /*
3783 * This one is right. Just check if cycle removal
3784 * prohibits us using it before we actually celebrate...
3785 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003786 if (m->in_progress)
3787 {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003788#if 0
H. Peter Anvin734b1882002-04-30 21:01:08 +00003789 error(ERR_NONFATAL,
3790 "self-reference in multi-line macro `%s'", m->name);
H. Peter Anvineba20a72002-04-30 20:53:55 +00003791#endif
H. Peter Anvin734b1882002-04-30 21:01:08 +00003792 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003793 return NULL;
3794 }
3795 /*
3796 * It's right, and we can use it. Add its default
3797 * parameters to the end of our list if necessary.
3798 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003799 if (m->defaults && nparam < m->nparam_min + m->ndefs)
3800 {
3801 params =
3802 nasm_realloc(params,
3803 ((m->nparam_min + m->ndefs + 1) * sizeof(*params)));
3804 while (nparam < m->nparam_min + m->ndefs)
3805 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003806 params[nparam] = m->defaults[nparam - m->nparam_min];
3807 nparam++;
3808 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003809 }
3810 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003811 * If we've gone over the maximum parameter count (and
3812 * we're in Plus mode), ignore parameters beyond
3813 * nparam_max.
3814 */
3815 if (m->plus && nparam > m->nparam_max)
3816 nparam = m->nparam_max;
3817 /*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003818 * Then terminate the parameter list, and leave.
3819 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003820 if (!params)
3821 { /* need this special case */
H. Peter Anvin76690a12002-04-30 20:52:49 +00003822 params = nasm_malloc(sizeof(*params));
3823 nparam = 0;
3824 }
3825 params[nparam] = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003826 *params_array = params;
3827 return m;
3828 }
3829 /*
3830 * This one wasn't right: look for the next one with the
3831 * same name.
3832 */
3833 for (m = m->next; m; m = m->next)
3834 if (!mstrcmp(m->name, tline->text, m->casesense))
3835 break;
3836 }
3837
3838 /*
3839 * After all that, we didn't find one with the right number of
3840 * parameters. Issue a warning, and fail to expand the macro.
3841 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003842 error(ERR_WARNING | ERR_WARN_MNP,
3843 "macro `%s' exists, but not taking %d parameters",
3844 tline->text, nparam);
3845 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003846 return NULL;
3847}
3848
3849/*
3850 * Expand the multi-line macro call made by the given line, if
3851 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00003852 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003853 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003854static int
3855expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003856{
3857 Token *startline = tline;
3858 Token *label = NULL;
3859 int dont_prepend = 0;
3860 Token **params, *t, *tt;
3861 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003862 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003863 int i, nparam, *paramlen;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003864
3865 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003866 skip_white_(t);
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00003867/* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
3868 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003869 return 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003870 m = is_mmacro(t, &params);
3871 if (!m)
3872 {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003873 Token *last;
3874 /*
3875 * We have an id which isn't a macro call. We'll assume
3876 * it might be a label; we'll also check to see if a
3877 * colon follows it. Then, if there's another id after
3878 * that lot, we'll check it again for macro-hood.
3879 */
3880 label = last = t;
3881 t = t->next;
3882 if (tok_type_(t, TOK_WHITESPACE))
3883 last = t, t = t->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003884 if (tok_is_(t, ":"))
3885 {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003886 dont_prepend = 1;
3887 last = t, t = t->next;
3888 if (tok_type_(t, TOK_WHITESPACE))
3889 last = t, t = t->next;
3890 }
3891 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
3892 return 0;
3893 last->next = NULL;
3894 tline = t;
3895 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003896
3897 /*
3898 * Fix up the parameters: this involves stripping leading and
3899 * trailing whitespace, then stripping braces if they are
3900 * present.
3901 */
H. Peter Anvineba20a72002-04-30 20:53:55 +00003902 for (nparam = 0; params[nparam]; nparam++)
3903 ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003904 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003905
H. Peter Anvin734b1882002-04-30 21:01:08 +00003906 for (i = 0; params[i]; i++)
3907 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003908 int brace = FALSE;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003909 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003910
3911 t = params[i];
H. Peter Anvineba20a72002-04-30 20:53:55 +00003912 skip_white_(t);
3913 if (tok_is_(t, "{"))
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003914 t = t->next, brace = TRUE, comma = FALSE;
3915 params[i] = t;
3916 paramlen[i] = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003917 while (t)
3918 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003919 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
H. Peter Anvin734b1882002-04-30 21:01:08 +00003920 break; /* ... because we have hit a comma */
H. Peter Anvineba20a72002-04-30 20:53:55 +00003921 if (comma && t->type == TOK_WHITESPACE && tok_is_(t->next, ","))
H. Peter Anvin734b1882002-04-30 21:01:08 +00003922 break; /* ... or a space then a comma */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003923 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
H. Peter Anvin734b1882002-04-30 21:01:08 +00003924 break; /* ... or a brace */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003925 t = t->next;
3926 paramlen[i]++;
3927 }
3928 }
3929
3930 /*
3931 * OK, we have a MMacro structure together with a set of
3932 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00003933 * copies of each Line on to istk->expansion. Substitution of
3934 * parameter tokens and macro-local tokens doesn't get done
3935 * until the single-line macro substitution process; this is
3936 * because delaying them allows us to change the semantics
3937 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003938 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00003939 * First, push an end marker on to istk->expansion, mark this
3940 * macro as in progress, and set up its invocation-specific
3941 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003942 */
3943 ll = nasm_malloc(sizeof(Line));
3944 ll->next = istk->expansion;
3945 ll->finishes = m;
3946 ll->first = NULL;
3947 istk->expansion = ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003948
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003949 m->in_progress = TRUE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003950 m->params = params;
3951 m->iline = tline;
3952 m->nparam = nparam;
3953 m->rotate = 0;
3954 m->paramlen = paramlen;
3955 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003956 m->lineno = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003957
3958 m->next_active = istk->mstk;
3959 istk->mstk = m;
3960
H. Peter Anvin734b1882002-04-30 21:01:08 +00003961 for (l = m->expansion; l; l = l->next)
3962 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003963 Token **tail;
3964
3965 ll = nasm_malloc(sizeof(Line));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003966 ll->finishes = NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003967 ll->next = istk->expansion;
3968 istk->expansion = ll;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003969 tail = &ll->first;
3970
H. Peter Anvin734b1882002-04-30 21:01:08 +00003971 for (t = l->first; t; t = t->next)
3972 {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003973 Token *x = t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003974 if (t->type == TOK_PREPROC_ID &&
3975 t->text[1] == '0' && t->text[2] == '0')
H. Peter Anvineba20a72002-04-30 20:53:55 +00003976 {
3977 dont_prepend = -1;
3978 x = label;
3979 if (!x)
3980 continue;
3981 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003982 tt = *tail = new_Token(NULL, x->type, x->text, 0);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003983 tail = &tt->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003984 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00003985 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003986 }
3987
3988 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00003989 * If we had a label, push it on as the first line of
3990 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003991 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003992 if (label)
3993 {
3994 if (dont_prepend < 0)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003995 free_tlist(startline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003996 else
3997 {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003998 ll = nasm_malloc(sizeof(Line));
3999 ll->finishes = NULL;
4000 ll->next = istk->expansion;
4001 istk->expansion = ll;
4002 ll->first = startline;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004003 if (!dont_prepend)
4004 {
H. Peter Anvineba20a72002-04-30 20:53:55 +00004005 while (label->next)
4006 label = label->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004007 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004008 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00004009 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004010 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004011
H. Peter Anvin734b1882002-04-30 21:01:08 +00004012 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004013
H. Peter Anvineba20a72002-04-30 20:53:55 +00004014 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004015}
4016
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004017/*
4018 * Since preprocessor always operate only on the line that didn't
4019 * arrived yet, we should always use ERR_OFFBY1. Also since user
4020 * won't want to see same error twice (preprocessing is done once
4021 * per pass) we will want to show errors only during pass one.
4022 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004023static void
Ed Beroset168c9c02002-05-17 03:10:13 +00004024error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004025{
4026 va_list arg;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004027 char buff[1024];
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004028
4029 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00004030 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004031 return;
4032
H. Peter Anvin734b1882002-04-30 21:01:08 +00004033 va_start(arg, fmt);
4034 vsprintf(buff, fmt, arg);
4035 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004036
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00004037 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvin99941bf2002-05-14 17:44:03 +00004038 _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name,
H. Peter Anvin734b1882002-04-30 21:01:08 +00004039 istk->mstk->lineno, buff);
Ed Beroset168c9c02002-05-17 03:10:13 +00004040 else
H. Peter Anvin99941bf2002-05-14 17:44:03 +00004041 _error(severity | ERR_PASS1, "%s", buff);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004042}
4043
H. Peter Anvin734b1882002-04-30 21:01:08 +00004044static void
4045pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
4046 ListGen * listgen)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004047{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004048 int h;
4049
H. Peter Anvin99941bf2002-05-14 17:44:03 +00004050 _error = errfunc;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004051 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004052 istk = nasm_malloc(sizeof(Include));
4053 istk->next = NULL;
4054 istk->conds = NULL;
4055 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004056 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004057 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00004058 istk->fname = NULL;
4059 src_set_fname(nasm_strdup(file));
4060 src_set_linnum(0);
4061 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004062 if (!istk->fp)
H. Peter Anvin734b1882002-04-30 21:01:08 +00004063 error(ERR_FATAL | ERR_NOFILE, "unable to open input file `%s'", file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004064 defining = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004065 for (h = 0; h < NHASH; h++)
4066 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004067 mmacros[h] = NULL;
4068 smacros[h] = NULL;
4069 }
4070 unique = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004071 if (tasm_compatible_mode) {
4072 stdmacpos = stdmac;
4073 } else {
4074 stdmacpos = &stdmac[TASM_MACRO_COUNT];
4075 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004076 any_extrastdmac = (extrastdmac != NULL);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004077 list = listgen;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004078 evaluate = eval;
4079 pass = apass;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004080}
4081
H. Peter Anvin734b1882002-04-30 21:01:08 +00004082static char *
4083pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004084{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004085 char *line;
4086 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004087
H. Peter Anvin734b1882002-04-30 21:01:08 +00004088 while (1)
4089 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004090 /*
4091 * Fetch a tokenised line, either from the macro-expansion
4092 * buffer or from the input file.
4093 */
4094 tline = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004095 while (istk->expansion && istk->expansion->finishes)
4096 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004097 Line *l = istk->expansion;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004098 if (!l->finishes->name && l->finishes->in_progress > 1)
4099 {
H. Peter Anvin76690a12002-04-30 20:52:49 +00004100 Line *ll;
4101
4102 /*
4103 * This is a macro-end marker for a macro with no
4104 * name, which means it's not really a macro at all
4105 * but a %rep block, and the `in_progress' field is
4106 * more than 1, meaning that we still need to
4107 * repeat. (1 means the natural last repetition; 0
4108 * means termination by %exitrep.) We have
4109 * therefore expanded up to the %endrep, and must
4110 * push the whole block on to the expansion buffer
4111 * again. We don't bother to remove the macro-end
4112 * marker: we'd only have to generate another one
4113 * if we did.
4114 */
4115 l->finishes->in_progress--;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004116 for (l = l->finishes->expansion; l; l = l->next)
4117 {
H. Peter Anvin76690a12002-04-30 20:52:49 +00004118 Token *t, *tt, **tail;
4119
4120 ll = nasm_malloc(sizeof(Line));
4121 ll->next = istk->expansion;
4122 ll->finishes = NULL;
4123 ll->first = NULL;
4124 tail = &ll->first;
4125
H. Peter Anvin734b1882002-04-30 21:01:08 +00004126 for (t = l->first; t; t = t->next)
4127 {
4128 if (t->text || t->type == TOK_WHITESPACE)
4129 {
4130 tt = *tail = new_Token(NULL, t->type, t->text, 0);
H. Peter Anvin76690a12002-04-30 20:52:49 +00004131 tail = &tt->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004132 }
4133 }
4134
4135 istk->expansion = ll;
4136 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004137 }
4138 else
4139 {
H. Peter Anvin87bc6192002-04-30 20:53:16 +00004140 /*
4141 * Check whether a `%rep' was started and not ended
4142 * within this macro expansion. This can happen and
4143 * should be detected. It's a fatal error because
4144 * I'm too confused to work out how to recover
4145 * sensibly from it.
4146 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004147 if (defining)
4148 {
H. Peter Anvin87bc6192002-04-30 20:53:16 +00004149 if (defining->name)
H. Peter Anvin734b1882002-04-30 21:01:08 +00004150 error(ERR_PANIC, "defining with name in expansion");
H. Peter Anvinb64535f2002-04-30 20:55:37 +00004151 else if (istk->mstk->name)
H. Peter Anvin734b1882002-04-30 21:01:08 +00004152 error(ERR_FATAL, "`%%rep' without `%%endrep' within"
4153 " expansion of macro `%s'", istk->mstk->name);
H. Peter Anvin87bc6192002-04-30 20:53:16 +00004154 }
4155
H. Peter Anvin734b1882002-04-30 21:01:08 +00004156 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004157 * FIXME: investigate the relationship at this point between
4158 * istk->mstk and l->finishes
4159 */
4160 {
4161 MMacro *m = istk->mstk;
4162 istk->mstk = m->next_active;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004163 if (m->name)
4164 {
H. Peter Anvineba20a72002-04-30 20:53:55 +00004165 /*
4166 * This was a real macro call, not a %rep, and
4167 * therefore the parameter information needs to
4168 * be freed.
4169 */
4170 nasm_free(m->params);
4171 free_tlist(m->iline);
4172 nasm_free(m->paramlen);
4173 l->finishes->in_progress = FALSE;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004174 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004175 else
4176 free_mmacro(m);
H. Peter Anvin76690a12002-04-30 20:52:49 +00004177 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004178 istk->expansion = l->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004179 nasm_free(l);
4180 list->downlevel(LIST_MACRO);
H. Peter Anvin76690a12002-04-30 20:52:49 +00004181 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004182 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004183 while (1)
4184 { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00004185
H. Peter Anvin734b1882002-04-30 21:01:08 +00004186 if (istk->expansion)
4187 { /* from a macro expansion */
H. Peter Anvineba20a72002-04-30 20:53:55 +00004188 char *p;
4189 Line *l = istk->expansion;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004190 if (istk->mstk)
4191 istk->mstk->lineno++;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004192 tline = l->first;
4193 istk->expansion = l->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004194 nasm_free(l);
4195 p = detoken(tline, FALSE);
4196 list->line(LIST_MACRO, p);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004197 nasm_free(p);
4198 break;
4199 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004200 line = read_line();
H. Peter Anvin734b1882002-04-30 21:01:08 +00004201 if (line)
4202 { /* from the current input file */
H. Peter Anvineba20a72002-04-30 20:53:55 +00004203 line = prepreproc(line);
4204 tline = tokenise(line);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004205 nasm_free(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004206 break;
4207 }
4208 /*
4209 * The current file has ended; work down the istk
4210 */
4211 {
4212 Include *i = istk;
4213 fclose(i->fp);
4214 if (i->conds)
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004215 error(ERR_FATAL, "expected `%%endif' before end of file");
Ed Beroset168c9c02002-05-17 03:10:13 +00004216 /* only set line and file name if there's a next node */
4217 if (i->next)
4218 {
4219 src_set_linnum(i->lineno);
4220 nasm_free(src_set_fname(i->fname));
4221 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004222 istk = i->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004223 list->downlevel(LIST_INCLUDE);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004224 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004225 if (!istk)
4226 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004227 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004228 }
4229
4230 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00004231 * We must expand MMacro parameters and MMacro-local labels
4232 * _before_ we plunge into directive processing, to cope
4233 * with things like `%define something %1' such as STRUC
4234 * uses. Unless we're _defining_ a MMacro, in which case
4235 * those tokens should be left alone to go into the
H. Peter Anvineba20a72002-04-30 20:53:55 +00004236 * definition; and unless we're in a non-emitting
4237 * condition, in which case we don't want to meddle with
4238 * anything.
H. Peter Anvin76690a12002-04-30 20:52:49 +00004239 */
H. Peter Anvineba20a72002-04-30 20:53:55 +00004240 if (!defining && !(istk->conds && !emitting(istk->conds->state)))
H. Peter Anvin76690a12002-04-30 20:52:49 +00004241 tline = expand_mmac_params(tline);
4242
4243 /*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004244 * Check the line to see if it's a preprocessor directive.
4245 */
Ed Beroset3ab3f412002-06-11 03:31:49 +00004246 if (do_directive(tline) == DIRECTIVE_FOUND)
H. Peter Anvin734b1882002-04-30 21:01:08 +00004247 {
4248 continue;
4249 }
4250 else if (defining)
4251 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004252 /*
4253 * We're defining a multi-line macro. We emit nothing
H. Peter Anvineba20a72002-04-30 20:53:55 +00004254 * at all, and just
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004255 * shove the tokenised line on to the macro definition.
4256 */
4257 Line *l = nasm_malloc(sizeof(Line));
4258 l->next = defining->expansion;
4259 l->first = tline;
4260 l->finishes = FALSE;
4261 defining->expansion = l;
4262 continue;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004263 }
4264 else if (istk->conds && !emitting(istk->conds->state))
4265 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004266 /*
4267 * We're in a non-emitting branch of a condition block.
4268 * Emit nothing at all, not even a blank line: when we
4269 * emerge from the condition we'll give a line-number
4270 * directive so we keep our place correctly.
4271 */
4272 free_tlist(tline);
4273 continue;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004274 }
4275 else if (istk->mstk && !istk->mstk->in_progress)
4276 {
H. Peter Anvin76690a12002-04-30 20:52:49 +00004277 /*
4278 * We're in a %rep block which has been terminated, so
4279 * we're walking through to the %endrep without
4280 * emitting anything. Emit nothing at all, not even a
4281 * blank line: when we emerge from the %rep block we'll
4282 * give a line-number directive so we keep our place
4283 * correctly.
4284 */
4285 free_tlist(tline);
4286 continue;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004287 }
4288 else
4289 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004290 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004291 if (!expand_mmacro(tline))
4292 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004293 /*
4294 * De-tokenise the line again, and emit it.
4295 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004296 line = detoken(tline, TRUE);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004297 free_tlist(tline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004298 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004299 }
4300 else
4301 {
4302 continue; /* expand_mmacro calls free_tlist */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004303 }
4304 }
4305 }
4306
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004307 return line;
4308}
4309
H. Peter Anvin734b1882002-04-30 21:01:08 +00004310static void
H. Peter Anvince616072002-04-30 21:02:23 +00004311pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004312{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004313 int h;
4314
H. Peter Anvin734b1882002-04-30 21:01:08 +00004315 if (defining)
4316 {
4317 error(ERR_NONFATAL, "end of file while still defining macro `%s'",
4318 defining->name);
4319 free_mmacro(defining);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004320 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004321 while (cstk)
4322 ctx_pop();
H. Peter Anvin734b1882002-04-30 21:01:08 +00004323 for (h = 0; h < NHASH; h++)
4324 {
4325 while (mmacros[h])
4326 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004327 MMacro *m = mmacros[h];
4328 mmacros[h] = mmacros[h]->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004329 free_mmacro(m);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004330 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004331 while (smacros[h])
4332 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004333 SMacro *s = smacros[h];
4334 smacros[h] = smacros[h]->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004335 nasm_free(s->name);
4336 free_tlist(s->expansion);
4337 nasm_free(s);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004338 }
4339 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004340 while (istk)
4341 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004342 Include *i = istk;
4343 istk = istk->next;
4344 fclose(i->fp);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004345 nasm_free(i->fname);
4346 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004347 }
4348 while (cstk)
4349 ctx_pop();
H. Peter Anvince616072002-04-30 21:02:23 +00004350 if (pass == 0)
4351 {
4352 free_llist(predef);
4353 delete_Blocks();
4354 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004355}
4356
H. Peter Anvin734b1882002-04-30 21:01:08 +00004357void
4358pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004359{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004360 IncPath *i;
4361
4362 i = nasm_malloc(sizeof(IncPath));
4363 i->path = nasm_strdup(path);
4364 i->next = ipath;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004365 ipath = i;
4366}
4367
H. Peter Anvin734b1882002-04-30 21:01:08 +00004368void
4369pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004370{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004371 Token *inc, *space, *name;
4372 Line *l;
4373
H. Peter Anvin734b1882002-04-30 21:01:08 +00004374 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4375 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4376 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004377
4378 l = nasm_malloc(sizeof(Line));
4379 l->next = predef;
4380 l->first = inc;
4381 l->finishes = FALSE;
4382 predef = l;
4383}
4384
H. Peter Anvin734b1882002-04-30 21:01:08 +00004385void
4386pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004387{
4388 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004389 Line *l;
4390 char *equals;
4391
4392 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00004393 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4394 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004395 if (equals)
4396 *equals = ' ';
H. Peter Anvineba20a72002-04-30 20:53:55 +00004397 space->next = tokenise(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004398 if (equals)
4399 *equals = '=';
4400
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004401 l = nasm_malloc(sizeof(Line));
4402 l->next = predef;
4403 l->first = def;
4404 l->finishes = FALSE;
4405 predef = l;
4406}
4407
H. Peter Anvin734b1882002-04-30 21:01:08 +00004408void
4409pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00004410{
4411 Token *def, *space;
4412 Line *l;
4413
H. Peter Anvin734b1882002-04-30 21:01:08 +00004414 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4415 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Frank Kotlerd3523022003-06-14 12:12:26 +00004416 space->next = tokenise(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00004417
4418 l = nasm_malloc(sizeof(Line));
4419 l->next = predef;
4420 l->first = def;
4421 l->finishes = FALSE;
4422 predef = l;
4423}
4424
H. Peter Anvin734b1882002-04-30 21:01:08 +00004425void
H. Peter Anvinbfebdb02002-09-12 02:23:54 +00004426pp_extra_stdmac(const char **macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004427{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004428 extrastdmac = macros;
4429}
4430
H. Peter Anvin734b1882002-04-30 21:01:08 +00004431static void
4432make_tok_num(Token * tok, long val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004433{
4434 char numbuf[20];
4435 sprintf(numbuf, "%ld", val);
4436 tok->text = nasm_strdup(numbuf);
4437 tok->type = TOK_NUMBER;
4438}
4439
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004440Preproc nasmpp = {
4441 pp_reset,
4442 pp_getline,
4443 pp_cleanup
4444};