blob: 7ce4ae587e696a3cb601b15837f745e597121684 [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 *
Keith Kaniosb7a89542007-04-12 02:40:54 +000014 * pp_getline gets tokenized lines, either
H. Peter Anvin4836e332002-04-30 20:56:43 +000015 *
16 * from a macro expansion
17 *
18 * or
19 * {
20 * read_line gets raw text from stdmacpos, or predef, or current input file
Keith Kaniosb7a89542007-04-12 02:40:54 +000021 * tokenize converts to tokens
H. Peter Anvin4836e332002-04-30 20:56:43 +000022 * }
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>
Keith Kaniosb7a89542007-04-12 02:40:54 +000044#include <inttypes.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000045
46#include "nasm.h"
47#include "nasmlib.h"
48
49typedef struct SMacro SMacro;
50typedef struct MMacro MMacro;
51typedef struct Context Context;
52typedef struct Token Token;
H. Peter Anvince616072002-04-30 21:02:23 +000053typedef struct Blocks Blocks;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000054typedef struct Line Line;
55typedef struct Include Include;
56typedef struct Cond Cond;
H. Peter Anvin6768eb72002-04-30 20:52:26 +000057typedef struct IncPath IncPath;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000058
59/*
60 * Store the definition of a single-line macro.
61 */
H. Peter Anvine2c80182005-01-15 22:15:51 +000062struct SMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000063 SMacro *next;
Keith Kaniosb7a89542007-04-12 02:40:54 +000064 int8_t *name;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000065 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 Anvine2c80182005-01-15 22:15:51 +000088struct MMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000089 MMacro *next;
Keith Kaniosb7a89542007-04-12 02:40:54 +000090 int8_t *name;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000091 int casesense;
Keith Kaniosb7a89542007-04-12 02:40:54 +000092 int64_t nparam_min, nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +000093 int plus; /* is the last parameter greedy? */
94 int nolist; /* is this macro listing-inhibited? */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000095 int in_progress;
H. Peter Anvine2c80182005-01-15 22:15:51 +000096 Token *dlist; /* All defaults as one list */
97 Token **defaults; /* Parameter default pointers */
98 int ndefs; /* number of default parameters */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000099 Line *expansion;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000100
101 MMacro *next_active;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000102 MMacro *rep_nest; /* used for nesting %rep */
103 Token **params; /* actual parameters */
104 Token *iline; /* invocation line */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000105 int nparam, rotate, *paramlen;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000106 uint32_t unique;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000107 int lineno; /* Current line number on expansion */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000108};
109
110/*
111 * The context stack is composed of a linked list of these.
112 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000113struct Context {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000114 Context *next;
115 SMacro *localmac;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000116 int8_t *name;
117 uint32_t number;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000118};
119
120/*
121 * This is the internal form which we break input lines up into.
122 * Typically stored in linked lists.
123 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000124 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
125 * necessarily used as-is, but is intended to denote the number of
126 * the substituted parameter. So in the definition
127 *
128 * %define a(x,y) ( (x) & ~(y) )
129 *
130 * the token representing `x' will have its type changed to
131 * TOK_SMAC_PARAM, but the one representing `y' will be
132 * TOK_SMAC_PARAM+1.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000133 *
134 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
135 * which doesn't need quotes around it. Used in the pre-include
136 * mechanism as an alternative to trying to find a sensible type of
137 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000138 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000139struct Token {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000140 Token *next;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000141 int8_t *text;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000142 SMacro *mac; /* associated macro for TOK_SMAC_END */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000143 int type;
144};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000145enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000146 TOK_WHITESPACE = 1, TOK_COMMENT, TOK_ID, TOK_PREPROC_ID, TOK_STRING,
H. Peter Anvineba20a72002-04-30 20:53:55 +0000147 TOK_NUMBER, TOK_SMAC_END, TOK_OTHER, TOK_SMAC_PARAM,
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000148 TOK_INTERNAL_STRING
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000149};
150
151/*
152 * Multi-line macro definitions are stored as a linked list of
153 * these, which is essentially a container to allow several linked
154 * lists of Tokens.
155 *
156 * Note that in this module, linked lists are treated as stacks
157 * wherever possible. For this reason, Lines are _pushed_ on to the
158 * `expansion' field in MMacro structures, so that the linked list,
159 * if walked, would give the macro lines in reverse order; this
160 * means that we can walk the list when expanding a macro, and thus
161 * push the lines on to the `expansion' field in _istk_ in reverse
162 * order (so that when popped back off they are in the right
163 * order). It may seem cockeyed, and it relies on my design having
164 * an even number of steps in, but it works...
165 *
166 * Some of these structures, rather than being actual lines, are
167 * markers delimiting the end of the expansion of a given macro.
H. Peter Anvin76690a12002-04-30 20:52:49 +0000168 * This is for use in the cycle-tracking and %rep-handling code.
169 * Such structures have `finishes' non-NULL, and `first' NULL. All
170 * others have `finishes' NULL, but `first' may still be NULL if
171 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000172 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000173struct Line {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000174 Line *next;
175 MMacro *finishes;
176 Token *first;
177};
178
179/*
180 * To handle an arbitrary level of file inclusion, we maintain a
181 * stack (ie linked list) of these things.
182 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000183struct Include {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000184 Include *next;
185 FILE *fp;
186 Cond *conds;
187 Line *expansion;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000188 int8_t *fname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000189 int lineno, lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000190 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000191};
192
193/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000194 * Include search path. This is simply a list of strings which get
195 * prepended, in turn, to the name of an include file, in an
196 * attempt to find the file if it's not in the current directory.
197 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000198struct IncPath {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000199 IncPath *next;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000200 int8_t *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000201};
202
203/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000204 * Conditional assembly: we maintain a separate stack of these for
205 * each level of file inclusion. (The only reason we keep the
206 * stacks separate is to ensure that a stray `%endif' in a file
207 * included from within the true branch of a `%if' won't terminate
208 * it and cause confusion: instead, rightly, it'll cause an error.)
209 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000210struct Cond {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000211 Cond *next;
212 int state;
213};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000214enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000215 /*
216 * These states are for use just after %if or %elif: IF_TRUE
217 * means the condition has evaluated to truth so we are
218 * currently emitting, whereas IF_FALSE means we are not
219 * currently emitting but will start doing so if a %else comes
220 * up. In these states, all directives are admissible: %elif,
221 * %else and %endif. (And of course %if.)
222 */
223 COND_IF_TRUE, COND_IF_FALSE,
224 /*
225 * These states come up after a %else: ELSE_TRUE means we're
226 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
227 * any %elif or %else will cause an error.
228 */
229 COND_ELSE_TRUE, COND_ELSE_FALSE,
230 /*
231 * This state means that we're not emitting now, and also that
232 * nothing until %endif will be emitted at all. It's for use in
233 * two circumstances: (i) when we've had our moment of emission
234 * and have now started seeing %elifs, and (ii) when the
235 * condition construct in question is contained within a
236 * non-emitting branch of a larger condition construct.
237 */
238 COND_NEVER
239};
240#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
241
Ed Beroset3ab3f412002-06-11 03:31:49 +0000242/*
243 * These defines are used as the possible return values for do_directive
244 */
245#define NO_DIRECTIVE_FOUND 0
246#define DIRECTIVE_FOUND 1
247
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000248/*
249 * Condition codes. Note that we use c_ prefix not C_ because C_ is
250 * used in nasm.h for the "real" condition codes. At _this_ level,
251 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
252 * ones, so we need a different enum...
253 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000254static const int8_t *conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000255 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
256 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
257 "np", "ns", "nz", "o", "p", "pe", "po", "s", "z"
258};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000259enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000260 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
261 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
262 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_S, c_Z
263};
264static int inverse_ccs[] = {
265 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
266 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,
267 c_Z, c_NO, c_NP, c_PO, c_PE, c_NS, c_NZ
268};
269
H. Peter Anvin76690a12002-04-30 20:52:49 +0000270/*
271 * Directive names.
272 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000273static const int8_t *directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000274 "%arg",
H. Peter Anvin76690a12002-04-30 20:52:49 +0000275 "%assign", "%clear", "%define", "%elif", "%elifctx", "%elifdef",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000276 "%elifid", "%elifidn", "%elifidni", "%elifmacro", "%elifnctx",
277 "%elifndef",
278 "%elifnid", "%elifnidn", "%elifnidni", "%elifnmacro", "%elifnnum",
279 "%elifnstr",
H. Peter Anvin76690a12002-04-30 20:52:49 +0000280 "%elifnum", "%elifstr", "%else", "%endif", "%endm", "%endmacro",
281 "%endrep", "%error", "%exitrep", "%iassign", "%idefine", "%if",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000282 "%ifctx", "%ifdef", "%ifid", "%ifidn", "%ifidni", "%ifmacro",
283 "%ifnctx",
H. Peter Anvin65747262002-05-07 00:10:05 +0000284 "%ifndef", "%ifnid", "%ifnidn", "%ifnidni", "%ifnmacro", "%ifnnum",
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000285 "%ifnstr", "%ifnum", "%ifstr", "%imacro", "%include",
286 "%ixdefine", "%line",
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000287 "%local",
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000288 "%macro", "%pop", "%push", "%rep", "%repl", "%rotate",
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000289 "%stacksize",
H. Peter Anvin734b1882002-04-30 21:01:08 +0000290 "%strlen", "%substr", "%undef", "%xdefine"
H. Peter Anvin76690a12002-04-30 20:52:49 +0000291};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000292enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000293 PP_ARG,
H. Peter Anvin76690a12002-04-30 20:52:49 +0000294 PP_ASSIGN, PP_CLEAR, PP_DEFINE, PP_ELIF, PP_ELIFCTX, PP_ELIFDEF,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000295 PP_ELIFID, PP_ELIFIDN, PP_ELIFIDNI, PP_ELIFMACRO, PP_ELIFNCTX,
296 PP_ELIFNDEF,
297 PP_ELIFNID, PP_ELIFNIDN, PP_ELIFNIDNI, PP_ELIFNMACRO, PP_ELIFNNUM,
298 PP_ELIFNSTR,
H. Peter Anvin76690a12002-04-30 20:52:49 +0000299 PP_ELIFNUM, PP_ELIFSTR, PP_ELSE, PP_ENDIF, PP_ENDM, PP_ENDMACRO,
300 PP_ENDREP, PP_ERROR, PP_EXITREP, PP_IASSIGN, PP_IDEFINE, PP_IF,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000301 PP_IFCTX, PP_IFDEF, PP_IFID, PP_IFIDN, PP_IFIDNI, PP_IFMACRO,
302 PP_IFNCTX,
H. Peter Anvin65747262002-05-07 00:10:05 +0000303 PP_IFNDEF, PP_IFNID, PP_IFNIDN, PP_IFNIDNI, PP_IFNMACRO, PP_IFNNUM,
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000304 PP_IFNSTR, PP_IFNUM, PP_IFSTR, PP_IMACRO, PP_INCLUDE,
305 PP_IXDEFINE, PP_LINE,
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000306 PP_LOCAL,
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000307 PP_MACRO, PP_POP, PP_PUSH, PP_REP, PP_REPL, PP_ROTATE,
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000308 PP_STACKSIZE,
H. Peter Anvin734b1882002-04-30 21:01:08 +0000309 PP_STRLEN, PP_SUBSTR, PP_UNDEF, PP_XDEFINE
H. Peter Anvin76690a12002-04-30 20:52:49 +0000310};
311
H. Peter Anvin65747262002-05-07 00:10:05 +0000312/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
313static int is_condition(int arg)
314{
315 return ((arg >= PP_ELIF) && (arg <= PP_ENDIF)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000316 ((arg >= PP_IF) && (arg <= PP_IFSTR));
H. Peter Anvin65747262002-05-07 00:10:05 +0000317}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000318
319/* For TASM compatibility we need to be able to recognise TASM compatible
320 * conditional compilation directives. Using the NASM pre-processor does
321 * not work, so we look for them specifically from the following list and
322 * then jam in the equivalent NASM directive into the input stream.
323 */
324
325#ifndef MAX
326# define MAX(a,b) ( ((a) > (b)) ? (a) : (b))
327#endif
328
H. Peter Anvine2c80182005-01-15 22:15:51 +0000329enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000330 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
331 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
332};
333
Keith Kaniosb7a89542007-04-12 02:40:54 +0000334static const int8_t *tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000335 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
336 "ifndef", "include", "local"
337};
338
339static int StackSize = 4;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000340static int8_t *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000341static int ArgOffset = 8;
342static int LocalOffset = 4;
343
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000344static Context *cstk;
345static Include *istk;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000346static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000347
H. Peter Anvine2c80182005-01-15 22:15:51 +0000348static efunc _error; /* Pointer to client-provided error reporting function */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000349static evalfunc evaluate;
350
H. Peter Anvine2c80182005-01-15 22:15:51 +0000351static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000352
Keith Kaniosb7a89542007-04-12 02:40:54 +0000353static uint32_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000354
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000355static Line *predef = NULL;
356
357static ListGen *list;
358
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000359/*
360 * The number of hash values we use for the macro lookup tables.
H. Peter Anvineba20a72002-04-30 20:53:55 +0000361 * FIXME: We should *really* be able to configure this at run time,
362 * or even have the hash table automatically expanding when necessary.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000363 */
364#define NHASH 31
365
366/*
367 * The current set of multi-line macros we have defined.
368 */
369static MMacro *mmacros[NHASH];
370
371/*
372 * The current set of single-line macros we have defined.
373 */
374static SMacro *smacros[NHASH];
375
376/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000377 * The multi-line macro we are currently defining, or the %rep
378 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000379 */
380static MMacro *defining;
381
382/*
383 * The number of macro parameters to allocate space for at a time.
384 */
385#define PARAM_DELTA 16
386
387/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000388 * The standard macro set: defined as `static int8_t *stdmac[]'. Also
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000389 * gives our position in the macro set, when we're processing it.
390 */
391#include "macros.c"
Keith Kaniosb7a89542007-04-12 02:40:54 +0000392static const int8_t **stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000393
394/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000395 * The extra standard macros that come from the object format, if
396 * any.
397 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000398static const int8_t **extrastdmac = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000399int any_extrastdmac;
400
401/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000402 * Tokens are allocated in blocks to improve speed
403 */
404#define TOKEN_BLOCKSIZE 4096
405static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000406struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000407 Blocks *next;
408 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000409};
410
411static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000412
413/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000414 * Forward declarations.
415 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000416static Token *expand_mmac_params(Token * tline);
417static Token *expand_smacro(Token * tline);
418static Token *expand_id(Token * tline);
Keith Kaniosb7a89542007-04-12 02:40:54 +0000419static Context *get_ctx(int8_t *name, int all_contexts);
420static void make_tok_num(Token * tok, int32_t val);
421static void error(int severity, const int8_t *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000422static void *new_Block(size_t size);
423static void delete_Blocks(void);
Keith Kaniosb7a89542007-04-12 02:40:54 +0000424static Token *new_Token(Token * next, int type, int8_t *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000425static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000426
427/*
428 * Macros for safe checking of token pointers, avoid *(NULL)
429 */
430#define tok_type_(x,t) ((x) && (x)->type == (t))
431#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
432#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
433#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000434
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000435/* Handle TASM specific directives, which do not contain a % in
436 * front of them. We do it here because I could not find any other
437 * place to do it for the moment, and it is a hack (ideally it would
438 * be nice to be able to use the NASM pre-processor to do it).
439 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000440static int8_t *check_tasm_directive(int8_t *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000441{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000442 int32_t i, j, k, m, len;
443 int8_t *p = line, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000444
445 /* Skip whitespace */
446 while (isspace(*p) && *p != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000447 p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000448
449 /* Binary search for the directive name */
450 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +0000451 j = elements(tasm_directives);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000452 len = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000453 while (!isspace(p[len]) && p[len] != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000454 len++;
455 if (len) {
456 oldchar = p[len];
457 p[len] = 0;
458 while (j - i > 1) {
459 k = (j + i) / 2;
460 m = nasm_stricmp(p, tasm_directives[k]);
461 if (m == 0) {
462 /* We have found a directive, so jam a % in front of it
463 * so that NASM will then recognise it as one if it's own.
464 */
465 p[len] = oldchar;
466 len = strlen(p);
467 oldline = line;
468 line = nasm_malloc(len + 2);
469 line[0] = '%';
470 if (k == TM_IFDIFI) {
471 /* NASM does not recognise IFDIFI, so we convert it to
472 * %ifdef BOGUS. This is not used in NASM comaptible
473 * code, but does need to parse for the TASM macro
474 * package.
475 */
476 strcpy(line + 1, "ifdef BOGUS");
477 } else {
478 memcpy(line + 1, p, len + 1);
479 }
480 nasm_free(oldline);
481 return line;
482 } else if (m < 0) {
483 j = k;
484 } else
485 i = k;
486 }
487 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000488 }
489 return line;
490}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000491
H. Peter Anvin76690a12002-04-30 20:52:49 +0000492/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000493 * The pre-preprocessing stage... This function translates line
494 * number indications as they emerge from GNU cpp (`# lineno "file"
495 * flags') into NASM preprocessor line number indications (`%line
496 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000497 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000498static int8_t *prepreproc(int8_t *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000499{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000500 int lineno, fnlen;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000501 int8_t *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000502
H. Peter Anvine2c80182005-01-15 22:15:51 +0000503 if (line[0] == '#' && line[1] == ' ') {
504 oldline = line;
505 fname = oldline + 2;
506 lineno = atoi(fname);
507 fname += strspn(fname, "0123456789 ");
508 if (*fname == '"')
509 fname++;
510 fnlen = strcspn(fname, "\"");
511 line = nasm_malloc(20 + fnlen);
512 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
513 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000514 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000515 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000516 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000517 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000518}
519
520/*
521 * The hash function for macro lookups. Note that due to some
522 * macros having case-insensitive names, the hash function must be
523 * invariant under case changes. We implement this by applying a
524 * perfectly normal hash function to the uppercase of the string.
525 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000526static int hash(int8_t *s)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000527{
528 unsigned int h = 0;
529 int i = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000530 /*
531 * Powers of three, mod 31.
532 */
533 static const int multipliers[] = {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000534 1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10,
535 30, 28, 22, 4, 12, 5, 15, 14, 11, 2, 6, 18, 23, 7, 21
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000536 };
H. Peter Anvineba20a72002-04-30 20:53:55 +0000537
H. Peter Anvine2c80182005-01-15 22:15:51 +0000538 while (*s) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000539 h += multipliers[i] * (uint8_t)(toupper(*s));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000540 s++;
541 if (++i >= elements(multipliers))
542 i = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000543 }
544 h %= NHASH;
545 return h;
546}
547
548/*
549 * Free a linked list of tokens.
550 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000551static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000552{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000553 while (list) {
554 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000555 }
556}
557
558/*
559 * Free a linked list of lines.
560 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000561static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000562{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000563 Line *l;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000564 while (list) {
565 l = list;
566 list = list->next;
567 free_tlist(l->first);
568 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000569 }
570}
571
572/*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000573 * Free an MMacro
574 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000575static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000576{
H. Peter Anvin734b1882002-04-30 21:01:08 +0000577 nasm_free(m->name);
578 free_tlist(m->dlist);
579 nasm_free(m->defaults);
580 free_llist(m->expansion);
581 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000582}
583
584/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000585 * Pop the context stack.
586 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000587static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000588{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000589 Context *c = cstk;
590 SMacro *smac, *s;
591
592 cstk = cstk->next;
593 smac = c->localmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000594 while (smac) {
595 s = smac;
596 smac = smac->next;
597 nasm_free(s->name);
598 free_tlist(s->expansion);
599 nasm_free(s);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000600 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000601 nasm_free(c->name);
602 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000603}
604
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000605#define BUF_DELTA 512
606/*
607 * Read a line from the top file in istk, handling multiple CR/LFs
608 * at the end of the line read, and handling spurious ^Zs. Will
609 * return lines from the standard macro set if this has not already
610 * been done.
611 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000612static int8_t *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000613{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000614 int8_t *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000615 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000616
H. Peter Anvine2c80182005-01-15 22:15:51 +0000617 if (stdmacpos) {
618 if (*stdmacpos) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000619 int8_t *ret = nasm_strdup(*stdmacpos++);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000620 if (!*stdmacpos && any_extrastdmac) {
621 stdmacpos = extrastdmac;
622 any_extrastdmac = FALSE;
623 return ret;
624 }
625 /*
626 * Nasty hack: here we push the contents of `predef' on
627 * to the top-level expansion stack, since this is the
628 * most convenient way to implement the pre-include and
629 * pre-define features.
630 */
631 if (!*stdmacpos) {
632 Line *pd, *l;
633 Token *head, **tail, *t;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000634
H. Peter Anvine2c80182005-01-15 22:15:51 +0000635 for (pd = predef; pd; pd = pd->next) {
636 head = NULL;
637 tail = &head;
638 for (t = pd->first; t; t = t->next) {
639 *tail = new_Token(NULL, t->type, t->text, 0);
640 tail = &(*tail)->next;
641 }
642 l = nasm_malloc(sizeof(Line));
643 l->next = istk->expansion;
644 l->first = head;
645 l->finishes = FALSE;
646 istk->expansion = l;
647 }
648 }
649 return ret;
650 } else {
651 stdmacpos = NULL;
652 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000653 }
654
655 bufsize = BUF_DELTA;
656 buffer = nasm_malloc(BUF_DELTA);
657 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000658 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000659 while (1) {
660 q = fgets(p, bufsize - (p - buffer), istk->fp);
661 if (!q)
662 break;
663 p += strlen(p);
664 if (p > buffer && p[-1] == '\n') {
665 /* Convert backslash-CRLF line continuation sequences into
666 nothing at all (for DOS and Windows) */
667 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
668 p -= 3;
669 *p = 0;
670 continued_count++;
671 }
672 /* Also convert backslash-LF line continuation sequences into
673 nothing at all (for Unix) */
674 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
675 p -= 2;
676 *p = 0;
677 continued_count++;
678 } else {
679 break;
680 }
681 }
682 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000683 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000684 bufsize += BUF_DELTA;
685 buffer = nasm_realloc(buffer, bufsize);
686 p = buffer + offset; /* prevent stale-pointer problems */
687 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000688 }
689
H. Peter Anvine2c80182005-01-15 22:15:51 +0000690 if (!q && p == buffer) {
691 nasm_free(buffer);
692 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000693 }
694
H. Peter Anvine2c80182005-01-15 22:15:51 +0000695 src_set_linnum(src_get_linnum() + istk->lineinc +
696 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000697
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000698 /*
699 * Play safe: remove CRs as well as LFs, if any of either are
700 * present at the end of the line.
701 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000702 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000703 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000704
705 /*
706 * Handle spurious ^Z, which may be inserted into source files
707 * by some file transfer utilities.
708 */
709 buffer[strcspn(buffer, "\032")] = '\0';
710
H. Peter Anvin734b1882002-04-30 21:01:08 +0000711 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000712
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000713 return buffer;
714}
715
716/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000717 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000718 * don't need to parse the value out of e.g. numeric tokens: we
719 * simply split one string into many.
720 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000721static Token *tokenize(int8_t *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000722{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000723 int8_t *p = line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000724 int type;
725 Token *list = NULL;
726 Token *t, **tail = &list;
727
H. Peter Anvine2c80182005-01-15 22:15:51 +0000728 while (*line) {
729 p = line;
730 if (*p == '%') {
731 p++;
732 if (isdigit(*p) ||
733 ((*p == '-' || *p == '+') && isdigit(p[1])) ||
734 ((*p == '+') && (isspace(p[1]) || !p[1]))) {
735 do {
736 p++;
737 }
738 while (isdigit(*p));
739 type = TOK_PREPROC_ID;
740 } else if (*p == '{') {
741 p++;
742 while (*p && *p != '}') {
743 p[-1] = *p;
744 p++;
745 }
746 p[-1] = '\0';
747 if (*p)
748 p++;
749 type = TOK_PREPROC_ID;
750 } else if (isidchar(*p) ||
751 ((*p == '!' || *p == '%' || *p == '$') &&
752 isidchar(p[1]))) {
753 do {
754 p++;
755 }
756 while (isidchar(*p));
757 type = TOK_PREPROC_ID;
758 } else {
759 type = TOK_OTHER;
760 if (*p == '%')
761 p++;
762 }
763 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
764 type = TOK_ID;
765 p++;
766 while (*p && isidchar(*p))
767 p++;
768 } else if (*p == '\'' || *p == '"') {
769 /*
770 * A string token.
771 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000772 int8_t c = *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000773 p++;
774 type = TOK_STRING;
775 while (*p && *p != c)
776 p++;
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000777
H. Peter Anvine2c80182005-01-15 22:15:51 +0000778 if (*p) {
779 p++;
780 } else {
781 error(ERR_WARNING, "unterminated string");
782 /* Handling unterminated strings by UNV */
783 /* type = -1; */
784 }
785 } else if (isnumstart(*p)) {
786 /*
787 * A number token.
788 */
789 type = TOK_NUMBER;
790 p++;
791 while (*p && isnumchar(*p))
792 p++;
793 } else if (isspace(*p)) {
794 type = TOK_WHITESPACE;
795 p++;
796 while (*p && isspace(*p))
797 p++;
798 /*
799 * Whitespace just before end-of-line is discarded by
800 * pretending it's a comment; whitespace just before a
801 * comment gets lumped into the comment.
802 */
803 if (!*p || *p == ';') {
804 type = TOK_COMMENT;
805 while (*p)
806 p++;
807 }
808 } else if (*p == ';') {
809 type = TOK_COMMENT;
810 while (*p)
811 p++;
812 } else {
813 /*
814 * Anything else is an operator of some kind. We check
815 * for all the double-character operators (>>, <<, //,
816 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosb7a89542007-04-12 02:40:54 +0000817 * else is a single-int8_tacter operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000818 */
819 type = TOK_OTHER;
820 if ((p[0] == '>' && p[1] == '>') ||
821 (p[0] == '<' && p[1] == '<') ||
822 (p[0] == '/' && p[1] == '/') ||
823 (p[0] == '<' && p[1] == '=') ||
824 (p[0] == '>' && p[1] == '=') ||
825 (p[0] == '=' && p[1] == '=') ||
826 (p[0] == '!' && p[1] == '=') ||
827 (p[0] == '<' && p[1] == '>') ||
828 (p[0] == '&' && p[1] == '&') ||
829 (p[0] == '|' && p[1] == '|') ||
830 (p[0] == '^' && p[1] == '^')) {
831 p++;
832 }
833 p++;
834 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000835
H. Peter Anvine2c80182005-01-15 22:15:51 +0000836 /* Handling unterminated string by UNV */
837 /*if (type == -1)
838 {
839 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
840 t->text[p-line] = *line;
841 tail = &t->next;
842 }
843 else */
844 if (type != TOK_COMMENT) {
845 *tail = t = new_Token(NULL, type, line, p - line);
846 tail = &t->next;
847 }
848 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000849 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000850 return list;
851}
852
H. Peter Anvince616072002-04-30 21:02:23 +0000853/*
854 * this function allocates a new managed block of memory and
855 * returns a pointer to the block. The managed blocks are
856 * deleted only all at once by the delete_Blocks function.
857 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000858static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +0000859{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000860 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000861
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000862 /* first, get to the end of the linked list */
863 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000864 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000865 /* now allocate the requested chunk */
866 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000867
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000868 /* now allocate a new block for the next request */
869 b->next = nasm_malloc(sizeof(Blocks));
870 /* and initialize the contents of the new block */
871 b->next->next = NULL;
872 b->next->chunk = NULL;
873 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000874}
875
876/*
877 * this function deletes all managed blocks of memory
878 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000879static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +0000880{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000881 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +0000882
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000883 /*
884 * keep in mind that the first block, pointed to by blocks
885 * is a static and not dynamically allocated, so we don't
886 * free it.
887 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000888 while (b) {
889 if (b->chunk)
890 nasm_free(b->chunk);
891 a = b;
892 b = b->next;
893 if (a != &blocks)
894 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000895 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000896}
H. Peter Anvin734b1882002-04-30 21:01:08 +0000897
898/*
899 * this function creates a new Token and passes a pointer to it
900 * back to the caller. It sets the type and text elements, and
901 * also the mac and next elements to NULL.
902 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000903static Token *new_Token(Token * next, int type, int8_t *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +0000904{
905 Token *t;
906 int i;
907
H. Peter Anvine2c80182005-01-15 22:15:51 +0000908 if (freeTokens == NULL) {
909 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
910 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
911 freeTokens[i].next = &freeTokens[i + 1];
912 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000913 }
914 t = freeTokens;
915 freeTokens = t->next;
916 t->next = next;
917 t->mac = NULL;
918 t->type = type;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000919 if (type == TOK_WHITESPACE || text == NULL) {
920 t->text = NULL;
921 } else {
922 if (txtlen == 0)
923 txtlen = strlen(text);
924 t->text = nasm_malloc(1 + txtlen);
925 strncpy(t->text, text, txtlen);
926 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +0000927 }
928 return t;
929}
930
H. Peter Anvine2c80182005-01-15 22:15:51 +0000931static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +0000932{
933 Token *next = t->next;
934 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +0000935 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000936 freeTokens = t;
937 return next;
938}
939
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000940/*
941 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000942 * If expand_locals is not zero, identifiers of the form "%$*xxx"
943 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000944 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000945static int8_t *detoken(Token * tlist, int expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000946{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000947 Token *t;
948 int len;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000949 int8_t *line, *p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000950
951 len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000952 for (t = tlist; t; t = t->next) {
953 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000954 int8_t *p = getenv(t->text + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000955 nasm_free(t->text);
956 if (p)
957 t->text = nasm_strdup(p);
958 else
959 t->text = NULL;
960 }
961 /* Expand local macros here and not during preprocessing */
962 if (expand_locals &&
963 t->type == TOK_PREPROC_ID && t->text &&
964 t->text[0] == '%' && t->text[1] == '$') {
965 Context *ctx = get_ctx(t->text, FALSE);
966 if (ctx) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000967 int8_t buffer[40];
968 int8_t *p, *q = t->text + 2;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000969
H. Peter Anvine2c80182005-01-15 22:15:51 +0000970 q += strspn(q, "$");
971 snprintf(buffer, sizeof(buffer), "..@%lu.", ctx->number);
972 p = nasm_strcat(buffer, q);
973 nasm_free(t->text);
974 t->text = p;
975 }
976 }
977 if (t->type == TOK_WHITESPACE) {
978 len++;
979 } else if (t->text) {
980 len += strlen(t->text);
981 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000982 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000983 p = line = nasm_malloc(len + 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000984 for (t = tlist; t; t = t->next) {
985 if (t->type == TOK_WHITESPACE) {
986 *p = ' ';
987 p++;
988 *p = '\0';
989 } else if (t->text) {
990 strcpy(p, t->text);
991 p += strlen(p);
992 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000993 }
994 *p = '\0';
995 return line;
996}
997
998/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000999 * A scanner, suitable for use by the expression evaluator, which
1000 * operates on a line of Tokens. Expects a pointer to a pointer to
1001 * the first token in the line to be passed in as its private_data
1002 * field.
1003 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001004static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001005{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001006 Token **tlineptr = private_data;
1007 Token *tline;
1008
H. Peter Anvine2c80182005-01-15 22:15:51 +00001009 do {
1010 tline = *tlineptr;
1011 *tlineptr = tline ? tline->next : NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001012 }
1013 while (tline && (tline->type == TOK_WHITESPACE ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001014 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001015
1016 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001017 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001018
1019 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001020 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001021 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001022 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001023
H. Peter Anvine2c80182005-01-15 22:15:51 +00001024 if (tline->type == TOK_ID) {
1025 tokval->t_charptr = tline->text;
1026 if (tline->text[0] == '$') {
1027 tokval->t_charptr++;
1028 return tokval->t_type = TOKEN_ID;
1029 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001030
H. Peter Anvine2c80182005-01-15 22:15:51 +00001031 /*
1032 * This is the only special case we actually need to worry
1033 * about in this restricted context.
1034 */
1035 if (!nasm_stricmp(tline->text, "seg"))
1036 return tokval->t_type = TOKEN_SEG;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001037
H. Peter Anvine2c80182005-01-15 22:15:51 +00001038 return tokval->t_type = TOKEN_ID;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001039 }
1040
H. Peter Anvine2c80182005-01-15 22:15:51 +00001041 if (tline->type == TOK_NUMBER) {
1042 int rn_error;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001043
H. Peter Anvine2c80182005-01-15 22:15:51 +00001044 tokval->t_integer = readnum(tline->text, &rn_error);
1045 if (rn_error)
1046 return tokval->t_type = TOKEN_ERRNUM;
1047 tokval->t_charptr = NULL;
1048 return tokval->t_type = TOKEN_NUM;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001049 }
1050
H. Peter Anvine2c80182005-01-15 22:15:51 +00001051 if (tline->type == TOK_STRING) {
1052 int rn_warn;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001053 int8_t q, *r;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001054 int l;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001055
H. Peter Anvine2c80182005-01-15 22:15:51 +00001056 r = tline->text;
1057 q = *r++;
1058 l = strlen(r);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001059
H. Peter Anvine2c80182005-01-15 22:15:51 +00001060 if (l == 0 || r[l - 1] != q)
1061 return tokval->t_type = TOKEN_ERRNUM;
1062 tokval->t_integer = readstrnum(r, l - 1, &rn_warn);
1063 if (rn_warn)
1064 error(ERR_WARNING | ERR_PASS1, "character constant too long");
1065 tokval->t_charptr = NULL;
1066 return tokval->t_type = TOKEN_NUM;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001067 }
1068
H. Peter Anvine2c80182005-01-15 22:15:51 +00001069 if (tline->type == TOK_OTHER) {
1070 if (!strcmp(tline->text, "<<"))
1071 return tokval->t_type = TOKEN_SHL;
1072 if (!strcmp(tline->text, ">>"))
1073 return tokval->t_type = TOKEN_SHR;
1074 if (!strcmp(tline->text, "//"))
1075 return tokval->t_type = TOKEN_SDIV;
1076 if (!strcmp(tline->text, "%%"))
1077 return tokval->t_type = TOKEN_SMOD;
1078 if (!strcmp(tline->text, "=="))
1079 return tokval->t_type = TOKEN_EQ;
1080 if (!strcmp(tline->text, "<>"))
1081 return tokval->t_type = TOKEN_NE;
1082 if (!strcmp(tline->text, "!="))
1083 return tokval->t_type = TOKEN_NE;
1084 if (!strcmp(tline->text, "<="))
1085 return tokval->t_type = TOKEN_LE;
1086 if (!strcmp(tline->text, ">="))
1087 return tokval->t_type = TOKEN_GE;
1088 if (!strcmp(tline->text, "&&"))
1089 return tokval->t_type = TOKEN_DBL_AND;
1090 if (!strcmp(tline->text, "^^"))
1091 return tokval->t_type = TOKEN_DBL_XOR;
1092 if (!strcmp(tline->text, "||"))
1093 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001094 }
1095
1096 /*
1097 * We have no other options: just return the first character of
1098 * the token text.
1099 */
1100 return tokval->t_type = tline->text[0];
1101}
1102
1103/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001104 * Compare a string to the name of an existing macro; this is a
1105 * simple wrapper which calls either strcmp or nasm_stricmp
1106 * depending on the value of the `casesense' parameter.
1107 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001108static int mstrcmp(int8_t *p, int8_t *q, int casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001109{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001110 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001111}
1112
1113/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001114 * Return the Context structure associated with a %$ token. Return
1115 * NULL, having _already_ reported an error condition, if the
1116 * context stack isn't deep enough for the supplied number of $
1117 * signs.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001118 * If all_contexts == TRUE, contexts that enclose current are
1119 * also scanned for such smacro, until it is found; if not -
1120 * only the context that directly results from the number of $'s
1121 * in variable's name.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001122 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001123static Context *get_ctx(int8_t *name, int all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001124{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001125 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001126 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001127 int i;
1128
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001129 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001130 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001131
H. Peter Anvine2c80182005-01-15 22:15:51 +00001132 if (!cstk) {
1133 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1134 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001135 }
1136
H. Peter Anvine2c80182005-01-15 22:15:51 +00001137 for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--) {
1138 ctx = ctx->next;
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00001139/* i--; Lino - 02/25/02 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001140 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001141 if (!ctx) {
1142 error(ERR_NONFATAL, "`%s': context stack is only"
1143 " %d level%s deep", name, i - 1, (i == 2 ? "" : "s"));
1144 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001145 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001146 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001147 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001148
H. Peter Anvine2c80182005-01-15 22:15:51 +00001149 do {
1150 /* Search for this smacro in found context */
1151 m = ctx->localmac;
1152 while (m) {
1153 if (!mstrcmp(m->name, name, m->casesense))
1154 return ctx;
1155 m = m->next;
1156 }
1157 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001158 }
1159 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001160 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001161}
1162
1163/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001164 * Open an include file. This routine must always return a valid
1165 * file pointer if it returns - it's responsible for throwing an
1166 * ERR_FATAL and bombing out completely if not. It should also try
1167 * the include path one by one until it finds the file or reaches
1168 * the end of the path.
1169 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001170static FILE *inc_fopen(int8_t *file)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001171{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001172 FILE *fp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001173 int8_t *prefix = "", *combine;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001174 IncPath *ip = ipath;
H. Peter Anvin620515a2002-04-30 20:57:38 +00001175 static int namelen = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001176 int len = strlen(file);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001177
H. Peter Anvine2c80182005-01-15 22:15:51 +00001178 while (1) {
1179 combine = nasm_malloc(strlen(prefix) + len + 1);
1180 strcpy(combine, prefix);
1181 strcat(combine, file);
1182 fp = fopen(combine, "r");
1183 if (pass == 0 && fp) {
1184 namelen += strlen(combine) + 1;
1185 if (namelen > 62) {
1186 printf(" \\\n ");
1187 namelen = 2;
1188 }
1189 printf(" %s", combine);
1190 }
1191 nasm_free(combine);
1192 if (fp)
1193 return fp;
1194 if (!ip)
1195 break;
1196 prefix = ip->path;
1197 ip = ip->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001198 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001199
H. Peter Anvin734b1882002-04-30 21:01:08 +00001200 error(ERR_FATAL, "unable to open include file `%s'", file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001201 return NULL; /* never reached - placate compilers */
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001202}
1203
1204/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001205 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001206 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001207 * return TRUE if _any_ single-line macro of that name is defined.
1208 * Otherwise, will return TRUE if a single-line macro with either
1209 * `nparam' or no parameters is defined.
1210 *
1211 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001212 * defined, or nparam is -1, the address of the definition structure
1213 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001214 * is NULL, no action will be taken regarding its contents, and no
1215 * error will occur.
1216 *
1217 * Note that this is also called with nparam zero to resolve
1218 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001219 *
1220 * If you already know which context macro belongs to, you can pass
1221 * the context pointer as first parameter; if you won't but name begins
1222 * with %$ the context will be automatically computed. If all_contexts
1223 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001224 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001225static int
Keith Kaniosb7a89542007-04-12 02:40:54 +00001226smacro_defined(Context * ctx, int8_t *name, int nparam, SMacro ** defn,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001227 int nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001228{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001229 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001230
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001231 if (ctx)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001232 m = ctx->localmac;
1233 else if (name[0] == '%' && name[1] == '$') {
1234 if (cstk)
1235 ctx = get_ctx(name, FALSE);
1236 if (!ctx)
1237 return FALSE; /* got to return _something_ */
1238 m = ctx->localmac;
1239 } else
1240 m = smacros[hash(name)];
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001241
H. Peter Anvine2c80182005-01-15 22:15:51 +00001242 while (m) {
1243 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
1244 (nparam <= 0 || m->nparam == 0 || nparam == m->nparam)) {
1245 if (defn) {
1246 if (nparam == m->nparam || nparam == -1)
1247 *defn = m;
1248 else
1249 *defn = NULL;
1250 }
1251 return TRUE;
1252 }
1253 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001254 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001255
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001256 return FALSE;
1257}
1258
1259/*
1260 * Count and mark off the parameters in a multi-line macro call.
1261 * This is called both from within the multi-line macro expansion
1262 * code, and also to mark off the default parameters when provided
1263 * in a %macro definition line.
1264 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001265static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001266{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001267 int paramsize, brace;
1268
1269 *nparam = paramsize = 0;
1270 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001271 while (t) {
1272 if (*nparam >= paramsize) {
1273 paramsize += PARAM_DELTA;
1274 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1275 }
1276 skip_white_(t);
1277 brace = FALSE;
1278 if (tok_is_(t, "{"))
1279 brace = TRUE;
1280 (*params)[(*nparam)++] = t;
1281 while (tok_isnt_(t, brace ? "}" : ","))
1282 t = t->next;
1283 if (t) { /* got a comma/brace */
1284 t = t->next;
1285 if (brace) {
1286 /*
1287 * Now we've found the closing brace, look further
1288 * for the comma.
1289 */
1290 skip_white_(t);
1291 if (tok_isnt_(t, ",")) {
1292 error(ERR_NONFATAL,
1293 "braces do not enclose all of macro parameter");
1294 while (tok_isnt_(t, ","))
1295 t = t->next;
1296 }
1297 if (t)
1298 t = t->next; /* eat the comma */
1299 }
1300 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001301 }
1302}
1303
1304/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001305 * Determine whether one of the various `if' conditions is true or
1306 * not.
1307 *
1308 * We must free the tline we get passed.
1309 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001310static int if_condition(Token * tline, int i)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001311{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001312 int j, casesense;
1313 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001314 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001315 expr *evalresult;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001316
1317 origline = tline;
1318
H. Peter Anvine2c80182005-01-15 22:15:51 +00001319 switch (i) {
1320 case PP_IFCTX:
1321 case PP_ELIFCTX:
1322 case PP_IFNCTX:
1323 case PP_ELIFNCTX:
1324 j = FALSE; /* have we matched yet? */
1325 while (cstk && tline) {
1326 skip_white_(tline);
1327 if (!tline || tline->type != TOK_ID) {
1328 error(ERR_NONFATAL,
1329 "`%s' expects context identifiers", directives[i]);
1330 free_tlist(origline);
1331 return -1;
1332 }
1333 if (!nasm_stricmp(tline->text, cstk->name))
1334 j = TRUE;
1335 tline = tline->next;
1336 }
1337 if (i == PP_IFNCTX || i == PP_ELIFNCTX)
1338 j = !j;
1339 free_tlist(origline);
1340 return j;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001341
H. Peter Anvine2c80182005-01-15 22:15:51 +00001342 case PP_IFDEF:
1343 case PP_ELIFDEF:
1344 case PP_IFNDEF:
1345 case PP_ELIFNDEF:
1346 j = FALSE; /* have we matched yet? */
1347 while (tline) {
1348 skip_white_(tline);
1349 if (!tline || (tline->type != TOK_ID &&
1350 (tline->type != TOK_PREPROC_ID ||
1351 tline->text[1] != '$'))) {
1352 error(ERR_NONFATAL,
1353 "`%s' expects macro identifiers", directives[i]);
1354 free_tlist(origline);
1355 return -1;
1356 }
1357 if (smacro_defined(NULL, tline->text, 0, NULL, 1))
1358 j = TRUE;
1359 tline = tline->next;
1360 }
1361 if (i == PP_IFNDEF || i == PP_ELIFNDEF)
1362 j = !j;
1363 free_tlist(origline);
1364 return j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001365
H. Peter Anvine2c80182005-01-15 22:15:51 +00001366 case PP_IFIDN:
1367 case PP_ELIFIDN:
1368 case PP_IFNIDN:
1369 case PP_ELIFNIDN:
1370 case PP_IFIDNI:
1371 case PP_ELIFIDNI:
1372 case PP_IFNIDNI:
1373 case PP_ELIFNIDNI:
1374 tline = expand_smacro(tline);
1375 t = tt = tline;
1376 while (tok_isnt_(tt, ","))
1377 tt = tt->next;
1378 if (!tt) {
1379 error(ERR_NONFATAL,
1380 "`%s' expects two comma-separated arguments",
1381 directives[i]);
1382 free_tlist(tline);
1383 return -1;
1384 }
1385 tt = tt->next;
1386 casesense = (i == PP_IFIDN || i == PP_ELIFIDN ||
1387 i == PP_IFNIDN || i == PP_ELIFNIDN);
1388 j = TRUE; /* assume equality unless proved not */
1389 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1390 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1391 error(ERR_NONFATAL, "`%s': more than one comma on line",
1392 directives[i]);
1393 free_tlist(tline);
1394 return -1;
1395 }
1396 if (t->type == TOK_WHITESPACE) {
1397 t = t->next;
1398 continue;
1399 }
1400 if (tt->type == TOK_WHITESPACE) {
1401 tt = tt->next;
1402 continue;
1403 }
1404 if (tt->type != t->type) {
1405 j = FALSE; /* found mismatching tokens */
1406 break;
1407 }
1408 /* Unify surrounding quotes for strings */
1409 if (t->type == TOK_STRING) {
1410 tt->text[0] = t->text[0];
1411 tt->text[strlen(tt->text) - 1] = t->text[0];
1412 }
1413 if (mstrcmp(tt->text, t->text, casesense) != 0) {
1414 j = FALSE; /* found mismatching tokens */
1415 break;
1416 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001417
H. Peter Anvine2c80182005-01-15 22:15:51 +00001418 t = t->next;
1419 tt = tt->next;
1420 }
1421 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
1422 j = FALSE; /* trailing gunk on one end or other */
1423 if (i == PP_IFNIDN || i == PP_ELIFNIDN ||
1424 i == PP_IFNIDNI || i == PP_ELIFNIDNI)
1425 j = !j;
1426 free_tlist(tline);
1427 return j;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001428
H. Peter Anvine2c80182005-01-15 22:15:51 +00001429 case PP_IFMACRO:
1430 case PP_ELIFMACRO:
1431 case PP_IFNMACRO:
1432 case PP_ELIFNMACRO:
1433 {
1434 int found = 0;
1435 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001436
H. Peter Anvine2c80182005-01-15 22:15:51 +00001437 tline = tline->next;
1438 skip_white_(tline);
1439 tline = expand_id(tline);
1440 if (!tok_type_(tline, TOK_ID)) {
1441 error(ERR_NONFATAL,
1442 "`%s' expects a macro name", directives[i]);
1443 return -1;
1444 }
1445 searching.name = nasm_strdup(tline->text);
1446 searching.casesense = (i == PP_MACRO);
1447 searching.plus = FALSE;
1448 searching.nolist = FALSE;
1449 searching.in_progress = FALSE;
1450 searching.rep_nest = NULL;
1451 searching.nparam_min = 0;
1452 searching.nparam_max = INT_MAX;
1453 tline = expand_smacro(tline->next);
1454 skip_white_(tline);
1455 if (!tline) {
1456 } else if (!tok_type_(tline, TOK_NUMBER)) {
1457 error(ERR_NONFATAL,
1458 "`%s' expects a parameter count or nothing",
1459 directives[i]);
1460 } else {
1461 searching.nparam_min = searching.nparam_max =
1462 readnum(tline->text, &j);
1463 if (j)
1464 error(ERR_NONFATAL,
1465 "unable to parse parameter count `%s'",
1466 tline->text);
1467 }
1468 if (tline && tok_is_(tline->next, "-")) {
1469 tline = tline->next->next;
1470 if (tok_is_(tline, "*"))
1471 searching.nparam_max = INT_MAX;
1472 else if (!tok_type_(tline, TOK_NUMBER))
1473 error(ERR_NONFATAL,
1474 "`%s' expects a parameter count after `-'",
1475 directives[i]);
1476 else {
1477 searching.nparam_max = readnum(tline->text, &j);
1478 if (j)
1479 error(ERR_NONFATAL,
1480 "unable to parse parameter count `%s'",
1481 tline->text);
1482 if (searching.nparam_min > searching.nparam_max)
1483 error(ERR_NONFATAL,
1484 "minimum parameter count exceeds maximum");
1485 }
1486 }
1487 if (tline && tok_is_(tline->next, "+")) {
1488 tline = tline->next;
1489 searching.plus = TRUE;
1490 }
1491 mmac = mmacros[hash(searching.name)];
1492 while (mmac) {
1493 if (!strcmp(mmac->name, searching.name) &&
1494 (mmac->nparam_min <= searching.nparam_max
1495 || searching.plus)
1496 && (searching.nparam_min <= mmac->nparam_max
1497 || mmac->plus)) {
1498 found = TRUE;
1499 break;
1500 }
1501 mmac = mmac->next;
1502 }
1503 nasm_free(searching.name);
1504 free_tlist(origline);
1505 if (i == PP_IFNMACRO || i == PP_ELIFNMACRO)
1506 found = !found;
1507 return found;
1508 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001509
H. Peter Anvine2c80182005-01-15 22:15:51 +00001510 case PP_IFID:
1511 case PP_ELIFID:
1512 case PP_IFNID:
1513 case PP_ELIFNID:
1514 case PP_IFNUM:
1515 case PP_ELIFNUM:
1516 case PP_IFNNUM:
1517 case PP_ELIFNNUM:
1518 case PP_IFSTR:
1519 case PP_ELIFSTR:
1520 case PP_IFNSTR:
1521 case PP_ELIFNSTR:
1522 tline = expand_smacro(tline);
1523 t = tline;
1524 while (tok_type_(t, TOK_WHITESPACE))
1525 t = t->next;
1526 j = FALSE; /* placate optimiser */
1527 if (t)
1528 switch (i) {
1529 case PP_IFID:
1530 case PP_ELIFID:
1531 case PP_IFNID:
1532 case PP_ELIFNID:
1533 j = (t->type == TOK_ID);
1534 break;
1535 case PP_IFNUM:
1536 case PP_ELIFNUM:
1537 case PP_IFNNUM:
1538 case PP_ELIFNNUM:
1539 j = (t->type == TOK_NUMBER);
1540 break;
1541 case PP_IFSTR:
1542 case PP_ELIFSTR:
1543 case PP_IFNSTR:
1544 case PP_ELIFNSTR:
1545 j = (t->type == TOK_STRING);
1546 break;
1547 }
1548 if (i == PP_IFNID || i == PP_ELIFNID ||
1549 i == PP_IFNNUM || i == PP_ELIFNNUM ||
1550 i == PP_IFNSTR || i == PP_ELIFNSTR)
1551 j = !j;
1552 free_tlist(tline);
1553 return j;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001554
H. Peter Anvine2c80182005-01-15 22:15:51 +00001555 case PP_IF:
1556 case PP_ELIF:
1557 t = tline = expand_smacro(tline);
1558 tptr = &t;
1559 tokval.t_type = TOKEN_INVALID;
1560 evalresult = evaluate(ppscan, tptr, &tokval,
1561 NULL, pass | CRITICAL, error, NULL);
1562 free_tlist(tline);
1563 if (!evalresult)
1564 return -1;
1565 if (tokval.t_type)
1566 error(ERR_WARNING,
1567 "trailing garbage after expression ignored");
1568 if (!is_simple(evalresult)) {
1569 error(ERR_NONFATAL,
1570 "non-constant value given to `%s'", directives[i]);
1571 return -1;
1572 }
1573 return reloc_value(evalresult) != 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001574
H. Peter Anvine2c80182005-01-15 22:15:51 +00001575 default:
1576 error(ERR_FATAL,
1577 "preprocessor directive `%s' not yet implemented",
1578 directives[i]);
1579 free_tlist(origline);
1580 return -1; /* yeah, right */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001581 }
1582}
1583
1584/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001585 * Expand macros in a string. Used in %error and %include directives.
Keith Kaniosb7a89542007-04-12 02:40:54 +00001586 * First tokenize the string, apply "expand_smacro" and then de-tokenize back.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001587 * The returned variable should ALWAYS be freed after usage.
1588 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001589void expand_macros_in_string(int8_t **p)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001590{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001591 Token *line = tokenize(*p);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001592 line = expand_smacro(line);
1593 *p = detoken(line, FALSE);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001594}
1595
Ed Beroset3ab3f412002-06-11 03:31:49 +00001596/**
1597 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001598 * Find out if a line contains a preprocessor directive, and deal
1599 * with it if so.
1600 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001601 * If a directive _is_ found, it is the responsibility of this routine
1602 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001603 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001604 * @param tline a pointer to the current tokeninzed line linked list
1605 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001606 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001607 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001608static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001609{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001610 int i, j, nparam, nolist;
1611 int64_t k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001612 int offset;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001613 int8_t *p, *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001614 Include *inc;
1615 Context *ctx;
1616 Cond *cond;
1617 SMacro *smac, **smhead;
1618 MMacro *mmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001619 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1620 Line *l;
1621 struct tokenval tokval;
1622 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001623 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001624
1625 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001626
H. Peter Anvineba20a72002-04-30 20:53:55 +00001627 skip_white_(tline);
1628 if (!tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001629 (tline->text[1] == '%' || tline->text[1] == '$'
1630 || tline->text[1] == '!'))
1631 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001632
1633 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +00001634 j = elements(directives);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001635 while (j - i > 1) {
1636 k = (j + i) / 2;
1637 m = nasm_stricmp(tline->text, directives[k]);
1638 if (m == 0) {
1639 if (tasm_compatible_mode) {
1640 i = k;
1641 j = -2;
1642 } else if (k != PP_ARG && k != PP_LOCAL && k != PP_STACKSIZE) {
1643 i = k;
1644 j = -2;
1645 }
1646 break;
1647 } else if (m < 0) {
1648 j = k;
1649 } else
1650 i = k;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001651 }
1652
1653 /*
1654 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00001655 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001656 * we should ignore all directives except for condition
1657 * directives.
1658 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001659 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001660 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
1661 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001662 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001663
1664 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001665 * If we're defining a macro or reading a %rep block, we should
1666 * ignore all directives except for %macro/%imacro (which
1667 * generate an error), %endm/%endmacro, and (only if we're in a
H. Peter Anvineba20a72002-04-30 20:53:55 +00001668 * %rep block) %endrep. If we're in a %rep block, another %rep
1669 * causes an error, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001670 */
1671 if (defining && i != PP_MACRO && i != PP_IMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001672 i != PP_ENDMACRO && i != PP_ENDM &&
1673 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
1674 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001675 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001676
H. Peter Anvine2c80182005-01-15 22:15:51 +00001677 if (j != -2) {
1678 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
1679 tline->text);
1680 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001681 }
1682
H. Peter Anvine2c80182005-01-15 22:15:51 +00001683 switch (i) {
1684 case PP_STACKSIZE:
1685 /* Directive to tell NASM what the default stack size is. The
1686 * default is for a 16-bit stack, and this can be overriden with
1687 * %stacksize large.
1688 * the following form:
1689 *
1690 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1691 */
1692 tline = tline->next;
1693 if (tline && tline->type == TOK_WHITESPACE)
1694 tline = tline->next;
1695 if (!tline || tline->type != TOK_ID) {
1696 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
1697 free_tlist(origline);
1698 return DIRECTIVE_FOUND;
1699 }
1700 if (nasm_stricmp(tline->text, "flat") == 0) {
1701 /* All subsequent ARG directives are for a 32-bit stack */
1702 StackSize = 4;
1703 StackPointer = "ebp";
1704 ArgOffset = 8;
1705 LocalOffset = 4;
1706 } else if (nasm_stricmp(tline->text, "large") == 0) {
1707 /* All subsequent ARG directives are for a 16-bit stack,
1708 * far function call.
1709 */
1710 StackSize = 2;
1711 StackPointer = "bp";
1712 ArgOffset = 4;
1713 LocalOffset = 2;
1714 } else if (nasm_stricmp(tline->text, "small") == 0) {
1715 /* All subsequent ARG directives are for a 16-bit stack,
1716 * far function call. We don't support near functions.
1717 */
1718 StackSize = 2;
1719 StackPointer = "bp";
1720 ArgOffset = 6;
1721 LocalOffset = 2;
1722 } else {
1723 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
1724 free_tlist(origline);
1725 return DIRECTIVE_FOUND;
1726 }
1727 free_tlist(origline);
1728 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001729
H. Peter Anvine2c80182005-01-15 22:15:51 +00001730 case PP_ARG:
1731 /* TASM like ARG directive to define arguments to functions, in
1732 * the following form:
1733 *
1734 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1735 */
1736 offset = ArgOffset;
1737 do {
Keith Kaniosb7a89542007-04-12 02:40:54 +00001738 int8_t *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001739 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001740
H. Peter Anvine2c80182005-01-15 22:15:51 +00001741 /* Find the argument name */
1742 tline = tline->next;
1743 if (tline && tline->type == TOK_WHITESPACE)
1744 tline = tline->next;
1745 if (!tline || tline->type != TOK_ID) {
1746 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
1747 free_tlist(origline);
1748 return DIRECTIVE_FOUND;
1749 }
1750 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001751
H. Peter Anvine2c80182005-01-15 22:15:51 +00001752 /* Find the argument size type */
1753 tline = tline->next;
1754 if (!tline || tline->type != TOK_OTHER
1755 || tline->text[0] != ':') {
1756 error(ERR_NONFATAL,
1757 "Syntax error processing `%%arg' directive");
1758 free_tlist(origline);
1759 return DIRECTIVE_FOUND;
1760 }
1761 tline = tline->next;
1762 if (!tline || tline->type != TOK_ID) {
1763 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
1764 free_tlist(origline);
1765 return DIRECTIVE_FOUND;
1766 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001767
H. Peter Anvine2c80182005-01-15 22:15:51 +00001768 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001769 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001770 tt = expand_smacro(tt);
1771 if (nasm_stricmp(tt->text, "byte") == 0) {
1772 size = MAX(StackSize, 1);
1773 } else if (nasm_stricmp(tt->text, "word") == 0) {
1774 size = MAX(StackSize, 2);
1775 } else if (nasm_stricmp(tt->text, "dword") == 0) {
1776 size = MAX(StackSize, 4);
1777 } else if (nasm_stricmp(tt->text, "qword") == 0) {
1778 size = MAX(StackSize, 8);
1779 } else if (nasm_stricmp(tt->text, "tword") == 0) {
1780 size = MAX(StackSize, 10);
1781 } else {
1782 error(ERR_NONFATAL,
1783 "Invalid size type for `%%arg' missing directive");
1784 free_tlist(tt);
1785 free_tlist(origline);
1786 return DIRECTIVE_FOUND;
1787 }
1788 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001789
H. Peter Anvine2c80182005-01-15 22:15:51 +00001790 /* Now define the macro for the argument */
1791 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
1792 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001793 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001794 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001795
H. Peter Anvine2c80182005-01-15 22:15:51 +00001796 /* Move to the next argument in the list */
1797 tline = tline->next;
1798 if (tline && tline->type == TOK_WHITESPACE)
1799 tline = tline->next;
1800 }
1801 while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1802 free_tlist(origline);
1803 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001804
H. Peter Anvine2c80182005-01-15 22:15:51 +00001805 case PP_LOCAL:
1806 /* TASM like LOCAL directive to define local variables for a
1807 * function, in the following form:
1808 *
1809 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
1810 *
1811 * The '= LocalSize' at the end is ignored by NASM, but is
1812 * required by TASM to define the local parameter size (and used
1813 * by the TASM macro package).
1814 */
1815 offset = LocalOffset;
1816 do {
Keith Kaniosb7a89542007-04-12 02:40:54 +00001817 int8_t *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001818 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001819
H. Peter Anvine2c80182005-01-15 22:15:51 +00001820 /* Find the argument name */
1821 tline = tline->next;
1822 if (tline && tline->type == TOK_WHITESPACE)
1823 tline = tline->next;
1824 if (!tline || tline->type != TOK_ID) {
1825 error(ERR_NONFATAL,
1826 "`%%local' missing argument parameter");
1827 free_tlist(origline);
1828 return DIRECTIVE_FOUND;
1829 }
1830 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001831
H. Peter Anvine2c80182005-01-15 22:15:51 +00001832 /* Find the argument size type */
1833 tline = tline->next;
1834 if (!tline || tline->type != TOK_OTHER
1835 || tline->text[0] != ':') {
1836 error(ERR_NONFATAL,
1837 "Syntax error processing `%%local' directive");
1838 free_tlist(origline);
1839 return DIRECTIVE_FOUND;
1840 }
1841 tline = tline->next;
1842 if (!tline || tline->type != TOK_ID) {
1843 error(ERR_NONFATAL,
1844 "`%%local' missing size type parameter");
1845 free_tlist(origline);
1846 return DIRECTIVE_FOUND;
1847 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001848
H. Peter Anvine2c80182005-01-15 22:15:51 +00001849 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001850 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001851 tt = expand_smacro(tt);
1852 if (nasm_stricmp(tt->text, "byte") == 0) {
1853 size = MAX(StackSize, 1);
1854 } else if (nasm_stricmp(tt->text, "word") == 0) {
1855 size = MAX(StackSize, 2);
1856 } else if (nasm_stricmp(tt->text, "dword") == 0) {
1857 size = MAX(StackSize, 4);
1858 } else if (nasm_stricmp(tt->text, "qword") == 0) {
1859 size = MAX(StackSize, 8);
1860 } else if (nasm_stricmp(tt->text, "tword") == 0) {
1861 size = MAX(StackSize, 10);
1862 } else {
1863 error(ERR_NONFATAL,
1864 "Invalid size type for `%%local' missing directive");
1865 free_tlist(tt);
1866 free_tlist(origline);
1867 return DIRECTIVE_FOUND;
1868 }
1869 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001870
H. Peter Anvine2c80182005-01-15 22:15:51 +00001871 /* Now define the macro for the argument */
1872 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
1873 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001874 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001875 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001876
H. Peter Anvine2c80182005-01-15 22:15:51 +00001877 /* Now define the assign to setup the enter_c macro correctly */
1878 snprintf(directive, sizeof(directive),
1879 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001880 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001881
H. Peter Anvine2c80182005-01-15 22:15:51 +00001882 /* Move to the next argument in the list */
1883 tline = tline->next;
1884 if (tline && tline->type == TOK_WHITESPACE)
1885 tline = tline->next;
1886 }
1887 while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1888 free_tlist(origline);
1889 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001890
H. Peter Anvine2c80182005-01-15 22:15:51 +00001891 case PP_CLEAR:
1892 if (tline->next)
1893 error(ERR_WARNING, "trailing garbage after `%%clear' ignored");
1894 for (j = 0; j < NHASH; j++) {
1895 while (mmacros[j]) {
1896 MMacro *m = mmacros[j];
1897 mmacros[j] = m->next;
1898 free_mmacro(m);
1899 }
1900 while (smacros[j]) {
1901 SMacro *s = smacros[j];
1902 smacros[j] = smacros[j]->next;
1903 nasm_free(s->name);
1904 free_tlist(s->expansion);
1905 nasm_free(s);
1906 }
1907 }
1908 free_tlist(origline);
1909 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001910
H. Peter Anvine2c80182005-01-15 22:15:51 +00001911 case PP_INCLUDE:
1912 tline = tline->next;
1913 skip_white_(tline);
1914 if (!tline || (tline->type != TOK_STRING &&
1915 tline->type != TOK_INTERNAL_STRING)) {
1916 error(ERR_NONFATAL, "`%%include' expects a file name");
1917 free_tlist(origline);
1918 return DIRECTIVE_FOUND; /* but we did _something_ */
1919 }
1920 if (tline->next)
1921 error(ERR_WARNING,
1922 "trailing garbage after `%%include' ignored");
1923 if (tline->type != TOK_INTERNAL_STRING) {
1924 p = tline->text + 1; /* point past the quote to the name */
1925 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
1926 } else
1927 p = tline->text; /* internal_string is easier */
1928 expand_macros_in_string(&p);
1929 inc = nasm_malloc(sizeof(Include));
1930 inc->next = istk;
1931 inc->conds = NULL;
1932 inc->fp = inc_fopen(p);
1933 inc->fname = src_set_fname(p);
1934 inc->lineno = src_set_linnum(0);
1935 inc->lineinc = 1;
1936 inc->expansion = NULL;
1937 inc->mstk = NULL;
1938 istk = inc;
1939 list->uplevel(LIST_INCLUDE);
1940 free_tlist(origline);
1941 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001942
H. Peter Anvine2c80182005-01-15 22:15:51 +00001943 case PP_PUSH:
1944 tline = tline->next;
1945 skip_white_(tline);
1946 tline = expand_id(tline);
1947 if (!tok_type_(tline, TOK_ID)) {
1948 error(ERR_NONFATAL, "`%%push' expects a context identifier");
1949 free_tlist(origline);
1950 return DIRECTIVE_FOUND; /* but we did _something_ */
1951 }
1952 if (tline->next)
1953 error(ERR_WARNING, "trailing garbage after `%%push' ignored");
1954 ctx = nasm_malloc(sizeof(Context));
1955 ctx->next = cstk;
1956 ctx->localmac = NULL;
1957 ctx->name = nasm_strdup(tline->text);
1958 ctx->number = unique++;
1959 cstk = ctx;
1960 free_tlist(origline);
1961 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001962
H. Peter Anvine2c80182005-01-15 22:15:51 +00001963 case PP_REPL:
1964 tline = tline->next;
1965 skip_white_(tline);
1966 tline = expand_id(tline);
1967 if (!tok_type_(tline, TOK_ID)) {
1968 error(ERR_NONFATAL, "`%%repl' expects a context identifier");
1969 free_tlist(origline);
1970 return DIRECTIVE_FOUND; /* but we did _something_ */
1971 }
1972 if (tline->next)
1973 error(ERR_WARNING, "trailing garbage after `%%repl' ignored");
1974 if (!cstk)
1975 error(ERR_NONFATAL, "`%%repl': context stack is empty");
1976 else {
1977 nasm_free(cstk->name);
1978 cstk->name = nasm_strdup(tline->text);
1979 }
1980 free_tlist(origline);
1981 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001982
H. Peter Anvine2c80182005-01-15 22:15:51 +00001983 case PP_POP:
1984 if (tline->next)
1985 error(ERR_WARNING, "trailing garbage after `%%pop' ignored");
1986 if (!cstk)
1987 error(ERR_NONFATAL, "`%%pop': context stack is already empty");
1988 else
1989 ctx_pop();
1990 free_tlist(origline);
1991 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001992
H. Peter Anvine2c80182005-01-15 22:15:51 +00001993 case PP_ERROR:
1994 tline->next = expand_smacro(tline->next);
1995 tline = tline->next;
1996 skip_white_(tline);
1997 if (tok_type_(tline, TOK_STRING)) {
1998 p = tline->text + 1; /* point past the quote to the name */
1999 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
2000 expand_macros_in_string(&p);
2001 error(ERR_NONFATAL, "%s", p);
2002 nasm_free(p);
2003 } else {
2004 p = detoken(tline, FALSE);
2005 error(ERR_WARNING, "%s", p);
2006 nasm_free(p);
2007 }
2008 free_tlist(origline);
2009 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002010
H. Peter Anvine2c80182005-01-15 22:15:51 +00002011 case PP_IF:
2012 case PP_IFCTX:
2013 case PP_IFDEF:
2014 case PP_IFID:
2015 case PP_IFIDN:
2016 case PP_IFIDNI:
2017 case PP_IFMACRO:
2018 case PP_IFNCTX:
2019 case PP_IFNDEF:
2020 case PP_IFNID:
2021 case PP_IFNIDN:
2022 case PP_IFNIDNI:
2023 case PP_IFNMACRO:
2024 case PP_IFNNUM:
2025 case PP_IFNSTR:
2026 case PP_IFNUM:
2027 case PP_IFSTR:
2028 if (istk->conds && !emitting(istk->conds->state))
2029 j = COND_NEVER;
2030 else {
2031 j = if_condition(tline->next, i);
2032 tline->next = NULL; /* it got freed */
2033 free_tlist(origline);
2034 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2035 }
2036 cond = nasm_malloc(sizeof(Cond));
2037 cond->next = istk->conds;
2038 cond->state = j;
2039 istk->conds = cond;
2040 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002041
H. Peter Anvine2c80182005-01-15 22:15:51 +00002042 case PP_ELIF:
2043 case PP_ELIFCTX:
2044 case PP_ELIFDEF:
2045 case PP_ELIFID:
2046 case PP_ELIFIDN:
2047 case PP_ELIFIDNI:
2048 case PP_ELIFMACRO:
2049 case PP_ELIFNCTX:
2050 case PP_ELIFNDEF:
2051 case PP_ELIFNID:
2052 case PP_ELIFNIDN:
2053 case PP_ELIFNIDNI:
2054 case PP_ELIFNMACRO:
2055 case PP_ELIFNNUM:
2056 case PP_ELIFNSTR:
2057 case PP_ELIFNUM:
2058 case PP_ELIFSTR:
2059 if (!istk->conds)
2060 error(ERR_FATAL, "`%s': no matching `%%if'", directives[i]);
2061 if (emitting(istk->conds->state)
2062 || istk->conds->state == COND_NEVER)
2063 istk->conds->state = COND_NEVER;
2064 else {
2065 /*
2066 * IMPORTANT: In the case of %if, we will already have
2067 * called expand_mmac_params(); however, if we're
2068 * processing an %elif we must have been in a
2069 * non-emitting mode, which would have inhibited
2070 * the normal invocation of expand_mmac_params(). Therefore,
2071 * we have to do it explicitly here.
2072 */
2073 j = if_condition(expand_mmac_params(tline->next), i);
2074 tline->next = NULL; /* it got freed */
2075 free_tlist(origline);
2076 istk->conds->state =
2077 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2078 }
2079 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002080
H. Peter Anvine2c80182005-01-15 22:15:51 +00002081 case PP_ELSE:
2082 if (tline->next)
2083 error(ERR_WARNING, "trailing garbage after `%%else' ignored");
2084 if (!istk->conds)
2085 error(ERR_FATAL, "`%%else': no matching `%%if'");
2086 if (emitting(istk->conds->state)
2087 || istk->conds->state == COND_NEVER)
2088 istk->conds->state = COND_ELSE_FALSE;
2089 else
2090 istk->conds->state = COND_ELSE_TRUE;
2091 free_tlist(origline);
2092 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002093
H. Peter Anvine2c80182005-01-15 22:15:51 +00002094 case PP_ENDIF:
2095 if (tline->next)
2096 error(ERR_WARNING, "trailing garbage after `%%endif' ignored");
2097 if (!istk->conds)
2098 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2099 cond = istk->conds;
2100 istk->conds = cond->next;
2101 nasm_free(cond);
2102 free_tlist(origline);
2103 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002104
H. Peter Anvine2c80182005-01-15 22:15:51 +00002105 case PP_MACRO:
2106 case PP_IMACRO:
2107 if (defining)
2108 error(ERR_FATAL,
2109 "`%%%smacro': already defining a macro",
2110 (i == PP_IMACRO ? "i" : ""));
2111 tline = tline->next;
2112 skip_white_(tline);
2113 tline = expand_id(tline);
2114 if (!tok_type_(tline, TOK_ID)) {
2115 error(ERR_NONFATAL,
2116 "`%%%smacro' expects a macro name",
2117 (i == PP_IMACRO ? "i" : ""));
2118 return DIRECTIVE_FOUND;
2119 }
2120 defining = nasm_malloc(sizeof(MMacro));
2121 defining->name = nasm_strdup(tline->text);
2122 defining->casesense = (i == PP_MACRO);
2123 defining->plus = FALSE;
2124 defining->nolist = FALSE;
2125 defining->in_progress = FALSE;
2126 defining->rep_nest = NULL;
2127 tline = expand_smacro(tline->next);
2128 skip_white_(tline);
2129 if (!tok_type_(tline, TOK_NUMBER)) {
2130 error(ERR_NONFATAL,
2131 "`%%%smacro' expects a parameter count",
2132 (i == PP_IMACRO ? "i" : ""));
2133 defining->nparam_min = defining->nparam_max = 0;
2134 } else {
2135 defining->nparam_min = defining->nparam_max =
2136 readnum(tline->text, &j);
2137 if (j)
2138 error(ERR_NONFATAL,
2139 "unable to parse parameter count `%s'", tline->text);
2140 }
2141 if (tline && tok_is_(tline->next, "-")) {
2142 tline = tline->next->next;
2143 if (tok_is_(tline, "*"))
2144 defining->nparam_max = INT_MAX;
2145 else if (!tok_type_(tline, TOK_NUMBER))
2146 error(ERR_NONFATAL,
2147 "`%%%smacro' expects a parameter count after `-'",
2148 (i == PP_IMACRO ? "i" : ""));
2149 else {
2150 defining->nparam_max = readnum(tline->text, &j);
2151 if (j)
2152 error(ERR_NONFATAL,
2153 "unable to parse parameter count `%s'",
2154 tline->text);
2155 if (defining->nparam_min > defining->nparam_max)
2156 error(ERR_NONFATAL,
2157 "minimum parameter count exceeds maximum");
2158 }
2159 }
2160 if (tline && tok_is_(tline->next, "+")) {
2161 tline = tline->next;
2162 defining->plus = TRUE;
2163 }
2164 if (tline && tok_type_(tline->next, TOK_ID) &&
2165 !nasm_stricmp(tline->next->text, ".nolist")) {
2166 tline = tline->next;
2167 defining->nolist = TRUE;
2168 }
2169 mmac = mmacros[hash(defining->name)];
2170 while (mmac) {
2171 if (!strcmp(mmac->name, defining->name) &&
2172 (mmac->nparam_min <= defining->nparam_max
2173 || defining->plus)
2174 && (defining->nparam_min <= mmac->nparam_max
2175 || mmac->plus)) {
2176 error(ERR_WARNING,
2177 "redefining multi-line macro `%s'", defining->name);
2178 break;
2179 }
2180 mmac = mmac->next;
2181 }
2182 /*
2183 * Handle default parameters.
2184 */
2185 if (tline && tline->next) {
2186 defining->dlist = tline->next;
2187 tline->next = NULL;
2188 count_mmac_params(defining->dlist, &defining->ndefs,
2189 &defining->defaults);
2190 } else {
2191 defining->dlist = NULL;
2192 defining->defaults = NULL;
2193 }
2194 defining->expansion = NULL;
2195 free_tlist(origline);
2196 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002197
H. Peter Anvine2c80182005-01-15 22:15:51 +00002198 case PP_ENDM:
2199 case PP_ENDMACRO:
2200 if (!defining) {
2201 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2202 return DIRECTIVE_FOUND;
2203 }
2204 k = hash(defining->name);
2205 defining->next = mmacros[k];
2206 mmacros[k] = defining;
2207 defining = NULL;
2208 free_tlist(origline);
2209 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002210
H. Peter Anvine2c80182005-01-15 22:15:51 +00002211 case PP_ROTATE:
2212 if (tline->next && tline->next->type == TOK_WHITESPACE)
2213 tline = tline->next;
2214 if (tline->next == NULL) {
2215 free_tlist(origline);
2216 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2217 return DIRECTIVE_FOUND;
2218 }
2219 t = expand_smacro(tline->next);
2220 tline->next = NULL;
2221 free_tlist(origline);
2222 tline = t;
2223 tptr = &t;
2224 tokval.t_type = TOKEN_INVALID;
2225 evalresult =
2226 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2227 free_tlist(tline);
2228 if (!evalresult)
2229 return DIRECTIVE_FOUND;
2230 if (tokval.t_type)
2231 error(ERR_WARNING,
2232 "trailing garbage after expression ignored");
2233 if (!is_simple(evalresult)) {
2234 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2235 return DIRECTIVE_FOUND;
2236 }
2237 mmac = istk->mstk;
2238 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2239 mmac = mmac->next_active;
2240 if (!mmac) {
2241 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2242 } else if (mmac->nparam == 0) {
2243 error(ERR_NONFATAL,
2244 "`%%rotate' invoked within macro without parameters");
2245 } else {
2246 mmac->rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002247
H. Peter Anvine2c80182005-01-15 22:15:51 +00002248 if (mmac->rotate < 0)
2249 mmac->rotate =
2250 mmac->nparam - (-mmac->rotate) % mmac->nparam;
2251 mmac->rotate %= mmac->nparam;
2252 }
2253 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002254
H. Peter Anvine2c80182005-01-15 22:15:51 +00002255 case PP_REP:
2256 nolist = FALSE;
2257 do {
2258 tline = tline->next;
2259 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002260
H. Peter Anvine2c80182005-01-15 22:15:51 +00002261 if (tok_type_(tline, TOK_ID) &&
2262 nasm_stricmp(tline->text, ".nolist") == 0) {
2263 nolist = TRUE;
2264 do {
2265 tline = tline->next;
2266 } while (tok_type_(tline, TOK_WHITESPACE));
2267 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002268
H. Peter Anvine2c80182005-01-15 22:15:51 +00002269 if (tline) {
2270 t = expand_smacro(tline);
2271 tptr = &t;
2272 tokval.t_type = TOKEN_INVALID;
2273 evalresult =
2274 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2275 if (!evalresult) {
2276 free_tlist(origline);
2277 return DIRECTIVE_FOUND;
2278 }
2279 if (tokval.t_type)
2280 error(ERR_WARNING,
2281 "trailing garbage after expression ignored");
2282 if (!is_simple(evalresult)) {
2283 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2284 return DIRECTIVE_FOUND;
2285 }
2286 i = (int)reloc_value(evalresult) + 1;
2287 } else {
2288 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
2289 i = 0;
2290 }
2291 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002292
H. Peter Anvine2c80182005-01-15 22:15:51 +00002293 tmp_defining = defining;
2294 defining = nasm_malloc(sizeof(MMacro));
2295 defining->name = NULL; /* flags this macro as a %rep block */
2296 defining->casesense = 0;
2297 defining->plus = FALSE;
2298 defining->nolist = nolist;
2299 defining->in_progress = i;
2300 defining->nparam_min = defining->nparam_max = 0;
2301 defining->defaults = NULL;
2302 defining->dlist = NULL;
2303 defining->expansion = NULL;
2304 defining->next_active = istk->mstk;
2305 defining->rep_nest = tmp_defining;
2306 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002307
H. Peter Anvine2c80182005-01-15 22:15:51 +00002308 case PP_ENDREP:
2309 if (!defining || defining->name) {
2310 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2311 return DIRECTIVE_FOUND;
2312 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002313
H. Peter Anvine2c80182005-01-15 22:15:51 +00002314 /*
2315 * Now we have a "macro" defined - although it has no name
2316 * and we won't be entering it in the hash tables - we must
2317 * push a macro-end marker for it on to istk->expansion.
2318 * After that, it will take care of propagating itself (a
2319 * macro-end marker line for a macro which is really a %rep
2320 * block will cause the macro to be re-expanded, complete
2321 * with another macro-end marker to ensure the process
2322 * continues) until the whole expansion is forcibly removed
2323 * from istk->expansion by a %exitrep.
2324 */
2325 l = nasm_malloc(sizeof(Line));
2326 l->next = istk->expansion;
2327 l->finishes = defining;
2328 l->first = NULL;
2329 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002330
H. Peter Anvine2c80182005-01-15 22:15:51 +00002331 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002332
H. Peter Anvine2c80182005-01-15 22:15:51 +00002333 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2334 tmp_defining = defining;
2335 defining = defining->rep_nest;
2336 free_tlist(origline);
2337 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002338
H. Peter Anvine2c80182005-01-15 22:15:51 +00002339 case PP_EXITREP:
2340 /*
2341 * We must search along istk->expansion until we hit a
2342 * macro-end marker for a macro with no name. Then we set
2343 * its `in_progress' flag to 0.
2344 */
2345 for (l = istk->expansion; l; l = l->next)
2346 if (l->finishes && !l->finishes->name)
2347 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002348
H. Peter Anvine2c80182005-01-15 22:15:51 +00002349 if (l)
2350 l->finishes->in_progress = 0;
2351 else
2352 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2353 free_tlist(origline);
2354 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002355
H. Peter Anvine2c80182005-01-15 22:15:51 +00002356 case PP_XDEFINE:
2357 case PP_IXDEFINE:
2358 case PP_DEFINE:
2359 case PP_IDEFINE:
2360 tline = tline->next;
2361 skip_white_(tline);
2362 tline = expand_id(tline);
2363 if (!tline || (tline->type != TOK_ID &&
2364 (tline->type != TOK_PREPROC_ID ||
2365 tline->text[1] != '$'))) {
2366 error(ERR_NONFATAL,
2367 "`%%%s%sdefine' expects a macro identifier",
2368 ((i == PP_IDEFINE || i == PP_IXDEFINE) ? "i" : ""),
2369 ((i == PP_XDEFINE || i == PP_IXDEFINE) ? "x" : ""));
2370 free_tlist(origline);
2371 return DIRECTIVE_FOUND;
2372 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002373
H. Peter Anvine2c80182005-01-15 22:15:51 +00002374 ctx = get_ctx(tline->text, FALSE);
2375 if (!ctx)
2376 smhead = &smacros[hash(tline->text)];
2377 else
2378 smhead = &ctx->localmac;
2379 mname = tline->text;
2380 last = tline;
2381 param_start = tline = tline->next;
2382 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002383
H. Peter Anvine2c80182005-01-15 22:15:51 +00002384 /* Expand the macro definition now for %xdefine and %ixdefine */
2385 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2386 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002387
H. Peter Anvine2c80182005-01-15 22:15:51 +00002388 if (tok_is_(tline, "(")) {
2389 /*
2390 * This macro has parameters.
2391 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002392
H. Peter Anvine2c80182005-01-15 22:15:51 +00002393 tline = tline->next;
2394 while (1) {
2395 skip_white_(tline);
2396 if (!tline) {
2397 error(ERR_NONFATAL, "parameter identifier expected");
2398 free_tlist(origline);
2399 return DIRECTIVE_FOUND;
2400 }
2401 if (tline->type != TOK_ID) {
2402 error(ERR_NONFATAL,
2403 "`%s': parameter identifier expected",
2404 tline->text);
2405 free_tlist(origline);
2406 return DIRECTIVE_FOUND;
2407 }
2408 tline->type = TOK_SMAC_PARAM + nparam++;
2409 tline = tline->next;
2410 skip_white_(tline);
2411 if (tok_is_(tline, ",")) {
2412 tline = tline->next;
2413 continue;
2414 }
2415 if (!tok_is_(tline, ")")) {
2416 error(ERR_NONFATAL,
2417 "`)' expected to terminate macro template");
2418 free_tlist(origline);
2419 return DIRECTIVE_FOUND;
2420 }
2421 break;
2422 }
2423 last = tline;
2424 tline = tline->next;
2425 }
2426 if (tok_type_(tline, TOK_WHITESPACE))
2427 last = tline, tline = tline->next;
2428 macro_start = NULL;
2429 last->next = NULL;
2430 t = tline;
2431 while (t) {
2432 if (t->type == TOK_ID) {
2433 for (tt = param_start; tt; tt = tt->next)
2434 if (tt->type >= TOK_SMAC_PARAM &&
2435 !strcmp(tt->text, t->text))
2436 t->type = tt->type;
2437 }
2438 tt = t->next;
2439 t->next = macro_start;
2440 macro_start = t;
2441 t = tt;
2442 }
2443 /*
2444 * Good. We now have a macro name, a parameter count, and a
2445 * token list (in reverse order) for an expansion. We ought
2446 * to be OK just to create an SMacro, store it, and let
2447 * free_tlist have the rest of the line (which we have
2448 * carefully re-terminated after chopping off the expansion
2449 * from the end).
2450 */
2451 if (smacro_defined(ctx, mname, nparam, &smac, i == PP_DEFINE)) {
2452 if (!smac) {
2453 error(ERR_WARNING,
2454 "single-line macro `%s' defined both with and"
2455 " without parameters", mname);
2456 free_tlist(origline);
2457 free_tlist(macro_start);
2458 return DIRECTIVE_FOUND;
2459 } else {
2460 /*
2461 * We're redefining, so we have to take over an
2462 * existing SMacro structure. This means freeing
2463 * what was already in it.
2464 */
2465 nasm_free(smac->name);
2466 free_tlist(smac->expansion);
2467 }
2468 } else {
2469 smac = nasm_malloc(sizeof(SMacro));
2470 smac->next = *smhead;
2471 *smhead = smac;
2472 }
2473 smac->name = nasm_strdup(mname);
2474 smac->casesense = ((i == PP_DEFINE) || (i == PP_XDEFINE));
2475 smac->nparam = nparam;
2476 smac->expansion = macro_start;
2477 smac->in_progress = FALSE;
2478 free_tlist(origline);
2479 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002480
H. Peter Anvine2c80182005-01-15 22:15:51 +00002481 case PP_UNDEF:
2482 tline = tline->next;
2483 skip_white_(tline);
2484 tline = expand_id(tline);
2485 if (!tline || (tline->type != TOK_ID &&
2486 (tline->type != TOK_PREPROC_ID ||
2487 tline->text[1] != '$'))) {
2488 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2489 free_tlist(origline);
2490 return DIRECTIVE_FOUND;
2491 }
2492 if (tline->next) {
2493 error(ERR_WARNING,
2494 "trailing garbage after macro name ignored");
2495 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002496
H. Peter Anvine2c80182005-01-15 22:15:51 +00002497 /* Find the context that symbol belongs to */
2498 ctx = get_ctx(tline->text, FALSE);
2499 if (!ctx)
2500 smhead = &smacros[hash(tline->text)];
2501 else
2502 smhead = &ctx->localmac;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002503
H. Peter Anvine2c80182005-01-15 22:15:51 +00002504 mname = tline->text;
2505 last = tline;
2506 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002507
H. Peter Anvine2c80182005-01-15 22:15:51 +00002508 /*
2509 * We now have a macro name... go hunt for it.
2510 */
2511 while (smacro_defined(ctx, mname, -1, &smac, 1)) {
2512 /* Defined, so we need to find its predecessor and nuke it */
2513 SMacro **s;
2514 for (s = smhead; *s && *s != smac; s = &(*s)->next) ;
2515 if (*s) {
2516 *s = smac->next;
2517 nasm_free(smac->name);
2518 free_tlist(smac->expansion);
2519 nasm_free(smac);
2520 }
2521 }
2522 free_tlist(origline);
2523 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002524
H. Peter Anvine2c80182005-01-15 22:15:51 +00002525 case PP_STRLEN:
2526 tline = tline->next;
2527 skip_white_(tline);
2528 tline = expand_id(tline);
2529 if (!tline || (tline->type != TOK_ID &&
2530 (tline->type != TOK_PREPROC_ID ||
2531 tline->text[1] != '$'))) {
2532 error(ERR_NONFATAL,
2533 "`%%strlen' expects a macro identifier as first parameter");
2534 free_tlist(origline);
2535 return DIRECTIVE_FOUND;
2536 }
2537 ctx = get_ctx(tline->text, FALSE);
2538 if (!ctx)
2539 smhead = &smacros[hash(tline->text)];
2540 else
2541 smhead = &ctx->localmac;
2542 mname = tline->text;
2543 last = tline;
2544 tline = expand_smacro(tline->next);
2545 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002546
H. Peter Anvine2c80182005-01-15 22:15:51 +00002547 t = tline;
2548 while (tok_type_(t, TOK_WHITESPACE))
2549 t = t->next;
2550 /* t should now point to the string */
2551 if (t->type != TOK_STRING) {
2552 error(ERR_NONFATAL,
2553 "`%%strlen` requires string as second parameter");
2554 free_tlist(tline);
2555 free_tlist(origline);
2556 return DIRECTIVE_FOUND;
2557 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002558
H. Peter Anvine2c80182005-01-15 22:15:51 +00002559 macro_start = nasm_malloc(sizeof(*macro_start));
2560 macro_start->next = NULL;
2561 make_tok_num(macro_start, strlen(t->text) - 2);
2562 macro_start->mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002563
H. Peter Anvine2c80182005-01-15 22:15:51 +00002564 /*
2565 * We now have a macro name, an implicit parameter count of
2566 * zero, and a numeric token to use as an expansion. Create
2567 * and store an SMacro.
2568 */
2569 if (smacro_defined(ctx, mname, 0, &smac, i == PP_STRLEN)) {
2570 if (!smac)
2571 error(ERR_WARNING,
2572 "single-line macro `%s' defined both with and"
2573 " without parameters", mname);
2574 else {
2575 /*
2576 * We're redefining, so we have to take over an
2577 * existing SMacro structure. This means freeing
2578 * what was already in it.
2579 */
2580 nasm_free(smac->name);
2581 free_tlist(smac->expansion);
2582 }
2583 } else {
2584 smac = nasm_malloc(sizeof(SMacro));
2585 smac->next = *smhead;
2586 *smhead = smac;
2587 }
2588 smac->name = nasm_strdup(mname);
2589 smac->casesense = (i == PP_STRLEN);
2590 smac->nparam = 0;
2591 smac->expansion = macro_start;
2592 smac->in_progress = FALSE;
2593 free_tlist(tline);
2594 free_tlist(origline);
2595 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002596
H. Peter Anvine2c80182005-01-15 22:15:51 +00002597 case PP_SUBSTR:
2598 tline = tline->next;
2599 skip_white_(tline);
2600 tline = expand_id(tline);
2601 if (!tline || (tline->type != TOK_ID &&
2602 (tline->type != TOK_PREPROC_ID ||
2603 tline->text[1] != '$'))) {
2604 error(ERR_NONFATAL,
2605 "`%%substr' expects a macro identifier as first parameter");
2606 free_tlist(origline);
2607 return DIRECTIVE_FOUND;
2608 }
2609 ctx = get_ctx(tline->text, FALSE);
2610 if (!ctx)
2611 smhead = &smacros[hash(tline->text)];
2612 else
2613 smhead = &ctx->localmac;
2614 mname = tline->text;
2615 last = tline;
2616 tline = expand_smacro(tline->next);
2617 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002618
H. Peter Anvine2c80182005-01-15 22:15:51 +00002619 t = tline->next;
2620 while (tok_type_(t, TOK_WHITESPACE))
2621 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002622
H. Peter Anvine2c80182005-01-15 22:15:51 +00002623 /* t should now point to the string */
2624 if (t->type != TOK_STRING) {
2625 error(ERR_NONFATAL,
2626 "`%%substr` requires string as second parameter");
2627 free_tlist(tline);
2628 free_tlist(origline);
2629 return DIRECTIVE_FOUND;
2630 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002631
H. Peter Anvine2c80182005-01-15 22:15:51 +00002632 tt = t->next;
2633 tptr = &tt;
2634 tokval.t_type = TOKEN_INVALID;
2635 evalresult =
2636 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2637 if (!evalresult) {
2638 free_tlist(tline);
2639 free_tlist(origline);
2640 return DIRECTIVE_FOUND;
2641 }
2642 if (!is_simple(evalresult)) {
2643 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
2644 free_tlist(tline);
2645 free_tlist(origline);
2646 return DIRECTIVE_FOUND;
2647 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002648
H. Peter Anvine2c80182005-01-15 22:15:51 +00002649 macro_start = nasm_malloc(sizeof(*macro_start));
2650 macro_start->next = NULL;
2651 macro_start->text = nasm_strdup("'''");
2652 if (evalresult->value > 0
2653 && evalresult->value < strlen(t->text) - 1) {
2654 macro_start->text[1] = t->text[evalresult->value];
2655 } else {
2656 macro_start->text[2] = '\0';
2657 }
2658 macro_start->type = TOK_STRING;
2659 macro_start->mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002660
H. Peter Anvine2c80182005-01-15 22:15:51 +00002661 /*
2662 * We now have a macro name, an implicit parameter count of
2663 * zero, and a numeric token to use as an expansion. Create
2664 * and store an SMacro.
2665 */
2666 if (smacro_defined(ctx, mname, 0, &smac, i == PP_SUBSTR)) {
2667 if (!smac)
2668 error(ERR_WARNING,
2669 "single-line macro `%s' defined both with and"
2670 " without parameters", mname);
2671 else {
2672 /*
2673 * We're redefining, so we have to take over an
2674 * existing SMacro structure. This means freeing
2675 * what was already in it.
2676 */
2677 nasm_free(smac->name);
2678 free_tlist(smac->expansion);
2679 }
2680 } else {
2681 smac = nasm_malloc(sizeof(SMacro));
2682 smac->next = *smhead;
2683 *smhead = smac;
2684 }
2685 smac->name = nasm_strdup(mname);
2686 smac->casesense = (i == PP_SUBSTR);
2687 smac->nparam = 0;
2688 smac->expansion = macro_start;
2689 smac->in_progress = FALSE;
2690 free_tlist(tline);
2691 free_tlist(origline);
2692 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002693
H. Peter Anvine2c80182005-01-15 22:15:51 +00002694 case PP_ASSIGN:
2695 case PP_IASSIGN:
2696 tline = tline->next;
2697 skip_white_(tline);
2698 tline = expand_id(tline);
2699 if (!tline || (tline->type != TOK_ID &&
2700 (tline->type != TOK_PREPROC_ID ||
2701 tline->text[1] != '$'))) {
2702 error(ERR_NONFATAL,
2703 "`%%%sassign' expects a macro identifier",
2704 (i == PP_IASSIGN ? "i" : ""));
2705 free_tlist(origline);
2706 return DIRECTIVE_FOUND;
2707 }
2708 ctx = get_ctx(tline->text, FALSE);
2709 if (!ctx)
2710 smhead = &smacros[hash(tline->text)];
2711 else
2712 smhead = &ctx->localmac;
2713 mname = tline->text;
2714 last = tline;
2715 tline = expand_smacro(tline->next);
2716 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002717
H. Peter Anvine2c80182005-01-15 22:15:51 +00002718 t = tline;
2719 tptr = &t;
2720 tokval.t_type = TOKEN_INVALID;
2721 evalresult =
2722 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2723 free_tlist(tline);
2724 if (!evalresult) {
2725 free_tlist(origline);
2726 return DIRECTIVE_FOUND;
2727 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002728
H. Peter Anvine2c80182005-01-15 22:15:51 +00002729 if (tokval.t_type)
2730 error(ERR_WARNING,
2731 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00002732
H. Peter Anvine2c80182005-01-15 22:15:51 +00002733 if (!is_simple(evalresult)) {
2734 error(ERR_NONFATAL,
2735 "non-constant value given to `%%%sassign'",
2736 (i == PP_IASSIGN ? "i" : ""));
2737 free_tlist(origline);
2738 return DIRECTIVE_FOUND;
2739 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002740
H. Peter Anvine2c80182005-01-15 22:15:51 +00002741 macro_start = nasm_malloc(sizeof(*macro_start));
2742 macro_start->next = NULL;
2743 make_tok_num(macro_start, reloc_value(evalresult));
2744 macro_start->mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002745
H. Peter Anvine2c80182005-01-15 22:15:51 +00002746 /*
2747 * We now have a macro name, an implicit parameter count of
2748 * zero, and a numeric token to use as an expansion. Create
2749 * and store an SMacro.
2750 */
2751 if (smacro_defined(ctx, mname, 0, &smac, i == PP_ASSIGN)) {
2752 if (!smac)
2753 error(ERR_WARNING,
2754 "single-line macro `%s' defined both with and"
2755 " without parameters", mname);
2756 else {
2757 /*
2758 * We're redefining, so we have to take over an
2759 * existing SMacro structure. This means freeing
2760 * what was already in it.
2761 */
2762 nasm_free(smac->name);
2763 free_tlist(smac->expansion);
2764 }
2765 } else {
2766 smac = nasm_malloc(sizeof(SMacro));
2767 smac->next = *smhead;
2768 *smhead = smac;
2769 }
2770 smac->name = nasm_strdup(mname);
2771 smac->casesense = (i == PP_ASSIGN);
2772 smac->nparam = 0;
2773 smac->expansion = macro_start;
2774 smac->in_progress = FALSE;
2775 free_tlist(origline);
2776 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002777
H. Peter Anvine2c80182005-01-15 22:15:51 +00002778 case PP_LINE:
2779 /*
2780 * Syntax is `%line nnn[+mmm] [filename]'
2781 */
2782 tline = tline->next;
2783 skip_white_(tline);
2784 if (!tok_type_(tline, TOK_NUMBER)) {
2785 error(ERR_NONFATAL, "`%%line' expects line number");
2786 free_tlist(origline);
2787 return DIRECTIVE_FOUND;
2788 }
2789 k = readnum(tline->text, &j);
2790 m = 1;
2791 tline = tline->next;
2792 if (tok_is_(tline, "+")) {
2793 tline = tline->next;
2794 if (!tok_type_(tline, TOK_NUMBER)) {
2795 error(ERR_NONFATAL, "`%%line' expects line increment");
2796 free_tlist(origline);
2797 return DIRECTIVE_FOUND;
2798 }
2799 m = readnum(tline->text, &j);
2800 tline = tline->next;
2801 }
2802 skip_white_(tline);
2803 src_set_linnum(k);
2804 istk->lineinc = m;
2805 if (tline) {
2806 nasm_free(src_set_fname(detoken(tline, FALSE)));
2807 }
2808 free_tlist(origline);
2809 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002810
H. Peter Anvine2c80182005-01-15 22:15:51 +00002811 default:
2812 error(ERR_FATAL,
2813 "preprocessor directive `%s' not yet implemented",
2814 directives[i]);
2815 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002816 }
Ed Beroset3ab3f412002-06-11 03:31:49 +00002817 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002818}
2819
2820/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002821 * Ensure that a macro parameter contains a condition code and
2822 * nothing else. Return the condition code index if so, or -1
2823 * otherwise.
2824 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002825static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002826{
H. Peter Anvin76690a12002-04-30 20:52:49 +00002827 Token *tt;
2828 int i, j, k, m;
2829
H. Peter Anvineba20a72002-04-30 20:53:55 +00002830 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002831 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002832 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002833 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002834 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002835 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002836 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002837
2838 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +00002839 j = elements(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002840 while (j - i > 1) {
2841 k = (j + i) / 2;
2842 m = nasm_stricmp(t->text, conditions[k]);
2843 if (m == 0) {
2844 i = k;
2845 j = -2;
2846 break;
2847 } else if (m < 0) {
2848 j = k;
2849 } else
2850 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002851 }
2852 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002853 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002854 return i;
2855}
2856
2857/*
2858 * Expand MMacro-local things: parameter references (%0, %n, %+n,
2859 * %-n) and MMacro-local identifiers (%%foo).
2860 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002861static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002862{
H. Peter Anvin734b1882002-04-30 21:01:08 +00002863 Token *t, *tt, **tail, *thead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002864
2865 tail = &thead;
2866 thead = NULL;
2867
H. Peter Anvine2c80182005-01-15 22:15:51 +00002868 while (tline) {
2869 if (tline->type == TOK_PREPROC_ID &&
2870 (((tline->text[1] == '+' || tline->text[1] == '-')
2871 && tline->text[2]) || tline->text[1] == '%'
2872 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002873 int8_t *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002874 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002875 int8_t tmpbuf[30];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002876 int n, i;
2877 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002878
H. Peter Anvine2c80182005-01-15 22:15:51 +00002879 t = tline;
2880 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002881
H. Peter Anvine2c80182005-01-15 22:15:51 +00002882 mac = istk->mstk;
2883 while (mac && !mac->name) /* avoid mistaking %reps for macros */
2884 mac = mac->next_active;
2885 if (!mac)
2886 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
2887 else
2888 switch (t->text[1]) {
2889 /*
2890 * We have to make a substitution of one of the
2891 * forms %1, %-1, %+1, %%foo, %0.
2892 */
2893 case '0':
2894 type = TOK_NUMBER;
2895 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
2896 text = nasm_strdup(tmpbuf);
2897 break;
2898 case '%':
2899 type = TOK_ID;
2900 snprintf(tmpbuf, sizeof(tmpbuf), "..@%lu.",
2901 mac->unique);
2902 text = nasm_strcat(tmpbuf, t->text + 2);
2903 break;
2904 case '-':
2905 n = atoi(t->text + 2) - 1;
2906 if (n >= mac->nparam)
2907 tt = NULL;
2908 else {
2909 if (mac->nparam > 1)
2910 n = (n + mac->rotate) % mac->nparam;
2911 tt = mac->params[n];
2912 }
2913 cc = find_cc(tt);
2914 if (cc == -1) {
2915 error(ERR_NONFATAL,
2916 "macro parameter %d is not a condition code",
2917 n + 1);
2918 text = NULL;
2919 } else {
2920 type = TOK_ID;
2921 if (inverse_ccs[cc] == -1) {
2922 error(ERR_NONFATAL,
2923 "condition code `%s' is not invertible",
2924 conditions[cc]);
2925 text = NULL;
2926 } else
2927 text =
2928 nasm_strdup(conditions[inverse_ccs[cc]]);
2929 }
2930 break;
2931 case '+':
2932 n = atoi(t->text + 2) - 1;
2933 if (n >= mac->nparam)
2934 tt = NULL;
2935 else {
2936 if (mac->nparam > 1)
2937 n = (n + mac->rotate) % mac->nparam;
2938 tt = mac->params[n];
2939 }
2940 cc = find_cc(tt);
2941 if (cc == -1) {
2942 error(ERR_NONFATAL,
2943 "macro parameter %d is not a condition code",
2944 n + 1);
2945 text = NULL;
2946 } else {
2947 type = TOK_ID;
2948 text = nasm_strdup(conditions[cc]);
2949 }
2950 break;
2951 default:
2952 n = atoi(t->text + 1) - 1;
2953 if (n >= mac->nparam)
2954 tt = NULL;
2955 else {
2956 if (mac->nparam > 1)
2957 n = (n + mac->rotate) % mac->nparam;
2958 tt = mac->params[n];
2959 }
2960 if (tt) {
2961 for (i = 0; i < mac->paramlen[n]; i++) {
2962 *tail = new_Token(NULL, tt->type, tt->text, 0);
2963 tail = &(*tail)->next;
2964 tt = tt->next;
2965 }
2966 }
2967 text = NULL; /* we've done it here */
2968 break;
2969 }
2970 if (!text) {
2971 delete_Token(t);
2972 } else {
2973 *tail = t;
2974 tail = &t->next;
2975 t->type = type;
2976 nasm_free(t->text);
2977 t->text = text;
2978 t->mac = NULL;
2979 }
2980 continue;
2981 } else {
2982 t = *tail = tline;
2983 tline = tline->next;
2984 t->mac = NULL;
2985 tail = &t->next;
2986 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002987 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00002988 *tail = NULL;
2989 t = thead;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002990 for (; t && (tt = t->next) != NULL; t = t->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002991 switch (t->type) {
2992 case TOK_WHITESPACE:
2993 if (tt->type == TOK_WHITESPACE) {
2994 t->next = delete_Token(tt);
2995 }
2996 break;
2997 case TOK_ID:
2998 if (tt->type == TOK_ID || tt->type == TOK_NUMBER) {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002999 int8_t *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003000 nasm_free(t->text);
3001 t->text = tmp;
3002 t->next = delete_Token(tt);
3003 }
3004 break;
3005 case TOK_NUMBER:
3006 if (tt->type == TOK_NUMBER) {
Keith Kaniosb7a89542007-04-12 02:40:54 +00003007 int8_t *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003008 nasm_free(t->text);
3009 t->text = tmp;
3010 t->next = delete_Token(tt);
3011 }
3012 break;
3013 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003014
H. Peter Anvin76690a12002-04-30 20:52:49 +00003015 return thead;
3016}
3017
3018/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003019 * Expand all single-line macro calls made in the given line.
3020 * Return the expanded version of the line. The original is deemed
3021 * to be destroyed in the process. (In reality we'll just move
3022 * Tokens from input to output a lot of the time, rather than
3023 * actually bothering to destroy and replicate.)
3024 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003025static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003026{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003027 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003028 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003029 Token **params;
3030 int *paramsize;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003031 int nparam, sparam, brackets, rescan;
3032 Token *org_tline = tline;
3033 Context *ctx;
Keith Kaniosb7a89542007-04-12 02:40:54 +00003034 int8_t *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003035
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003036 /*
3037 * Trick: we should avoid changing the start token pointer since it can
3038 * be contained in "next" field of other token. Because of this
3039 * we allocate a copy of first token and work with it; at the end of
3040 * routine we copy it back
3041 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003042 if (org_tline) {
3043 tline =
3044 new_Token(org_tline->next, org_tline->type, org_tline->text,
3045 0);
3046 tline->mac = org_tline->mac;
3047 nasm_free(org_tline->text);
3048 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003049 }
3050
H. Peter Anvin734b1882002-04-30 21:01:08 +00003051 again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003052 tail = &thead;
3053 thead = NULL;
3054
H. Peter Anvine2c80182005-01-15 22:15:51 +00003055 while (tline) { /* main token loop */
3056 if ((mname = tline->text)) {
3057 /* if this token is a local macro, look in local context */
3058 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID)
3059 ctx = get_ctx(mname, TRUE);
3060 else
3061 ctx = NULL;
3062 if (!ctx)
3063 head = smacros[hash(mname)];
3064 else
3065 head = ctx->localmac;
3066 /*
3067 * We've hit an identifier. As in is_mmacro below, we first
3068 * check whether the identifier is a single-line macro at
3069 * all, then think about checking for parameters if
3070 * necessary.
3071 */
3072 for (m = head; m; m = m->next)
3073 if (!mstrcmp(m->name, mname, m->casesense))
3074 break;
3075 if (m) {
3076 mstart = tline;
3077 params = NULL;
3078 paramsize = NULL;
3079 if (m->nparam == 0) {
3080 /*
3081 * Simple case: the macro is parameterless. Discard the
3082 * one token that the macro call took, and push the
3083 * expansion back on the to-do stack.
3084 */
3085 if (!m->expansion) {
3086 if (!strcmp("__FILE__", m->name)) {
Keith Kaniosb7a89542007-04-12 02:40:54 +00003087 int32_t num = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003088 src_get(&num, &(tline->text));
3089 nasm_quote(&(tline->text));
3090 tline->type = TOK_STRING;
3091 continue;
3092 }
3093 if (!strcmp("__LINE__", m->name)) {
3094 nasm_free(tline->text);
3095 make_tok_num(tline, src_get_linnum());
3096 continue;
3097 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00003098 if (!strcmp("__BITS__", m->name)) {
3099 nasm_free(tline->text);
3100 make_tok_num(tline, globalbits);
3101 continue;
3102 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003103 tline = delete_Token(tline);
3104 continue;
3105 }
3106 } else {
3107 /*
3108 * Complicated case: at least one macro with this name
3109 * exists and takes parameters. We must find the
3110 * parameters in the call, count them, find the SMacro
3111 * that corresponds to that form of the macro call, and
3112 * substitute for the parameters when we expand. What a
3113 * pain.
3114 */
3115 /*tline = tline->next;
3116 skip_white_(tline); */
3117 do {
3118 t = tline->next;
3119 while (tok_type_(t, TOK_SMAC_END)) {
3120 t->mac->in_progress = FALSE;
3121 t->text = NULL;
3122 t = tline->next = delete_Token(t);
3123 }
3124 tline = t;
3125 } while (tok_type_(tline, TOK_WHITESPACE));
3126 if (!tok_is_(tline, "(")) {
3127 /*
3128 * This macro wasn't called with parameters: ignore
3129 * the call. (Behaviour borrowed from gnu cpp.)
3130 */
3131 tline = mstart;
3132 m = NULL;
3133 } else {
3134 int paren = 0;
3135 int white = 0;
3136 brackets = 0;
3137 nparam = 0;
3138 sparam = PARAM_DELTA;
3139 params = nasm_malloc(sparam * sizeof(Token *));
3140 params[0] = tline->next;
3141 paramsize = nasm_malloc(sparam * sizeof(int));
3142 paramsize[0] = 0;
3143 while (TRUE) { /* parameter loop */
3144 /*
3145 * For some unusual expansions
3146 * which concatenates function call
3147 */
3148 t = tline->next;
3149 while (tok_type_(t, TOK_SMAC_END)) {
3150 t->mac->in_progress = FALSE;
3151 t->text = NULL;
3152 t = tline->next = delete_Token(t);
3153 }
3154 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003155
H. Peter Anvine2c80182005-01-15 22:15:51 +00003156 if (!tline) {
3157 error(ERR_NONFATAL,
3158 "macro call expects terminating `)'");
3159 break;
3160 }
3161 if (tline->type == TOK_WHITESPACE
3162 && brackets <= 0) {
3163 if (paramsize[nparam])
3164 white++;
3165 else
3166 params[nparam] = tline->next;
3167 continue; /* parameter loop */
3168 }
3169 if (tline->type == TOK_OTHER
3170 && tline->text[1] == 0) {
Keith Kaniosb7a89542007-04-12 02:40:54 +00003171 int8_t ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003172 if (ch == ',' && !paren && brackets <= 0) {
3173 if (++nparam >= sparam) {
3174 sparam += PARAM_DELTA;
3175 params = nasm_realloc(params,
3176 sparam *
3177 sizeof(Token
3178 *));
3179 paramsize =
3180 nasm_realloc(paramsize,
3181 sparam *
3182 sizeof(int));
3183 }
3184 params[nparam] = tline->next;
3185 paramsize[nparam] = 0;
3186 white = 0;
3187 continue; /* parameter loop */
3188 }
3189 if (ch == '{' &&
3190 (brackets > 0 || (brackets == 0 &&
3191 !paramsize[nparam])))
3192 {
3193 if (!(brackets++)) {
3194 params[nparam] = tline->next;
3195 continue; /* parameter loop */
3196 }
3197 }
3198 if (ch == '}' && brackets > 0)
3199 if (--brackets == 0) {
3200 brackets = -1;
3201 continue; /* parameter loop */
3202 }
3203 if (ch == '(' && !brackets)
3204 paren++;
3205 if (ch == ')' && brackets <= 0)
3206 if (--paren < 0)
3207 break;
3208 }
3209 if (brackets < 0) {
3210 brackets = 0;
3211 error(ERR_NONFATAL, "braces do not "
3212 "enclose all of macro parameter");
3213 }
3214 paramsize[nparam] += white + 1;
3215 white = 0;
3216 } /* parameter loop */
3217 nparam++;
3218 while (m && (m->nparam != nparam ||
3219 mstrcmp(m->name, mname,
3220 m->casesense)))
3221 m = m->next;
3222 if (!m)
3223 error(ERR_WARNING | ERR_WARN_MNP,
3224 "macro `%s' exists, "
3225 "but not taking %d parameters",
3226 mstart->text, nparam);
3227 }
3228 }
3229 if (m && m->in_progress)
3230 m = NULL;
3231 if (!m) { /* in progess or didn't find '(' or wrong nparam */
3232 /*
3233 * Design question: should we handle !tline, which
3234 * indicates missing ')' here, or expand those
3235 * macros anyway, which requires the (t) test a few
3236 * lines down?
3237 */
3238 nasm_free(params);
3239 nasm_free(paramsize);
3240 tline = mstart;
3241 } else {
3242 /*
3243 * Expand the macro: we are placed on the last token of the
3244 * call, so that we can easily split the call from the
3245 * following tokens. We also start by pushing an SMAC_END
3246 * token for the cycle removal.
3247 */
3248 t = tline;
3249 if (t) {
3250 tline = t->next;
3251 t->next = NULL;
3252 }
3253 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
3254 tt->mac = m;
3255 m->in_progress = TRUE;
3256 tline = tt;
3257 for (t = m->expansion; t; t = t->next) {
3258 if (t->type >= TOK_SMAC_PARAM) {
3259 Token *pcopy = tline, **ptail = &pcopy;
3260 Token *ttt, *pt;
3261 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003262
H. Peter Anvine2c80182005-01-15 22:15:51 +00003263 ttt = params[t->type - TOK_SMAC_PARAM];
3264 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3265 --i >= 0;) {
3266 pt = *ptail =
3267 new_Token(tline, ttt->type, ttt->text,
3268 0);
3269 ptail = &pt->next;
3270 ttt = ttt->next;
3271 }
3272 tline = pcopy;
3273 } else {
3274 tt = new_Token(tline, t->type, t->text, 0);
3275 tline = tt;
3276 }
3277 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003278
H. Peter Anvine2c80182005-01-15 22:15:51 +00003279 /*
3280 * Having done that, get rid of the macro call, and clean
3281 * up the parameters.
3282 */
3283 nasm_free(params);
3284 nasm_free(paramsize);
3285 free_tlist(mstart);
3286 continue; /* main token loop */
3287 }
3288 }
3289 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003290
H. Peter Anvine2c80182005-01-15 22:15:51 +00003291 if (tline->type == TOK_SMAC_END) {
3292 tline->mac->in_progress = FALSE;
3293 tline = delete_Token(tline);
3294 } else {
3295 t = *tail = tline;
3296 tline = tline->next;
3297 t->mac = NULL;
3298 t->next = NULL;
3299 tail = &t->next;
3300 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003301 }
3302
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003303 /*
3304 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00003305 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003306 * TOK_IDs should be concatenated.
3307 * Also we look for %+ tokens and concatenate the tokens before and after
3308 * them (without white spaces in between).
3309 */
3310 t = thead;
3311 rescan = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003312 while (t) {
3313 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3314 t = t->next;
3315 if (!t || !t->next)
3316 break;
3317 if (t->next->type == TOK_ID ||
3318 t->next->type == TOK_PREPROC_ID ||
3319 t->next->type == TOK_NUMBER) {
Keith Kaniosb7a89542007-04-12 02:40:54 +00003320 int8_t *p = nasm_strcat(t->text, t->next->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003321 nasm_free(t->text);
3322 t->next = delete_Token(t->next);
3323 t->text = p;
3324 rescan = 1;
3325 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3326 t->next->next->type == TOK_PREPROC_ID &&
3327 strcmp(t->next->next->text, "%+") == 0) {
3328 /* free the next whitespace, the %+ token and next whitespace */
3329 int i;
3330 for (i = 1; i <= 3; i++) {
3331 if (!t->next
3332 || (i != 2 && t->next->type != TOK_WHITESPACE))
3333 break;
3334 t->next = delete_Token(t->next);
3335 } /* endfor */
3336 } else
3337 t = t->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003338 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003339 /* If we concatenaded something, re-scan the line for macros */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003340 if (rescan) {
3341 tline = thead;
3342 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003343 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003344
H. Peter Anvine2c80182005-01-15 22:15:51 +00003345 if (org_tline) {
3346 if (thead) {
3347 *org_tline = *thead;
3348 /* since we just gave text to org_line, don't free it */
3349 thead->text = NULL;
3350 delete_Token(thead);
3351 } else {
3352 /* the expression expanded to empty line;
3353 we can't return NULL for some reasons
3354 we just set the line to a single WHITESPACE token. */
3355 memset(org_tline, 0, sizeof(*org_tline));
3356 org_tline->text = NULL;
3357 org_tline->type = TOK_WHITESPACE;
3358 }
3359 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003360 }
3361
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003362 return thead;
3363}
3364
3365/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003366 * Similar to expand_smacro but used exclusively with macro identifiers
3367 * right before they are fetched in. The reason is that there can be
3368 * identifiers consisting of several subparts. We consider that if there
3369 * are more than one element forming the name, user wants a expansion,
3370 * otherwise it will be left as-is. Example:
3371 *
3372 * %define %$abc cde
3373 *
3374 * the identifier %$abc will be left as-is so that the handler for %define
3375 * will suck it and define the corresponding value. Other case:
3376 *
3377 * %define _%$abc cde
3378 *
3379 * In this case user wants name to be expanded *before* %define starts
3380 * working, so we'll expand %$abc into something (if it has a value;
3381 * otherwise it will be left as-is) then concatenate all successive
3382 * PP_IDs into one.
3383 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003384static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003385{
3386 Token *cur, *oldnext = NULL;
3387
H. Peter Anvin734b1882002-04-30 21:01:08 +00003388 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003389 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003390
3391 cur = tline;
3392 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003393 (cur->next->type == TOK_ID ||
3394 cur->next->type == TOK_PREPROC_ID
3395 || cur->next->type == TOK_NUMBER))
3396 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003397
3398 /* If identifier consists of just one token, don't expand */
3399 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003400 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003401
H. Peter Anvine2c80182005-01-15 22:15:51 +00003402 if (cur) {
3403 oldnext = cur->next; /* Detach the tail past identifier */
3404 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003405 }
3406
H. Peter Anvin734b1882002-04-30 21:01:08 +00003407 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003408
H. Peter Anvine2c80182005-01-15 22:15:51 +00003409 if (cur) {
3410 /* expand_smacro possibly changhed tline; re-scan for EOL */
3411 cur = tline;
3412 while (cur && cur->next)
3413 cur = cur->next;
3414 if (cur)
3415 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003416 }
3417
3418 return tline;
3419}
3420
3421/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003422 * Determine whether the given line constitutes a multi-line macro
3423 * call, and return the MMacro structure called if so. Doesn't have
3424 * to check for an initial label - that's taken care of in
3425 * expand_mmacro - but must check numbers of parameters. Guaranteed
3426 * to be called with tline->type == TOK_ID, so the putative macro
3427 * name is easy to find.
3428 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003429static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003430{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003431 MMacro *head, *m;
3432 Token **params;
3433 int nparam;
3434
3435 head = mmacros[hash(tline->text)];
3436
3437 /*
3438 * Efficiency: first we see if any macro exists with the given
3439 * name. If not, we can return NULL immediately. _Then_ we
3440 * count the parameters, and then we look further along the
3441 * list if necessary to find the proper MMacro.
3442 */
3443 for (m = head; m; m = m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003444 if (!mstrcmp(m->name, tline->text, m->casesense))
3445 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003446 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003447 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003448
3449 /*
3450 * OK, we have a potential macro. Count and demarcate the
3451 * parameters.
3452 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003453 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003454
3455 /*
3456 * So we know how many parameters we've got. Find the MMacro
3457 * structure that handles this number.
3458 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003459 while (m) {
3460 if (m->nparam_min <= nparam
3461 && (m->plus || nparam <= m->nparam_max)) {
3462 /*
3463 * This one is right. Just check if cycle removal
3464 * prohibits us using it before we actually celebrate...
3465 */
3466 if (m->in_progress) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003467#if 0
H. Peter Anvine2c80182005-01-15 22:15:51 +00003468 error(ERR_NONFATAL,
3469 "self-reference in multi-line macro `%s'", m->name);
H. Peter Anvineba20a72002-04-30 20:53:55 +00003470#endif
H. Peter Anvine2c80182005-01-15 22:15:51 +00003471 nasm_free(params);
3472 return NULL;
3473 }
3474 /*
3475 * It's right, and we can use it. Add its default
3476 * parameters to the end of our list if necessary.
3477 */
3478 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
3479 params =
3480 nasm_realloc(params,
3481 ((m->nparam_min + m->ndefs +
3482 1) * sizeof(*params)));
3483 while (nparam < m->nparam_min + m->ndefs) {
3484 params[nparam] = m->defaults[nparam - m->nparam_min];
3485 nparam++;
3486 }
3487 }
3488 /*
3489 * If we've gone over the maximum parameter count (and
3490 * we're in Plus mode), ignore parameters beyond
3491 * nparam_max.
3492 */
3493 if (m->plus && nparam > m->nparam_max)
3494 nparam = m->nparam_max;
3495 /*
3496 * Then terminate the parameter list, and leave.
3497 */
3498 if (!params) { /* need this special case */
3499 params = nasm_malloc(sizeof(*params));
3500 nparam = 0;
3501 }
3502 params[nparam] = NULL;
3503 *params_array = params;
3504 return m;
3505 }
3506 /*
3507 * This one wasn't right: look for the next one with the
3508 * same name.
3509 */
3510 for (m = m->next; m; m = m->next)
3511 if (!mstrcmp(m->name, tline->text, m->casesense))
3512 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003513 }
3514
3515 /*
3516 * After all that, we didn't find one with the right number of
3517 * parameters. Issue a warning, and fail to expand the macro.
3518 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003519 error(ERR_WARNING | ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003520 "macro `%s' exists, but not taking %d parameters",
3521 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003522 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003523 return NULL;
3524}
3525
3526/*
3527 * Expand the multi-line macro call made by the given line, if
3528 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00003529 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003530 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003531static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003532{
3533 Token *startline = tline;
3534 Token *label = NULL;
3535 int dont_prepend = 0;
3536 Token **params, *t, *tt;
3537 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003538 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003539 int i, nparam, *paramlen;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003540
3541 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003542 skip_white_(t);
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00003543/* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
3544 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003545 return 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003546 m = is_mmacro(t, &params);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003547 if (!m) {
3548 Token *last;
3549 /*
3550 * We have an id which isn't a macro call. We'll assume
3551 * it might be a label; we'll also check to see if a
3552 * colon follows it. Then, if there's another id after
3553 * that lot, we'll check it again for macro-hood.
3554 */
3555 label = last = t;
3556 t = t->next;
3557 if (tok_type_(t, TOK_WHITESPACE))
3558 last = t, t = t->next;
3559 if (tok_is_(t, ":")) {
3560 dont_prepend = 1;
3561 last = t, t = t->next;
3562 if (tok_type_(t, TOK_WHITESPACE))
3563 last = t, t = t->next;
3564 }
3565 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
3566 return 0;
3567 last->next = NULL;
3568 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003569 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003570
3571 /*
3572 * Fix up the parameters: this involves stripping leading and
3573 * trailing whitespace, then stripping braces if they are
3574 * present.
3575 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003576 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003577 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003578
H. Peter Anvine2c80182005-01-15 22:15:51 +00003579 for (i = 0; params[i]; i++) {
3580 int brace = FALSE;
3581 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003582
H. Peter Anvine2c80182005-01-15 22:15:51 +00003583 t = params[i];
3584 skip_white_(t);
3585 if (tok_is_(t, "{"))
3586 t = t->next, brace = TRUE, comma = FALSE;
3587 params[i] = t;
3588 paramlen[i] = 0;
3589 while (t) {
3590 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
3591 break; /* ... because we have hit a comma */
3592 if (comma && t->type == TOK_WHITESPACE
3593 && tok_is_(t->next, ","))
3594 break; /* ... or a space then a comma */
3595 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
3596 break; /* ... or a brace */
3597 t = t->next;
3598 paramlen[i]++;
3599 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003600 }
3601
3602 /*
3603 * OK, we have a MMacro structure together with a set of
3604 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00003605 * copies of each Line on to istk->expansion. Substitution of
3606 * parameter tokens and macro-local tokens doesn't get done
3607 * until the single-line macro substitution process; this is
3608 * because delaying them allows us to change the semantics
3609 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003610 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00003611 * First, push an end marker on to istk->expansion, mark this
3612 * macro as in progress, and set up its invocation-specific
3613 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003614 */
3615 ll = nasm_malloc(sizeof(Line));
3616 ll->next = istk->expansion;
3617 ll->finishes = m;
3618 ll->first = NULL;
3619 istk->expansion = ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003620
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003621 m->in_progress = TRUE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003622 m->params = params;
3623 m->iline = tline;
3624 m->nparam = nparam;
3625 m->rotate = 0;
3626 m->paramlen = paramlen;
3627 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003628 m->lineno = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003629
3630 m->next_active = istk->mstk;
3631 istk->mstk = m;
3632
H. Peter Anvine2c80182005-01-15 22:15:51 +00003633 for (l = m->expansion; l; l = l->next) {
3634 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003635
H. Peter Anvine2c80182005-01-15 22:15:51 +00003636 ll = nasm_malloc(sizeof(Line));
3637 ll->finishes = NULL;
3638 ll->next = istk->expansion;
3639 istk->expansion = ll;
3640 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003641
H. Peter Anvine2c80182005-01-15 22:15:51 +00003642 for (t = l->first; t; t = t->next) {
3643 Token *x = t;
3644 if (t->type == TOK_PREPROC_ID &&
3645 t->text[1] == '0' && t->text[2] == '0') {
3646 dont_prepend = -1;
3647 x = label;
3648 if (!x)
3649 continue;
3650 }
3651 tt = *tail = new_Token(NULL, x->type, x->text, 0);
3652 tail = &tt->next;
3653 }
3654 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003655 }
3656
3657 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00003658 * If we had a label, push it on as the first line of
3659 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003660 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003661 if (label) {
3662 if (dont_prepend < 0)
3663 free_tlist(startline);
3664 else {
3665 ll = nasm_malloc(sizeof(Line));
3666 ll->finishes = NULL;
3667 ll->next = istk->expansion;
3668 istk->expansion = ll;
3669 ll->first = startline;
3670 if (!dont_prepend) {
3671 while (label->next)
3672 label = label->next;
3673 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
3674 }
3675 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003676 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003677
H. Peter Anvin734b1882002-04-30 21:01:08 +00003678 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003679
H. Peter Anvineba20a72002-04-30 20:53:55 +00003680 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003681}
3682
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003683/*
3684 * Since preprocessor always operate only on the line that didn't
3685 * arrived yet, we should always use ERR_OFFBY1. Also since user
3686 * won't want to see same error twice (preprocessing is done once
3687 * per pass) we will want to show errors only during pass one.
3688 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00003689static void error(int severity, const int8_t *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003690{
3691 va_list arg;
Keith Kaniosb7a89542007-04-12 02:40:54 +00003692 int8_t buff[1024];
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003693
3694 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00003695 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003696 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003697
H. Peter Anvin734b1882002-04-30 21:01:08 +00003698 va_start(arg, fmt);
Ed Beroset19f927a2004-12-15 17:07:03 +00003699 vsnprintf(buff, sizeof(buff), fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003700 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003701
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00003702 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003703 _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name,
3704 istk->mstk->lineno, buff);
3705 else
3706 _error(severity | ERR_PASS1, "%s", buff);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003707}
3708
H. Peter Anvin734b1882002-04-30 21:01:08 +00003709static void
Keith Kaniosb7a89542007-04-12 02:40:54 +00003710pp_reset(int8_t *file, int apass, efunc errfunc, evalfunc eval,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003711 ListGen * listgen)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003712{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003713 int h;
3714
H. Peter Anvin99941bf2002-05-14 17:44:03 +00003715 _error = errfunc;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003716 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003717 istk = nasm_malloc(sizeof(Include));
3718 istk->next = NULL;
3719 istk->conds = NULL;
3720 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003721 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003722 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00003723 istk->fname = NULL;
3724 src_set_fname(nasm_strdup(file));
3725 src_set_linnum(0);
3726 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003727 if (!istk->fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003728 error(ERR_FATAL | ERR_NOFILE, "unable to open input file `%s'",
3729 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003730 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003731 for (h = 0; h < NHASH; h++) {
3732 mmacros[h] = NULL;
3733 smacros[h] = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003734 }
3735 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003736 if (tasm_compatible_mode) {
3737 stdmacpos = stdmac;
3738 } else {
3739 stdmacpos = &stdmac[TASM_MACRO_COUNT];
3740 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003741 any_extrastdmac = (extrastdmac != NULL);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003742 list = listgen;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003743 evaluate = eval;
3744 pass = apass;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003745}
3746
Keith Kaniosb7a89542007-04-12 02:40:54 +00003747static int8_t *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003748{
Keith Kaniosb7a89542007-04-12 02:40:54 +00003749 int8_t *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003750 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003751
H. Peter Anvine2c80182005-01-15 22:15:51 +00003752 while (1) {
3753 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00003754 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00003755 * buffer or from the input file.
3756 */
3757 tline = NULL;
3758 while (istk->expansion && istk->expansion->finishes) {
3759 Line *l = istk->expansion;
3760 if (!l->finishes->name && l->finishes->in_progress > 1) {
3761 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003762
H. Peter Anvine2c80182005-01-15 22:15:51 +00003763 /*
3764 * This is a macro-end marker for a macro with no
3765 * name, which means it's not really a macro at all
3766 * but a %rep block, and the `in_progress' field is
3767 * more than 1, meaning that we still need to
3768 * repeat. (1 means the natural last repetition; 0
3769 * means termination by %exitrep.) We have
3770 * therefore expanded up to the %endrep, and must
3771 * push the whole block on to the expansion buffer
3772 * again. We don't bother to remove the macro-end
3773 * marker: we'd only have to generate another one
3774 * if we did.
3775 */
3776 l->finishes->in_progress--;
3777 for (l = l->finishes->expansion; l; l = l->next) {
3778 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003779
H. Peter Anvine2c80182005-01-15 22:15:51 +00003780 ll = nasm_malloc(sizeof(Line));
3781 ll->next = istk->expansion;
3782 ll->finishes = NULL;
3783 ll->first = NULL;
3784 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003785
H. Peter Anvine2c80182005-01-15 22:15:51 +00003786 for (t = l->first; t; t = t->next) {
3787 if (t->text || t->type == TOK_WHITESPACE) {
3788 tt = *tail =
3789 new_Token(NULL, t->type, t->text, 0);
3790 tail = &tt->next;
3791 }
3792 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003793
H. Peter Anvine2c80182005-01-15 22:15:51 +00003794 istk->expansion = ll;
3795 }
3796 } else {
3797 /*
3798 * Check whether a `%rep' was started and not ended
3799 * within this macro expansion. This can happen and
3800 * should be detected. It's a fatal error because
3801 * I'm too confused to work out how to recover
3802 * sensibly from it.
3803 */
3804 if (defining) {
3805 if (defining->name)
3806 error(ERR_PANIC,
3807 "defining with name in expansion");
3808 else if (istk->mstk->name)
3809 error(ERR_FATAL,
3810 "`%%rep' without `%%endrep' within"
3811 " expansion of macro `%s'",
3812 istk->mstk->name);
3813 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00003814
H. Peter Anvine2c80182005-01-15 22:15:51 +00003815 /*
3816 * FIXME: investigate the relationship at this point between
3817 * istk->mstk and l->finishes
3818 */
3819 {
3820 MMacro *m = istk->mstk;
3821 istk->mstk = m->next_active;
3822 if (m->name) {
3823 /*
3824 * This was a real macro call, not a %rep, and
3825 * therefore the parameter information needs to
3826 * be freed.
3827 */
3828 nasm_free(m->params);
3829 free_tlist(m->iline);
3830 nasm_free(m->paramlen);
3831 l->finishes->in_progress = FALSE;
3832 } else
3833 free_mmacro(m);
3834 }
3835 istk->expansion = l->next;
3836 nasm_free(l);
3837 list->downlevel(LIST_MACRO);
3838 }
3839 }
3840 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00003841
H. Peter Anvine2c80182005-01-15 22:15:51 +00003842 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosb7a89542007-04-12 02:40:54 +00003843 int8_t *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003844 Line *l = istk->expansion;
3845 if (istk->mstk)
3846 istk->mstk->lineno++;
3847 tline = l->first;
3848 istk->expansion = l->next;
3849 nasm_free(l);
3850 p = detoken(tline, FALSE);
3851 list->line(LIST_MACRO, p);
3852 nasm_free(p);
3853 break;
3854 }
3855 line = read_line();
3856 if (line) { /* from the current input file */
3857 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00003858 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003859 nasm_free(line);
3860 break;
3861 }
3862 /*
3863 * The current file has ended; work down the istk
3864 */
3865 {
3866 Include *i = istk;
3867 fclose(i->fp);
3868 if (i->conds)
3869 error(ERR_FATAL,
3870 "expected `%%endif' before end of file");
3871 /* only set line and file name if there's a next node */
3872 if (i->next) {
3873 src_set_linnum(i->lineno);
3874 nasm_free(src_set_fname(i->fname));
3875 }
3876 istk = i->next;
3877 list->downlevel(LIST_INCLUDE);
3878 nasm_free(i);
3879 if (!istk)
3880 return NULL;
3881 }
3882 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003883
H. Peter Anvine2c80182005-01-15 22:15:51 +00003884 /*
3885 * We must expand MMacro parameters and MMacro-local labels
3886 * _before_ we plunge into directive processing, to cope
3887 * with things like `%define something %1' such as STRUC
3888 * uses. Unless we're _defining_ a MMacro, in which case
3889 * those tokens should be left alone to go into the
3890 * definition; and unless we're in a non-emitting
3891 * condition, in which case we don't want to meddle with
3892 * anything.
3893 */
3894 if (!defining && !(istk->conds && !emitting(istk->conds->state)))
3895 tline = expand_mmac_params(tline);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003896
H. Peter Anvine2c80182005-01-15 22:15:51 +00003897 /*
3898 * Check the line to see if it's a preprocessor directive.
3899 */
3900 if (do_directive(tline) == DIRECTIVE_FOUND) {
3901 continue;
3902 } else if (defining) {
3903 /*
3904 * We're defining a multi-line macro. We emit nothing
3905 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00003906 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003907 */
3908 Line *l = nasm_malloc(sizeof(Line));
3909 l->next = defining->expansion;
3910 l->first = tline;
3911 l->finishes = FALSE;
3912 defining->expansion = l;
3913 continue;
3914 } else if (istk->conds && !emitting(istk->conds->state)) {
3915 /*
3916 * We're in a non-emitting branch of a condition block.
3917 * Emit nothing at all, not even a blank line: when we
3918 * emerge from the condition we'll give a line-number
3919 * directive so we keep our place correctly.
3920 */
3921 free_tlist(tline);
3922 continue;
3923 } else if (istk->mstk && !istk->mstk->in_progress) {
3924 /*
3925 * We're in a %rep block which has been terminated, so
3926 * we're walking through to the %endrep without
3927 * emitting anything. Emit nothing at all, not even a
3928 * blank line: when we emerge from the %rep block we'll
3929 * give a line-number directive so we keep our place
3930 * correctly.
3931 */
3932 free_tlist(tline);
3933 continue;
3934 } else {
3935 tline = expand_smacro(tline);
3936 if (!expand_mmacro(tline)) {
3937 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00003938 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003939 */
3940 line = detoken(tline, TRUE);
3941 free_tlist(tline);
3942 break;
3943 } else {
3944 continue; /* expand_mmacro calls free_tlist */
3945 }
3946 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003947 }
3948
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003949 return line;
3950}
3951
H. Peter Anvine2c80182005-01-15 22:15:51 +00003952static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003953{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003954 int h;
3955
H. Peter Anvine2c80182005-01-15 22:15:51 +00003956 if (defining) {
3957 error(ERR_NONFATAL, "end of file while still defining macro `%s'",
3958 defining->name);
3959 free_mmacro(defining);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003960 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003961 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003962 ctx_pop();
3963 for (h = 0; h < NHASH; h++) {
3964 while (mmacros[h]) {
3965 MMacro *m = mmacros[h];
3966 mmacros[h] = mmacros[h]->next;
3967 free_mmacro(m);
3968 }
3969 while (smacros[h]) {
3970 SMacro *s = smacros[h];
3971 smacros[h] = smacros[h]->next;
3972 nasm_free(s->name);
3973 free_tlist(s->expansion);
3974 nasm_free(s);
3975 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003976 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003977 while (istk) {
3978 Include *i = istk;
3979 istk = istk->next;
3980 fclose(i->fp);
3981 nasm_free(i->fname);
3982 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003983 }
3984 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003985 ctx_pop();
3986 if (pass == 0) {
3987 free_llist(predef);
3988 delete_Blocks();
3989 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003990}
3991
Keith Kaniosb7a89542007-04-12 02:40:54 +00003992void pp_include_path(int8_t *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003993{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003994 IncPath *i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003995/* by alexfru: order of path inclusion fixed (was reverse order) */
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003996 i = nasm_malloc(sizeof(IncPath));
3997 i->path = nasm_strdup(path);
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003998 i->next = NULL;
3999
H. Peter Anvine2c80182005-01-15 22:15:51 +00004000 if (ipath != NULL) {
4001 IncPath *j = ipath;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004002 while (j->next != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004003 j = j->next;
4004 j->next = i;
4005 } else {
4006 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004007 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004008}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004009
4010/*
4011 * added by alexfru:
4012 *
4013 * This function is used to "export" the include paths, e.g.
4014 * the paths specified in the '-I' command switch.
4015 * The need for such exporting is due to the 'incbin' directive,
4016 * which includes raw binary files (unlike '%include', which
4017 * includes text source files). It would be real nice to be
4018 * able to specify paths to search for incbin'ned files also.
4019 * So, this is a simple workaround.
4020 *
4021 * The function use is simple:
4022 *
4023 * The 1st call (with NULL argument) returns a pointer to the 1st path
Keith Kaniosb7a89542007-04-12 02:40:54 +00004024 * (int8_t** type) or NULL if none include paths available.
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004025 *
4026 * All subsequent calls take as argument the value returned by this
4027 * function last. The return value is either the next path
Keith Kaniosb7a89542007-04-12 02:40:54 +00004028 * (int8_t** type) or NULL if the end of the paths list is reached.
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004029 *
4030 * It is maybe not the best way to do things, but I didn't want
4031 * to export too much, just one or two functions and no types or
4032 * variables exported.
4033 *
4034 * Can't say I like the current situation with e.g. this path list either,
4035 * it seems to be never deallocated after creation...
4036 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00004037int8_t **pp_get_include_path_ptr(int8_t **pPrevPath)
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004038{
4039/* This macro returns offset of a member of a structure */
4040#define GetMemberOffset(StructType,MemberName)\
4041 ((size_t)&((StructType*)0)->MemberName)
4042 IncPath *i;
4043
H. Peter Anvine2c80182005-01-15 22:15:51 +00004044 if (pPrevPath == NULL) {
4045 if (ipath != NULL)
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004046 return &ipath->path;
4047 else
4048 return NULL;
4049 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00004050 i = (IncPath *) ((int8_t *)pPrevPath - GetMemberOffset(IncPath, path));
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004051 i = i->next;
4052 if (i != NULL)
4053 return &i->path;
4054 else
4055 return NULL;
4056#undef GetMemberOffset
4057}
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004058
Keith Kaniosb7a89542007-04-12 02:40:54 +00004059void pp_pre_include(int8_t *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004060{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004061 Token *inc, *space, *name;
4062 Line *l;
4063
H. Peter Anvin734b1882002-04-30 21:01:08 +00004064 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4065 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4066 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004067
4068 l = nasm_malloc(sizeof(Line));
4069 l->next = predef;
4070 l->first = inc;
4071 l->finishes = FALSE;
4072 predef = l;
4073}
4074
Keith Kaniosb7a89542007-04-12 02:40:54 +00004075void pp_pre_define(int8_t *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004076{
4077 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004078 Line *l;
Keith Kaniosb7a89542007-04-12 02:40:54 +00004079 int8_t *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004080
4081 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00004082 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4083 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004084 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004085 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00004086 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004087 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004088 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004089
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004090 l = nasm_malloc(sizeof(Line));
4091 l->next = predef;
4092 l->first = def;
4093 l->finishes = FALSE;
4094 predef = l;
4095}
4096
Keith Kaniosb7a89542007-04-12 02:40:54 +00004097void pp_pre_undefine(int8_t *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00004098{
4099 Token *def, *space;
4100 Line *l;
4101
H. Peter Anvin734b1882002-04-30 21:01:08 +00004102 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4103 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004104 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00004105
4106 l = nasm_malloc(sizeof(Line));
4107 l->next = predef;
4108 l->first = def;
4109 l->finishes = FALSE;
4110 predef = l;
4111}
4112
Keith Kaniosb7a89542007-04-12 02:40:54 +00004113/*
4114 * Added by Keith Kanios:
4115 *
4116 * This function is used to assist with "runtime" preprocessor
4117 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4118 *
4119 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4120 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4121 */
4122
4123void pp_runtime(int8_t *definition)
4124{
4125 Token *def;
4126
4127 def = tokenize(definition);
4128 if(do_directive(def) == NO_DIRECTIVE_FOUND)
4129 free_tlist(def);
4130
4131}
4132
4133void pp_extra_stdmac(const int8_t **macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004134{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004135 extrastdmac = macros;
4136}
4137
Keith Kaniosb7a89542007-04-12 02:40:54 +00004138static void make_tok_num(Token * tok, int32_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004139{
Keith Kaniosb7a89542007-04-12 02:40:54 +00004140 int8_t numbuf[20];
Ed Beroset19f927a2004-12-15 17:07:03 +00004141 snprintf(numbuf, sizeof(numbuf), "%ld", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004142 tok->text = nasm_strdup(numbuf);
4143 tok->type = TOK_NUMBER;
4144}
4145
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004146Preproc nasmpp = {
4147 pp_reset,
4148 pp_getline,
4149 pp_cleanup
4150};