blob: 68aea7e6e2751ebca8900e6d90a821926cac822d [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"
H. Peter Anvin4169a472007-09-12 01:29:43 +000048#include "preproc.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000049
50typedef struct SMacro SMacro;
51typedef struct MMacro MMacro;
52typedef struct Context Context;
53typedef struct Token Token;
H. Peter Anvince616072002-04-30 21:02:23 +000054typedef struct Blocks Blocks;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000055typedef struct Line Line;
56typedef struct Include Include;
57typedef struct Cond Cond;
H. Peter Anvin6768eb72002-04-30 20:52:26 +000058typedef struct IncPath IncPath;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000059
60/*
61 * Store the definition of a single-line macro.
62 */
H. Peter Anvine2c80182005-01-15 22:15:51 +000063struct SMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000064 SMacro *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +000065 char *name;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000066 int casesense;
67 int nparam;
68 int in_progress;
69 Token *expansion;
70};
71
72/*
H. Peter Anvin76690a12002-04-30 20:52:49 +000073 * Store the definition of a multi-line macro. This is also used to
74 * store the interiors of `%rep...%endrep' blocks, which are
75 * effectively self-re-invoking multi-line macros which simply
76 * don't have a name or bother to appear in the hash tables. %rep
77 * blocks are signified by having a NULL `name' field.
78 *
79 * In a MMacro describing a `%rep' block, the `in_progress' field
80 * isn't merely boolean, but gives the number of repeats left to
81 * run.
82 *
83 * The `next' field is used for storing MMacros in hash tables; the
84 * `next_active' field is for stacking them on istk entries.
85 *
86 * When a MMacro is being expanded, `params', `iline', `nparam',
87 * `paramlen', `rotate' and `unique' are local to the invocation.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000088 */
H. Peter Anvine2c80182005-01-15 22:15:51 +000089struct MMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000090 MMacro *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +000091 char *name;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000092 int casesense;
Keith Kaniosb7a89542007-04-12 02:40:54 +000093 int64_t nparam_min, nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +000094 int plus; /* is the last parameter greedy? */
95 int nolist; /* is this macro listing-inhibited? */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000096 int in_progress;
H. Peter Anvine2c80182005-01-15 22:15:51 +000097 Token *dlist; /* All defaults as one list */
98 Token **defaults; /* Parameter default pointers */
99 int ndefs; /* number of default parameters */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000100 Line *expansion;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000101
102 MMacro *next_active;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000103 MMacro *rep_nest; /* used for nesting %rep */
104 Token **params; /* actual parameters */
105 Token *iline; /* invocation line */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000106 int nparam, rotate, *paramlen;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000107 uint32_t unique;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000108 int lineno; /* Current line number on expansion */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000109};
110
111/*
112 * The context stack is composed of a linked list of these.
113 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000114struct Context {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000115 Context *next;
116 SMacro *localmac;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000117 char *name;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000118 uint32_t number;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000119};
120
121/*
122 * This is the internal form which we break input lines up into.
123 * Typically stored in linked lists.
124 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000125 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
126 * necessarily used as-is, but is intended to denote the number of
127 * the substituted parameter. So in the definition
128 *
129 * %define a(x,y) ( (x) & ~(y) )
130 *
131 * the token representing `x' will have its type changed to
132 * TOK_SMAC_PARAM, but the one representing `y' will be
133 * TOK_SMAC_PARAM+1.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000134 *
135 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
136 * which doesn't need quotes around it. Used in the pre-include
137 * mechanism as an alternative to trying to find a sensible type of
138 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000139 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000140struct Token {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000141 Token *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000142 char *text;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000143 SMacro *mac; /* associated macro for TOK_SMAC_END */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000144 int type;
145};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000146enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000147 TOK_WHITESPACE = 1, TOK_COMMENT, TOK_ID, TOK_PREPROC_ID, TOK_STRING,
H. Peter Anvineba20a72002-04-30 20:53:55 +0000148 TOK_NUMBER, TOK_SMAC_END, TOK_OTHER, TOK_SMAC_PARAM,
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000149 TOK_INTERNAL_STRING
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000150};
151
152/*
153 * Multi-line macro definitions are stored as a linked list of
154 * these, which is essentially a container to allow several linked
155 * lists of Tokens.
156 *
157 * Note that in this module, linked lists are treated as stacks
158 * wherever possible. For this reason, Lines are _pushed_ on to the
159 * `expansion' field in MMacro structures, so that the linked list,
160 * if walked, would give the macro lines in reverse order; this
161 * means that we can walk the list when expanding a macro, and thus
162 * push the lines on to the `expansion' field in _istk_ in reverse
163 * order (so that when popped back off they are in the right
164 * order). It may seem cockeyed, and it relies on my design having
165 * an even number of steps in, but it works...
166 *
167 * Some of these structures, rather than being actual lines, are
168 * markers delimiting the end of the expansion of a given macro.
H. Peter Anvin76690a12002-04-30 20:52:49 +0000169 * This is for use in the cycle-tracking and %rep-handling code.
170 * Such structures have `finishes' non-NULL, and `first' NULL. All
171 * others have `finishes' NULL, but `first' may still be NULL if
172 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000173 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000174struct Line {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000175 Line *next;
176 MMacro *finishes;
177 Token *first;
178};
179
180/*
181 * To handle an arbitrary level of file inclusion, we maintain a
182 * stack (ie linked list) of these things.
183 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000184struct Include {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000185 Include *next;
186 FILE *fp;
187 Cond *conds;
188 Line *expansion;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000189 char *fname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000190 int lineno, lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000191 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000192};
193
194/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000195 * Include search path. This is simply a list of strings which get
196 * prepended, in turn, to the name of an include file, in an
197 * attempt to find the file if it's not in the current directory.
198 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000199struct IncPath {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000200 IncPath *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000201 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000202};
203
204/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000205 * Conditional assembly: we maintain a separate stack of these for
206 * each level of file inclusion. (The only reason we keep the
207 * stacks separate is to ensure that a stray `%endif' in a file
208 * included from within the true branch of a `%if' won't terminate
209 * it and cause confusion: instead, rightly, it'll cause an error.)
210 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000211struct Cond {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000212 Cond *next;
213 int state;
214};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000215enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000216 /*
217 * These states are for use just after %if or %elif: IF_TRUE
218 * means the condition has evaluated to truth so we are
219 * currently emitting, whereas IF_FALSE means we are not
220 * currently emitting but will start doing so if a %else comes
221 * up. In these states, all directives are admissible: %elif,
222 * %else and %endif. (And of course %if.)
223 */
224 COND_IF_TRUE, COND_IF_FALSE,
225 /*
226 * These states come up after a %else: ELSE_TRUE means we're
227 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
228 * any %elif or %else will cause an error.
229 */
230 COND_ELSE_TRUE, COND_ELSE_FALSE,
231 /*
232 * This state means that we're not emitting now, and also that
233 * nothing until %endif will be emitted at all. It's for use in
234 * two circumstances: (i) when we've had our moment of emission
235 * and have now started seeing %elifs, and (ii) when the
236 * condition construct in question is contained within a
237 * non-emitting branch of a larger condition construct.
238 */
239 COND_NEVER
240};
241#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
242
Ed Beroset3ab3f412002-06-11 03:31:49 +0000243/*
244 * These defines are used as the possible return values for do_directive
245 */
246#define NO_DIRECTIVE_FOUND 0
247#define DIRECTIVE_FOUND 1
248
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000249/*
250 * Condition codes. Note that we use c_ prefix not C_ because C_ is
251 * used in nasm.h for the "real" condition codes. At _this_ level,
252 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
253 * ones, so we need a different enum...
254 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000255static const char *conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000256 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
257 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000258 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000259};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000260enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000261 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
262 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
H. Peter Anvince9be342007-09-12 00:22:29 +0000263 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000264};
265static int inverse_ccs[] = {
266 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
267 c_A, c_AE, c_B, c_BE, c_C, c_E, c_G, c_GE, c_L, c_LE, c_O, c_P, c_S,
H. Peter Anvince9be342007-09-12 00:22:29 +0000268 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000269};
270
H. Peter Anvin76690a12002-04-30 20:52:49 +0000271/*
272 * Directive names.
273 */
H. Peter Anvin65747262002-05-07 00:10:05 +0000274/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
H. Peter Anvin9bf0aa72007-09-12 02:12:07 +0000275static int is_condition(enum preproc_token arg)
H. Peter Anvin65747262002-05-07 00:10:05 +0000276{
H. Peter Anvin9bf0aa72007-09-12 02:12:07 +0000277 return IS_PP_IF(arg) || IS_PP_ELIF(arg) ||
278 (arg == PP_ELSE) || (arg == PP_ENDIF);
H. Peter Anvin65747262002-05-07 00:10:05 +0000279}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000280
281/* For TASM compatibility we need to be able to recognise TASM compatible
282 * conditional compilation directives. Using the NASM pre-processor does
283 * not work, so we look for them specifically from the following list and
284 * then jam in the equivalent NASM directive into the input stream.
285 */
286
287#ifndef MAX
288# define MAX(a,b) ( ((a) > (b)) ? (a) : (b))
289#endif
290
H. Peter Anvine2c80182005-01-15 22:15:51 +0000291enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000292 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
293 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
294};
295
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000296static const char *tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000297 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
298 "ifndef", "include", "local"
299};
300
301static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000302static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000303static int ArgOffset = 8;
304static int LocalOffset = 4;
305
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000306static Context *cstk;
307static Include *istk;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000308static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000309
H. Peter Anvine2c80182005-01-15 22:15:51 +0000310static efunc _error; /* Pointer to client-provided error reporting function */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000311static evalfunc evaluate;
312
H. Peter Anvine2c80182005-01-15 22:15:51 +0000313static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000314
Keith Kaniosb7a89542007-04-12 02:40:54 +0000315static uint32_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000316
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000317static Line *predef = NULL;
318
319static ListGen *list;
320
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000321/*
322 * The number of hash values we use for the macro lookup tables.
H. Peter Anvineba20a72002-04-30 20:53:55 +0000323 * FIXME: We should *really* be able to configure this at run time,
324 * or even have the hash table automatically expanding when necessary.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000325 */
326#define NHASH 31
327
328/*
329 * The current set of multi-line macros we have defined.
330 */
331static MMacro *mmacros[NHASH];
332
333/*
334 * The current set of single-line macros we have defined.
335 */
336static SMacro *smacros[NHASH];
337
338/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000339 * The multi-line macro we are currently defining, or the %rep
340 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000341 */
342static MMacro *defining;
343
344/*
345 * The number of macro parameters to allocate space for at a time.
346 */
347#define PARAM_DELTA 16
348
349/*
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000350 * The standard macro set: defined as `static char *stdmac[]'. Also
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000351 * gives our position in the macro set, when we're processing it.
352 */
353#include "macros.c"
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000354static const char **stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000355
356/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000357 * The extra standard macros that come from the object format, if
358 * any.
359 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000360static const char **extrastdmac = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000361int any_extrastdmac;
362
363/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000364 * Tokens are allocated in blocks to improve speed
365 */
366#define TOKEN_BLOCKSIZE 4096
367static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000368struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000369 Blocks *next;
370 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000371};
372
373static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000374
375/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000376 * Forward declarations.
377 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000378static Token *expand_mmac_params(Token * tline);
379static Token *expand_smacro(Token * tline);
380static Token *expand_id(Token * tline);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000381static Context *get_ctx(char *name, int all_contexts);
Keith Kaniosb7a89542007-04-12 02:40:54 +0000382static void make_tok_num(Token * tok, int32_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000383static void error(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000384static void *new_Block(size_t size);
385static void delete_Blocks(void);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000386static Token *new_Token(Token * next, int type, char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000387static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000388
389/*
390 * Macros for safe checking of token pointers, avoid *(NULL)
391 */
392#define tok_type_(x,t) ((x) && (x)->type == (t))
393#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
394#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
395#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000396
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000397/* Handle TASM specific directives, which do not contain a % in
398 * front of them. We do it here because I could not find any other
399 * place to do it for the moment, and it is a hack (ideally it would
400 * be nice to be able to use the NASM pre-processor to do it).
401 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000402static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000403{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000404 int32_t i, j, k, m, len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000405 char *p = line, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000406
407 /* Skip whitespace */
408 while (isspace(*p) && *p != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000409 p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000410
411 /* Binary search for the directive name */
412 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +0000413 j = elements(tasm_directives);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000414 len = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000415 while (!isspace(p[len]) && p[len] != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000416 len++;
417 if (len) {
418 oldchar = p[len];
419 p[len] = 0;
420 while (j - i > 1) {
421 k = (j + i) / 2;
422 m = nasm_stricmp(p, tasm_directives[k]);
423 if (m == 0) {
424 /* We have found a directive, so jam a % in front of it
425 * so that NASM will then recognise it as one if it's own.
426 */
427 p[len] = oldchar;
428 len = strlen(p);
429 oldline = line;
430 line = nasm_malloc(len + 2);
431 line[0] = '%';
432 if (k == TM_IFDIFI) {
433 /* NASM does not recognise IFDIFI, so we convert it to
434 * %ifdef BOGUS. This is not used in NASM comaptible
435 * code, but does need to parse for the TASM macro
436 * package.
437 */
438 strcpy(line + 1, "ifdef BOGUS");
439 } else {
440 memcpy(line + 1, p, len + 1);
441 }
442 nasm_free(oldline);
443 return line;
444 } else if (m < 0) {
445 j = k;
446 } else
447 i = k;
448 }
449 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000450 }
451 return line;
452}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000453
H. Peter Anvin76690a12002-04-30 20:52:49 +0000454/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000455 * The pre-preprocessing stage... This function translates line
456 * number indications as they emerge from GNU cpp (`# lineno "file"
457 * flags') into NASM preprocessor line number indications (`%line
458 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000459 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000460static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000461{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000462 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000463 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000464
H. Peter Anvine2c80182005-01-15 22:15:51 +0000465 if (line[0] == '#' && line[1] == ' ') {
466 oldline = line;
467 fname = oldline + 2;
468 lineno = atoi(fname);
469 fname += strspn(fname, "0123456789 ");
470 if (*fname == '"')
471 fname++;
472 fnlen = strcspn(fname, "\"");
473 line = nasm_malloc(20 + fnlen);
474 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
475 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000476 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000477 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000478 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000479 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000480}
481
482/*
483 * The hash function for macro lookups. Note that due to some
484 * macros having case-insensitive names, the hash function must be
485 * invariant under case changes. We implement this by applying a
486 * perfectly normal hash function to the uppercase of the string.
487 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000488static int hash(char *s)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000489{
490 unsigned int h = 0;
491 int i = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000492 /*
493 * Powers of three, mod 31.
494 */
495 static const int multipliers[] = {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000496 1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10,
497 30, 28, 22, 4, 12, 5, 15, 14, 11, 2, 6, 18, 23, 7, 21
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000498 };
H. Peter Anvineba20a72002-04-30 20:53:55 +0000499
H. Peter Anvine2c80182005-01-15 22:15:51 +0000500 while (*s) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000501 h += multipliers[i] * (uint8_t)(toupper(*s));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000502 s++;
503 if (++i >= elements(multipliers))
504 i = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000505 }
506 h %= NHASH;
507 return h;
508}
509
510/*
511 * Free a linked list of tokens.
512 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000513static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000514{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000515 while (list) {
516 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000517 }
518}
519
520/*
521 * Free a linked list of lines.
522 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000523static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000524{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000525 Line *l;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000526 while (list) {
527 l = list;
528 list = list->next;
529 free_tlist(l->first);
530 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000531 }
532}
533
534/*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000535 * Free an MMacro
536 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000537static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000538{
H. Peter Anvin734b1882002-04-30 21:01:08 +0000539 nasm_free(m->name);
540 free_tlist(m->dlist);
541 nasm_free(m->defaults);
542 free_llist(m->expansion);
543 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000544}
545
546/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000547 * Pop the context stack.
548 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000549static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000550{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000551 Context *c = cstk;
552 SMacro *smac, *s;
553
554 cstk = cstk->next;
555 smac = c->localmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000556 while (smac) {
557 s = smac;
558 smac = smac->next;
559 nasm_free(s->name);
560 free_tlist(s->expansion);
561 nasm_free(s);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000562 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000563 nasm_free(c->name);
564 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000565}
566
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000567#define BUF_DELTA 512
568/*
569 * Read a line from the top file in istk, handling multiple CR/LFs
570 * at the end of the line read, and handling spurious ^Zs. Will
571 * return lines from the standard macro set if this has not already
572 * been done.
573 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000574static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000575{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000576 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000577 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000578
H. Peter Anvine2c80182005-01-15 22:15:51 +0000579 if (stdmacpos) {
580 if (*stdmacpos) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000581 char *ret = nasm_strdup(*stdmacpos++);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000582 if (!*stdmacpos && any_extrastdmac) {
583 stdmacpos = extrastdmac;
584 any_extrastdmac = FALSE;
585 return ret;
586 }
587 /*
588 * Nasty hack: here we push the contents of `predef' on
589 * to the top-level expansion stack, since this is the
590 * most convenient way to implement the pre-include and
591 * pre-define features.
592 */
593 if (!*stdmacpos) {
594 Line *pd, *l;
595 Token *head, **tail, *t;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000596
H. Peter Anvine2c80182005-01-15 22:15:51 +0000597 for (pd = predef; pd; pd = pd->next) {
598 head = NULL;
599 tail = &head;
600 for (t = pd->first; t; t = t->next) {
601 *tail = new_Token(NULL, t->type, t->text, 0);
602 tail = &(*tail)->next;
603 }
604 l = nasm_malloc(sizeof(Line));
605 l->next = istk->expansion;
606 l->first = head;
607 l->finishes = FALSE;
608 istk->expansion = l;
609 }
610 }
611 return ret;
612 } else {
613 stdmacpos = NULL;
614 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000615 }
616
617 bufsize = BUF_DELTA;
618 buffer = nasm_malloc(BUF_DELTA);
619 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000620 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000621 while (1) {
622 q = fgets(p, bufsize - (p - buffer), istk->fp);
623 if (!q)
624 break;
625 p += strlen(p);
626 if (p > buffer && p[-1] == '\n') {
627 /* Convert backslash-CRLF line continuation sequences into
628 nothing at all (for DOS and Windows) */
629 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
630 p -= 3;
631 *p = 0;
632 continued_count++;
633 }
634 /* Also convert backslash-LF line continuation sequences into
635 nothing at all (for Unix) */
636 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
637 p -= 2;
638 *p = 0;
639 continued_count++;
640 } else {
641 break;
642 }
643 }
644 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000645 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000646 bufsize += BUF_DELTA;
647 buffer = nasm_realloc(buffer, bufsize);
648 p = buffer + offset; /* prevent stale-pointer problems */
649 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000650 }
651
H. Peter Anvine2c80182005-01-15 22:15:51 +0000652 if (!q && p == buffer) {
653 nasm_free(buffer);
654 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000655 }
656
H. Peter Anvine2c80182005-01-15 22:15:51 +0000657 src_set_linnum(src_get_linnum() + istk->lineinc +
658 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000659
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000660 /*
661 * Play safe: remove CRs as well as LFs, if any of either are
662 * present at the end of the line.
663 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000664 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000665 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000666
667 /*
668 * Handle spurious ^Z, which may be inserted into source files
669 * by some file transfer utilities.
670 */
671 buffer[strcspn(buffer, "\032")] = '\0';
672
H. Peter Anvin734b1882002-04-30 21:01:08 +0000673 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000674
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000675 return buffer;
676}
677
678/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000679 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000680 * don't need to parse the value out of e.g. numeric tokens: we
681 * simply split one string into many.
682 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000683static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000684{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000685 char *p = line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000686 int type;
687 Token *list = NULL;
688 Token *t, **tail = &list;
689
H. Peter Anvine2c80182005-01-15 22:15:51 +0000690 while (*line) {
691 p = line;
692 if (*p == '%') {
693 p++;
694 if (isdigit(*p) ||
695 ((*p == '-' || *p == '+') && isdigit(p[1])) ||
696 ((*p == '+') && (isspace(p[1]) || !p[1]))) {
697 do {
698 p++;
699 }
700 while (isdigit(*p));
701 type = TOK_PREPROC_ID;
702 } else if (*p == '{') {
703 p++;
704 while (*p && *p != '}') {
705 p[-1] = *p;
706 p++;
707 }
708 p[-1] = '\0';
709 if (*p)
710 p++;
711 type = TOK_PREPROC_ID;
712 } else if (isidchar(*p) ||
713 ((*p == '!' || *p == '%' || *p == '$') &&
714 isidchar(p[1]))) {
715 do {
716 p++;
717 }
718 while (isidchar(*p));
719 type = TOK_PREPROC_ID;
720 } else {
721 type = TOK_OTHER;
722 if (*p == '%')
723 p++;
724 }
725 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
726 type = TOK_ID;
727 p++;
728 while (*p && isidchar(*p))
729 p++;
730 } else if (*p == '\'' || *p == '"') {
731 /*
732 * A string token.
733 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000734 char c = *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000735 p++;
736 type = TOK_STRING;
737 while (*p && *p != c)
738 p++;
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000739
H. Peter Anvine2c80182005-01-15 22:15:51 +0000740 if (*p) {
741 p++;
742 } else {
743 error(ERR_WARNING, "unterminated string");
744 /* Handling unterminated strings by UNV */
745 /* type = -1; */
746 }
747 } else if (isnumstart(*p)) {
748 /*
749 * A number token.
750 */
751 type = TOK_NUMBER;
752 p++;
753 while (*p && isnumchar(*p))
754 p++;
755 } else if (isspace(*p)) {
756 type = TOK_WHITESPACE;
757 p++;
758 while (*p && isspace(*p))
759 p++;
760 /*
761 * Whitespace just before end-of-line is discarded by
762 * pretending it's a comment; whitespace just before a
763 * comment gets lumped into the comment.
764 */
765 if (!*p || *p == ';') {
766 type = TOK_COMMENT;
767 while (*p)
768 p++;
769 }
770 } else if (*p == ';') {
771 type = TOK_COMMENT;
772 while (*p)
773 p++;
774 } else {
775 /*
776 * Anything else is an operator of some kind. We check
777 * for all the double-character operators (>>, <<, //,
778 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000779 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000780 */
781 type = TOK_OTHER;
782 if ((p[0] == '>' && p[1] == '>') ||
783 (p[0] == '<' && p[1] == '<') ||
784 (p[0] == '/' && p[1] == '/') ||
785 (p[0] == '<' && p[1] == '=') ||
786 (p[0] == '>' && p[1] == '=') ||
787 (p[0] == '=' && p[1] == '=') ||
788 (p[0] == '!' && p[1] == '=') ||
789 (p[0] == '<' && p[1] == '>') ||
790 (p[0] == '&' && p[1] == '&') ||
791 (p[0] == '|' && p[1] == '|') ||
792 (p[0] == '^' && p[1] == '^')) {
793 p++;
794 }
795 p++;
796 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000797
H. Peter Anvine2c80182005-01-15 22:15:51 +0000798 /* Handling unterminated string by UNV */
799 /*if (type == -1)
800 {
801 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
802 t->text[p-line] = *line;
803 tail = &t->next;
804 }
805 else */
806 if (type != TOK_COMMENT) {
807 *tail = t = new_Token(NULL, type, line, p - line);
808 tail = &t->next;
809 }
810 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000811 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000812 return list;
813}
814
H. Peter Anvince616072002-04-30 21:02:23 +0000815/*
816 * this function allocates a new managed block of memory and
817 * returns a pointer to the block. The managed blocks are
818 * deleted only all at once by the delete_Blocks function.
819 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000820static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +0000821{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000822 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000823
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000824 /* first, get to the end of the linked list */
825 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000826 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000827 /* now allocate the requested chunk */
828 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000829
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000830 /* now allocate a new block for the next request */
831 b->next = nasm_malloc(sizeof(Blocks));
832 /* and initialize the contents of the new block */
833 b->next->next = NULL;
834 b->next->chunk = NULL;
835 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000836}
837
838/*
839 * this function deletes all managed blocks of memory
840 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000841static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +0000842{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000843 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +0000844
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000845 /*
846 * keep in mind that the first block, pointed to by blocks
847 * is a static and not dynamically allocated, so we don't
848 * free it.
849 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000850 while (b) {
851 if (b->chunk)
852 nasm_free(b->chunk);
853 a = b;
854 b = b->next;
855 if (a != &blocks)
856 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000857 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000858}
H. Peter Anvin734b1882002-04-30 21:01:08 +0000859
860/*
861 * this function creates a new Token and passes a pointer to it
862 * back to the caller. It sets the type and text elements, and
863 * also the mac and next elements to NULL.
864 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000865static Token *new_Token(Token * next, int type, char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +0000866{
867 Token *t;
868 int i;
869
H. Peter Anvine2c80182005-01-15 22:15:51 +0000870 if (freeTokens == NULL) {
871 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
872 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
873 freeTokens[i].next = &freeTokens[i + 1];
874 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000875 }
876 t = freeTokens;
877 freeTokens = t->next;
878 t->next = next;
879 t->mac = NULL;
880 t->type = type;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000881 if (type == TOK_WHITESPACE || text == NULL) {
882 t->text = NULL;
883 } else {
884 if (txtlen == 0)
885 txtlen = strlen(text);
886 t->text = nasm_malloc(1 + txtlen);
887 strncpy(t->text, text, txtlen);
888 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +0000889 }
890 return t;
891}
892
H. Peter Anvine2c80182005-01-15 22:15:51 +0000893static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +0000894{
895 Token *next = t->next;
896 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +0000897 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000898 freeTokens = t;
899 return next;
900}
901
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000902/*
903 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000904 * If expand_locals is not zero, identifiers of the form "%$*xxx"
905 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000906 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000907static char *detoken(Token * tlist, int expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000908{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000909 Token *t;
910 int len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000911 char *line, *p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000912
913 len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000914 for (t = tlist; t; t = t->next) {
915 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000916 char *p = getenv(t->text + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000917 nasm_free(t->text);
918 if (p)
919 t->text = nasm_strdup(p);
920 else
921 t->text = NULL;
922 }
923 /* Expand local macros here and not during preprocessing */
924 if (expand_locals &&
925 t->type == TOK_PREPROC_ID && t->text &&
926 t->text[0] == '%' && t->text[1] == '$') {
927 Context *ctx = get_ctx(t->text, FALSE);
928 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000929 char buffer[40];
930 char *p, *q = t->text + 2;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000931
H. Peter Anvine2c80182005-01-15 22:15:51 +0000932 q += strspn(q, "$");
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000933 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000934 p = nasm_strcat(buffer, q);
935 nasm_free(t->text);
936 t->text = p;
937 }
938 }
939 if (t->type == TOK_WHITESPACE) {
940 len++;
941 } else if (t->text) {
942 len += strlen(t->text);
943 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000944 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000945 p = line = nasm_malloc(len + 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000946 for (t = tlist; t; t = t->next) {
947 if (t->type == TOK_WHITESPACE) {
948 *p = ' ';
949 p++;
950 *p = '\0';
951 } else if (t->text) {
952 strcpy(p, t->text);
953 p += strlen(p);
954 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000955 }
956 *p = '\0';
957 return line;
958}
959
960/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000961 * A scanner, suitable for use by the expression evaluator, which
962 * operates on a line of Tokens. Expects a pointer to a pointer to
963 * the first token in the line to be passed in as its private_data
964 * field.
965 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000966static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000967{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000968 Token **tlineptr = private_data;
969 Token *tline;
970
H. Peter Anvine2c80182005-01-15 22:15:51 +0000971 do {
972 tline = *tlineptr;
973 *tlineptr = tline ? tline->next : NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000974 }
975 while (tline && (tline->type == TOK_WHITESPACE ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000976 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000977
978 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000979 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000980
981 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +0000982 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +0000983 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +0000984 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000985
H. Peter Anvine2c80182005-01-15 22:15:51 +0000986 if (tline->type == TOK_ID) {
987 tokval->t_charptr = tline->text;
988 if (tline->text[0] == '$') {
989 tokval->t_charptr++;
990 return tokval->t_type = TOKEN_ID;
991 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000992
H. Peter Anvine2c80182005-01-15 22:15:51 +0000993 /*
994 * This is the only special case we actually need to worry
995 * about in this restricted context.
996 */
997 if (!nasm_stricmp(tline->text, "seg"))
998 return tokval->t_type = TOKEN_SEG;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000999
H. Peter Anvine2c80182005-01-15 22:15:51 +00001000 return tokval->t_type = TOKEN_ID;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001001 }
1002
H. Peter Anvine2c80182005-01-15 22:15:51 +00001003 if (tline->type == TOK_NUMBER) {
1004 int rn_error;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001005
H. Peter Anvine2c80182005-01-15 22:15:51 +00001006 tokval->t_integer = readnum(tline->text, &rn_error);
1007 if (rn_error)
1008 return tokval->t_type = TOKEN_ERRNUM;
1009 tokval->t_charptr = NULL;
1010 return tokval->t_type = TOKEN_NUM;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001011 }
1012
H. Peter Anvine2c80182005-01-15 22:15:51 +00001013 if (tline->type == TOK_STRING) {
1014 int rn_warn;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001015 char q, *r;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001016 int l;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001017
H. Peter Anvine2c80182005-01-15 22:15:51 +00001018 r = tline->text;
1019 q = *r++;
1020 l = strlen(r);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001021
H. Peter Anvine2c80182005-01-15 22:15:51 +00001022 if (l == 0 || r[l - 1] != q)
1023 return tokval->t_type = TOKEN_ERRNUM;
1024 tokval->t_integer = readstrnum(r, l - 1, &rn_warn);
1025 if (rn_warn)
1026 error(ERR_WARNING | ERR_PASS1, "character constant too long");
1027 tokval->t_charptr = NULL;
1028 return tokval->t_type = TOKEN_NUM;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001029 }
1030
H. Peter Anvine2c80182005-01-15 22:15:51 +00001031 if (tline->type == TOK_OTHER) {
1032 if (!strcmp(tline->text, "<<"))
1033 return tokval->t_type = TOKEN_SHL;
1034 if (!strcmp(tline->text, ">>"))
1035 return tokval->t_type = TOKEN_SHR;
1036 if (!strcmp(tline->text, "//"))
1037 return tokval->t_type = TOKEN_SDIV;
1038 if (!strcmp(tline->text, "%%"))
1039 return tokval->t_type = TOKEN_SMOD;
1040 if (!strcmp(tline->text, "=="))
1041 return tokval->t_type = TOKEN_EQ;
1042 if (!strcmp(tline->text, "<>"))
1043 return tokval->t_type = TOKEN_NE;
1044 if (!strcmp(tline->text, "!="))
1045 return tokval->t_type = TOKEN_NE;
1046 if (!strcmp(tline->text, "<="))
1047 return tokval->t_type = TOKEN_LE;
1048 if (!strcmp(tline->text, ">="))
1049 return tokval->t_type = TOKEN_GE;
1050 if (!strcmp(tline->text, "&&"))
1051 return tokval->t_type = TOKEN_DBL_AND;
1052 if (!strcmp(tline->text, "^^"))
1053 return tokval->t_type = TOKEN_DBL_XOR;
1054 if (!strcmp(tline->text, "||"))
1055 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001056 }
1057
1058 /*
1059 * We have no other options: just return the first character of
1060 * the token text.
1061 */
1062 return tokval->t_type = tline->text[0];
1063}
1064
1065/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001066 * Compare a string to the name of an existing macro; this is a
1067 * simple wrapper which calls either strcmp or nasm_stricmp
1068 * depending on the value of the `casesense' parameter.
1069 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001070static int mstrcmp(char *p, char *q, int casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001071{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001072 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001073}
1074
1075/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001076 * Return the Context structure associated with a %$ token. Return
1077 * NULL, having _already_ reported an error condition, if the
1078 * context stack isn't deep enough for the supplied number of $
1079 * signs.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001080 * If all_contexts == TRUE, contexts that enclose current are
1081 * also scanned for such smacro, until it is found; if not -
1082 * only the context that directly results from the number of $'s
1083 * in variable's name.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001084 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001085static Context *get_ctx(char *name, int all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001086{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001087 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001088 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001089 int i;
1090
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001091 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001092 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001093
H. Peter Anvine2c80182005-01-15 22:15:51 +00001094 if (!cstk) {
1095 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1096 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001097 }
1098
H. Peter Anvine2c80182005-01-15 22:15:51 +00001099 for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--) {
1100 ctx = ctx->next;
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00001101/* i--; Lino - 02/25/02 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001102 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001103 if (!ctx) {
1104 error(ERR_NONFATAL, "`%s': context stack is only"
1105 " %d level%s deep", name, i - 1, (i == 2 ? "" : "s"));
1106 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001107 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001108 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001109 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001110
H. Peter Anvine2c80182005-01-15 22:15:51 +00001111 do {
1112 /* Search for this smacro in found context */
1113 m = ctx->localmac;
1114 while (m) {
1115 if (!mstrcmp(m->name, name, m->casesense))
1116 return ctx;
1117 m = m->next;
1118 }
1119 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001120 }
1121 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001122 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001123}
1124
1125/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001126 * Open an include file. This routine must always return a valid
1127 * file pointer if it returns - it's responsible for throwing an
1128 * ERR_FATAL and bombing out completely if not. It should also try
1129 * the include path one by one until it finds the file or reaches
1130 * the end of the path.
1131 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001132static FILE *inc_fopen(char *file)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001133{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001134 FILE *fp;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001135 char *prefix = "", *combine;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001136 IncPath *ip = ipath;
H. Peter Anvin620515a2002-04-30 20:57:38 +00001137 static int namelen = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001138 int len = strlen(file);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001139
H. Peter Anvine2c80182005-01-15 22:15:51 +00001140 while (1) {
1141 combine = nasm_malloc(strlen(prefix) + len + 1);
1142 strcpy(combine, prefix);
1143 strcat(combine, file);
1144 fp = fopen(combine, "r");
1145 if (pass == 0 && fp) {
1146 namelen += strlen(combine) + 1;
1147 if (namelen > 62) {
1148 printf(" \\\n ");
1149 namelen = 2;
1150 }
1151 printf(" %s", combine);
1152 }
1153 nasm_free(combine);
1154 if (fp)
1155 return fp;
1156 if (!ip)
1157 break;
1158 prefix = ip->path;
1159 ip = ip->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001160 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001161
H. Peter Anvin734b1882002-04-30 21:01:08 +00001162 error(ERR_FATAL, "unable to open include file `%s'", file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001163 return NULL; /* never reached - placate compilers */
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001164}
1165
1166/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001167 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001168 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001169 * return TRUE if _any_ single-line macro of that name is defined.
1170 * Otherwise, will return TRUE if a single-line macro with either
1171 * `nparam' or no parameters is defined.
1172 *
1173 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001174 * defined, or nparam is -1, the address of the definition structure
1175 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001176 * is NULL, no action will be taken regarding its contents, and no
1177 * error will occur.
1178 *
1179 * Note that this is also called with nparam zero to resolve
1180 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001181 *
1182 * If you already know which context macro belongs to, you can pass
1183 * the context pointer as first parameter; if you won't but name begins
1184 * with %$ the context will be automatically computed. If all_contexts
1185 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001186 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001187static int
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001188smacro_defined(Context * ctx, char *name, int nparam, SMacro ** defn,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001189 int nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001190{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001191 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001192
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001193 if (ctx)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001194 m = ctx->localmac;
1195 else if (name[0] == '%' && name[1] == '$') {
1196 if (cstk)
1197 ctx = get_ctx(name, FALSE);
1198 if (!ctx)
1199 return FALSE; /* got to return _something_ */
1200 m = ctx->localmac;
1201 } else
1202 m = smacros[hash(name)];
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001203
H. Peter Anvine2c80182005-01-15 22:15:51 +00001204 while (m) {
1205 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
1206 (nparam <= 0 || m->nparam == 0 || nparam == m->nparam)) {
1207 if (defn) {
1208 if (nparam == m->nparam || nparam == -1)
1209 *defn = m;
1210 else
1211 *defn = NULL;
1212 }
1213 return TRUE;
1214 }
1215 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001216 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001217
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001218 return FALSE;
1219}
1220
1221/*
1222 * Count and mark off the parameters in a multi-line macro call.
1223 * This is called both from within the multi-line macro expansion
1224 * code, and also to mark off the default parameters when provided
1225 * in a %macro definition line.
1226 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001227static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001228{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001229 int paramsize, brace;
1230
1231 *nparam = paramsize = 0;
1232 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001233 while (t) {
1234 if (*nparam >= paramsize) {
1235 paramsize += PARAM_DELTA;
1236 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1237 }
1238 skip_white_(t);
1239 brace = FALSE;
1240 if (tok_is_(t, "{"))
1241 brace = TRUE;
1242 (*params)[(*nparam)++] = t;
1243 while (tok_isnt_(t, brace ? "}" : ","))
1244 t = t->next;
1245 if (t) { /* got a comma/brace */
1246 t = t->next;
1247 if (brace) {
1248 /*
1249 * Now we've found the closing brace, look further
1250 * for the comma.
1251 */
1252 skip_white_(t);
1253 if (tok_isnt_(t, ",")) {
1254 error(ERR_NONFATAL,
1255 "braces do not enclose all of macro parameter");
1256 while (tok_isnt_(t, ","))
1257 t = t->next;
1258 }
1259 if (t)
1260 t = t->next; /* eat the comma */
1261 }
1262 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001263 }
1264}
1265
1266/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001267 * Determine whether one of the various `if' conditions is true or
1268 * not.
1269 *
1270 * We must free the tline we get passed.
1271 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001272static int if_condition(Token * tline, int i)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001273{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001274 int j, casesense;
1275 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001276 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001277 expr *evalresult;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001278
1279 origline = tline;
1280
H. Peter Anvine2c80182005-01-15 22:15:51 +00001281 switch (i) {
1282 case PP_IFCTX:
1283 case PP_ELIFCTX:
1284 case PP_IFNCTX:
1285 case PP_ELIFNCTX:
1286 j = FALSE; /* have we matched yet? */
1287 while (cstk && tline) {
1288 skip_white_(tline);
1289 if (!tline || tline->type != TOK_ID) {
1290 error(ERR_NONFATAL,
H. Peter Anvin4169a472007-09-12 01:29:43 +00001291 "`%s' expects context identifiers", pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001292 free_tlist(origline);
1293 return -1;
1294 }
1295 if (!nasm_stricmp(tline->text, cstk->name))
1296 j = TRUE;
1297 tline = tline->next;
1298 }
1299 if (i == PP_IFNCTX || i == PP_ELIFNCTX)
1300 j = !j;
1301 free_tlist(origline);
1302 return j;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001303
H. Peter Anvine2c80182005-01-15 22:15:51 +00001304 case PP_IFDEF:
1305 case PP_ELIFDEF:
1306 case PP_IFNDEF:
1307 case PP_ELIFNDEF:
1308 j = FALSE; /* have we matched yet? */
1309 while (tline) {
1310 skip_white_(tline);
1311 if (!tline || (tline->type != TOK_ID &&
1312 (tline->type != TOK_PREPROC_ID ||
1313 tline->text[1] != '$'))) {
1314 error(ERR_NONFATAL,
H. Peter Anvin4169a472007-09-12 01:29:43 +00001315 "`%s' expects macro identifiers", pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001316 free_tlist(origline);
1317 return -1;
1318 }
1319 if (smacro_defined(NULL, tline->text, 0, NULL, 1))
1320 j = TRUE;
1321 tline = tline->next;
1322 }
1323 if (i == PP_IFNDEF || i == PP_ELIFNDEF)
1324 j = !j;
1325 free_tlist(origline);
1326 return j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001327
H. Peter Anvine2c80182005-01-15 22:15:51 +00001328 case PP_IFIDN:
1329 case PP_ELIFIDN:
1330 case PP_IFNIDN:
1331 case PP_ELIFNIDN:
1332 case PP_IFIDNI:
1333 case PP_ELIFIDNI:
1334 case PP_IFNIDNI:
1335 case PP_ELIFNIDNI:
1336 tline = expand_smacro(tline);
1337 t = tt = tline;
1338 while (tok_isnt_(tt, ","))
1339 tt = tt->next;
1340 if (!tt) {
1341 error(ERR_NONFATAL,
1342 "`%s' expects two comma-separated arguments",
H. Peter Anvin4169a472007-09-12 01:29:43 +00001343 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001344 free_tlist(tline);
1345 return -1;
1346 }
1347 tt = tt->next;
1348 casesense = (i == PP_IFIDN || i == PP_ELIFIDN ||
1349 i == PP_IFNIDN || i == PP_ELIFNIDN);
1350 j = TRUE; /* assume equality unless proved not */
1351 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1352 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1353 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvin4169a472007-09-12 01:29:43 +00001354 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001355 free_tlist(tline);
1356 return -1;
1357 }
1358 if (t->type == TOK_WHITESPACE) {
1359 t = t->next;
1360 continue;
1361 }
1362 if (tt->type == TOK_WHITESPACE) {
1363 tt = tt->next;
1364 continue;
1365 }
1366 if (tt->type != t->type) {
1367 j = FALSE; /* found mismatching tokens */
1368 break;
1369 }
1370 /* Unify surrounding quotes for strings */
1371 if (t->type == TOK_STRING) {
1372 tt->text[0] = t->text[0];
1373 tt->text[strlen(tt->text) - 1] = t->text[0];
1374 }
1375 if (mstrcmp(tt->text, t->text, casesense) != 0) {
1376 j = FALSE; /* found mismatching tokens */
1377 break;
1378 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001379
H. Peter Anvine2c80182005-01-15 22:15:51 +00001380 t = t->next;
1381 tt = tt->next;
1382 }
1383 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
1384 j = FALSE; /* trailing gunk on one end or other */
1385 if (i == PP_IFNIDN || i == PP_ELIFNIDN ||
1386 i == PP_IFNIDNI || i == PP_ELIFNIDNI)
1387 j = !j;
1388 free_tlist(tline);
1389 return j;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001390
H. Peter Anvine2c80182005-01-15 22:15:51 +00001391 case PP_IFMACRO:
1392 case PP_ELIFMACRO:
1393 case PP_IFNMACRO:
1394 case PP_ELIFNMACRO:
1395 {
1396 int found = 0;
1397 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001398
H. Peter Anvine2c80182005-01-15 22:15:51 +00001399 tline = tline->next;
1400 skip_white_(tline);
1401 tline = expand_id(tline);
1402 if (!tok_type_(tline, TOK_ID)) {
1403 error(ERR_NONFATAL,
H. Peter Anvin4169a472007-09-12 01:29:43 +00001404 "`%s' expects a macro name", pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001405 return -1;
1406 }
1407 searching.name = nasm_strdup(tline->text);
1408 searching.casesense = (i == PP_MACRO);
1409 searching.plus = FALSE;
1410 searching.nolist = FALSE;
1411 searching.in_progress = FALSE;
1412 searching.rep_nest = NULL;
1413 searching.nparam_min = 0;
1414 searching.nparam_max = INT_MAX;
1415 tline = expand_smacro(tline->next);
1416 skip_white_(tline);
1417 if (!tline) {
1418 } else if (!tok_type_(tline, TOK_NUMBER)) {
1419 error(ERR_NONFATAL,
1420 "`%s' expects a parameter count or nothing",
H. Peter Anvin4169a472007-09-12 01:29:43 +00001421 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001422 } else {
1423 searching.nparam_min = searching.nparam_max =
1424 readnum(tline->text, &j);
1425 if (j)
1426 error(ERR_NONFATAL,
1427 "unable to parse parameter count `%s'",
1428 tline->text);
1429 }
1430 if (tline && tok_is_(tline->next, "-")) {
1431 tline = tline->next->next;
1432 if (tok_is_(tline, "*"))
1433 searching.nparam_max = INT_MAX;
1434 else if (!tok_type_(tline, TOK_NUMBER))
1435 error(ERR_NONFATAL,
1436 "`%s' expects a parameter count after `-'",
H. Peter Anvin4169a472007-09-12 01:29:43 +00001437 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001438 else {
1439 searching.nparam_max = readnum(tline->text, &j);
1440 if (j)
1441 error(ERR_NONFATAL,
1442 "unable to parse parameter count `%s'",
1443 tline->text);
1444 if (searching.nparam_min > searching.nparam_max)
1445 error(ERR_NONFATAL,
1446 "minimum parameter count exceeds maximum");
1447 }
1448 }
1449 if (tline && tok_is_(tline->next, "+")) {
1450 tline = tline->next;
1451 searching.plus = TRUE;
1452 }
1453 mmac = mmacros[hash(searching.name)];
1454 while (mmac) {
1455 if (!strcmp(mmac->name, searching.name) &&
1456 (mmac->nparam_min <= searching.nparam_max
1457 || searching.plus)
1458 && (searching.nparam_min <= mmac->nparam_max
1459 || mmac->plus)) {
1460 found = TRUE;
1461 break;
1462 }
1463 mmac = mmac->next;
1464 }
1465 nasm_free(searching.name);
1466 free_tlist(origline);
1467 if (i == PP_IFNMACRO || i == PP_ELIFNMACRO)
1468 found = !found;
1469 return found;
1470 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001471
H. Peter Anvine2c80182005-01-15 22:15:51 +00001472 case PP_IFID:
1473 case PP_ELIFID:
1474 case PP_IFNID:
1475 case PP_ELIFNID:
1476 case PP_IFNUM:
1477 case PP_ELIFNUM:
1478 case PP_IFNNUM:
1479 case PP_ELIFNNUM:
1480 case PP_IFSTR:
1481 case PP_ELIFSTR:
1482 case PP_IFNSTR:
1483 case PP_ELIFNSTR:
1484 tline = expand_smacro(tline);
1485 t = tline;
1486 while (tok_type_(t, TOK_WHITESPACE))
1487 t = t->next;
1488 j = FALSE; /* placate optimiser */
1489 if (t)
1490 switch (i) {
1491 case PP_IFID:
1492 case PP_ELIFID:
1493 case PP_IFNID:
1494 case PP_ELIFNID:
1495 j = (t->type == TOK_ID);
1496 break;
1497 case PP_IFNUM:
1498 case PP_ELIFNUM:
1499 case PP_IFNNUM:
1500 case PP_ELIFNNUM:
1501 j = (t->type == TOK_NUMBER);
1502 break;
1503 case PP_IFSTR:
1504 case PP_ELIFSTR:
1505 case PP_IFNSTR:
1506 case PP_ELIFNSTR:
1507 j = (t->type == TOK_STRING);
1508 break;
1509 }
1510 if (i == PP_IFNID || i == PP_ELIFNID ||
1511 i == PP_IFNNUM || i == PP_ELIFNNUM ||
1512 i == PP_IFNSTR || i == PP_ELIFNSTR)
1513 j = !j;
1514 free_tlist(tline);
1515 return j;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001516
H. Peter Anvine2c80182005-01-15 22:15:51 +00001517 case PP_IF:
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001518 case PP_IFN:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001519 case PP_ELIF:
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001520 case PP_ELIFN:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001521 t = tline = expand_smacro(tline);
1522 tptr = &t;
1523 tokval.t_type = TOKEN_INVALID;
1524 evalresult = evaluate(ppscan, tptr, &tokval,
1525 NULL, pass | CRITICAL, error, NULL);
1526 free_tlist(tline);
1527 if (!evalresult)
1528 return -1;
1529 if (tokval.t_type)
1530 error(ERR_WARNING,
1531 "trailing garbage after expression ignored");
1532 if (!is_simple(evalresult)) {
1533 error(ERR_NONFATAL,
H. Peter Anvin4169a472007-09-12 01:29:43 +00001534 "non-constant value given to `%s'", pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001535 return -1;
1536 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001537 j = reloc_value(evalresult) != 0;
1538 if (i == PP_IFN || i == PP_ELIFN)
1539 j = !j;
1540 return j;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001541 default:
1542 error(ERR_FATAL,
1543 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00001544 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001545 free_tlist(origline);
1546 return -1; /* yeah, right */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001547 }
1548}
1549
1550/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001551 * Expand macros in a string. Used in %error and %include directives.
Keith Kaniosb7a89542007-04-12 02:40:54 +00001552 * First tokenize the string, apply "expand_smacro" and then de-tokenize back.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001553 * The returned variable should ALWAYS be freed after usage.
1554 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001555void expand_macros_in_string(char **p)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001556{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001557 Token *line = tokenize(*p);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001558 line = expand_smacro(line);
1559 *p = detoken(line, FALSE);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001560}
1561
Ed Beroset3ab3f412002-06-11 03:31:49 +00001562/**
1563 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001564 * Find out if a line contains a preprocessor directive, and deal
1565 * with it if so.
1566 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001567 * If a directive _is_ found, it is the responsibility of this routine
1568 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001569 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001570 * @param tline a pointer to the current tokeninzed line linked list
1571 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001572 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001573 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001574static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001575{
H. Peter Anvin4169a472007-09-12 01:29:43 +00001576 enum preproc_token i;
1577 int j;
1578 int nparam, nolist;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001579 int64_t k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001580 int offset;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001581 char *p, *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001582 Include *inc;
1583 Context *ctx;
1584 Cond *cond;
1585 SMacro *smac, **smhead;
1586 MMacro *mmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001587 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1588 Line *l;
1589 struct tokenval tokval;
1590 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001591 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001592
1593 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001594
H. Peter Anvineba20a72002-04-30 20:53:55 +00001595 skip_white_(tline);
1596 if (!tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001597 (tline->text[1] == '%' || tline->text[1] == '$'
1598 || tline->text[1] == '!'))
1599 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001600
H. Peter Anvin4169a472007-09-12 01:29:43 +00001601 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001602
1603 /*
1604 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00001605 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001606 * we should ignore all directives except for condition
1607 * directives.
1608 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001609 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001610 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
1611 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001612 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001613
1614 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001615 * If we're defining a macro or reading a %rep block, we should
1616 * ignore all directives except for %macro/%imacro (which
1617 * generate an error), %endm/%endmacro, and (only if we're in a
H. Peter Anvineba20a72002-04-30 20:53:55 +00001618 * %rep block) %endrep. If we're in a %rep block, another %rep
1619 * causes an error, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001620 */
1621 if (defining && i != PP_MACRO && i != PP_IMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001622 i != PP_ENDMACRO && i != PP_ENDM &&
1623 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
1624 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001625 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001626
H. Peter Anvin4169a472007-09-12 01:29:43 +00001627 switch (i) {
1628 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001629 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
1630 tline->text);
1631 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001632
H. Peter Anvine2c80182005-01-15 22:15:51 +00001633 case PP_STACKSIZE:
1634 /* Directive to tell NASM what the default stack size is. The
1635 * default is for a 16-bit stack, and this can be overriden with
1636 * %stacksize large.
1637 * the following form:
1638 *
1639 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1640 */
1641 tline = tline->next;
1642 if (tline && tline->type == TOK_WHITESPACE)
1643 tline = tline->next;
1644 if (!tline || tline->type != TOK_ID) {
1645 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
1646 free_tlist(origline);
1647 return DIRECTIVE_FOUND;
1648 }
1649 if (nasm_stricmp(tline->text, "flat") == 0) {
1650 /* All subsequent ARG directives are for a 32-bit stack */
1651 StackSize = 4;
1652 StackPointer = "ebp";
1653 ArgOffset = 8;
1654 LocalOffset = 4;
1655 } else if (nasm_stricmp(tline->text, "large") == 0) {
1656 /* All subsequent ARG directives are for a 16-bit stack,
1657 * far function call.
1658 */
1659 StackSize = 2;
1660 StackPointer = "bp";
1661 ArgOffset = 4;
1662 LocalOffset = 2;
1663 } else if (nasm_stricmp(tline->text, "small") == 0) {
1664 /* All subsequent ARG directives are for a 16-bit stack,
1665 * far function call. We don't support near functions.
1666 */
1667 StackSize = 2;
1668 StackPointer = "bp";
1669 ArgOffset = 6;
1670 LocalOffset = 2;
1671 } else {
1672 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
1673 free_tlist(origline);
1674 return DIRECTIVE_FOUND;
1675 }
1676 free_tlist(origline);
1677 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001678
H. Peter Anvine2c80182005-01-15 22:15:51 +00001679 case PP_ARG:
1680 /* TASM like ARG directive to define arguments to functions, in
1681 * the following form:
1682 *
1683 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1684 */
1685 offset = ArgOffset;
1686 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001687 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001688 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001689
H. Peter Anvine2c80182005-01-15 22:15:51 +00001690 /* Find the argument name */
1691 tline = tline->next;
1692 if (tline && tline->type == TOK_WHITESPACE)
1693 tline = tline->next;
1694 if (!tline || tline->type != TOK_ID) {
1695 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
1696 free_tlist(origline);
1697 return DIRECTIVE_FOUND;
1698 }
1699 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001700
H. Peter Anvine2c80182005-01-15 22:15:51 +00001701 /* Find the argument size type */
1702 tline = tline->next;
1703 if (!tline || tline->type != TOK_OTHER
1704 || tline->text[0] != ':') {
1705 error(ERR_NONFATAL,
1706 "Syntax error processing `%%arg' directive");
1707 free_tlist(origline);
1708 return DIRECTIVE_FOUND;
1709 }
1710 tline = tline->next;
1711 if (!tline || tline->type != TOK_ID) {
1712 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
1713 free_tlist(origline);
1714 return DIRECTIVE_FOUND;
1715 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001716
H. Peter Anvine2c80182005-01-15 22:15:51 +00001717 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001718 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001719 tt = expand_smacro(tt);
1720 if (nasm_stricmp(tt->text, "byte") == 0) {
1721 size = MAX(StackSize, 1);
1722 } else if (nasm_stricmp(tt->text, "word") == 0) {
1723 size = MAX(StackSize, 2);
1724 } else if (nasm_stricmp(tt->text, "dword") == 0) {
1725 size = MAX(StackSize, 4);
1726 } else if (nasm_stricmp(tt->text, "qword") == 0) {
1727 size = MAX(StackSize, 8);
1728 } else if (nasm_stricmp(tt->text, "tword") == 0) {
1729 size = MAX(StackSize, 10);
1730 } else {
1731 error(ERR_NONFATAL,
1732 "Invalid size type for `%%arg' missing directive");
1733 free_tlist(tt);
1734 free_tlist(origline);
1735 return DIRECTIVE_FOUND;
1736 }
1737 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001738
H. Peter Anvine2c80182005-01-15 22:15:51 +00001739 /* Now define the macro for the argument */
1740 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
1741 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001742 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001743 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001744
H. Peter Anvine2c80182005-01-15 22:15:51 +00001745 /* Move to the next argument in the list */
1746 tline = tline->next;
1747 if (tline && tline->type == TOK_WHITESPACE)
1748 tline = tline->next;
1749 }
1750 while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1751 free_tlist(origline);
1752 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001753
H. Peter Anvine2c80182005-01-15 22:15:51 +00001754 case PP_LOCAL:
1755 /* TASM like LOCAL directive to define local variables for a
1756 * function, in the following form:
1757 *
1758 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
1759 *
1760 * The '= LocalSize' at the end is ignored by NASM, but is
1761 * required by TASM to define the local parameter size (and used
1762 * by the TASM macro package).
1763 */
1764 offset = LocalOffset;
1765 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001766 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001767 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001768
H. Peter Anvine2c80182005-01-15 22:15:51 +00001769 /* Find the argument name */
1770 tline = tline->next;
1771 if (tline && tline->type == TOK_WHITESPACE)
1772 tline = tline->next;
1773 if (!tline || tline->type != TOK_ID) {
1774 error(ERR_NONFATAL,
1775 "`%%local' missing argument parameter");
1776 free_tlist(origline);
1777 return DIRECTIVE_FOUND;
1778 }
1779 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001780
H. Peter Anvine2c80182005-01-15 22:15:51 +00001781 /* Find the argument size type */
1782 tline = tline->next;
1783 if (!tline || tline->type != TOK_OTHER
1784 || tline->text[0] != ':') {
1785 error(ERR_NONFATAL,
1786 "Syntax error processing `%%local' directive");
1787 free_tlist(origline);
1788 return DIRECTIVE_FOUND;
1789 }
1790 tline = tline->next;
1791 if (!tline || tline->type != TOK_ID) {
1792 error(ERR_NONFATAL,
1793 "`%%local' missing size type parameter");
1794 free_tlist(origline);
1795 return DIRECTIVE_FOUND;
1796 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001797
H. Peter Anvine2c80182005-01-15 22:15:51 +00001798 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001799 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001800 tt = expand_smacro(tt);
1801 if (nasm_stricmp(tt->text, "byte") == 0) {
1802 size = MAX(StackSize, 1);
1803 } else if (nasm_stricmp(tt->text, "word") == 0) {
1804 size = MAX(StackSize, 2);
1805 } else if (nasm_stricmp(tt->text, "dword") == 0) {
1806 size = MAX(StackSize, 4);
1807 } else if (nasm_stricmp(tt->text, "qword") == 0) {
1808 size = MAX(StackSize, 8);
1809 } else if (nasm_stricmp(tt->text, "tword") == 0) {
1810 size = MAX(StackSize, 10);
1811 } else {
1812 error(ERR_NONFATAL,
1813 "Invalid size type for `%%local' missing directive");
1814 free_tlist(tt);
1815 free_tlist(origline);
1816 return DIRECTIVE_FOUND;
1817 }
1818 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001819
H. Peter Anvine2c80182005-01-15 22:15:51 +00001820 /* Now define the macro for the argument */
1821 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
1822 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001823 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001824 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001825
H. Peter Anvine2c80182005-01-15 22:15:51 +00001826 /* Now define the assign to setup the enter_c macro correctly */
1827 snprintf(directive, sizeof(directive),
1828 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001829 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001830
H. Peter Anvine2c80182005-01-15 22:15:51 +00001831 /* Move to the next argument in the list */
1832 tline = tline->next;
1833 if (tline && tline->type == TOK_WHITESPACE)
1834 tline = tline->next;
1835 }
1836 while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1837 free_tlist(origline);
1838 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001839
H. Peter Anvine2c80182005-01-15 22:15:51 +00001840 case PP_CLEAR:
1841 if (tline->next)
1842 error(ERR_WARNING, "trailing garbage after `%%clear' ignored");
1843 for (j = 0; j < NHASH; j++) {
1844 while (mmacros[j]) {
1845 MMacro *m = mmacros[j];
1846 mmacros[j] = m->next;
1847 free_mmacro(m);
1848 }
1849 while (smacros[j]) {
1850 SMacro *s = smacros[j];
1851 smacros[j] = smacros[j]->next;
1852 nasm_free(s->name);
1853 free_tlist(s->expansion);
1854 nasm_free(s);
1855 }
1856 }
1857 free_tlist(origline);
1858 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001859
H. Peter Anvine2c80182005-01-15 22:15:51 +00001860 case PP_INCLUDE:
1861 tline = tline->next;
1862 skip_white_(tline);
1863 if (!tline || (tline->type != TOK_STRING &&
1864 tline->type != TOK_INTERNAL_STRING)) {
1865 error(ERR_NONFATAL, "`%%include' expects a file name");
1866 free_tlist(origline);
1867 return DIRECTIVE_FOUND; /* but we did _something_ */
1868 }
1869 if (tline->next)
1870 error(ERR_WARNING,
1871 "trailing garbage after `%%include' ignored");
1872 if (tline->type != TOK_INTERNAL_STRING) {
1873 p = tline->text + 1; /* point past the quote to the name */
1874 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
1875 } else
1876 p = tline->text; /* internal_string is easier */
1877 expand_macros_in_string(&p);
1878 inc = nasm_malloc(sizeof(Include));
1879 inc->next = istk;
1880 inc->conds = NULL;
1881 inc->fp = inc_fopen(p);
1882 inc->fname = src_set_fname(p);
1883 inc->lineno = src_set_linnum(0);
1884 inc->lineinc = 1;
1885 inc->expansion = NULL;
1886 inc->mstk = NULL;
1887 istk = inc;
1888 list->uplevel(LIST_INCLUDE);
1889 free_tlist(origline);
1890 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001891
H. Peter Anvine2c80182005-01-15 22:15:51 +00001892 case PP_PUSH:
1893 tline = tline->next;
1894 skip_white_(tline);
1895 tline = expand_id(tline);
1896 if (!tok_type_(tline, TOK_ID)) {
1897 error(ERR_NONFATAL, "`%%push' expects a context identifier");
1898 free_tlist(origline);
1899 return DIRECTIVE_FOUND; /* but we did _something_ */
1900 }
1901 if (tline->next)
1902 error(ERR_WARNING, "trailing garbage after `%%push' ignored");
1903 ctx = nasm_malloc(sizeof(Context));
1904 ctx->next = cstk;
1905 ctx->localmac = NULL;
1906 ctx->name = nasm_strdup(tline->text);
1907 ctx->number = unique++;
1908 cstk = ctx;
1909 free_tlist(origline);
1910 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001911
H. Peter Anvine2c80182005-01-15 22:15:51 +00001912 case PP_REPL:
1913 tline = tline->next;
1914 skip_white_(tline);
1915 tline = expand_id(tline);
1916 if (!tok_type_(tline, TOK_ID)) {
1917 error(ERR_NONFATAL, "`%%repl' expects a context identifier");
1918 free_tlist(origline);
1919 return DIRECTIVE_FOUND; /* but we did _something_ */
1920 }
1921 if (tline->next)
1922 error(ERR_WARNING, "trailing garbage after `%%repl' ignored");
1923 if (!cstk)
1924 error(ERR_NONFATAL, "`%%repl': context stack is empty");
1925 else {
1926 nasm_free(cstk->name);
1927 cstk->name = nasm_strdup(tline->text);
1928 }
1929 free_tlist(origline);
1930 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001931
H. Peter Anvine2c80182005-01-15 22:15:51 +00001932 case PP_POP:
1933 if (tline->next)
1934 error(ERR_WARNING, "trailing garbage after `%%pop' ignored");
1935 if (!cstk)
1936 error(ERR_NONFATAL, "`%%pop': context stack is already empty");
1937 else
1938 ctx_pop();
1939 free_tlist(origline);
1940 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001941
H. Peter Anvine2c80182005-01-15 22:15:51 +00001942 case PP_ERROR:
1943 tline->next = expand_smacro(tline->next);
1944 tline = tline->next;
1945 skip_white_(tline);
1946 if (tok_type_(tline, TOK_STRING)) {
1947 p = tline->text + 1; /* point past the quote to the name */
1948 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
1949 expand_macros_in_string(&p);
1950 error(ERR_NONFATAL, "%s", p);
1951 nasm_free(p);
1952 } else {
1953 p = detoken(tline, FALSE);
1954 error(ERR_WARNING, "%s", p);
1955 nasm_free(p);
1956 }
1957 free_tlist(origline);
1958 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001959
H. Peter Anvine2c80182005-01-15 22:15:51 +00001960 case PP_IF:
1961 case PP_IFCTX:
1962 case PP_IFDEF:
1963 case PP_IFID:
1964 case PP_IFIDN:
1965 case PP_IFIDNI:
1966 case PP_IFMACRO:
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001967 case PP_IFN:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001968 case PP_IFNCTX:
1969 case PP_IFNDEF:
1970 case PP_IFNID:
1971 case PP_IFNIDN:
1972 case PP_IFNIDNI:
1973 case PP_IFNMACRO:
1974 case PP_IFNNUM:
1975 case PP_IFNSTR:
1976 case PP_IFNUM:
1977 case PP_IFSTR:
1978 if (istk->conds && !emitting(istk->conds->state))
1979 j = COND_NEVER;
1980 else {
1981 j = if_condition(tline->next, i);
1982 tline->next = NULL; /* it got freed */
1983 free_tlist(origline);
1984 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
1985 }
1986 cond = nasm_malloc(sizeof(Cond));
1987 cond->next = istk->conds;
1988 cond->state = j;
1989 istk->conds = cond;
1990 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001991
H. Peter Anvine2c80182005-01-15 22:15:51 +00001992 case PP_ELIF:
1993 case PP_ELIFCTX:
1994 case PP_ELIFDEF:
1995 case PP_ELIFID:
1996 case PP_ELIFIDN:
1997 case PP_ELIFIDNI:
1998 case PP_ELIFMACRO:
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001999 case PP_ELIFN:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002000 case PP_ELIFNCTX:
2001 case PP_ELIFNDEF:
2002 case PP_ELIFNID:
2003 case PP_ELIFNIDN:
2004 case PP_ELIFNIDNI:
2005 case PP_ELIFNMACRO:
2006 case PP_ELIFNNUM:
2007 case PP_ELIFNSTR:
2008 case PP_ELIFNUM:
2009 case PP_ELIFSTR:
2010 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00002011 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002012 if (emitting(istk->conds->state)
2013 || istk->conds->state == COND_NEVER)
2014 istk->conds->state = COND_NEVER;
2015 else {
2016 /*
2017 * IMPORTANT: In the case of %if, we will already have
2018 * called expand_mmac_params(); however, if we're
2019 * processing an %elif we must have been in a
2020 * non-emitting mode, which would have inhibited
2021 * the normal invocation of expand_mmac_params(). Therefore,
2022 * we have to do it explicitly here.
2023 */
2024 j = if_condition(expand_mmac_params(tline->next), i);
2025 tline->next = NULL; /* it got freed */
2026 free_tlist(origline);
2027 istk->conds->state =
2028 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2029 }
2030 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002031
H. Peter Anvine2c80182005-01-15 22:15:51 +00002032 case PP_ELSE:
2033 if (tline->next)
2034 error(ERR_WARNING, "trailing garbage after `%%else' ignored");
2035 if (!istk->conds)
2036 error(ERR_FATAL, "`%%else': no matching `%%if'");
2037 if (emitting(istk->conds->state)
2038 || istk->conds->state == COND_NEVER)
2039 istk->conds->state = COND_ELSE_FALSE;
2040 else
2041 istk->conds->state = COND_ELSE_TRUE;
2042 free_tlist(origline);
2043 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002044
H. Peter Anvine2c80182005-01-15 22:15:51 +00002045 case PP_ENDIF:
2046 if (tline->next)
2047 error(ERR_WARNING, "trailing garbage after `%%endif' ignored");
2048 if (!istk->conds)
2049 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2050 cond = istk->conds;
2051 istk->conds = cond->next;
2052 nasm_free(cond);
2053 free_tlist(origline);
2054 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002055
H. Peter Anvine2c80182005-01-15 22:15:51 +00002056 case PP_MACRO:
2057 case PP_IMACRO:
2058 if (defining)
2059 error(ERR_FATAL,
2060 "`%%%smacro': already defining a macro",
2061 (i == PP_IMACRO ? "i" : ""));
2062 tline = tline->next;
2063 skip_white_(tline);
2064 tline = expand_id(tline);
2065 if (!tok_type_(tline, TOK_ID)) {
2066 error(ERR_NONFATAL,
2067 "`%%%smacro' expects a macro name",
2068 (i == PP_IMACRO ? "i" : ""));
2069 return DIRECTIVE_FOUND;
2070 }
2071 defining = nasm_malloc(sizeof(MMacro));
2072 defining->name = nasm_strdup(tline->text);
2073 defining->casesense = (i == PP_MACRO);
2074 defining->plus = FALSE;
2075 defining->nolist = FALSE;
2076 defining->in_progress = FALSE;
2077 defining->rep_nest = NULL;
2078 tline = expand_smacro(tline->next);
2079 skip_white_(tline);
2080 if (!tok_type_(tline, TOK_NUMBER)) {
2081 error(ERR_NONFATAL,
2082 "`%%%smacro' expects a parameter count",
2083 (i == PP_IMACRO ? "i" : ""));
2084 defining->nparam_min = defining->nparam_max = 0;
2085 } else {
2086 defining->nparam_min = defining->nparam_max =
2087 readnum(tline->text, &j);
2088 if (j)
2089 error(ERR_NONFATAL,
2090 "unable to parse parameter count `%s'", tline->text);
2091 }
2092 if (tline && tok_is_(tline->next, "-")) {
2093 tline = tline->next->next;
2094 if (tok_is_(tline, "*"))
2095 defining->nparam_max = INT_MAX;
2096 else if (!tok_type_(tline, TOK_NUMBER))
2097 error(ERR_NONFATAL,
2098 "`%%%smacro' expects a parameter count after `-'",
2099 (i == PP_IMACRO ? "i" : ""));
2100 else {
2101 defining->nparam_max = readnum(tline->text, &j);
2102 if (j)
2103 error(ERR_NONFATAL,
2104 "unable to parse parameter count `%s'",
2105 tline->text);
2106 if (defining->nparam_min > defining->nparam_max)
2107 error(ERR_NONFATAL,
2108 "minimum parameter count exceeds maximum");
2109 }
2110 }
2111 if (tline && tok_is_(tline->next, "+")) {
2112 tline = tline->next;
2113 defining->plus = TRUE;
2114 }
2115 if (tline && tok_type_(tline->next, TOK_ID) &&
2116 !nasm_stricmp(tline->next->text, ".nolist")) {
2117 tline = tline->next;
2118 defining->nolist = TRUE;
2119 }
2120 mmac = mmacros[hash(defining->name)];
2121 while (mmac) {
2122 if (!strcmp(mmac->name, defining->name) &&
2123 (mmac->nparam_min <= defining->nparam_max
2124 || defining->plus)
2125 && (defining->nparam_min <= mmac->nparam_max
2126 || mmac->plus)) {
2127 error(ERR_WARNING,
2128 "redefining multi-line macro `%s'", defining->name);
2129 break;
2130 }
2131 mmac = mmac->next;
2132 }
2133 /*
2134 * Handle default parameters.
2135 */
2136 if (tline && tline->next) {
2137 defining->dlist = tline->next;
2138 tline->next = NULL;
2139 count_mmac_params(defining->dlist, &defining->ndefs,
2140 &defining->defaults);
2141 } else {
2142 defining->dlist = NULL;
2143 defining->defaults = NULL;
2144 }
2145 defining->expansion = NULL;
2146 free_tlist(origline);
2147 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002148
H. Peter Anvine2c80182005-01-15 22:15:51 +00002149 case PP_ENDM:
2150 case PP_ENDMACRO:
2151 if (!defining) {
2152 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2153 return DIRECTIVE_FOUND;
2154 }
2155 k = hash(defining->name);
2156 defining->next = mmacros[k];
2157 mmacros[k] = defining;
2158 defining = NULL;
2159 free_tlist(origline);
2160 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002161
H. Peter Anvine2c80182005-01-15 22:15:51 +00002162 case PP_ROTATE:
2163 if (tline->next && tline->next->type == TOK_WHITESPACE)
2164 tline = tline->next;
2165 if (tline->next == NULL) {
2166 free_tlist(origline);
2167 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2168 return DIRECTIVE_FOUND;
2169 }
2170 t = expand_smacro(tline->next);
2171 tline->next = NULL;
2172 free_tlist(origline);
2173 tline = t;
2174 tptr = &t;
2175 tokval.t_type = TOKEN_INVALID;
2176 evalresult =
2177 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2178 free_tlist(tline);
2179 if (!evalresult)
2180 return DIRECTIVE_FOUND;
2181 if (tokval.t_type)
2182 error(ERR_WARNING,
2183 "trailing garbage after expression ignored");
2184 if (!is_simple(evalresult)) {
2185 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2186 return DIRECTIVE_FOUND;
2187 }
2188 mmac = istk->mstk;
2189 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2190 mmac = mmac->next_active;
2191 if (!mmac) {
2192 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2193 } else if (mmac->nparam == 0) {
2194 error(ERR_NONFATAL,
2195 "`%%rotate' invoked within macro without parameters");
2196 } else {
2197 mmac->rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002198
H. Peter Anvine2c80182005-01-15 22:15:51 +00002199 if (mmac->rotate < 0)
2200 mmac->rotate =
2201 mmac->nparam - (-mmac->rotate) % mmac->nparam;
2202 mmac->rotate %= mmac->nparam;
2203 }
2204 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002205
H. Peter Anvine2c80182005-01-15 22:15:51 +00002206 case PP_REP:
2207 nolist = FALSE;
2208 do {
2209 tline = tline->next;
2210 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002211
H. Peter Anvine2c80182005-01-15 22:15:51 +00002212 if (tok_type_(tline, TOK_ID) &&
2213 nasm_stricmp(tline->text, ".nolist") == 0) {
2214 nolist = TRUE;
2215 do {
2216 tline = tline->next;
2217 } while (tok_type_(tline, TOK_WHITESPACE));
2218 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002219
H. Peter Anvine2c80182005-01-15 22:15:51 +00002220 if (tline) {
2221 t = expand_smacro(tline);
2222 tptr = &t;
2223 tokval.t_type = TOKEN_INVALID;
2224 evalresult =
2225 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2226 if (!evalresult) {
2227 free_tlist(origline);
2228 return DIRECTIVE_FOUND;
2229 }
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 `%%rep'");
2235 return DIRECTIVE_FOUND;
2236 }
2237 i = (int)reloc_value(evalresult) + 1;
2238 } else {
2239 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
2240 i = 0;
2241 }
2242 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002243
H. Peter Anvine2c80182005-01-15 22:15:51 +00002244 tmp_defining = defining;
2245 defining = nasm_malloc(sizeof(MMacro));
2246 defining->name = NULL; /* flags this macro as a %rep block */
2247 defining->casesense = 0;
2248 defining->plus = FALSE;
2249 defining->nolist = nolist;
2250 defining->in_progress = i;
2251 defining->nparam_min = defining->nparam_max = 0;
2252 defining->defaults = NULL;
2253 defining->dlist = NULL;
2254 defining->expansion = NULL;
2255 defining->next_active = istk->mstk;
2256 defining->rep_nest = tmp_defining;
2257 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002258
H. Peter Anvine2c80182005-01-15 22:15:51 +00002259 case PP_ENDREP:
2260 if (!defining || defining->name) {
2261 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2262 return DIRECTIVE_FOUND;
2263 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002264
H. Peter Anvine2c80182005-01-15 22:15:51 +00002265 /*
2266 * Now we have a "macro" defined - although it has no name
2267 * and we won't be entering it in the hash tables - we must
2268 * push a macro-end marker for it on to istk->expansion.
2269 * After that, it will take care of propagating itself (a
2270 * macro-end marker line for a macro which is really a %rep
2271 * block will cause the macro to be re-expanded, complete
2272 * with another macro-end marker to ensure the process
2273 * continues) until the whole expansion is forcibly removed
2274 * from istk->expansion by a %exitrep.
2275 */
2276 l = nasm_malloc(sizeof(Line));
2277 l->next = istk->expansion;
2278 l->finishes = defining;
2279 l->first = NULL;
2280 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002281
H. Peter Anvine2c80182005-01-15 22:15:51 +00002282 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002283
H. Peter Anvine2c80182005-01-15 22:15:51 +00002284 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2285 tmp_defining = defining;
2286 defining = defining->rep_nest;
2287 free_tlist(origline);
2288 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002289
H. Peter Anvine2c80182005-01-15 22:15:51 +00002290 case PP_EXITREP:
2291 /*
2292 * We must search along istk->expansion until we hit a
2293 * macro-end marker for a macro with no name. Then we set
2294 * its `in_progress' flag to 0.
2295 */
2296 for (l = istk->expansion; l; l = l->next)
2297 if (l->finishes && !l->finishes->name)
2298 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002299
H. Peter Anvine2c80182005-01-15 22:15:51 +00002300 if (l)
2301 l->finishes->in_progress = 0;
2302 else
2303 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2304 free_tlist(origline);
2305 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002306
H. Peter Anvine2c80182005-01-15 22:15:51 +00002307 case PP_XDEFINE:
2308 case PP_IXDEFINE:
2309 case PP_DEFINE:
2310 case PP_IDEFINE:
2311 tline = tline->next;
2312 skip_white_(tline);
2313 tline = expand_id(tline);
2314 if (!tline || (tline->type != TOK_ID &&
2315 (tline->type != TOK_PREPROC_ID ||
2316 tline->text[1] != '$'))) {
2317 error(ERR_NONFATAL,
2318 "`%%%s%sdefine' expects a macro identifier",
2319 ((i == PP_IDEFINE || i == PP_IXDEFINE) ? "i" : ""),
2320 ((i == PP_XDEFINE || i == PP_IXDEFINE) ? "x" : ""));
2321 free_tlist(origline);
2322 return DIRECTIVE_FOUND;
2323 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002324
H. Peter Anvine2c80182005-01-15 22:15:51 +00002325 ctx = get_ctx(tline->text, FALSE);
2326 if (!ctx)
2327 smhead = &smacros[hash(tline->text)];
2328 else
2329 smhead = &ctx->localmac;
2330 mname = tline->text;
2331 last = tline;
2332 param_start = tline = tline->next;
2333 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002334
H. Peter Anvine2c80182005-01-15 22:15:51 +00002335 /* Expand the macro definition now for %xdefine and %ixdefine */
2336 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2337 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002338
H. Peter Anvine2c80182005-01-15 22:15:51 +00002339 if (tok_is_(tline, "(")) {
2340 /*
2341 * This macro has parameters.
2342 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002343
H. Peter Anvine2c80182005-01-15 22:15:51 +00002344 tline = tline->next;
2345 while (1) {
2346 skip_white_(tline);
2347 if (!tline) {
2348 error(ERR_NONFATAL, "parameter identifier expected");
2349 free_tlist(origline);
2350 return DIRECTIVE_FOUND;
2351 }
2352 if (tline->type != TOK_ID) {
2353 error(ERR_NONFATAL,
2354 "`%s': parameter identifier expected",
2355 tline->text);
2356 free_tlist(origline);
2357 return DIRECTIVE_FOUND;
2358 }
2359 tline->type = TOK_SMAC_PARAM + nparam++;
2360 tline = tline->next;
2361 skip_white_(tline);
2362 if (tok_is_(tline, ",")) {
2363 tline = tline->next;
2364 continue;
2365 }
2366 if (!tok_is_(tline, ")")) {
2367 error(ERR_NONFATAL,
2368 "`)' expected to terminate macro template");
2369 free_tlist(origline);
2370 return DIRECTIVE_FOUND;
2371 }
2372 break;
2373 }
2374 last = tline;
2375 tline = tline->next;
2376 }
2377 if (tok_type_(tline, TOK_WHITESPACE))
2378 last = tline, tline = tline->next;
2379 macro_start = NULL;
2380 last->next = NULL;
2381 t = tline;
2382 while (t) {
2383 if (t->type == TOK_ID) {
2384 for (tt = param_start; tt; tt = tt->next)
2385 if (tt->type >= TOK_SMAC_PARAM &&
2386 !strcmp(tt->text, t->text))
2387 t->type = tt->type;
2388 }
2389 tt = t->next;
2390 t->next = macro_start;
2391 macro_start = t;
2392 t = tt;
2393 }
2394 /*
2395 * Good. We now have a macro name, a parameter count, and a
2396 * token list (in reverse order) for an expansion. We ought
2397 * to be OK just to create an SMacro, store it, and let
2398 * free_tlist have the rest of the line (which we have
2399 * carefully re-terminated after chopping off the expansion
2400 * from the end).
2401 */
2402 if (smacro_defined(ctx, mname, nparam, &smac, i == PP_DEFINE)) {
2403 if (!smac) {
2404 error(ERR_WARNING,
2405 "single-line macro `%s' defined both with and"
2406 " without parameters", mname);
2407 free_tlist(origline);
2408 free_tlist(macro_start);
2409 return DIRECTIVE_FOUND;
2410 } else {
2411 /*
2412 * We're redefining, so we have to take over an
2413 * existing SMacro structure. This means freeing
2414 * what was already in it.
2415 */
2416 nasm_free(smac->name);
2417 free_tlist(smac->expansion);
2418 }
2419 } else {
2420 smac = nasm_malloc(sizeof(SMacro));
2421 smac->next = *smhead;
2422 *smhead = smac;
2423 }
2424 smac->name = nasm_strdup(mname);
2425 smac->casesense = ((i == PP_DEFINE) || (i == PP_XDEFINE));
2426 smac->nparam = nparam;
2427 smac->expansion = macro_start;
2428 smac->in_progress = FALSE;
2429 free_tlist(origline);
2430 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002431
H. Peter Anvine2c80182005-01-15 22:15:51 +00002432 case PP_UNDEF:
2433 tline = tline->next;
2434 skip_white_(tline);
2435 tline = expand_id(tline);
2436 if (!tline || (tline->type != TOK_ID &&
2437 (tline->type != TOK_PREPROC_ID ||
2438 tline->text[1] != '$'))) {
2439 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2440 free_tlist(origline);
2441 return DIRECTIVE_FOUND;
2442 }
2443 if (tline->next) {
2444 error(ERR_WARNING,
2445 "trailing garbage after macro name ignored");
2446 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002447
H. Peter Anvine2c80182005-01-15 22:15:51 +00002448 /* Find the context that symbol belongs to */
2449 ctx = get_ctx(tline->text, FALSE);
2450 if (!ctx)
2451 smhead = &smacros[hash(tline->text)];
2452 else
2453 smhead = &ctx->localmac;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002454
H. Peter Anvine2c80182005-01-15 22:15:51 +00002455 mname = tline->text;
2456 last = tline;
2457 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002458
H. Peter Anvine2c80182005-01-15 22:15:51 +00002459 /*
2460 * We now have a macro name... go hunt for it.
2461 */
2462 while (smacro_defined(ctx, mname, -1, &smac, 1)) {
2463 /* Defined, so we need to find its predecessor and nuke it */
2464 SMacro **s;
2465 for (s = smhead; *s && *s != smac; s = &(*s)->next) ;
2466 if (*s) {
2467 *s = smac->next;
2468 nasm_free(smac->name);
2469 free_tlist(smac->expansion);
2470 nasm_free(smac);
2471 }
2472 }
2473 free_tlist(origline);
2474 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002475
H. Peter Anvine2c80182005-01-15 22:15:51 +00002476 case PP_STRLEN:
2477 tline = tline->next;
2478 skip_white_(tline);
2479 tline = expand_id(tline);
2480 if (!tline || (tline->type != TOK_ID &&
2481 (tline->type != TOK_PREPROC_ID ||
2482 tline->text[1] != '$'))) {
2483 error(ERR_NONFATAL,
2484 "`%%strlen' expects a macro identifier as first parameter");
2485 free_tlist(origline);
2486 return DIRECTIVE_FOUND;
2487 }
2488 ctx = get_ctx(tline->text, FALSE);
2489 if (!ctx)
2490 smhead = &smacros[hash(tline->text)];
2491 else
2492 smhead = &ctx->localmac;
2493 mname = tline->text;
2494 last = tline;
2495 tline = expand_smacro(tline->next);
2496 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002497
H. Peter Anvine2c80182005-01-15 22:15:51 +00002498 t = tline;
2499 while (tok_type_(t, TOK_WHITESPACE))
2500 t = t->next;
2501 /* t should now point to the string */
2502 if (t->type != TOK_STRING) {
2503 error(ERR_NONFATAL,
2504 "`%%strlen` requires string as second parameter");
2505 free_tlist(tline);
2506 free_tlist(origline);
2507 return DIRECTIVE_FOUND;
2508 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002509
H. Peter Anvine2c80182005-01-15 22:15:51 +00002510 macro_start = nasm_malloc(sizeof(*macro_start));
2511 macro_start->next = NULL;
2512 make_tok_num(macro_start, strlen(t->text) - 2);
2513 macro_start->mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002514
H. Peter Anvine2c80182005-01-15 22:15:51 +00002515 /*
2516 * We now have a macro name, an implicit parameter count of
2517 * zero, and a numeric token to use as an expansion. Create
2518 * and store an SMacro.
2519 */
2520 if (smacro_defined(ctx, mname, 0, &smac, i == PP_STRLEN)) {
2521 if (!smac)
2522 error(ERR_WARNING,
2523 "single-line macro `%s' defined both with and"
2524 " without parameters", mname);
2525 else {
2526 /*
2527 * We're redefining, so we have to take over an
2528 * existing SMacro structure. This means freeing
2529 * what was already in it.
2530 */
2531 nasm_free(smac->name);
2532 free_tlist(smac->expansion);
2533 }
2534 } else {
2535 smac = nasm_malloc(sizeof(SMacro));
2536 smac->next = *smhead;
2537 *smhead = smac;
2538 }
2539 smac->name = nasm_strdup(mname);
2540 smac->casesense = (i == PP_STRLEN);
2541 smac->nparam = 0;
2542 smac->expansion = macro_start;
2543 smac->in_progress = FALSE;
2544 free_tlist(tline);
2545 free_tlist(origline);
2546 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002547
H. Peter Anvine2c80182005-01-15 22:15:51 +00002548 case PP_SUBSTR:
2549 tline = tline->next;
2550 skip_white_(tline);
2551 tline = expand_id(tline);
2552 if (!tline || (tline->type != TOK_ID &&
2553 (tline->type != TOK_PREPROC_ID ||
2554 tline->text[1] != '$'))) {
2555 error(ERR_NONFATAL,
2556 "`%%substr' expects a macro identifier as first parameter");
2557 free_tlist(origline);
2558 return DIRECTIVE_FOUND;
2559 }
2560 ctx = get_ctx(tline->text, FALSE);
2561 if (!ctx)
2562 smhead = &smacros[hash(tline->text)];
2563 else
2564 smhead = &ctx->localmac;
2565 mname = tline->text;
2566 last = tline;
2567 tline = expand_smacro(tline->next);
2568 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002569
H. Peter Anvine2c80182005-01-15 22:15:51 +00002570 t = tline->next;
2571 while (tok_type_(t, TOK_WHITESPACE))
2572 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002573
H. Peter Anvine2c80182005-01-15 22:15:51 +00002574 /* t should now point to the string */
2575 if (t->type != TOK_STRING) {
2576 error(ERR_NONFATAL,
2577 "`%%substr` requires string as second parameter");
2578 free_tlist(tline);
2579 free_tlist(origline);
2580 return DIRECTIVE_FOUND;
2581 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002582
H. Peter Anvine2c80182005-01-15 22:15:51 +00002583 tt = t->next;
2584 tptr = &tt;
2585 tokval.t_type = TOKEN_INVALID;
2586 evalresult =
2587 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2588 if (!evalresult) {
2589 free_tlist(tline);
2590 free_tlist(origline);
2591 return DIRECTIVE_FOUND;
2592 }
2593 if (!is_simple(evalresult)) {
2594 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
2595 free_tlist(tline);
2596 free_tlist(origline);
2597 return DIRECTIVE_FOUND;
2598 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002599
H. Peter Anvine2c80182005-01-15 22:15:51 +00002600 macro_start = nasm_malloc(sizeof(*macro_start));
2601 macro_start->next = NULL;
2602 macro_start->text = nasm_strdup("'''");
2603 if (evalresult->value > 0
2604 && evalresult->value < strlen(t->text) - 1) {
2605 macro_start->text[1] = t->text[evalresult->value];
2606 } else {
2607 macro_start->text[2] = '\0';
2608 }
2609 macro_start->type = TOK_STRING;
2610 macro_start->mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002611
H. Peter Anvine2c80182005-01-15 22:15:51 +00002612 /*
2613 * We now have a macro name, an implicit parameter count of
2614 * zero, and a numeric token to use as an expansion. Create
2615 * and store an SMacro.
2616 */
2617 if (smacro_defined(ctx, mname, 0, &smac, i == PP_SUBSTR)) {
2618 if (!smac)
2619 error(ERR_WARNING,
2620 "single-line macro `%s' defined both with and"
2621 " without parameters", mname);
2622 else {
2623 /*
2624 * We're redefining, so we have to take over an
2625 * existing SMacro structure. This means freeing
2626 * what was already in it.
2627 */
2628 nasm_free(smac->name);
2629 free_tlist(smac->expansion);
2630 }
2631 } else {
2632 smac = nasm_malloc(sizeof(SMacro));
2633 smac->next = *smhead;
2634 *smhead = smac;
2635 }
2636 smac->name = nasm_strdup(mname);
2637 smac->casesense = (i == PP_SUBSTR);
2638 smac->nparam = 0;
2639 smac->expansion = macro_start;
2640 smac->in_progress = FALSE;
2641 free_tlist(tline);
2642 free_tlist(origline);
2643 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002644
H. Peter Anvine2c80182005-01-15 22:15:51 +00002645 case PP_ASSIGN:
2646 case PP_IASSIGN:
2647 tline = tline->next;
2648 skip_white_(tline);
2649 tline = expand_id(tline);
2650 if (!tline || (tline->type != TOK_ID &&
2651 (tline->type != TOK_PREPROC_ID ||
2652 tline->text[1] != '$'))) {
2653 error(ERR_NONFATAL,
2654 "`%%%sassign' expects a macro identifier",
2655 (i == PP_IASSIGN ? "i" : ""));
2656 free_tlist(origline);
2657 return DIRECTIVE_FOUND;
2658 }
2659 ctx = get_ctx(tline->text, FALSE);
2660 if (!ctx)
2661 smhead = &smacros[hash(tline->text)];
2662 else
2663 smhead = &ctx->localmac;
2664 mname = tline->text;
2665 last = tline;
2666 tline = expand_smacro(tline->next);
2667 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002668
H. Peter Anvine2c80182005-01-15 22:15:51 +00002669 t = tline;
2670 tptr = &t;
2671 tokval.t_type = TOKEN_INVALID;
2672 evalresult =
2673 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2674 free_tlist(tline);
2675 if (!evalresult) {
2676 free_tlist(origline);
2677 return DIRECTIVE_FOUND;
2678 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002679
H. Peter Anvine2c80182005-01-15 22:15:51 +00002680 if (tokval.t_type)
2681 error(ERR_WARNING,
2682 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00002683
H. Peter Anvine2c80182005-01-15 22:15:51 +00002684 if (!is_simple(evalresult)) {
2685 error(ERR_NONFATAL,
2686 "non-constant value given to `%%%sassign'",
2687 (i == PP_IASSIGN ? "i" : ""));
2688 free_tlist(origline);
2689 return DIRECTIVE_FOUND;
2690 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002691
H. Peter Anvine2c80182005-01-15 22:15:51 +00002692 macro_start = nasm_malloc(sizeof(*macro_start));
2693 macro_start->next = NULL;
2694 make_tok_num(macro_start, reloc_value(evalresult));
2695 macro_start->mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002696
H. Peter Anvine2c80182005-01-15 22:15:51 +00002697 /*
2698 * We now have a macro name, an implicit parameter count of
2699 * zero, and a numeric token to use as an expansion. Create
2700 * and store an SMacro.
2701 */
2702 if (smacro_defined(ctx, mname, 0, &smac, i == PP_ASSIGN)) {
2703 if (!smac)
2704 error(ERR_WARNING,
2705 "single-line macro `%s' defined both with and"
2706 " without parameters", mname);
2707 else {
2708 /*
2709 * We're redefining, so we have to take over an
2710 * existing SMacro structure. This means freeing
2711 * what was already in it.
2712 */
2713 nasm_free(smac->name);
2714 free_tlist(smac->expansion);
2715 }
2716 } else {
2717 smac = nasm_malloc(sizeof(SMacro));
2718 smac->next = *smhead;
2719 *smhead = smac;
2720 }
2721 smac->name = nasm_strdup(mname);
2722 smac->casesense = (i == PP_ASSIGN);
2723 smac->nparam = 0;
2724 smac->expansion = macro_start;
2725 smac->in_progress = FALSE;
2726 free_tlist(origline);
2727 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002728
H. Peter Anvine2c80182005-01-15 22:15:51 +00002729 case PP_LINE:
2730 /*
2731 * Syntax is `%line nnn[+mmm] [filename]'
2732 */
2733 tline = tline->next;
2734 skip_white_(tline);
2735 if (!tok_type_(tline, TOK_NUMBER)) {
2736 error(ERR_NONFATAL, "`%%line' expects line number");
2737 free_tlist(origline);
2738 return DIRECTIVE_FOUND;
2739 }
2740 k = readnum(tline->text, &j);
2741 m = 1;
2742 tline = tline->next;
2743 if (tok_is_(tline, "+")) {
2744 tline = tline->next;
2745 if (!tok_type_(tline, TOK_NUMBER)) {
2746 error(ERR_NONFATAL, "`%%line' expects line increment");
2747 free_tlist(origline);
2748 return DIRECTIVE_FOUND;
2749 }
2750 m = readnum(tline->text, &j);
2751 tline = tline->next;
2752 }
2753 skip_white_(tline);
2754 src_set_linnum(k);
2755 istk->lineinc = m;
2756 if (tline) {
2757 nasm_free(src_set_fname(detoken(tline, FALSE)));
2758 }
2759 free_tlist(origline);
2760 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002761
H. Peter Anvine2c80182005-01-15 22:15:51 +00002762 default:
2763 error(ERR_FATAL,
2764 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00002765 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002766 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002767 }
Ed Beroset3ab3f412002-06-11 03:31:49 +00002768 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002769}
2770
2771/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002772 * Ensure that a macro parameter contains a condition code and
2773 * nothing else. Return the condition code index if so, or -1
2774 * otherwise.
2775 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002776static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002777{
H. Peter Anvin76690a12002-04-30 20:52:49 +00002778 Token *tt;
2779 int i, j, k, m;
2780
H. Peter Anvineba20a72002-04-30 20:53:55 +00002781 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002782 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002783 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002784 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002785 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002786 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002787 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002788
2789 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +00002790 j = elements(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002791 while (j - i > 1) {
2792 k = (j + i) / 2;
2793 m = nasm_stricmp(t->text, conditions[k]);
2794 if (m == 0) {
2795 i = k;
2796 j = -2;
2797 break;
2798 } else if (m < 0) {
2799 j = k;
2800 } else
2801 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002802 }
2803 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002804 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002805 return i;
2806}
2807
2808/*
2809 * Expand MMacro-local things: parameter references (%0, %n, %+n,
2810 * %-n) and MMacro-local identifiers (%%foo).
2811 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002812static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002813{
H. Peter Anvin734b1882002-04-30 21:01:08 +00002814 Token *t, *tt, **tail, *thead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002815
2816 tail = &thead;
2817 thead = NULL;
2818
H. Peter Anvine2c80182005-01-15 22:15:51 +00002819 while (tline) {
2820 if (tline->type == TOK_PREPROC_ID &&
2821 (((tline->text[1] == '+' || tline->text[1] == '-')
2822 && tline->text[2]) || tline->text[1] == '%'
2823 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002824 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002825 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002826 char tmpbuf[30];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002827 int n, i;
2828 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002829
H. Peter Anvine2c80182005-01-15 22:15:51 +00002830 t = tline;
2831 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002832
H. Peter Anvine2c80182005-01-15 22:15:51 +00002833 mac = istk->mstk;
2834 while (mac && !mac->name) /* avoid mistaking %reps for macros */
2835 mac = mac->next_active;
2836 if (!mac)
2837 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
2838 else
2839 switch (t->text[1]) {
2840 /*
2841 * We have to make a substitution of one of the
2842 * forms %1, %-1, %+1, %%foo, %0.
2843 */
2844 case '0':
2845 type = TOK_NUMBER;
2846 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
2847 text = nasm_strdup(tmpbuf);
2848 break;
2849 case '%':
2850 type = TOK_ID;
Keith Kanios93f2e9a2007-04-14 00:10:59 +00002851 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu32".",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002852 mac->unique);
2853 text = nasm_strcat(tmpbuf, t->text + 2);
2854 break;
2855 case '-':
2856 n = atoi(t->text + 2) - 1;
2857 if (n >= mac->nparam)
2858 tt = NULL;
2859 else {
2860 if (mac->nparam > 1)
2861 n = (n + mac->rotate) % mac->nparam;
2862 tt = mac->params[n];
2863 }
2864 cc = find_cc(tt);
2865 if (cc == -1) {
2866 error(ERR_NONFATAL,
2867 "macro parameter %d is not a condition code",
2868 n + 1);
2869 text = NULL;
2870 } else {
2871 type = TOK_ID;
2872 if (inverse_ccs[cc] == -1) {
2873 error(ERR_NONFATAL,
2874 "condition code `%s' is not invertible",
2875 conditions[cc]);
2876 text = NULL;
2877 } else
2878 text =
2879 nasm_strdup(conditions[inverse_ccs[cc]]);
2880 }
2881 break;
2882 case '+':
2883 n = atoi(t->text + 2) - 1;
2884 if (n >= mac->nparam)
2885 tt = NULL;
2886 else {
2887 if (mac->nparam > 1)
2888 n = (n + mac->rotate) % mac->nparam;
2889 tt = mac->params[n];
2890 }
2891 cc = find_cc(tt);
2892 if (cc == -1) {
2893 error(ERR_NONFATAL,
2894 "macro parameter %d is not a condition code",
2895 n + 1);
2896 text = NULL;
2897 } else {
2898 type = TOK_ID;
2899 text = nasm_strdup(conditions[cc]);
2900 }
2901 break;
2902 default:
2903 n = atoi(t->text + 1) - 1;
2904 if (n >= mac->nparam)
2905 tt = NULL;
2906 else {
2907 if (mac->nparam > 1)
2908 n = (n + mac->rotate) % mac->nparam;
2909 tt = mac->params[n];
2910 }
2911 if (tt) {
2912 for (i = 0; i < mac->paramlen[n]; i++) {
2913 *tail = new_Token(NULL, tt->type, tt->text, 0);
2914 tail = &(*tail)->next;
2915 tt = tt->next;
2916 }
2917 }
2918 text = NULL; /* we've done it here */
2919 break;
2920 }
2921 if (!text) {
2922 delete_Token(t);
2923 } else {
2924 *tail = t;
2925 tail = &t->next;
2926 t->type = type;
2927 nasm_free(t->text);
2928 t->text = text;
2929 t->mac = NULL;
2930 }
2931 continue;
2932 } else {
2933 t = *tail = tline;
2934 tline = tline->next;
2935 t->mac = NULL;
2936 tail = &t->next;
2937 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002938 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00002939 *tail = NULL;
2940 t = thead;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002941 for (; t && (tt = t->next) != NULL; t = t->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002942 switch (t->type) {
2943 case TOK_WHITESPACE:
2944 if (tt->type == TOK_WHITESPACE) {
2945 t->next = delete_Token(tt);
2946 }
2947 break;
2948 case TOK_ID:
2949 if (tt->type == TOK_ID || tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002950 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002951 nasm_free(t->text);
2952 t->text = tmp;
2953 t->next = delete_Token(tt);
2954 }
2955 break;
2956 case TOK_NUMBER:
2957 if (tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002958 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002959 nasm_free(t->text);
2960 t->text = tmp;
2961 t->next = delete_Token(tt);
2962 }
2963 break;
2964 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002965
H. Peter Anvin76690a12002-04-30 20:52:49 +00002966 return thead;
2967}
2968
2969/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002970 * Expand all single-line macro calls made in the given line.
2971 * Return the expanded version of the line. The original is deemed
2972 * to be destroyed in the process. (In reality we'll just move
2973 * Tokens from input to output a lot of the time, rather than
2974 * actually bothering to destroy and replicate.)
2975 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002976static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002977{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002978 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002979 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002980 Token **params;
2981 int *paramsize;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002982 int nparam, sparam, brackets, rescan;
2983 Token *org_tline = tline;
2984 Context *ctx;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002985 char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002986
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002987 /*
2988 * Trick: we should avoid changing the start token pointer since it can
2989 * be contained in "next" field of other token. Because of this
2990 * we allocate a copy of first token and work with it; at the end of
2991 * routine we copy it back
2992 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002993 if (org_tline) {
2994 tline =
2995 new_Token(org_tline->next, org_tline->type, org_tline->text,
2996 0);
2997 tline->mac = org_tline->mac;
2998 nasm_free(org_tline->text);
2999 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003000 }
3001
H. Peter Anvin734b1882002-04-30 21:01:08 +00003002 again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003003 tail = &thead;
3004 thead = NULL;
3005
H. Peter Anvine2c80182005-01-15 22:15:51 +00003006 while (tline) { /* main token loop */
3007 if ((mname = tline->text)) {
3008 /* if this token is a local macro, look in local context */
3009 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID)
3010 ctx = get_ctx(mname, TRUE);
3011 else
3012 ctx = NULL;
3013 if (!ctx)
3014 head = smacros[hash(mname)];
3015 else
3016 head = ctx->localmac;
3017 /*
3018 * We've hit an identifier. As in is_mmacro below, we first
3019 * check whether the identifier is a single-line macro at
3020 * all, then think about checking for parameters if
3021 * necessary.
3022 */
3023 for (m = head; m; m = m->next)
3024 if (!mstrcmp(m->name, mname, m->casesense))
3025 break;
3026 if (m) {
3027 mstart = tline;
3028 params = NULL;
3029 paramsize = NULL;
3030 if (m->nparam == 0) {
3031 /*
3032 * Simple case: the macro is parameterless. Discard the
3033 * one token that the macro call took, and push the
3034 * expansion back on the to-do stack.
3035 */
3036 if (!m->expansion) {
3037 if (!strcmp("__FILE__", m->name)) {
Keith Kaniosb7a89542007-04-12 02:40:54 +00003038 int32_t num = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003039 src_get(&num, &(tline->text));
3040 nasm_quote(&(tline->text));
3041 tline->type = TOK_STRING;
3042 continue;
3043 }
3044 if (!strcmp("__LINE__", m->name)) {
3045 nasm_free(tline->text);
3046 make_tok_num(tline, src_get_linnum());
3047 continue;
3048 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00003049 if (!strcmp("__BITS__", m->name)) {
3050 nasm_free(tline->text);
3051 make_tok_num(tline, globalbits);
3052 continue;
3053 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003054 tline = delete_Token(tline);
3055 continue;
3056 }
3057 } else {
3058 /*
3059 * Complicated case: at least one macro with this name
3060 * exists and takes parameters. We must find the
3061 * parameters in the call, count them, find the SMacro
3062 * that corresponds to that form of the macro call, and
3063 * substitute for the parameters when we expand. What a
3064 * pain.
3065 */
3066 /*tline = tline->next;
3067 skip_white_(tline); */
3068 do {
3069 t = tline->next;
3070 while (tok_type_(t, TOK_SMAC_END)) {
3071 t->mac->in_progress = FALSE;
3072 t->text = NULL;
3073 t = tline->next = delete_Token(t);
3074 }
3075 tline = t;
3076 } while (tok_type_(tline, TOK_WHITESPACE));
3077 if (!tok_is_(tline, "(")) {
3078 /*
3079 * This macro wasn't called with parameters: ignore
3080 * the call. (Behaviour borrowed from gnu cpp.)
3081 */
3082 tline = mstart;
3083 m = NULL;
3084 } else {
3085 int paren = 0;
3086 int white = 0;
3087 brackets = 0;
3088 nparam = 0;
3089 sparam = PARAM_DELTA;
3090 params = nasm_malloc(sparam * sizeof(Token *));
3091 params[0] = tline->next;
3092 paramsize = nasm_malloc(sparam * sizeof(int));
3093 paramsize[0] = 0;
3094 while (TRUE) { /* parameter loop */
3095 /*
3096 * For some unusual expansions
3097 * which concatenates function call
3098 */
3099 t = tline->next;
3100 while (tok_type_(t, TOK_SMAC_END)) {
3101 t->mac->in_progress = FALSE;
3102 t->text = NULL;
3103 t = tline->next = delete_Token(t);
3104 }
3105 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003106
H. Peter Anvine2c80182005-01-15 22:15:51 +00003107 if (!tline) {
3108 error(ERR_NONFATAL,
3109 "macro call expects terminating `)'");
3110 break;
3111 }
3112 if (tline->type == TOK_WHITESPACE
3113 && brackets <= 0) {
3114 if (paramsize[nparam])
3115 white++;
3116 else
3117 params[nparam] = tline->next;
3118 continue; /* parameter loop */
3119 }
3120 if (tline->type == TOK_OTHER
3121 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003122 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003123 if (ch == ',' && !paren && brackets <= 0) {
3124 if (++nparam >= sparam) {
3125 sparam += PARAM_DELTA;
3126 params = nasm_realloc(params,
3127 sparam *
3128 sizeof(Token
3129 *));
3130 paramsize =
3131 nasm_realloc(paramsize,
3132 sparam *
3133 sizeof(int));
3134 }
3135 params[nparam] = tline->next;
3136 paramsize[nparam] = 0;
3137 white = 0;
3138 continue; /* parameter loop */
3139 }
3140 if (ch == '{' &&
3141 (brackets > 0 || (brackets == 0 &&
3142 !paramsize[nparam])))
3143 {
3144 if (!(brackets++)) {
3145 params[nparam] = tline->next;
3146 continue; /* parameter loop */
3147 }
3148 }
3149 if (ch == '}' && brackets > 0)
3150 if (--brackets == 0) {
3151 brackets = -1;
3152 continue; /* parameter loop */
3153 }
3154 if (ch == '(' && !brackets)
3155 paren++;
3156 if (ch == ')' && brackets <= 0)
3157 if (--paren < 0)
3158 break;
3159 }
3160 if (brackets < 0) {
3161 brackets = 0;
3162 error(ERR_NONFATAL, "braces do not "
3163 "enclose all of macro parameter");
3164 }
3165 paramsize[nparam] += white + 1;
3166 white = 0;
3167 } /* parameter loop */
3168 nparam++;
3169 while (m && (m->nparam != nparam ||
3170 mstrcmp(m->name, mname,
3171 m->casesense)))
3172 m = m->next;
3173 if (!m)
3174 error(ERR_WARNING | ERR_WARN_MNP,
3175 "macro `%s' exists, "
3176 "but not taking %d parameters",
3177 mstart->text, nparam);
3178 }
3179 }
3180 if (m && m->in_progress)
3181 m = NULL;
3182 if (!m) { /* in progess or didn't find '(' or wrong nparam */
3183 /*
3184 * Design question: should we handle !tline, which
3185 * indicates missing ')' here, or expand those
3186 * macros anyway, which requires the (t) test a few
3187 * lines down?
3188 */
3189 nasm_free(params);
3190 nasm_free(paramsize);
3191 tline = mstart;
3192 } else {
3193 /*
3194 * Expand the macro: we are placed on the last token of the
3195 * call, so that we can easily split the call from the
3196 * following tokens. We also start by pushing an SMAC_END
3197 * token for the cycle removal.
3198 */
3199 t = tline;
3200 if (t) {
3201 tline = t->next;
3202 t->next = NULL;
3203 }
3204 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
3205 tt->mac = m;
3206 m->in_progress = TRUE;
3207 tline = tt;
3208 for (t = m->expansion; t; t = t->next) {
3209 if (t->type >= TOK_SMAC_PARAM) {
3210 Token *pcopy = tline, **ptail = &pcopy;
3211 Token *ttt, *pt;
3212 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003213
H. Peter Anvine2c80182005-01-15 22:15:51 +00003214 ttt = params[t->type - TOK_SMAC_PARAM];
3215 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3216 --i >= 0;) {
3217 pt = *ptail =
3218 new_Token(tline, ttt->type, ttt->text,
3219 0);
3220 ptail = &pt->next;
3221 ttt = ttt->next;
3222 }
3223 tline = pcopy;
3224 } else {
3225 tt = new_Token(tline, t->type, t->text, 0);
3226 tline = tt;
3227 }
3228 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003229
H. Peter Anvine2c80182005-01-15 22:15:51 +00003230 /*
3231 * Having done that, get rid of the macro call, and clean
3232 * up the parameters.
3233 */
3234 nasm_free(params);
3235 nasm_free(paramsize);
3236 free_tlist(mstart);
3237 continue; /* main token loop */
3238 }
3239 }
3240 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003241
H. Peter Anvine2c80182005-01-15 22:15:51 +00003242 if (tline->type == TOK_SMAC_END) {
3243 tline->mac->in_progress = FALSE;
3244 tline = delete_Token(tline);
3245 } else {
3246 t = *tail = tline;
3247 tline = tline->next;
3248 t->mac = NULL;
3249 t->next = NULL;
3250 tail = &t->next;
3251 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003252 }
3253
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003254 /*
3255 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00003256 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003257 * TOK_IDs should be concatenated.
3258 * Also we look for %+ tokens and concatenate the tokens before and after
3259 * them (without white spaces in between).
3260 */
3261 t = thead;
3262 rescan = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003263 while (t) {
3264 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3265 t = t->next;
3266 if (!t || !t->next)
3267 break;
3268 if (t->next->type == TOK_ID ||
3269 t->next->type == TOK_PREPROC_ID ||
3270 t->next->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003271 char *p = nasm_strcat(t->text, t->next->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003272 nasm_free(t->text);
3273 t->next = delete_Token(t->next);
3274 t->text = p;
3275 rescan = 1;
3276 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3277 t->next->next->type == TOK_PREPROC_ID &&
3278 strcmp(t->next->next->text, "%+") == 0) {
3279 /* free the next whitespace, the %+ token and next whitespace */
3280 int i;
3281 for (i = 1; i <= 3; i++) {
3282 if (!t->next
3283 || (i != 2 && t->next->type != TOK_WHITESPACE))
3284 break;
3285 t->next = delete_Token(t->next);
3286 } /* endfor */
3287 } else
3288 t = t->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003289 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003290 /* If we concatenaded something, re-scan the line for macros */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003291 if (rescan) {
3292 tline = thead;
3293 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003294 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003295
H. Peter Anvine2c80182005-01-15 22:15:51 +00003296 if (org_tline) {
3297 if (thead) {
3298 *org_tline = *thead;
3299 /* since we just gave text to org_line, don't free it */
3300 thead->text = NULL;
3301 delete_Token(thead);
3302 } else {
3303 /* the expression expanded to empty line;
3304 we can't return NULL for some reasons
3305 we just set the line to a single WHITESPACE token. */
3306 memset(org_tline, 0, sizeof(*org_tline));
3307 org_tline->text = NULL;
3308 org_tline->type = TOK_WHITESPACE;
3309 }
3310 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003311 }
3312
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003313 return thead;
3314}
3315
3316/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003317 * Similar to expand_smacro but used exclusively with macro identifiers
3318 * right before they are fetched in. The reason is that there can be
3319 * identifiers consisting of several subparts. We consider that if there
3320 * are more than one element forming the name, user wants a expansion,
3321 * otherwise it will be left as-is. Example:
3322 *
3323 * %define %$abc cde
3324 *
3325 * the identifier %$abc will be left as-is so that the handler for %define
3326 * will suck it and define the corresponding value. Other case:
3327 *
3328 * %define _%$abc cde
3329 *
3330 * In this case user wants name to be expanded *before* %define starts
3331 * working, so we'll expand %$abc into something (if it has a value;
3332 * otherwise it will be left as-is) then concatenate all successive
3333 * PP_IDs into one.
3334 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003335static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003336{
3337 Token *cur, *oldnext = NULL;
3338
H. Peter Anvin734b1882002-04-30 21:01:08 +00003339 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003340 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003341
3342 cur = tline;
3343 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003344 (cur->next->type == TOK_ID ||
3345 cur->next->type == TOK_PREPROC_ID
3346 || cur->next->type == TOK_NUMBER))
3347 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003348
3349 /* If identifier consists of just one token, don't expand */
3350 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003351 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003352
H. Peter Anvine2c80182005-01-15 22:15:51 +00003353 if (cur) {
3354 oldnext = cur->next; /* Detach the tail past identifier */
3355 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003356 }
3357
H. Peter Anvin734b1882002-04-30 21:01:08 +00003358 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003359
H. Peter Anvine2c80182005-01-15 22:15:51 +00003360 if (cur) {
3361 /* expand_smacro possibly changhed tline; re-scan for EOL */
3362 cur = tline;
3363 while (cur && cur->next)
3364 cur = cur->next;
3365 if (cur)
3366 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003367 }
3368
3369 return tline;
3370}
3371
3372/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003373 * Determine whether the given line constitutes a multi-line macro
3374 * call, and return the MMacro structure called if so. Doesn't have
3375 * to check for an initial label - that's taken care of in
3376 * expand_mmacro - but must check numbers of parameters. Guaranteed
3377 * to be called with tline->type == TOK_ID, so the putative macro
3378 * name is easy to find.
3379 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003380static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003381{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003382 MMacro *head, *m;
3383 Token **params;
3384 int nparam;
3385
3386 head = mmacros[hash(tline->text)];
3387
3388 /*
3389 * Efficiency: first we see if any macro exists with the given
3390 * name. If not, we can return NULL immediately. _Then_ we
3391 * count the parameters, and then we look further along the
3392 * list if necessary to find the proper MMacro.
3393 */
3394 for (m = head; m; m = m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003395 if (!mstrcmp(m->name, tline->text, m->casesense))
3396 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003397 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003398 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003399
3400 /*
3401 * OK, we have a potential macro. Count and demarcate the
3402 * parameters.
3403 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003404 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003405
3406 /*
3407 * So we know how many parameters we've got. Find the MMacro
3408 * structure that handles this number.
3409 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003410 while (m) {
3411 if (m->nparam_min <= nparam
3412 && (m->plus || nparam <= m->nparam_max)) {
3413 /*
3414 * This one is right. Just check if cycle removal
3415 * prohibits us using it before we actually celebrate...
3416 */
3417 if (m->in_progress) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003418#if 0
H. Peter Anvine2c80182005-01-15 22:15:51 +00003419 error(ERR_NONFATAL,
3420 "self-reference in multi-line macro `%s'", m->name);
H. Peter Anvineba20a72002-04-30 20:53:55 +00003421#endif
H. Peter Anvine2c80182005-01-15 22:15:51 +00003422 nasm_free(params);
3423 return NULL;
3424 }
3425 /*
3426 * It's right, and we can use it. Add its default
3427 * parameters to the end of our list if necessary.
3428 */
3429 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
3430 params =
3431 nasm_realloc(params,
3432 ((m->nparam_min + m->ndefs +
3433 1) * sizeof(*params)));
3434 while (nparam < m->nparam_min + m->ndefs) {
3435 params[nparam] = m->defaults[nparam - m->nparam_min];
3436 nparam++;
3437 }
3438 }
3439 /*
3440 * If we've gone over the maximum parameter count (and
3441 * we're in Plus mode), ignore parameters beyond
3442 * nparam_max.
3443 */
3444 if (m->plus && nparam > m->nparam_max)
3445 nparam = m->nparam_max;
3446 /*
3447 * Then terminate the parameter list, and leave.
3448 */
3449 if (!params) { /* need this special case */
3450 params = nasm_malloc(sizeof(*params));
3451 nparam = 0;
3452 }
3453 params[nparam] = NULL;
3454 *params_array = params;
3455 return m;
3456 }
3457 /*
3458 * This one wasn't right: look for the next one with the
3459 * same name.
3460 */
3461 for (m = m->next; m; m = m->next)
3462 if (!mstrcmp(m->name, tline->text, m->casesense))
3463 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003464 }
3465
3466 /*
3467 * After all that, we didn't find one with the right number of
3468 * parameters. Issue a warning, and fail to expand the macro.
3469 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003470 error(ERR_WARNING | ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003471 "macro `%s' exists, but not taking %d parameters",
3472 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003473 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003474 return NULL;
3475}
3476
3477/*
3478 * Expand the multi-line macro call made by the given line, if
3479 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00003480 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003481 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003482static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003483{
3484 Token *startline = tline;
3485 Token *label = NULL;
3486 int dont_prepend = 0;
3487 Token **params, *t, *tt;
3488 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003489 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003490 int i, nparam, *paramlen;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003491
3492 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003493 skip_white_(t);
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00003494/* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
3495 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003496 return 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003497 m = is_mmacro(t, &params);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003498 if (!m) {
3499 Token *last;
3500 /*
3501 * We have an id which isn't a macro call. We'll assume
3502 * it might be a label; we'll also check to see if a
3503 * colon follows it. Then, if there's another id after
3504 * that lot, we'll check it again for macro-hood.
3505 */
3506 label = last = t;
3507 t = t->next;
3508 if (tok_type_(t, TOK_WHITESPACE))
3509 last = t, t = t->next;
3510 if (tok_is_(t, ":")) {
3511 dont_prepend = 1;
3512 last = t, t = t->next;
3513 if (tok_type_(t, TOK_WHITESPACE))
3514 last = t, t = t->next;
3515 }
3516 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
3517 return 0;
3518 last->next = NULL;
3519 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003520 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003521
3522 /*
3523 * Fix up the parameters: this involves stripping leading and
3524 * trailing whitespace, then stripping braces if they are
3525 * present.
3526 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003527 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003528 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003529
H. Peter Anvine2c80182005-01-15 22:15:51 +00003530 for (i = 0; params[i]; i++) {
3531 int brace = FALSE;
3532 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003533
H. Peter Anvine2c80182005-01-15 22:15:51 +00003534 t = params[i];
3535 skip_white_(t);
3536 if (tok_is_(t, "{"))
3537 t = t->next, brace = TRUE, comma = FALSE;
3538 params[i] = t;
3539 paramlen[i] = 0;
3540 while (t) {
3541 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
3542 break; /* ... because we have hit a comma */
3543 if (comma && t->type == TOK_WHITESPACE
3544 && tok_is_(t->next, ","))
3545 break; /* ... or a space then a comma */
3546 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
3547 break; /* ... or a brace */
3548 t = t->next;
3549 paramlen[i]++;
3550 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003551 }
3552
3553 /*
3554 * OK, we have a MMacro structure together with a set of
3555 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00003556 * copies of each Line on to istk->expansion. Substitution of
3557 * parameter tokens and macro-local tokens doesn't get done
3558 * until the single-line macro substitution process; this is
3559 * because delaying them allows us to change the semantics
3560 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003561 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00003562 * First, push an end marker on to istk->expansion, mark this
3563 * macro as in progress, and set up its invocation-specific
3564 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003565 */
3566 ll = nasm_malloc(sizeof(Line));
3567 ll->next = istk->expansion;
3568 ll->finishes = m;
3569 ll->first = NULL;
3570 istk->expansion = ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003571
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003572 m->in_progress = TRUE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003573 m->params = params;
3574 m->iline = tline;
3575 m->nparam = nparam;
3576 m->rotate = 0;
3577 m->paramlen = paramlen;
3578 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003579 m->lineno = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003580
3581 m->next_active = istk->mstk;
3582 istk->mstk = m;
3583
H. Peter Anvine2c80182005-01-15 22:15:51 +00003584 for (l = m->expansion; l; l = l->next) {
3585 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003586
H. Peter Anvine2c80182005-01-15 22:15:51 +00003587 ll = nasm_malloc(sizeof(Line));
3588 ll->finishes = NULL;
3589 ll->next = istk->expansion;
3590 istk->expansion = ll;
3591 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003592
H. Peter Anvine2c80182005-01-15 22:15:51 +00003593 for (t = l->first; t; t = t->next) {
3594 Token *x = t;
3595 if (t->type == TOK_PREPROC_ID &&
3596 t->text[1] == '0' && t->text[2] == '0') {
3597 dont_prepend = -1;
3598 x = label;
3599 if (!x)
3600 continue;
3601 }
3602 tt = *tail = new_Token(NULL, x->type, x->text, 0);
3603 tail = &tt->next;
3604 }
3605 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003606 }
3607
3608 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00003609 * If we had a label, push it on as the first line of
3610 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003611 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003612 if (label) {
3613 if (dont_prepend < 0)
3614 free_tlist(startline);
3615 else {
3616 ll = nasm_malloc(sizeof(Line));
3617 ll->finishes = NULL;
3618 ll->next = istk->expansion;
3619 istk->expansion = ll;
3620 ll->first = startline;
3621 if (!dont_prepend) {
3622 while (label->next)
3623 label = label->next;
3624 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
3625 }
3626 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003627 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003628
H. Peter Anvin734b1882002-04-30 21:01:08 +00003629 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003630
H. Peter Anvineba20a72002-04-30 20:53:55 +00003631 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003632}
3633
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003634/*
3635 * Since preprocessor always operate only on the line that didn't
3636 * arrived yet, we should always use ERR_OFFBY1. Also since user
3637 * won't want to see same error twice (preprocessing is done once
3638 * per pass) we will want to show errors only during pass one.
3639 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003640static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003641{
3642 va_list arg;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003643 char buff[1024];
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003644
3645 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00003646 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003647 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003648
H. Peter Anvin734b1882002-04-30 21:01:08 +00003649 va_start(arg, fmt);
Ed Beroset19f927a2004-12-15 17:07:03 +00003650 vsnprintf(buff, sizeof(buff), fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003651 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003652
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00003653 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003654 _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name,
3655 istk->mstk->lineno, buff);
3656 else
3657 _error(severity | ERR_PASS1, "%s", buff);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003658}
3659
H. Peter Anvin734b1882002-04-30 21:01:08 +00003660static void
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003661pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003662 ListGen * listgen)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003663{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003664 int h;
3665
H. Peter Anvin99941bf2002-05-14 17:44:03 +00003666 _error = errfunc;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003667 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003668 istk = nasm_malloc(sizeof(Include));
3669 istk->next = NULL;
3670 istk->conds = NULL;
3671 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003672 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003673 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00003674 istk->fname = NULL;
3675 src_set_fname(nasm_strdup(file));
3676 src_set_linnum(0);
3677 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003678 if (!istk->fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003679 error(ERR_FATAL | ERR_NOFILE, "unable to open input file `%s'",
3680 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003681 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003682 for (h = 0; h < NHASH; h++) {
3683 mmacros[h] = NULL;
3684 smacros[h] = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003685 }
3686 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003687 if (tasm_compatible_mode) {
3688 stdmacpos = stdmac;
3689 } else {
3690 stdmacpos = &stdmac[TASM_MACRO_COUNT];
3691 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003692 any_extrastdmac = (extrastdmac != NULL);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003693 list = listgen;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003694 evaluate = eval;
3695 pass = apass;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003696}
3697
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003698static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003699{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003700 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003701 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003702
H. Peter Anvine2c80182005-01-15 22:15:51 +00003703 while (1) {
3704 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00003705 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00003706 * buffer or from the input file.
3707 */
3708 tline = NULL;
3709 while (istk->expansion && istk->expansion->finishes) {
3710 Line *l = istk->expansion;
3711 if (!l->finishes->name && l->finishes->in_progress > 1) {
3712 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003713
H. Peter Anvine2c80182005-01-15 22:15:51 +00003714 /*
3715 * This is a macro-end marker for a macro with no
3716 * name, which means it's not really a macro at all
3717 * but a %rep block, and the `in_progress' field is
3718 * more than 1, meaning that we still need to
3719 * repeat. (1 means the natural last repetition; 0
3720 * means termination by %exitrep.) We have
3721 * therefore expanded up to the %endrep, and must
3722 * push the whole block on to the expansion buffer
3723 * again. We don't bother to remove the macro-end
3724 * marker: we'd only have to generate another one
3725 * if we did.
3726 */
3727 l->finishes->in_progress--;
3728 for (l = l->finishes->expansion; l; l = l->next) {
3729 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003730
H. Peter Anvine2c80182005-01-15 22:15:51 +00003731 ll = nasm_malloc(sizeof(Line));
3732 ll->next = istk->expansion;
3733 ll->finishes = NULL;
3734 ll->first = NULL;
3735 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003736
H. Peter Anvine2c80182005-01-15 22:15:51 +00003737 for (t = l->first; t; t = t->next) {
3738 if (t->text || t->type == TOK_WHITESPACE) {
3739 tt = *tail =
3740 new_Token(NULL, t->type, t->text, 0);
3741 tail = &tt->next;
3742 }
3743 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003744
H. Peter Anvine2c80182005-01-15 22:15:51 +00003745 istk->expansion = ll;
3746 }
3747 } else {
3748 /*
3749 * Check whether a `%rep' was started and not ended
3750 * within this macro expansion. This can happen and
3751 * should be detected. It's a fatal error because
3752 * I'm too confused to work out how to recover
3753 * sensibly from it.
3754 */
3755 if (defining) {
3756 if (defining->name)
3757 error(ERR_PANIC,
3758 "defining with name in expansion");
3759 else if (istk->mstk->name)
3760 error(ERR_FATAL,
3761 "`%%rep' without `%%endrep' within"
3762 " expansion of macro `%s'",
3763 istk->mstk->name);
3764 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00003765
H. Peter Anvine2c80182005-01-15 22:15:51 +00003766 /*
3767 * FIXME: investigate the relationship at this point between
3768 * istk->mstk and l->finishes
3769 */
3770 {
3771 MMacro *m = istk->mstk;
3772 istk->mstk = m->next_active;
3773 if (m->name) {
3774 /*
3775 * This was a real macro call, not a %rep, and
3776 * therefore the parameter information needs to
3777 * be freed.
3778 */
3779 nasm_free(m->params);
3780 free_tlist(m->iline);
3781 nasm_free(m->paramlen);
3782 l->finishes->in_progress = FALSE;
3783 } else
3784 free_mmacro(m);
3785 }
3786 istk->expansion = l->next;
3787 nasm_free(l);
3788 list->downlevel(LIST_MACRO);
3789 }
3790 }
3791 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00003792
H. Peter Anvine2c80182005-01-15 22:15:51 +00003793 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003794 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003795 Line *l = istk->expansion;
3796 if (istk->mstk)
3797 istk->mstk->lineno++;
3798 tline = l->first;
3799 istk->expansion = l->next;
3800 nasm_free(l);
3801 p = detoken(tline, FALSE);
3802 list->line(LIST_MACRO, p);
3803 nasm_free(p);
3804 break;
3805 }
3806 line = read_line();
3807 if (line) { /* from the current input file */
3808 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00003809 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003810 nasm_free(line);
3811 break;
3812 }
3813 /*
3814 * The current file has ended; work down the istk
3815 */
3816 {
3817 Include *i = istk;
3818 fclose(i->fp);
3819 if (i->conds)
3820 error(ERR_FATAL,
3821 "expected `%%endif' before end of file");
3822 /* only set line and file name if there's a next node */
3823 if (i->next) {
3824 src_set_linnum(i->lineno);
3825 nasm_free(src_set_fname(i->fname));
3826 }
3827 istk = i->next;
3828 list->downlevel(LIST_INCLUDE);
3829 nasm_free(i);
3830 if (!istk)
3831 return NULL;
3832 }
3833 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003834
H. Peter Anvine2c80182005-01-15 22:15:51 +00003835 /*
3836 * We must expand MMacro parameters and MMacro-local labels
3837 * _before_ we plunge into directive processing, to cope
3838 * with things like `%define something %1' such as STRUC
3839 * uses. Unless we're _defining_ a MMacro, in which case
3840 * those tokens should be left alone to go into the
3841 * definition; and unless we're in a non-emitting
3842 * condition, in which case we don't want to meddle with
3843 * anything.
3844 */
3845 if (!defining && !(istk->conds && !emitting(istk->conds->state)))
3846 tline = expand_mmac_params(tline);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003847
H. Peter Anvine2c80182005-01-15 22:15:51 +00003848 /*
3849 * Check the line to see if it's a preprocessor directive.
3850 */
3851 if (do_directive(tline) == DIRECTIVE_FOUND) {
3852 continue;
3853 } else if (defining) {
3854 /*
3855 * We're defining a multi-line macro. We emit nothing
3856 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00003857 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003858 */
3859 Line *l = nasm_malloc(sizeof(Line));
3860 l->next = defining->expansion;
3861 l->first = tline;
3862 l->finishes = FALSE;
3863 defining->expansion = l;
3864 continue;
3865 } else if (istk->conds && !emitting(istk->conds->state)) {
3866 /*
3867 * We're in a non-emitting branch of a condition block.
3868 * Emit nothing at all, not even a blank line: when we
3869 * emerge from the condition we'll give a line-number
3870 * directive so we keep our place correctly.
3871 */
3872 free_tlist(tline);
3873 continue;
3874 } else if (istk->mstk && !istk->mstk->in_progress) {
3875 /*
3876 * We're in a %rep block which has been terminated, so
3877 * we're walking through to the %endrep without
3878 * emitting anything. Emit nothing at all, not even a
3879 * blank line: when we emerge from the %rep block we'll
3880 * give a line-number directive so we keep our place
3881 * correctly.
3882 */
3883 free_tlist(tline);
3884 continue;
3885 } else {
3886 tline = expand_smacro(tline);
3887 if (!expand_mmacro(tline)) {
3888 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00003889 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003890 */
3891 line = detoken(tline, TRUE);
3892 free_tlist(tline);
3893 break;
3894 } else {
3895 continue; /* expand_mmacro calls free_tlist */
3896 }
3897 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003898 }
3899
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003900 return line;
3901}
3902
H. Peter Anvine2c80182005-01-15 22:15:51 +00003903static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003904{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003905 int h;
3906
H. Peter Anvine2c80182005-01-15 22:15:51 +00003907 if (defining) {
3908 error(ERR_NONFATAL, "end of file while still defining macro `%s'",
3909 defining->name);
3910 free_mmacro(defining);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003911 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003912 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003913 ctx_pop();
3914 for (h = 0; h < NHASH; h++) {
3915 while (mmacros[h]) {
3916 MMacro *m = mmacros[h];
3917 mmacros[h] = mmacros[h]->next;
3918 free_mmacro(m);
3919 }
3920 while (smacros[h]) {
3921 SMacro *s = smacros[h];
3922 smacros[h] = smacros[h]->next;
3923 nasm_free(s->name);
3924 free_tlist(s->expansion);
3925 nasm_free(s);
3926 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003927 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003928 while (istk) {
3929 Include *i = istk;
3930 istk = istk->next;
3931 fclose(i->fp);
3932 nasm_free(i->fname);
3933 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003934 }
3935 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003936 ctx_pop();
3937 if (pass == 0) {
3938 free_llist(predef);
3939 delete_Blocks();
3940 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003941}
3942
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003943void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003944{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003945 IncPath *i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003946/* by alexfru: order of path inclusion fixed (was reverse order) */
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003947 i = nasm_malloc(sizeof(IncPath));
3948 i->path = nasm_strdup(path);
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003949 i->next = NULL;
3950
H. Peter Anvine2c80182005-01-15 22:15:51 +00003951 if (ipath != NULL) {
3952 IncPath *j = ipath;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003953 while (j->next != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003954 j = j->next;
3955 j->next = i;
3956 } else {
3957 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003958 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003959}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003960
3961/*
3962 * added by alexfru:
3963 *
3964 * This function is used to "export" the include paths, e.g.
3965 * the paths specified in the '-I' command switch.
3966 * The need for such exporting is due to the 'incbin' directive,
3967 * which includes raw binary files (unlike '%include', which
3968 * includes text source files). It would be real nice to be
3969 * able to specify paths to search for incbin'ned files also.
3970 * So, this is a simple workaround.
3971 *
3972 * The function use is simple:
3973 *
3974 * The 1st call (with NULL argument) returns a pointer to the 1st path
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003975 * (char** type) or NULL if none include paths available.
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003976 *
3977 * All subsequent calls take as argument the value returned by this
3978 * function last. The return value is either the next path
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003979 * (char** type) or NULL if the end of the paths list is reached.
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003980 *
3981 * It is maybe not the best way to do things, but I didn't want
3982 * to export too much, just one or two functions and no types or
3983 * variables exported.
3984 *
3985 * Can't say I like the current situation with e.g. this path list either,
3986 * it seems to be never deallocated after creation...
3987 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003988char **pp_get_include_path_ptr(char **pPrevPath)
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003989{
3990/* This macro returns offset of a member of a structure */
3991#define GetMemberOffset(StructType,MemberName)\
3992 ((size_t)&((StructType*)0)->MemberName)
3993 IncPath *i;
3994
H. Peter Anvine2c80182005-01-15 22:15:51 +00003995 if (pPrevPath == NULL) {
3996 if (ipath != NULL)
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003997 return &ipath->path;
3998 else
3999 return NULL;
4000 }
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004001 i = (IncPath *) ((char *)pPrevPath - GetMemberOffset(IncPath, path));
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004002 i = i->next;
4003 if (i != NULL)
4004 return &i->path;
4005 else
4006 return NULL;
4007#undef GetMemberOffset
4008}
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004009
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004010void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004011{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004012 Token *inc, *space, *name;
4013 Line *l;
4014
H. Peter Anvin734b1882002-04-30 21:01:08 +00004015 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4016 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4017 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004018
4019 l = nasm_malloc(sizeof(Line));
4020 l->next = predef;
4021 l->first = inc;
4022 l->finishes = FALSE;
4023 predef = l;
4024}
4025
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004026void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004027{
4028 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004029 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004030 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004031
4032 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00004033 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4034 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004035 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004036 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00004037 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004038 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004039 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004040
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004041 l = nasm_malloc(sizeof(Line));
4042 l->next = predef;
4043 l->first = def;
4044 l->finishes = FALSE;
4045 predef = l;
4046}
4047
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004048void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00004049{
4050 Token *def, *space;
4051 Line *l;
4052
H. Peter Anvin734b1882002-04-30 21:01:08 +00004053 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4054 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004055 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00004056
4057 l = nasm_malloc(sizeof(Line));
4058 l->next = predef;
4059 l->first = def;
4060 l->finishes = FALSE;
4061 predef = l;
4062}
4063
Keith Kaniosb7a89542007-04-12 02:40:54 +00004064/*
4065 * Added by Keith Kanios:
4066 *
4067 * This function is used to assist with "runtime" preprocessor
4068 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4069 *
4070 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4071 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4072 */
4073
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004074void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00004075{
4076 Token *def;
4077
4078 def = tokenize(definition);
4079 if(do_directive(def) == NO_DIRECTIVE_FOUND)
4080 free_tlist(def);
4081
4082}
4083
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004084void pp_extra_stdmac(const char **macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004085{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004086 extrastdmac = macros;
4087}
4088
Keith Kaniosb7a89542007-04-12 02:40:54 +00004089static void make_tok_num(Token * tok, int32_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004090{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004091 char numbuf[20];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00004092 snprintf(numbuf, sizeof(numbuf), "%"PRId32"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004093 tok->text = nasm_strdup(numbuf);
4094 tok->type = TOK_NUMBER;
4095}
4096
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004097Preproc nasmpp = {
4098 pp_reset,
4099 pp_getline,
4100 pp_cleanup
4101};