H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1 | /* preproc.c macro preprocessor for the Netwide Assembler |
| 2 | * |
| 3 | * The Netwide Assembler is copyright (C) 1996 Simon Tatham and |
| 4 | * Julian Hall. All rights reserved. The software is |
| 5 | * redistributable under the licence given in the file "Licence" |
| 6 | * distributed in the NASM archive. |
| 7 | * |
| 8 | * initial version 18/iii/97 by Simon Tatham |
| 9 | */ |
| 10 | |
H. Peter Anvin | 4836e33 | 2002-04-30 20:56:43 +0000 | [diff] [blame^] | 11 | /* Typical flow of text through preproc |
| 12 | * |
| 13 | * pp_getline gets tokenised lines, either |
| 14 | * |
| 15 | * from a macro expansion |
| 16 | * |
| 17 | * or |
| 18 | * { |
| 19 | * read_line gets raw text from stdmacpos, or predef, or current input file |
| 20 | * tokenise converts to tokens |
| 21 | * } |
| 22 | * |
| 23 | * expand_mmac_params is used to expand %1 etc., unless a macro is being |
| 24 | * defined or a false conditional is being processed |
| 25 | * (%0, %1, %+1, %-1, %%foo |
| 26 | * |
| 27 | * do_directive checks for directives |
| 28 | * |
| 29 | * expand_smacro is used to expand single line macros |
| 30 | * |
| 31 | * expand_mmacro is used to expand multi-line macros |
| 32 | * |
| 33 | * detoken is used to convert the line back to text |
| 34 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 35 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 36 | #include <stdio.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <stddef.h> |
| 39 | #include <string.h> |
| 40 | #include <ctype.h> |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 41 | #include <limits.h> |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 42 | |
| 43 | #include "nasm.h" |
| 44 | #include "nasmlib.h" |
| 45 | |
| 46 | typedef struct SMacro SMacro; |
| 47 | typedef struct MMacro MMacro; |
| 48 | typedef struct Context Context; |
| 49 | typedef struct Token Token; |
| 50 | typedef struct Line Line; |
| 51 | typedef struct Include Include; |
| 52 | typedef struct Cond Cond; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 53 | typedef struct IncPath IncPath; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 54 | |
| 55 | /* |
| 56 | * Store the definition of a single-line macro. |
| 57 | */ |
| 58 | struct SMacro { |
| 59 | SMacro *next; |
| 60 | char *name; |
| 61 | int casesense; |
| 62 | int nparam; |
| 63 | int in_progress; |
| 64 | Token *expansion; |
| 65 | }; |
| 66 | |
| 67 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 68 | * Store the definition of a multi-line macro. This is also used to |
| 69 | * store the interiors of `%rep...%endrep' blocks, which are |
| 70 | * effectively self-re-invoking multi-line macros which simply |
| 71 | * don't have a name or bother to appear in the hash tables. %rep |
| 72 | * blocks are signified by having a NULL `name' field. |
| 73 | * |
| 74 | * In a MMacro describing a `%rep' block, the `in_progress' field |
| 75 | * isn't merely boolean, but gives the number of repeats left to |
| 76 | * run. |
| 77 | * |
| 78 | * The `next' field is used for storing MMacros in hash tables; the |
| 79 | * `next_active' field is for stacking them on istk entries. |
| 80 | * |
| 81 | * When a MMacro is being expanded, `params', `iline', `nparam', |
| 82 | * `paramlen', `rotate' and `unique' are local to the invocation. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 83 | */ |
| 84 | struct MMacro { |
| 85 | MMacro *next; |
| 86 | char *name; |
| 87 | int casesense; |
| 88 | int nparam_min, nparam_max; |
| 89 | int plus; /* is the last parameter greedy? */ |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 90 | int nolist; /* is this macro listing-inhibited? */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 91 | int in_progress; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 92 | Token *dlist; /* All defaults as one list */ |
| 93 | Token **defaults; /* Parameter default pointers */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 94 | int ndefs; /* number of default parameters */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 95 | Line *expansion; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 96 | |
| 97 | MMacro *next_active; |
H. Peter Anvin | b64535f | 2002-04-30 20:55:37 +0000 | [diff] [blame] | 98 | MMacro *rep_nest; /* used for nesting %rep */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 99 | Token **params; /* actual parameters */ |
| 100 | Token *iline; /* invocation line */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 101 | int nparam, rotate, *paramlen; |
| 102 | unsigned long unique; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | /* |
| 106 | * The context stack is composed of a linked list of these. |
| 107 | */ |
| 108 | struct Context { |
| 109 | Context *next; |
| 110 | SMacro *localmac; |
| 111 | char *name; |
| 112 | unsigned long number; |
| 113 | }; |
| 114 | |
| 115 | /* |
| 116 | * This is the internal form which we break input lines up into. |
| 117 | * Typically stored in linked lists. |
| 118 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 119 | * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not |
| 120 | * necessarily used as-is, but is intended to denote the number of |
| 121 | * the substituted parameter. So in the definition |
| 122 | * |
| 123 | * %define a(x,y) ( (x) & ~(y) ) |
| 124 | * |
| 125 | * the token representing `x' will have its type changed to |
| 126 | * TOK_SMAC_PARAM, but the one representing `y' will be |
| 127 | * TOK_SMAC_PARAM+1. |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 128 | * |
| 129 | * TOK_INTERNAL_STRING is a dirty hack: it's a single string token |
| 130 | * which doesn't need quotes around it. Used in the pre-include |
| 131 | * mechanism as an alternative to trying to find a sensible type of |
| 132 | * quote to use on the filename we were passed. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 133 | */ |
| 134 | struct Token { |
| 135 | Token *next; |
| 136 | char *text; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 137 | SMacro *mac; /* associated macro for TOK_SMAC_END */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 138 | int type; |
| 139 | }; |
| 140 | enum { |
| 141 | TOK_WHITESPACE = 1, TOK_COMMENT, TOK_ID, TOK_PREPROC_ID, TOK_STRING, |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 142 | TOK_NUMBER, TOK_SMAC_END, TOK_OTHER, TOK_SMAC_PARAM, |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 143 | TOK_INTERNAL_STRING |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | /* |
| 147 | * Multi-line macro definitions are stored as a linked list of |
| 148 | * these, which is essentially a container to allow several linked |
| 149 | * lists of Tokens. |
| 150 | * |
| 151 | * Note that in this module, linked lists are treated as stacks |
| 152 | * wherever possible. For this reason, Lines are _pushed_ on to the |
| 153 | * `expansion' field in MMacro structures, so that the linked list, |
| 154 | * if walked, would give the macro lines in reverse order; this |
| 155 | * means that we can walk the list when expanding a macro, and thus |
| 156 | * push the lines on to the `expansion' field in _istk_ in reverse |
| 157 | * order (so that when popped back off they are in the right |
| 158 | * order). It may seem cockeyed, and it relies on my design having |
| 159 | * an even number of steps in, but it works... |
| 160 | * |
| 161 | * Some of these structures, rather than being actual lines, are |
| 162 | * markers delimiting the end of the expansion of a given macro. |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 163 | * This is for use in the cycle-tracking and %rep-handling code. |
| 164 | * Such structures have `finishes' non-NULL, and `first' NULL. All |
| 165 | * others have `finishes' NULL, but `first' may still be NULL if |
| 166 | * the line is blank. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 167 | */ |
| 168 | struct Line { |
| 169 | Line *next; |
| 170 | MMacro *finishes; |
| 171 | Token *first; |
| 172 | }; |
| 173 | |
| 174 | /* |
| 175 | * To handle an arbitrary level of file inclusion, we maintain a |
| 176 | * stack (ie linked list) of these things. |
| 177 | */ |
| 178 | struct Include { |
| 179 | Include *next; |
| 180 | FILE *fp; |
| 181 | Cond *conds; |
| 182 | Line *expansion; |
| 183 | char *fname; |
| 184 | int lineno, lineinc; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 185 | MMacro *mstk; /* stack of active macros/reps */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 189 | * Include search path. This is simply a list of strings which get |
| 190 | * prepended, in turn, to the name of an include file, in an |
| 191 | * attempt to find the file if it's not in the current directory. |
| 192 | */ |
| 193 | struct IncPath { |
| 194 | IncPath *next; |
| 195 | char *path; |
| 196 | }; |
| 197 | |
| 198 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 199 | * Conditional assembly: we maintain a separate stack of these for |
| 200 | * each level of file inclusion. (The only reason we keep the |
| 201 | * stacks separate is to ensure that a stray `%endif' in a file |
| 202 | * included from within the true branch of a `%if' won't terminate |
| 203 | * it and cause confusion: instead, rightly, it'll cause an error.) |
| 204 | */ |
| 205 | struct Cond { |
| 206 | Cond *next; |
| 207 | int state; |
| 208 | }; |
| 209 | enum { |
| 210 | /* |
| 211 | * These states are for use just after %if or %elif: IF_TRUE |
| 212 | * means the condition has evaluated to truth so we are |
| 213 | * currently emitting, whereas IF_FALSE means we are not |
| 214 | * currently emitting but will start doing so if a %else comes |
| 215 | * up. In these states, all directives are admissible: %elif, |
| 216 | * %else and %endif. (And of course %if.) |
| 217 | */ |
| 218 | COND_IF_TRUE, COND_IF_FALSE, |
| 219 | /* |
| 220 | * These states come up after a %else: ELSE_TRUE means we're |
| 221 | * emitting, and ELSE_FALSE means we're not. In ELSE_* states, |
| 222 | * any %elif or %else will cause an error. |
| 223 | */ |
| 224 | COND_ELSE_TRUE, COND_ELSE_FALSE, |
| 225 | /* |
| 226 | * This state means that we're not emitting now, and also that |
| 227 | * nothing until %endif will be emitted at all. It's for use in |
| 228 | * two circumstances: (i) when we've had our moment of emission |
| 229 | * and have now started seeing %elifs, and (ii) when the |
| 230 | * condition construct in question is contained within a |
| 231 | * non-emitting branch of a larger condition construct. |
| 232 | */ |
| 233 | COND_NEVER |
| 234 | }; |
| 235 | #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE ) |
| 236 | |
| 237 | /* |
| 238 | * Condition codes. Note that we use c_ prefix not C_ because C_ is |
| 239 | * used in nasm.h for the "real" condition codes. At _this_ level, |
| 240 | * we treat CXZ and ECXZ as condition codes, albeit non-invertible |
| 241 | * ones, so we need a different enum... |
| 242 | */ |
| 243 | static char *conditions[] = { |
| 244 | "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le", |
| 245 | "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no", |
| 246 | "np", "ns", "nz", "o", "p", "pe", "po", "s", "z" |
| 247 | }; |
| 248 | enum { |
| 249 | c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE, |
| 250 | c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO, |
| 251 | c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_S, c_Z |
| 252 | }; |
| 253 | static int inverse_ccs[] = { |
| 254 | c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE, |
| 255 | 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, |
| 256 | c_Z, c_NO, c_NP, c_PO, c_PE, c_NS, c_NZ |
| 257 | }; |
| 258 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 259 | /* |
| 260 | * Directive names. |
| 261 | */ |
| 262 | static char *directives[] = { |
| 263 | "%assign", "%clear", "%define", "%elif", "%elifctx", "%elifdef", |
| 264 | "%elifid", "%elifidn", "%elifidni", "%elifnctx", "%elifndef", |
| 265 | "%elifnid", "%elifnidn", "%elifnidni", "%elifnnum", "%elifnstr", |
| 266 | "%elifnum", "%elifstr", "%else", "%endif", "%endm", "%endmacro", |
| 267 | "%endrep", "%error", "%exitrep", "%iassign", "%idefine", "%if", |
| 268 | "%ifctx", "%ifdef", "%ifid", "%ifidn", "%ifidni", "%ifnctx", |
| 269 | "%ifndef", "%ifnid", "%ifnidn", "%ifnidni", "%ifnnum", |
| 270 | "%ifnstr", "%ifnum", "%ifstr", "%imacro", "%include", "%line", |
| 271 | "%macro", "%pop", "%push", "%rep", "%repl", "%rotate" |
| 272 | }; |
| 273 | enum { |
| 274 | PP_ASSIGN, PP_CLEAR, PP_DEFINE, PP_ELIF, PP_ELIFCTX, PP_ELIFDEF, |
| 275 | PP_ELIFID, PP_ELIFIDN, PP_ELIFIDNI, PP_ELIFNCTX, PP_ELIFNDEF, |
| 276 | PP_ELIFNID, PP_ELIFNIDN, PP_ELIFNIDNI, PP_ELIFNNUM, PP_ELIFNSTR, |
| 277 | PP_ELIFNUM, PP_ELIFSTR, PP_ELSE, PP_ENDIF, PP_ENDM, PP_ENDMACRO, |
| 278 | PP_ENDREP, PP_ERROR, PP_EXITREP, PP_IASSIGN, PP_IDEFINE, PP_IF, |
| 279 | PP_IFCTX, PP_IFDEF, PP_IFID, PP_IFIDN, PP_IFIDNI, PP_IFNCTX, |
| 280 | PP_IFNDEF, PP_IFNID, PP_IFNIDN, PP_IFNIDNI, PP_IFNNUM, |
| 281 | PP_IFNSTR, PP_IFNUM, PP_IFSTR, PP_IMACRO, PP_INCLUDE, PP_LINE, |
| 282 | PP_MACRO, PP_POP, PP_PUSH, PP_REP, PP_REPL, PP_ROTATE |
| 283 | }; |
| 284 | |
| 285 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 286 | static Context *cstk; |
| 287 | static Include *istk; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 288 | static IncPath *ipath = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 289 | |
| 290 | static efunc error; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 291 | static evalfunc evaluate; |
| 292 | |
| 293 | static int pass; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 294 | |
| 295 | static unsigned long unique; /* unique identifier numbers */ |
| 296 | |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 297 | static Line *predef = NULL; |
| 298 | |
| 299 | static ListGen *list; |
| 300 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 301 | /* |
| 302 | * The number of hash values we use for the macro lookup tables. |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 303 | * FIXME: We should *really* be able to configure this at run time, |
| 304 | * or even have the hash table automatically expanding when necessary. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 305 | */ |
| 306 | #define NHASH 31 |
| 307 | |
| 308 | /* |
| 309 | * The current set of multi-line macros we have defined. |
| 310 | */ |
| 311 | static MMacro *mmacros[NHASH]; |
| 312 | |
| 313 | /* |
| 314 | * The current set of single-line macros we have defined. |
| 315 | */ |
| 316 | static SMacro *smacros[NHASH]; |
| 317 | |
| 318 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 319 | * The multi-line macro we are currently defining, or the %rep |
| 320 | * block we are currently reading, if any. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 321 | */ |
| 322 | static MMacro *defining; |
| 323 | |
| 324 | /* |
| 325 | * The number of macro parameters to allocate space for at a time. |
| 326 | */ |
| 327 | #define PARAM_DELTA 16 |
| 328 | |
| 329 | /* |
| 330 | * The standard macro set: defined as `static char *stdmac[]'. Also |
| 331 | * gives our position in the macro set, when we're processing it. |
| 332 | */ |
| 333 | #include "macros.c" |
| 334 | static char **stdmacpos; |
| 335 | |
| 336 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 337 | * The extra standard macros that come from the object format, if |
| 338 | * any. |
| 339 | */ |
| 340 | static char **extrastdmac = NULL; |
| 341 | int any_extrastdmac; |
| 342 | |
| 343 | /* |
| 344 | * Forward declarations. |
| 345 | */ |
H. Peter Anvin | 4836e33 | 2002-04-30 20:56:43 +0000 | [diff] [blame^] | 346 | static Token *expand_mmac_params (Token *tline); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 347 | static Token *expand_smacro (Token *tline); |
H. Peter Anvin | 4836e33 | 2002-04-30 20:56:43 +0000 | [diff] [blame^] | 348 | static void make_tok_num(Token *tok, long val); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 349 | |
| 350 | /* |
| 351 | * Macros for safe checking of token pointers, avoid *(NULL) |
| 352 | */ |
| 353 | #define tok_type_(x,t) ((x) && (x)->type == (t)) |
| 354 | #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next |
| 355 | #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v))) |
| 356 | #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v)))) |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 357 | |
| 358 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 359 | * The pre-preprocessing stage... This function translates line |
| 360 | * number indications as they emerge from GNU cpp (`# lineno "file" |
| 361 | * flags') into NASM preprocessor line number indications (`%line |
| 362 | * lineno file'). |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 363 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 364 | static char *prepreproc(char *line) |
| 365 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 366 | int lineno, fnlen; |
| 367 | char *fname, *oldline; |
| 368 | |
| 369 | if (line[0] == '#' && line[1] == ' ') { |
| 370 | oldline = line; |
| 371 | fname = oldline+2; |
| 372 | lineno = atoi(fname); |
| 373 | fname += strspn(fname, "0123456789 "); |
| 374 | if (*fname == '"') |
| 375 | fname++; |
| 376 | fnlen = strcspn(fname, "\""); |
| 377 | line = nasm_malloc(20+fnlen); |
| 378 | sprintf(line, "%%line %d %.*s", lineno, fnlen, fname); |
| 379 | nasm_free (oldline); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 380 | } |
| 381 | return line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | /* |
| 385 | * The hash function for macro lookups. Note that due to some |
| 386 | * macros having case-insensitive names, the hash function must be |
| 387 | * invariant under case changes. We implement this by applying a |
| 388 | * perfectly normal hash function to the uppercase of the string. |
| 389 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 390 | static int hash(char *s) |
| 391 | { |
| 392 | unsigned int h = 0; |
| 393 | int i = 0; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 394 | /* |
| 395 | * Powers of three, mod 31. |
| 396 | */ |
| 397 | static const int multipliers[] = { |
| 398 | 1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, |
| 399 | 30, 28, 22, 4, 12, 5, 15, 14, 11, 2, 6, 18, 23, 7, 21 |
| 400 | }; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 401 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 402 | |
| 403 | while (*s) { |
| 404 | h += multipliers[i] * (unsigned char) (toupper(*s)); |
| 405 | s++; |
| 406 | if (++i >= sizeof(multipliers)/sizeof(*multipliers)) |
| 407 | i = 0; |
| 408 | } |
| 409 | h %= NHASH; |
| 410 | return h; |
| 411 | } |
| 412 | |
| 413 | /* |
| 414 | * Free a linked list of tokens. |
| 415 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 416 | static void free_tlist (Token *list) |
| 417 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 418 | Token *t; |
| 419 | while (list) { |
| 420 | t = list; |
| 421 | list = list->next; |
| 422 | nasm_free (t->text); |
| 423 | nasm_free (t); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | /* |
| 428 | * Free a linked list of lines. |
| 429 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 430 | static void free_llist (Line *list) |
| 431 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 432 | Line *l; |
| 433 | while (list) { |
| 434 | l = list; |
| 435 | list = list->next; |
| 436 | free_tlist (l->first); |
| 437 | nasm_free (l); |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | /* |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 442 | * Free an MMacro |
| 443 | */ |
| 444 | static void free_mmacro (MMacro *m) |
| 445 | { |
| 446 | nasm_free (m->name); |
| 447 | free_tlist (m->dlist); |
| 448 | nasm_free (m->defaults); |
| 449 | free_llist (m->expansion); |
| 450 | nasm_free (m); |
| 451 | } |
| 452 | |
| 453 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 454 | * Pop the context stack. |
| 455 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 456 | static void ctx_pop (void) |
| 457 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 458 | Context *c = cstk; |
| 459 | SMacro *smac, *s; |
| 460 | |
| 461 | cstk = cstk->next; |
| 462 | smac = c->localmac; |
| 463 | while (smac) { |
| 464 | s = smac; |
| 465 | smac = smac->next; |
| 466 | nasm_free (s->name); |
| 467 | free_tlist (s->expansion); |
| 468 | nasm_free (s); |
| 469 | } |
| 470 | nasm_free (c->name); |
| 471 | nasm_free (c); |
| 472 | } |
| 473 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 474 | #define BUF_DELTA 512 |
| 475 | /* |
| 476 | * Read a line from the top file in istk, handling multiple CR/LFs |
| 477 | * at the end of the line read, and handling spurious ^Zs. Will |
| 478 | * return lines from the standard macro set if this has not already |
| 479 | * been done. |
| 480 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 481 | static char *read_line (void) |
| 482 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 483 | char *buffer, *p, *q; |
| 484 | int bufsize; |
| 485 | |
| 486 | if (stdmacpos) { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 487 | if (*stdmacpos) { |
| 488 | char *ret = nasm_strdup(*stdmacpos++); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 489 | if (!*stdmacpos && any_extrastdmac) |
| 490 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 491 | stdmacpos = extrastdmac; |
| 492 | any_extrastdmac = FALSE; |
| 493 | return ret; |
| 494 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 495 | /* |
| 496 | * Nasty hack: here we push the contents of `predef' on |
| 497 | * to the top-level expansion stack, since this is the |
| 498 | * most convenient way to implement the pre-include and |
| 499 | * pre-define features. |
| 500 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 501 | if (!*stdmacpos) |
| 502 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 503 | Line *pd, *l; |
| 504 | Token *head, **tail, *t, *tt; |
| 505 | |
| 506 | for (pd = predef; pd; pd = pd->next) { |
| 507 | head = NULL; |
| 508 | tail = &head; |
| 509 | for (t = pd->first; t; t = t->next) { |
| 510 | tt = *tail = nasm_malloc(sizeof(Token)); |
| 511 | tt->next = NULL; |
| 512 | tail = &tt->next; |
| 513 | tt->type = t->type; |
| 514 | tt->text = nasm_strdup(t->text); |
| 515 | tt->mac = t->mac; /* always NULL here, in fact */ |
| 516 | } |
| 517 | l = nasm_malloc(sizeof(Line)); |
| 518 | l->next = istk->expansion; |
| 519 | l->first = head; |
| 520 | l->finishes = FALSE; |
| 521 | istk->expansion = l; |
| 522 | } |
| 523 | } |
| 524 | return ret; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 525 | } |
| 526 | else { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 527 | stdmacpos = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | |
| 531 | bufsize = BUF_DELTA; |
| 532 | buffer = nasm_malloc(BUF_DELTA); |
| 533 | p = buffer; |
| 534 | while (1) { |
| 535 | q = fgets(p, bufsize-(p-buffer), istk->fp); |
| 536 | if (!q) |
| 537 | break; |
| 538 | p += strlen(p); |
| 539 | if (p > buffer && p[-1] == '\n') { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 540 | break; |
| 541 | } |
| 542 | if (p-buffer > bufsize-10) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 543 | long offset = p-buffer; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 544 | bufsize += BUF_DELTA; |
| 545 | buffer = nasm_realloc(buffer, bufsize); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 546 | p = buffer+offset; /* prevent stale-pointer problems */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 547 | } |
| 548 | } |
| 549 | |
| 550 | if (!q && p == buffer) { |
| 551 | nasm_free (buffer); |
| 552 | return NULL; |
| 553 | } |
| 554 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 555 | src_set_linnum(src_get_linnum() + istk->lineinc); |
| 556 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 557 | /* |
| 558 | * Play safe: remove CRs as well as LFs, if any of either are |
| 559 | * present at the end of the line. |
| 560 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 561 | while (--p >= buffer && (*p == '\n' || *p == '\r')) |
| 562 | *p = '\0'; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 563 | |
| 564 | /* |
| 565 | * Handle spurious ^Z, which may be inserted into source files |
| 566 | * by some file transfer utilities. |
| 567 | */ |
| 568 | buffer[strcspn(buffer, "\032")] = '\0'; |
| 569 | |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 570 | list->line (LIST_READ, buffer); |
| 571 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 572 | return buffer; |
| 573 | } |
| 574 | |
| 575 | /* |
| 576 | * Tokenise a line of text. This is a very simple process since we |
| 577 | * don't need to parse the value out of e.g. numeric tokens: we |
| 578 | * simply split one string into many. |
| 579 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 580 | static Token *tokenise (char *line) |
| 581 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 582 | char *p = line; |
| 583 | int type; |
| 584 | Token *list = NULL; |
| 585 | Token *t, **tail = &list; |
| 586 | |
| 587 | while (*line) { |
| 588 | p = line; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 589 | if (*p == '%' && ( isdigit(p[1]) || |
| 590 | ((p[1] == '-' || p[1] == '+') && isdigit(p[2])))) |
| 591 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 592 | p++; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 593 | do { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 594 | p++; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 595 | } while (isdigit(*p)); |
| 596 | type = TOK_PREPROC_ID; |
| 597 | } |
| 598 | else if (*p == '%' && p[1] == '{') { |
| 599 | p += 2; |
| 600 | while (*p && *p != '}') { |
| 601 | p[-1] = *p; |
| 602 | p++; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 603 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 604 | p[-1] = '\0'; |
| 605 | if (*p) p++; |
| 606 | type = TOK_PREPROC_ID; |
| 607 | } |
| 608 | else if (*p == '%' && (isidchar(p[1]) || |
| 609 | ((p[1] == '!' || p[1] == '%' || p[1] == '$') && |
| 610 | isidchar(p[2])))) |
| 611 | { |
| 612 | p++; |
| 613 | do { |
| 614 | p++; |
| 615 | } while (isidchar(*p)); |
| 616 | type = TOK_PREPROC_ID; |
| 617 | } |
| 618 | else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 619 | type = TOK_ID; |
| 620 | p++; |
| 621 | while (*p && isidchar(*p)) |
| 622 | p++; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 623 | } |
| 624 | else if (*p == '\'' || *p == '"') { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 625 | /* |
| 626 | * A string token. |
| 627 | */ |
| 628 | char c = *p; |
| 629 | p++; |
| 630 | type = TOK_STRING; |
| 631 | while (*p && *p != c) |
| 632 | p++; |
| 633 | if (*p) p++; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 634 | } |
| 635 | else if (isnumstart(*p)) { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 636 | /* |
| 637 | * A number token. |
| 638 | */ |
| 639 | type = TOK_NUMBER; |
| 640 | p++; |
| 641 | while (*p && isnumchar(*p)) |
| 642 | p++; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 643 | } |
| 644 | else if (isspace(*p)) { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 645 | type = TOK_WHITESPACE; |
| 646 | p++; |
| 647 | while (*p && isspace(*p)) |
| 648 | p++; |
| 649 | /* |
| 650 | * Whitespace just before end-of-line is discarded by |
| 651 | * pretending it's a comment; whitespace just before a |
| 652 | * comment gets lumped into the comment. |
| 653 | */ |
| 654 | if (!*p || *p == ';') { |
| 655 | type = TOK_COMMENT; |
| 656 | while (*p) p++; |
| 657 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 658 | } |
| 659 | else if (*p == ';') { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 660 | type = TOK_COMMENT; |
| 661 | while (*p) p++; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 662 | } |
| 663 | else { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 664 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 665 | * Anything else is an operator of some kind. We check |
| 666 | * for all the double-character operators (>>, <<, //, |
| 667 | * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything |
| 668 | * else is a single-character operator. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 669 | */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 670 | type = TOK_OTHER; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 671 | if ((p[0] == '>' && p[1] == '>') || |
| 672 | (p[0] == '<' && p[1] == '<') || |
| 673 | (p[0] == '/' && p[1] == '/') || |
| 674 | (p[0] == '%' && p[1] == '%') || |
| 675 | (p[0] == '<' && p[1] == '=') || |
| 676 | (p[0] == '>' && p[1] == '=') || |
| 677 | (p[0] == '=' && p[1] == '=') || |
| 678 | (p[0] == '!' && p[1] == '=') || |
| 679 | (p[0] == '<' && p[1] == '>') || |
| 680 | (p[0] == '&' && p[1] == '&') || |
| 681 | (p[0] == '|' && p[1] == '|') || |
| 682 | (p[0] == '^' && p[1] == '^')) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 683 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 684 | p++; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 685 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 686 | p++; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 687 | } |
| 688 | if (type != TOK_COMMENT) { |
| 689 | *tail = t = nasm_malloc (sizeof(Token)); |
| 690 | tail = &t->next; |
| 691 | t->next = NULL; |
| 692 | t->type = type; |
| 693 | t->text = nasm_malloc(1+p-line); |
| 694 | strncpy(t->text, line, p-line); |
| 695 | t->text[p-line] = '\0'; |
| 696 | } |
| 697 | line = p; |
| 698 | } |
| 699 | |
| 700 | return list; |
| 701 | } |
| 702 | |
| 703 | /* |
| 704 | * Convert a line of tokens back into text. |
| 705 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 706 | char *detoken (Token *tlist) |
| 707 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 708 | Token *t; |
| 709 | int len; |
| 710 | char *line, *p; |
| 711 | |
| 712 | len = 0; |
| 713 | for (t = tlist; t; t = t->next) { |
| 714 | if (t->type == TOK_PREPROC_ID && t->text[1] == '!') { |
| 715 | char *p = getenv(t->text+2); |
| 716 | nasm_free (t->text); |
| 717 | if (p) |
| 718 | t->text = nasm_strdup(p); |
| 719 | else |
| 720 | t->text = NULL; |
| 721 | } |
| 722 | if (t->text) |
| 723 | len += strlen(t->text); |
| 724 | } |
| 725 | p = line = nasm_malloc(len+1); |
| 726 | for (t = tlist; t; t = t->next) { |
| 727 | if (t->text) { |
| 728 | strcpy (p, t->text); |
| 729 | p += strlen(p); |
| 730 | } |
| 731 | } |
| 732 | *p = '\0'; |
| 733 | return line; |
| 734 | } |
| 735 | |
| 736 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 737 | * A scanner, suitable for use by the expression evaluator, which |
| 738 | * operates on a line of Tokens. Expects a pointer to a pointer to |
| 739 | * the first token in the line to be passed in as its private_data |
| 740 | * field. |
| 741 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 742 | static int ppscan(void *private_data, struct tokenval *tokval) |
| 743 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 744 | Token **tlineptr = private_data; |
| 745 | Token *tline; |
| 746 | |
| 747 | do { |
| 748 | tline = *tlineptr; |
| 749 | *tlineptr = tline ? tline->next : NULL; |
| 750 | } while (tline && (tline->type == TOK_WHITESPACE || |
| 751 | tline->type == TOK_COMMENT)); |
| 752 | |
| 753 | if (!tline) |
| 754 | return tokval->t_type = TOKEN_EOS; |
| 755 | |
| 756 | if (tline->text[0] == '$' && !tline->text[1]) |
| 757 | return tokval->t_type = TOKEN_HERE; |
| 758 | if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[1]) |
| 759 | return tokval->t_type = TOKEN_BASE; |
| 760 | |
| 761 | if (tline->type == TOK_ID) { |
| 762 | tokval->t_charptr = tline->text; |
| 763 | if (tline->text[0] == '$') { |
| 764 | tokval->t_charptr++; |
| 765 | return tokval->t_type = TOKEN_ID; |
| 766 | } |
| 767 | |
| 768 | /* |
| 769 | * This is the only special case we actually need to worry |
| 770 | * about in this restricted context. |
| 771 | */ |
| 772 | if (!nasm_stricmp(tline->text, "seg")) |
| 773 | return tokval->t_type = TOKEN_SEG; |
| 774 | |
| 775 | return tokval->t_type = TOKEN_ID; |
| 776 | } |
| 777 | |
| 778 | if (tline->type == TOK_NUMBER) { |
| 779 | int rn_error; |
| 780 | |
| 781 | tokval->t_integer = readnum(tline->text, &rn_error); |
| 782 | if (rn_error) |
| 783 | return tokval->t_type = TOKEN_ERRNUM; |
| 784 | tokval->t_charptr = NULL; |
| 785 | return tokval->t_type = TOKEN_NUM; |
| 786 | } |
| 787 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 788 | if (tline->type == TOK_STRING) { |
| 789 | int rn_warn; |
| 790 | char q, *r; |
| 791 | int l; |
| 792 | |
| 793 | r = tline->text; |
| 794 | q = *r++; |
| 795 | l = strlen(r); |
| 796 | |
| 797 | if (l == 0 || r[l-1] != q) |
| 798 | return tokval->t_type = TOKEN_ERRNUM; |
| 799 | tokval->t_integer = readstrnum(r, l-1, &rn_warn); |
| 800 | if (rn_warn) |
| 801 | error(ERR_WARNING|ERR_PASS1, |
| 802 | "character constant too long"); |
| 803 | tokval->t_charptr = NULL; |
| 804 | return tokval->t_type = TOKEN_NUM; |
| 805 | } |
| 806 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 807 | if (tline->type == TOK_OTHER) { |
| 808 | if (!strcmp(tline->text, "<<")) return tokval->t_type = TOKEN_SHL; |
| 809 | if (!strcmp(tline->text, ">>")) return tokval->t_type = TOKEN_SHR; |
| 810 | if (!strcmp(tline->text, "//")) return tokval->t_type = TOKEN_SDIV; |
| 811 | if (!strcmp(tline->text, "%%")) return tokval->t_type = TOKEN_SMOD; |
| 812 | if (!strcmp(tline->text, "==")) return tokval->t_type = TOKEN_EQ; |
| 813 | if (!strcmp(tline->text, "<>")) return tokval->t_type = TOKEN_NE; |
| 814 | if (!strcmp(tline->text, "!=")) return tokval->t_type = TOKEN_NE; |
| 815 | if (!strcmp(tline->text, "<=")) return tokval->t_type = TOKEN_LE; |
| 816 | if (!strcmp(tline->text, ">=")) return tokval->t_type = TOKEN_GE; |
| 817 | if (!strcmp(tline->text, "&&")) return tokval->t_type = TOKEN_DBL_AND; |
| 818 | if (!strcmp(tline->text, "^^")) return tokval->t_type = TOKEN_DBL_XOR; |
| 819 | if (!strcmp(tline->text, "||")) return tokval->t_type = TOKEN_DBL_OR; |
| 820 | } |
| 821 | |
| 822 | /* |
| 823 | * We have no other options: just return the first character of |
| 824 | * the token text. |
| 825 | */ |
| 826 | return tokval->t_type = tline->text[0]; |
| 827 | } |
| 828 | |
| 829 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 830 | * Return the Context structure associated with a %$ token. Return |
| 831 | * NULL, having _already_ reported an error condition, if the |
| 832 | * context stack isn't deep enough for the supplied number of $ |
| 833 | * signs. |
| 834 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 835 | static Context *get_ctx (char *name) |
| 836 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 837 | Context *ctx; |
| 838 | int i; |
| 839 | |
| 840 | if (!cstk) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 841 | error (ERR_NONFATAL, "`%s': context stack is empty", name); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 842 | return NULL; |
| 843 | } |
| 844 | |
| 845 | i = 1; |
| 846 | ctx = cstk; |
| 847 | while (name[i+1] == '$') { |
| 848 | i++; |
| 849 | ctx = ctx->next; |
| 850 | if (!ctx) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 851 | error (ERR_NONFATAL, "`%s': context stack is only" |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 852 | " %d level%s deep", name, i-1, (i==2 ? "" : "s")); |
| 853 | return NULL; |
| 854 | } |
| 855 | } |
| 856 | return ctx; |
| 857 | } |
| 858 | |
| 859 | /* |
| 860 | * Compare a string to the name of an existing macro; this is a |
| 861 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 862 | * depending on the value of the `casesense' parameter. |
| 863 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 864 | static int mstrcmp(char *p, char *q, int casesense) |
| 865 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 866 | return casesense ? strcmp(p,q) : nasm_stricmp(p,q); |
| 867 | } |
| 868 | |
| 869 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 870 | * Open an include file. This routine must always return a valid |
| 871 | * file pointer if it returns - it's responsible for throwing an |
| 872 | * ERR_FATAL and bombing out completely if not. It should also try |
| 873 | * the include path one by one until it finds the file or reaches |
| 874 | * the end of the path. |
| 875 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 876 | static FILE *inc_fopen(char *file) |
| 877 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 878 | FILE *fp; |
| 879 | char *prefix = "", *combine; |
| 880 | IncPath *ip = ipath; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 881 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 882 | while (1) { |
| 883 | combine = nasm_strcat(prefix,file); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 884 | fp = fopen(combine, "r"); |
| 885 | nasm_free (combine); |
| 886 | if (fp) |
| 887 | return fp; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 888 | if (!ip) |
| 889 | break; |
| 890 | prefix = ip->path; |
| 891 | ip = ip->next; |
| 892 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 893 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 894 | error (ERR_FATAL, |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 895 | "unable to open include file `%s'", file); |
| 896 | return NULL; /* never reached - placate compilers */ |
| 897 | } |
| 898 | |
| 899 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 900 | * Determine if we should warn on defining a single-line macro of |
| 901 | * name `name', with `nparam' parameters. If nparam is 0, will |
| 902 | * return TRUE if _any_ single-line macro of that name is defined. |
| 903 | * Otherwise, will return TRUE if a single-line macro with either |
| 904 | * `nparam' or no parameters is defined. |
| 905 | * |
| 906 | * If a macro with precisely the right number of parameters is |
| 907 | * defined, the address of the definition structure will be |
| 908 | * returned in `defn'; otherwise NULL will be returned. If `defn' |
| 909 | * is NULL, no action will be taken regarding its contents, and no |
| 910 | * error will occur. |
| 911 | * |
| 912 | * Note that this is also called with nparam zero to resolve |
| 913 | * `ifdef'. |
| 914 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 915 | static int smacro_defined (char *name, int nparam, SMacro **defn, int nocase) |
| 916 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 917 | SMacro *m; |
| 918 | Context *ctx; |
| 919 | char *p; |
| 920 | |
| 921 | if (name[0] == '%' && name[1] == '$') { |
| 922 | ctx = get_ctx (name); |
| 923 | if (!ctx) |
| 924 | return FALSE; /* got to return _something_ */ |
| 925 | m = ctx->localmac; |
| 926 | p = name+1; |
| 927 | p += strspn(p, "$"); |
| 928 | } else { |
| 929 | m = smacros[hash(name)]; |
| 930 | p = name; |
| 931 | } |
| 932 | |
| 933 | while (m) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 934 | if (!mstrcmp(m->name, p, m->casesense & nocase) && |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 935 | (nparam == 0 || m->nparam == 0 || nparam == m->nparam)) { |
| 936 | if (defn) { |
| 937 | if (nparam == m->nparam) |
| 938 | *defn = m; |
| 939 | else |
| 940 | *defn = NULL; |
| 941 | } |
| 942 | return TRUE; |
| 943 | } |
| 944 | m = m->next; |
| 945 | } |
| 946 | return FALSE; |
| 947 | } |
| 948 | |
| 949 | /* |
| 950 | * Count and mark off the parameters in a multi-line macro call. |
| 951 | * This is called both from within the multi-line macro expansion |
| 952 | * code, and also to mark off the default parameters when provided |
| 953 | * in a %macro definition line. |
| 954 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 955 | static void count_mmac_params (Token *t, int *nparam, Token ***params) |
| 956 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 957 | int paramsize, brace; |
| 958 | |
| 959 | *nparam = paramsize = 0; |
| 960 | *params = NULL; |
| 961 | while (t) { |
| 962 | if (*nparam >= paramsize) { |
| 963 | paramsize += PARAM_DELTA; |
| 964 | *params = nasm_realloc(*params, sizeof(**params) * paramsize); |
| 965 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 966 | skip_white_(t); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 967 | brace = FALSE; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 968 | if (tok_is_(t, "{")) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 969 | brace = TRUE; |
| 970 | (*params)[(*nparam)++] = t; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 971 | while (tok_isnt_(t, brace ? "}" : ",")) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 972 | t = t->next; |
| 973 | if (t) { /* got a comma/brace */ |
| 974 | t = t->next; |
| 975 | if (brace) { |
| 976 | /* |
| 977 | * Now we've found the closing brace, look further |
| 978 | * for the comma. |
| 979 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 980 | skip_white_(t); |
| 981 | if (tok_isnt_(t, ",")) { |
| 982 | error (ERR_NONFATAL, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 983 | "braces do not enclose all of macro parameter"); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 984 | while (tok_isnt_(t, ",")) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 985 | t = t->next; |
| 986 | } |
| 987 | if (t) |
| 988 | t = t->next; /* eat the comma */ |
| 989 | } |
| 990 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 991 | } |
| 992 | } |
| 993 | |
| 994 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 995 | * Determine whether one of the various `if' conditions is true or |
| 996 | * not. |
| 997 | * |
| 998 | * We must free the tline we get passed. |
| 999 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1000 | static int if_condition (Token *tline, int i) |
| 1001 | { |
| 1002 | int j, casesense; |
| 1003 | Token * t, * tt, ** tptr, * origline; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1004 | struct tokenval tokval; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1005 | expr * evalresult; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1006 | |
| 1007 | origline = tline; |
| 1008 | |
| 1009 | switch (i) { |
| 1010 | case PP_IFCTX: case PP_ELIFCTX: |
| 1011 | case PP_IFNCTX: case PP_ELIFNCTX: |
| 1012 | j = FALSE; /* have we matched yet? */ |
| 1013 | if (!cstk) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1014 | error(ERR_FATAL, |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1015 | "`%s': context stack is empty", directives[i]); |
| 1016 | else while (tline) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1017 | skip_white_(tline); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1018 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1019 | error(ERR_NONFATAL, |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1020 | "`%s' expects context identifiers", directives[i]); |
| 1021 | free_tlist (origline); |
| 1022 | return -1; |
| 1023 | } |
| 1024 | if (!nasm_stricmp(tline->text, cstk->name)) |
| 1025 | j = TRUE; |
| 1026 | tline = tline->next; |
| 1027 | } |
| 1028 | if (i == PP_IFNCTX || i == PP_ELIFNCTX) |
| 1029 | j = !j; |
| 1030 | free_tlist (origline); |
| 1031 | return j; |
| 1032 | |
| 1033 | case PP_IFDEF: case PP_ELIFDEF: |
| 1034 | case PP_IFNDEF: case PP_ELIFNDEF: |
| 1035 | j = FALSE; /* have we matched yet? */ |
| 1036 | while (tline) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1037 | skip_white_(tline); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1038 | if (!tline || (tline->type != TOK_ID && |
| 1039 | (tline->type != TOK_PREPROC_ID || |
| 1040 | tline->text[1] != '$'))) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1041 | error(ERR_NONFATAL, |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1042 | "`%%if%sdef' expects macro identifiers", |
| 1043 | (i==PP_ELIFNDEF ? "n" : "")); |
| 1044 | free_tlist (origline); |
| 1045 | return -1; |
| 1046 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1047 | if (smacro_defined(tline->text, 0, NULL, 1)) |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1048 | j = TRUE; |
| 1049 | tline = tline->next; |
| 1050 | } |
| 1051 | if (i == PP_IFNDEF || i == PP_ELIFNDEF) |
| 1052 | j = !j; |
| 1053 | free_tlist (origline); |
| 1054 | return j; |
| 1055 | |
| 1056 | case PP_IFIDN: case PP_ELIFIDN: case PP_IFNIDN: case PP_ELIFNIDN: |
| 1057 | case PP_IFIDNI: case PP_ELIFIDNI: case PP_IFNIDNI: case PP_ELIFNIDNI: |
| 1058 | tline = expand_smacro(tline); |
| 1059 | t = tt = tline; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1060 | while (tok_isnt_(tt, ",")) |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1061 | tt = tt->next; |
| 1062 | if (!tt) { |
| 1063 | error(ERR_NONFATAL, "`%s' expects two comma-separated arguments"); |
| 1064 | free_tlist (tline); |
| 1065 | return -1; |
| 1066 | } |
| 1067 | tt = tt->next; |
| 1068 | casesense = (i == PP_IFIDN || i == PP_ELIFIDN || |
| 1069 | i == PP_IFNIDN || i == PP_ELIFNIDN); |
| 1070 | j = TRUE; /* assume equality unless proved not */ |
| 1071 | while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) { |
| 1072 | if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) { |
| 1073 | error(ERR_NONFATAL, "`%s': more than one comma on line", |
| 1074 | directives[i]); |
| 1075 | free_tlist (tline); |
| 1076 | return -1; |
| 1077 | } |
| 1078 | if (t->type == TOK_WHITESPACE) { |
| 1079 | t = t->next; |
| 1080 | continue; |
| 1081 | } else if (tt->type == TOK_WHITESPACE) { |
| 1082 | tt = tt->next; |
| 1083 | continue; |
| 1084 | } else if (tt->type != t->type || |
| 1085 | (casesense ? strcmp(tt->text, t->text) : |
| 1086 | nasm_stricmp(tt->text, t->text))) { |
| 1087 | j = FALSE; /* found mismatching tokens */ |
| 1088 | break; |
| 1089 | } else { |
| 1090 | t = t->next; |
| 1091 | tt = tt->next; |
| 1092 | continue; |
| 1093 | } |
| 1094 | } |
| 1095 | if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt) |
| 1096 | j = FALSE; /* trailing gunk on one end or other */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1097 | if (i == PP_IFNIDN || i == PP_ELIFNIDN || |
| 1098 | i == PP_IFNIDNI || i == PP_ELIFNIDNI) |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1099 | j = !j; |
| 1100 | free_tlist (tline); |
| 1101 | return j; |
| 1102 | |
| 1103 | case PP_IFID: case PP_ELIFID: case PP_IFNID: case PP_ELIFNID: |
| 1104 | case PP_IFNUM: case PP_ELIFNUM: case PP_IFNNUM: case PP_ELIFNNUM: |
| 1105 | case PP_IFSTR: case PP_ELIFSTR: case PP_IFNSTR: case PP_ELIFNSTR: |
| 1106 | tline = expand_smacro(tline); |
| 1107 | t = tline; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1108 | while (tok_type_(t, TOK_WHITESPACE)) |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1109 | t = t->next; |
| 1110 | j = FALSE; /* placate optimiser */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1111 | if (t) switch (i) { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1112 | case PP_IFID: case PP_ELIFID: case PP_IFNID: case PP_ELIFNID: |
| 1113 | j = (t->type == TOK_ID); |
| 1114 | break; |
| 1115 | case PP_IFNUM: case PP_ELIFNUM: case PP_IFNNUM: case PP_ELIFNNUM: |
| 1116 | j = (t->type == TOK_NUMBER); |
| 1117 | break; |
| 1118 | case PP_IFSTR: case PP_ELIFSTR: case PP_IFNSTR: case PP_ELIFNSTR: |
| 1119 | j = (t->type == TOK_STRING); |
| 1120 | break; |
| 1121 | } |
| 1122 | if (i == PP_IFNID || i == PP_ELIFNID || |
| 1123 | i == PP_IFNNUM || i == PP_ELIFNNUM || |
| 1124 | i == PP_IFNSTR || i == PP_ELIFNSTR) |
| 1125 | j = !j; |
| 1126 | free_tlist (tline); |
| 1127 | return j; |
| 1128 | |
| 1129 | case PP_IF: case PP_ELIF: |
| 1130 | t = tline = expand_smacro(tline); |
| 1131 | tptr = &t; |
| 1132 | tokval.t_type = TOKEN_INVALID; |
| 1133 | evalresult = evaluate (ppscan, tptr, &tokval, |
| 1134 | NULL, pass | 0x10, error, NULL); |
| 1135 | free_tlist (tline); |
| 1136 | if (!evalresult) |
| 1137 | return -1; |
| 1138 | if (tokval.t_type) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1139 | error(ERR_WARNING, |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1140 | "trailing garbage after expression ignored"); |
| 1141 | if (!is_simple(evalresult)) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1142 | error(ERR_NONFATAL, |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1143 | "non-constant value given to `%s'", directives[i]); |
| 1144 | return -1; |
| 1145 | } |
| 1146 | return reloc_value(evalresult) != 0; |
| 1147 | |
| 1148 | default: |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1149 | error(ERR_FATAL, |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1150 | "preprocessor directive `%s' not yet implemented", |
| 1151 | directives[i]); |
| 1152 | free_tlist (origline); |
| 1153 | return -1; /* yeah, right */ |
| 1154 | } |
| 1155 | } |
| 1156 | |
| 1157 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1158 | * Find out if a line contains a preprocessor directive, and deal |
| 1159 | * with it if so. |
| 1160 | * |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1161 | * If a directive _is_ found, we are expected to free_tlist() the |
| 1162 | * line. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1163 | * |
| 1164 | * Return values go like this: |
| 1165 | * |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1166 | * bit 0 is set if a directive was found (so the line gets freed) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1167 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1168 | static int do_directive (Token *tline) |
| 1169 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1170 | int i, j, k, m, nparam, nolist; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1171 | char *p, *mname; |
| 1172 | Include *inc; |
| 1173 | Context *ctx; |
| 1174 | Cond *cond; |
| 1175 | SMacro *smac, **smhead; |
| 1176 | MMacro *mmac; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1177 | Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline; |
| 1178 | Line *l; |
| 1179 | struct tokenval tokval; |
| 1180 | expr *evalresult; |
H. Peter Anvin | b64535f | 2002-04-30 20:55:37 +0000 | [diff] [blame] | 1181 | MMacro *tmp_defining; /* Used when manipulating rep_nest */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1182 | |
| 1183 | origline = tline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1184 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1185 | skip_white_(tline); |
| 1186 | if (!tok_type_(tline, TOK_PREPROC_ID) || |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1187 | (tline->text[1]=='%' || tline->text[1]=='$' || tline->text[1]=='!')) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1188 | return 0; |
| 1189 | |
| 1190 | i = -1; |
| 1191 | j = sizeof(directives)/sizeof(*directives); |
| 1192 | while (j-i > 1) { |
| 1193 | k = (j+i) / 2; |
| 1194 | m = nasm_stricmp(tline->text, directives[k]); |
| 1195 | if (m == 0) { |
| 1196 | i = k; |
| 1197 | j = -2; |
| 1198 | break; |
| 1199 | } else if (m < 0) { |
| 1200 | j = k; |
| 1201 | } else |
| 1202 | i = k; |
| 1203 | } |
| 1204 | |
| 1205 | /* |
| 1206 | * If we're in a non-emitting branch of a condition construct, |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1207 | * or walking to the end of an already terminated %rep block, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1208 | * we should ignore all directives except for condition |
| 1209 | * directives. |
| 1210 | */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1211 | if (((istk->conds && !emitting(istk->conds->state)) || |
| 1212 | (istk->mstk && !istk->mstk->in_progress)) && |
| 1213 | i != PP_IF && i != PP_ELIF && |
| 1214 | i != PP_IFCTX && i != PP_ELIFCTX && |
| 1215 | i != PP_IFDEF && i != PP_ELIFDEF && |
| 1216 | i != PP_IFID && i != PP_ELIFID && |
| 1217 | i != PP_IFIDN && i != PP_ELIFIDN && |
| 1218 | i != PP_IFIDNI && i != PP_ELIFIDNI && |
| 1219 | i != PP_IFNCTX && i != PP_ELIFNCTX && |
| 1220 | i != PP_IFNDEF && i != PP_ELIFNDEF && |
| 1221 | i != PP_IFNID && i != PP_ELIFNID && |
| 1222 | i != PP_IFNIDN && i != PP_ELIFNIDN && |
| 1223 | i != PP_IFNIDNI && i != PP_ELIFNIDNI && |
| 1224 | i != PP_IFNNUM && i != PP_ELIFNNUM && |
| 1225 | i != PP_IFNSTR && i != PP_ELIFNSTR && |
| 1226 | i != PP_IFNUM && i != PP_ELIFNUM && |
| 1227 | i != PP_IFSTR && i != PP_ELIFSTR && |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1228 | i != PP_ELSE && i != PP_ENDIF) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1229 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1230 | return 0; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1231 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1232 | |
| 1233 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1234 | * If we're defining a macro or reading a %rep block, we should |
| 1235 | * ignore all directives except for %macro/%imacro (which |
| 1236 | * generate an error), %endm/%endmacro, and (only if we're in a |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1237 | * %rep block) %endrep. If we're in a %rep block, another %rep |
| 1238 | * causes an error, so should be let through. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1239 | */ |
| 1240 | if (defining && i != PP_MACRO && i != PP_IMACRO && |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1241 | i != PP_ENDMACRO && i != PP_ENDM && |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1242 | (defining->name || (i != PP_ENDREP && i != PP_REP))) |
| 1243 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1244 | return 0; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1245 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1246 | |
| 1247 | if (j != -2) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1248 | error(ERR_NONFATAL, "unknown preprocessor directive `%s'", |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1249 | tline->text); |
| 1250 | return 0; /* didn't get it */ |
| 1251 | } |
| 1252 | |
| 1253 | switch (i) { |
| 1254 | |
| 1255 | case PP_CLEAR: |
| 1256 | if (tline->next) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1257 | error(ERR_WARNING, |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1258 | "trailing garbage after `%%clear' ignored"); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1259 | for (j=0; j<NHASH; j++) { |
| 1260 | while (mmacros[j]) { |
| 1261 | MMacro *m = mmacros[j]; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1262 | mmacros[j] = m->next; |
| 1263 | free_mmacro(m); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1264 | } |
| 1265 | while (smacros[j]) { |
| 1266 | SMacro *s = smacros[j]; |
| 1267 | smacros[j] = smacros[j]->next; |
| 1268 | nasm_free (s->name); |
| 1269 | free_tlist (s->expansion); |
| 1270 | nasm_free (s); |
| 1271 | } |
| 1272 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1273 | free_tlist (origline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1274 | return 3; |
| 1275 | |
| 1276 | case PP_INCLUDE: |
| 1277 | tline = tline->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1278 | skip_white_(tline); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1279 | if (!tline || (tline->type != TOK_STRING && |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1280 | tline->type != TOK_INTERNAL_STRING)) |
| 1281 | { |
| 1282 | error(ERR_NONFATAL, "`%%include' expects a file name"); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1283 | free_tlist (origline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1284 | return 3; /* but we did _something_ */ |
| 1285 | } |
| 1286 | if (tline->next) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1287 | error(ERR_WARNING, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1288 | "trailing garbage after `%%include' ignored"); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1289 | if (tline->type != TOK_INTERNAL_STRING) { |
| 1290 | p = tline->text+1; /* point past the quote to the name */ |
| 1291 | p[strlen(p)-1] = '\0'; /* remove the trailing quote */ |
| 1292 | } else |
| 1293 | p = tline->text; /* internal_string is easier */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1294 | inc = nasm_malloc(sizeof(Include)); |
| 1295 | inc->next = istk; |
| 1296 | inc->conds = NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1297 | inc->fp = inc_fopen(p); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1298 | inc->fname = src_set_fname(nasm_strdup(p)); |
| 1299 | inc->lineno = src_set_linnum(0); |
| 1300 | inc->lineinc = 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1301 | inc->expansion = NULL; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1302 | inc->mstk = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1303 | istk = inc; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1304 | list->uplevel (LIST_INCLUDE); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1305 | free_tlist (origline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1306 | return 5; |
| 1307 | |
| 1308 | case PP_PUSH: |
| 1309 | tline = tline->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1310 | skip_white_(tline); |
| 1311 | if (!tok_type_(tline, TOK_ID)) { |
| 1312 | error(ERR_NONFATAL, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1313 | "`%%push' expects a context identifier"); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1314 | free_tlist (origline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1315 | return 3; /* but we did _something_ */ |
| 1316 | } |
| 1317 | if (tline->next) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1318 | error(ERR_WARNING, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1319 | "trailing garbage after `%%push' ignored"); |
| 1320 | ctx = nasm_malloc(sizeof(Context)); |
| 1321 | ctx->next = cstk; |
| 1322 | ctx->localmac = NULL; |
| 1323 | ctx->name = nasm_strdup(tline->text); |
| 1324 | ctx->number = unique++; |
| 1325 | cstk = ctx; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1326 | free_tlist (origline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1327 | break; |
| 1328 | |
| 1329 | case PP_REPL: |
| 1330 | tline = tline->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1331 | skip_white_(tline); |
| 1332 | if (!tok_type_(tline, TOK_ID)) { |
| 1333 | error(ERR_NONFATAL, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1334 | "`%%repl' expects a context identifier"); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1335 | free_tlist (origline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1336 | return 3; /* but we did _something_ */ |
| 1337 | } |
| 1338 | if (tline->next) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1339 | error(ERR_WARNING, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1340 | "trailing garbage after `%%repl' ignored"); |
| 1341 | if (!cstk) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1342 | error(ERR_NONFATAL, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1343 | "`%%repl': context stack is empty"); |
| 1344 | else { |
| 1345 | nasm_free (cstk->name); |
| 1346 | cstk->name = nasm_strdup(tline->text); |
| 1347 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1348 | free_tlist (origline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1349 | break; |
| 1350 | |
| 1351 | case PP_POP: |
| 1352 | if (tline->next) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1353 | error(ERR_WARNING, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1354 | "trailing garbage after `%%pop' ignored"); |
| 1355 | if (!cstk) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1356 | error(ERR_NONFATAL, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1357 | "`%%pop': context stack is already empty"); |
| 1358 | else |
| 1359 | ctx_pop(); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1360 | free_tlist (origline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1361 | break; |
| 1362 | |
| 1363 | case PP_ERROR: |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1364 | tline->next = expand_smacro (tline->next); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1365 | tline = tline->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1366 | skip_white_(tline); |
| 1367 | if (tok_type_(tline, TOK_STRING)) { |
| 1368 | p = tline->text+1; /* point past the quote to the name */ |
| 1369 | p[strlen(p)-1] = '\0'; /* remove the trailing quote */ |
| 1370 | error(ERR_NONFATAL, "user error: %s", p); |
| 1371 | } else { |
| 1372 | p = detoken(tline); |
| 1373 | error(ERR_WARNING, "user error: %s", p); |
| 1374 | nasm_free(p); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1375 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1376 | free_tlist (origline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1377 | break; |
| 1378 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1379 | case PP_IF: |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1380 | case PP_IFCTX: |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1381 | case PP_IFDEF: |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1382 | case PP_IFID: |
| 1383 | case PP_IFIDN: |
| 1384 | case PP_IFIDNI: |
| 1385 | case PP_IFNCTX: |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1386 | case PP_IFNDEF: |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1387 | case PP_IFNID: |
| 1388 | case PP_IFNIDN: |
| 1389 | case PP_IFNIDNI: |
| 1390 | case PP_IFNNUM: |
| 1391 | case PP_IFNSTR: |
| 1392 | case PP_IFNUM: |
| 1393 | case PP_IFSTR: |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1394 | if (istk->conds && !emitting(istk->conds->state)) |
| 1395 | j = COND_NEVER; |
| 1396 | else { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1397 | j = if_condition(tline->next, i); |
| 1398 | tline->next = NULL; /* it got freed */ |
| 1399 | free_tlist (origline); |
| 1400 | if (j < 0) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1401 | /* |
| 1402 | * Bogus expression in %if, but we should pretend |
| 1403 | * it was OK anyway, so that we don't get an error |
| 1404 | * cascade on the subsequent %else / %endif. |
| 1405 | */ |
| 1406 | j = COND_NEVER; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1407 | else |
| 1408 | j = j ? COND_IF_TRUE : COND_IF_FALSE; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1409 | } |
| 1410 | cond = nasm_malloc(sizeof(Cond)); |
| 1411 | cond->next = istk->conds; |
| 1412 | cond->state = j; |
| 1413 | istk->conds = cond; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1414 | return (j == COND_IF_TRUE ? 3 : 1); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1415 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1416 | case PP_ELIF: |
| 1417 | case PP_ELIFCTX: |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1418 | case PP_ELIFDEF: |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1419 | case PP_ELIFID: |
| 1420 | case PP_ELIFIDN: |
| 1421 | case PP_ELIFIDNI: |
| 1422 | case PP_ELIFNCTX: |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1423 | case PP_ELIFNDEF: |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1424 | case PP_ELIFNID: |
| 1425 | case PP_ELIFNIDN: |
| 1426 | case PP_ELIFNIDNI: |
| 1427 | case PP_ELIFNNUM: |
| 1428 | case PP_ELIFNSTR: |
| 1429 | case PP_ELIFNUM: |
| 1430 | case PP_ELIFSTR: |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1431 | if (!istk->conds) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1432 | error(ERR_FATAL, "`%s': no matching `%%if'", |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1433 | directives[i]); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1434 | if (emitting(istk->conds->state) || istk->conds->state == COND_NEVER) |
| 1435 | istk->conds->state = COND_NEVER; |
| 1436 | else { |
H. Peter Anvin | 4836e33 | 2002-04-30 20:56:43 +0000 | [diff] [blame^] | 1437 | j = if_condition(expand_mmac_params(tline->next), i); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1438 | tline->next = NULL; /* it got freed */ |
| 1439 | free_tlist (origline); |
| 1440 | if (j < 0) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1441 | /* |
| 1442 | * The expression was bogus, but let's make |
| 1443 | * %endif not complain about missing %if |
| 1444 | */ |
| 1445 | j = COND_NEVER; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1446 | else |
| 1447 | istk->conds->state = j ? COND_IF_TRUE : COND_IF_FALSE; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1448 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1449 | return (istk->conds->state == COND_IF_TRUE ? 5 : 1); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1450 | |
| 1451 | case PP_ELSE: |
| 1452 | if (tline->next) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1453 | error(ERR_WARNING, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1454 | "trailing garbage after `%%else' ignored"); |
| 1455 | if (!istk->conds) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1456 | error(ERR_FATAL, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1457 | "`%%else': no matching `%%if'"); |
| 1458 | if (emitting(istk->conds->state) || istk->conds->state == COND_NEVER) |
| 1459 | istk->conds->state = COND_ELSE_FALSE; |
| 1460 | else |
| 1461 | istk->conds->state = COND_ELSE_TRUE; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1462 | free_tlist (origline); |
| 1463 | return 5; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1464 | |
| 1465 | case PP_ENDIF: |
| 1466 | if (tline->next) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1467 | error(ERR_WARNING, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1468 | "trailing garbage after `%%endif' ignored"); |
| 1469 | if (!istk->conds) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1470 | error(ERR_FATAL, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1471 | "`%%endif': no matching `%%if'"); |
| 1472 | cond = istk->conds; |
| 1473 | istk->conds = cond->next; |
| 1474 | nasm_free (cond); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1475 | free_tlist (origline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1476 | return 5; |
| 1477 | |
| 1478 | case PP_MACRO: |
| 1479 | case PP_IMACRO: |
| 1480 | if (defining) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1481 | error (ERR_FATAL, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1482 | "`%%%smacro': already defining a macro", |
| 1483 | (i == PP_IMACRO ? "i" : "")); |
| 1484 | tline = tline->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1485 | skip_white_(tline); |
| 1486 | if (!tok_type_(tline, TOK_ID)) { |
| 1487 | error (ERR_NONFATAL, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1488 | "`%%%smacro' expects a macro name", |
| 1489 | (i == PP_IMACRO ? "i" : "")); |
| 1490 | return 3; |
| 1491 | } |
| 1492 | defining = nasm_malloc(sizeof(MMacro)); |
| 1493 | defining->name = nasm_strdup(tline->text); |
| 1494 | defining->casesense = (i == PP_MACRO); |
| 1495 | defining->plus = FALSE; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1496 | defining->nolist = FALSE; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1497 | defining->in_progress = FALSE; |
H. Peter Anvin | b64535f | 2002-04-30 20:55:37 +0000 | [diff] [blame] | 1498 | defining->rep_nest = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1499 | tline = tline->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1500 | skip_white_(tline); |
| 1501 | if (!tok_type_(tline, TOK_NUMBER)) { |
| 1502 | error (ERR_NONFATAL, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1503 | "`%%%smacro' expects a parameter count", |
| 1504 | (i == PP_IMACRO ? "i" : "")); |
| 1505 | defining->nparam_min = defining->nparam_max = 0; |
| 1506 | } else { |
| 1507 | defining->nparam_min = defining->nparam_max = |
| 1508 | readnum(tline->text, &j); |
| 1509 | if (j) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1510 | error (ERR_NONFATAL, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1511 | "unable to parse parameter count `%s'", tline->text); |
| 1512 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1513 | if (tline && tok_is_(tline->next, "-")) { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1514 | tline = tline->next->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1515 | if (tok_is_(tline, "*")) |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1516 | defining->nparam_max = INT_MAX; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1517 | else if (!tok_type_(tline, TOK_NUMBER)) |
| 1518 | error (ERR_NONFATAL, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1519 | "`%%%smacro' expects a parameter count after `-'", |
| 1520 | (i == PP_IMACRO ? "i" : "")); |
| 1521 | else { |
| 1522 | defining->nparam_max = readnum(tline->text, &j); |
| 1523 | if (j) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1524 | error (ERR_NONFATAL, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1525 | "unable to parse parameter count `%s'", |
| 1526 | tline->text); |
| 1527 | if (defining->nparam_min > defining->nparam_max) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1528 | error (ERR_NONFATAL, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1529 | "minimum parameter count exceeds maximum"); |
| 1530 | } |
| 1531 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1532 | if (tline && tok_is_(tline->next, "+")) { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1533 | tline = tline->next; |
| 1534 | defining->plus = TRUE; |
| 1535 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1536 | if (tline && tok_type_(tline->next, TOK_ID) && |
| 1537 | !nasm_stricmp(tline->next->text, ".nolist")) |
| 1538 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1539 | tline = tline->next; |
| 1540 | defining->nolist = TRUE; |
| 1541 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1542 | mmac = mmacros[hash(defining->name)]; |
| 1543 | while (mmac) { |
| 1544 | if (!strcmp(mmac->name, defining->name) && |
| 1545 | (mmac->nparam_min<=defining->nparam_max || defining->plus) && |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1546 | (defining->nparam_min<=mmac->nparam_max || mmac->plus)) |
| 1547 | { |
| 1548 | error (ERR_WARNING, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1549 | "redefining multi-line macro `%s'", defining->name); |
| 1550 | break; |
| 1551 | } |
| 1552 | mmac = mmac->next; |
| 1553 | } |
| 1554 | /* |
| 1555 | * Handle default parameters. |
| 1556 | */ |
| 1557 | if (tline && tline->next) { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1558 | defining->dlist = tline->next; |
| 1559 | tline->next = NULL; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1560 | count_mmac_params (defining->dlist, &defining->ndefs, |
| 1561 | &defining->defaults); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1562 | } else { |
| 1563 | defining->dlist = NULL; |
| 1564 | defining->defaults = NULL; |
| 1565 | } |
| 1566 | defining->expansion = NULL; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1567 | free_tlist (origline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1568 | return 1; |
| 1569 | |
| 1570 | case PP_ENDM: |
| 1571 | case PP_ENDMACRO: |
| 1572 | if (!defining) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1573 | error (ERR_NONFATAL, "`%s': not defining a macro", |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1574 | tline->text); |
| 1575 | return 3; |
| 1576 | } |
| 1577 | k = hash(defining->name); |
| 1578 | defining->next = mmacros[k]; |
| 1579 | mmacros[k] = defining; |
| 1580 | defining = NULL; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1581 | free_tlist (origline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1582 | return 5; |
| 1583 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1584 | case PP_ROTATE: |
| 1585 | if (tline->next && tline->next->type == TOK_WHITESPACE) |
| 1586 | tline = tline->next; |
| 1587 | t = expand_smacro(tline->next); |
| 1588 | tline->next = NULL; |
| 1589 | free_tlist (origline); |
| 1590 | tline = t; |
| 1591 | tptr = &t; |
| 1592 | tokval.t_type = TOKEN_INVALID; |
| 1593 | evalresult = evaluate (ppscan, tptr, &tokval, NULL, pass, error, NULL); |
| 1594 | free_tlist (tline); |
| 1595 | if (!evalresult) |
| 1596 | return 3; |
| 1597 | if (tokval.t_type) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1598 | error(ERR_WARNING, |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1599 | "trailing garbage after expression ignored"); |
| 1600 | if (!is_simple(evalresult)) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1601 | error(ERR_NONFATAL, |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1602 | "non-constant value given to `%%rotate'"); |
| 1603 | return 3; |
| 1604 | } |
| 1605 | mmac = istk->mstk; |
| 1606 | while (mmac && !mmac->name) /* avoid mistaking %reps for macros */ |
| 1607 | mmac = mmac->next_active; |
| 1608 | if (!mmac) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1609 | error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call"); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1610 | mmac->rotate = mmac->rotate + reloc_value(evalresult); |
| 1611 | if (mmac->rotate < 0) |
| 1612 | mmac->rotate = mmac->nparam - (-mmac->rotate) % mmac->nparam; |
| 1613 | mmac->rotate %= mmac->nparam; |
| 1614 | return 1; |
| 1615 | |
| 1616 | case PP_REP: |
| 1617 | nolist = FALSE; |
| 1618 | tline = tline->next; |
| 1619 | if (tline->next && tline->next->type == TOK_WHITESPACE) |
| 1620 | tline = tline->next; |
| 1621 | if (tline->next && tline->next->type == TOK_ID && |
| 1622 | !nasm_stricmp(tline->next->text, ".nolist")) { |
| 1623 | tline = tline->next; |
| 1624 | nolist = TRUE; |
| 1625 | } |
| 1626 | t = expand_smacro(tline->next); |
| 1627 | tline->next = NULL; |
| 1628 | free_tlist (origline); |
| 1629 | tline = t; |
| 1630 | tptr = &t; |
| 1631 | tokval.t_type = TOKEN_INVALID; |
| 1632 | evalresult = evaluate (ppscan, tptr, &tokval, NULL, pass, error, NULL); |
| 1633 | free_tlist (tline); |
| 1634 | if (!evalresult) |
| 1635 | return 3; |
| 1636 | if (tokval.t_type) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1637 | error(ERR_WARNING, |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1638 | "trailing garbage after expression ignored"); |
| 1639 | if (!is_simple(evalresult)) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1640 | error(ERR_NONFATAL, |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1641 | "non-constant value given to `%%rep'"); |
| 1642 | return 3; |
| 1643 | } |
H. Peter Anvin | b64535f | 2002-04-30 20:55:37 +0000 | [diff] [blame] | 1644 | tmp_defining = defining; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1645 | defining = nasm_malloc(sizeof(MMacro)); |
| 1646 | defining->name = NULL; /* flags this macro as a %rep block */ |
| 1647 | defining->casesense = 0; |
| 1648 | defining->plus = FALSE; |
| 1649 | defining->nolist = nolist; |
| 1650 | defining->in_progress = reloc_value(evalresult) + 1; |
| 1651 | defining->nparam_min = defining->nparam_max = 0; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1652 | defining->defaults = NULL; |
| 1653 | defining->dlist = NULL; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1654 | defining->expansion = NULL; |
| 1655 | defining->next_active = istk->mstk; |
H. Peter Anvin | b64535f | 2002-04-30 20:55:37 +0000 | [diff] [blame] | 1656 | defining->rep_nest = tmp_defining; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1657 | return 1; |
| 1658 | |
| 1659 | case PP_ENDREP: |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1660 | if (!defining || defining->name) { |
| 1661 | error (ERR_NONFATAL, |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1662 | "`%%endrep': no matching `%%rep'"); |
| 1663 | return 3; |
| 1664 | } |
| 1665 | |
| 1666 | /* |
| 1667 | * Now we have a "macro" defined - although it has no name |
| 1668 | * and we won't be entering it in the hash tables - we must |
| 1669 | * push a macro-end marker for it on to istk->expansion. |
| 1670 | * After that, it will take care of propagating itself (a |
| 1671 | * macro-end marker line for a macro which is really a %rep |
| 1672 | * block will cause the macro to be re-expanded, complete |
| 1673 | * with another macro-end marker to ensure the process |
| 1674 | * continues) until the whole expansion is forcibly removed |
| 1675 | * from istk->expansion by a %exitrep. |
| 1676 | */ |
| 1677 | l = nasm_malloc(sizeof(Line)); |
| 1678 | l->next = istk->expansion; |
| 1679 | l->finishes = defining; |
| 1680 | l->first = NULL; |
| 1681 | istk->expansion = l; |
| 1682 | |
| 1683 | istk->mstk = defining; |
| 1684 | |
| 1685 | list->uplevel (defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); |
H. Peter Anvin | b64535f | 2002-04-30 20:55:37 +0000 | [diff] [blame] | 1686 | tmp_defining = defining; |
| 1687 | defining = defining->rep_nest; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1688 | free_tlist (origline); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1689 | return 1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1690 | |
| 1691 | case PP_EXITREP: |
| 1692 | /* |
| 1693 | * We must search along istk->expansion until we hit a |
| 1694 | * macro-end marker for a macro with no name. Then we set |
| 1695 | * its `in_progress' flag to 0. |
| 1696 | */ |
| 1697 | for (l = istk->expansion; l; l = l->next) |
| 1698 | if (l->finishes && !l->finishes->name) |
| 1699 | break; |
| 1700 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1701 | if (l) |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1702 | l->finishes->in_progress = 0; |
| 1703 | else |
| 1704 | error (ERR_NONFATAL, "`%%exitrep' not within `%%rep' block"); |
| 1705 | free_tlist (origline); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1706 | return 1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1707 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1708 | case PP_DEFINE: |
| 1709 | case PP_IDEFINE: |
| 1710 | tline = tline->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1711 | skip_white_(tline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1712 | if (!tline || (tline->type != TOK_ID && |
| 1713 | (tline->type != TOK_PREPROC_ID || |
| 1714 | tline->text[1] != '$'))) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1715 | error (ERR_NONFATAL, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1716 | "`%%%sdefine' expects a macro identifier", |
| 1717 | (i == PP_IDEFINE ? "i" : "")); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1718 | free_tlist (origline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1719 | return 3; |
| 1720 | } |
| 1721 | mname = tline->text; |
| 1722 | if (tline->type == TOK_ID) { |
| 1723 | p = tline->text; |
| 1724 | smhead = &smacros[hash(mname)]; |
| 1725 | } else { |
| 1726 | ctx = get_ctx (tline->text); |
| 1727 | if (ctx == NULL) |
| 1728 | return 3; |
| 1729 | else { |
| 1730 | p = tline->text+1; |
| 1731 | p += strspn(p, "$"); |
| 1732 | smhead = &ctx->localmac; |
| 1733 | } |
| 1734 | } |
| 1735 | last = tline; |
| 1736 | param_start = tline = tline->next; |
| 1737 | nparam = 0; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1738 | if (tok_is_(tline, "(")) { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1739 | /* |
| 1740 | * This macro has parameters. |
| 1741 | */ |
| 1742 | |
| 1743 | tline = tline->next; |
| 1744 | while (1) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1745 | skip_white_(tline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1746 | if (!tline) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1747 | error (ERR_NONFATAL, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1748 | "parameter identifier expected"); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1749 | free_tlist (origline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1750 | return 3; |
| 1751 | } |
| 1752 | if (tline->type != TOK_ID) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1753 | error (ERR_NONFATAL, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1754 | "`%s': parameter identifier expected", |
| 1755 | tline->text); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1756 | free_tlist (origline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1757 | return 3; |
| 1758 | } |
| 1759 | tline->type = TOK_SMAC_PARAM + nparam++; |
| 1760 | tline = tline->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1761 | skip_white_(tline); |
| 1762 | if (tok_is_(tline, ",")) { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1763 | tline = tline->next; |
| 1764 | continue; |
| 1765 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1766 | if (!tok_is_(tline, ")")) { |
| 1767 | error (ERR_NONFATAL, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1768 | "`)' expected to terminate macro template"); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1769 | free_tlist (origline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1770 | return 3; |
| 1771 | } |
| 1772 | break; |
| 1773 | } |
| 1774 | last = tline; |
| 1775 | tline = tline->next; |
| 1776 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1777 | if (tok_type_(tline, TOK_WHITESPACE)) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1778 | last = tline, tline = tline->next; |
| 1779 | macro_start = NULL; |
| 1780 | last->next = NULL; |
| 1781 | t = tline; |
| 1782 | while (t) { |
| 1783 | if (t->type == TOK_ID) { |
| 1784 | for (tt = param_start; tt; tt = tt->next) |
| 1785 | if (tt->type >= TOK_SMAC_PARAM && |
| 1786 | !strcmp(tt->text, t->text)) |
| 1787 | t->type = tt->type; |
| 1788 | } |
| 1789 | tt = t->next; |
| 1790 | t->next = macro_start; |
| 1791 | macro_start = t; |
| 1792 | t = tt; |
| 1793 | } |
| 1794 | /* |
| 1795 | * Good. We now have a macro name, a parameter count, and a |
| 1796 | * token list (in reverse order) for an expansion. We ought |
| 1797 | * to be OK just to create an SMacro, store it, and let |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1798 | * free_tlist have the rest of the line (which we have |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1799 | * carefully re-terminated after chopping off the expansion |
| 1800 | * from the end). |
| 1801 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1802 | if (smacro_defined (mname, nparam, &smac, i==PP_DEFINE)) { |
| 1803 | if (!smac) { |
| 1804 | error (ERR_WARNING, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1805 | "single-line macro `%s' defined both with and" |
| 1806 | " without parameters", mname); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1807 | free_tlist (origline); |
| 1808 | free_tlist (macro_start); |
| 1809 | return 3; |
| 1810 | } else { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1811 | /* |
| 1812 | * We're redefining, so we have to take over an |
| 1813 | * existing SMacro structure. This means freeing |
| 1814 | * what was already in it. |
| 1815 | */ |
| 1816 | nasm_free (smac->name); |
| 1817 | free_tlist (smac->expansion); |
| 1818 | } |
| 1819 | } else { |
| 1820 | smac = nasm_malloc(sizeof(SMacro)); |
| 1821 | smac->next = *smhead; |
| 1822 | *smhead = smac; |
| 1823 | } |
| 1824 | smac->name = nasm_strdup(p); |
| 1825 | smac->casesense = (i == PP_DEFINE); |
| 1826 | smac->nparam = nparam; |
| 1827 | smac->expansion = macro_start; |
| 1828 | smac->in_progress = FALSE; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1829 | free_tlist (origline); |
| 1830 | return 3; |
| 1831 | |
| 1832 | case PP_ASSIGN: |
| 1833 | case PP_IASSIGN: |
| 1834 | tline = tline->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1835 | skip_white_(tline); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1836 | if (!tline || (tline->type != TOK_ID && |
| 1837 | (tline->type != TOK_PREPROC_ID || |
| 1838 | tline->text[1] != '$'))) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1839 | error (ERR_NONFATAL, |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1840 | "`%%%sassign' expects a macro identifier", |
| 1841 | (i == PP_IASSIGN ? "i" : "")); |
| 1842 | free_tlist (origline); |
| 1843 | return 3; |
| 1844 | } |
| 1845 | mname = tline->text; |
| 1846 | if (tline->type == TOK_ID) { |
| 1847 | p = tline->text; |
| 1848 | smhead = &smacros[hash(mname)]; |
| 1849 | } else { |
| 1850 | ctx = get_ctx (tline->text); |
| 1851 | if (ctx == NULL) { |
| 1852 | free_tlist (origline); |
| 1853 | return 3; |
| 1854 | } else { |
| 1855 | p = tline->text+1; |
| 1856 | p += strspn(p, "$"); |
| 1857 | smhead = &ctx->localmac; |
| 1858 | } |
| 1859 | } |
| 1860 | last = tline; |
| 1861 | tline = tline->next; |
| 1862 | last->next = NULL; |
| 1863 | |
| 1864 | tline = expand_smacro (tline); |
| 1865 | t = tline; |
| 1866 | tptr = &t; |
| 1867 | tokval.t_type = TOKEN_INVALID; |
| 1868 | evalresult = evaluate (ppscan, tptr, &tokval, NULL, pass, error, NULL); |
| 1869 | free_tlist (tline); |
| 1870 | if (!evalresult) { |
| 1871 | free_tlist (origline); |
| 1872 | return 3; |
| 1873 | } |
| 1874 | |
| 1875 | if (tokval.t_type) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1876 | error(ERR_WARNING, |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1877 | "trailing garbage after expression ignored"); |
| 1878 | |
| 1879 | if (!is_simple(evalresult)) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1880 | error(ERR_NONFATAL, |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1881 | "non-constant value given to `%%%sassign'", |
| 1882 | (i == PP_IASSIGN ? "i" : "")); |
| 1883 | free_tlist (origline); |
| 1884 | return 3; |
| 1885 | } |
| 1886 | |
| 1887 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 1888 | macro_start->next = NULL; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1889 | make_tok_num(macro_start, reloc_value(evalresult)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1890 | macro_start->mac = NULL; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1891 | |
| 1892 | /* |
| 1893 | * We now have a macro name, an implicit parameter count of |
| 1894 | * zero, and a numeric token to use as an expansion. Create |
| 1895 | * and store an SMacro. |
| 1896 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1897 | if (smacro_defined (mname, 0, &smac, i==PP_ASSIGN)) { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1898 | if (!smac) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1899 | error (ERR_WARNING, |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1900 | "single-line macro `%s' defined both with and" |
| 1901 | " without parameters", mname); |
| 1902 | else { |
| 1903 | /* |
| 1904 | * We're redefining, so we have to take over an |
| 1905 | * existing SMacro structure. This means freeing |
| 1906 | * what was already in it. |
| 1907 | */ |
| 1908 | nasm_free (smac->name); |
| 1909 | free_tlist (smac->expansion); |
| 1910 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1911 | } |
| 1912 | else { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1913 | smac = nasm_malloc(sizeof(SMacro)); |
| 1914 | smac->next = *smhead; |
| 1915 | *smhead = smac; |
| 1916 | } |
| 1917 | smac->name = nasm_strdup(p); |
| 1918 | smac->casesense = (i == PP_ASSIGN); |
| 1919 | smac->nparam = 0; |
| 1920 | smac->expansion = macro_start; |
| 1921 | smac->in_progress = FALSE; |
| 1922 | free_tlist (origline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1923 | return 3; |
| 1924 | |
| 1925 | case PP_LINE: |
| 1926 | /* |
| 1927 | * Syntax is `%line nnn[+mmm] [filename]' |
| 1928 | */ |
| 1929 | tline = tline->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1930 | skip_white_(tline); |
| 1931 | if (!tok_type_(tline, TOK_NUMBER)) { |
| 1932 | error (ERR_NONFATAL, "`%%line' expects line number"); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1933 | free_tlist (origline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1934 | return 3; |
| 1935 | } |
| 1936 | k = readnum(tline->text, &j); |
| 1937 | m = 1; |
| 1938 | tline = tline->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1939 | if (tok_is_(tline, "+")) { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1940 | tline = tline->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1941 | if (!tok_type_(tline, TOK_NUMBER)) { |
| 1942 | error (ERR_NONFATAL, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1943 | "`%%line' expects line increment"); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1944 | free_tlist (origline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1945 | return 3; |
| 1946 | } |
| 1947 | m = readnum(tline->text, &j); |
| 1948 | tline = tline->next; |
| 1949 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1950 | skip_white_(tline); |
| 1951 | src_set_linnum(k); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1952 | istk->lineinc = m; |
| 1953 | if (tline) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1954 | nasm_free ( src_set_fname ( detoken(tline) ) ); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1955 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1956 | free_tlist (origline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1957 | return 5; |
| 1958 | |
| 1959 | default: |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1960 | error(ERR_FATAL, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1961 | "preprocessor directive `%s' not yet implemented", |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1962 | directives[i]); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1963 | break; |
| 1964 | } |
| 1965 | return 3; |
| 1966 | } |
| 1967 | |
| 1968 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1969 | * Ensure that a macro parameter contains a condition code and |
| 1970 | * nothing else. Return the condition code index if so, or -1 |
| 1971 | * otherwise. |
| 1972 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1973 | static int find_cc (Token *t) |
| 1974 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1975 | Token *tt; |
| 1976 | int i, j, k, m; |
| 1977 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1978 | skip_white_(t); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1979 | if (t->type != TOK_ID) |
| 1980 | return -1; |
| 1981 | tt = t->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1982 | skip_white_(tt); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1983 | if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ","))) |
| 1984 | return -1; |
| 1985 | |
| 1986 | i = -1; |
| 1987 | j = sizeof(conditions)/sizeof(*conditions); |
| 1988 | while (j-i > 1) { |
| 1989 | k = (j+i) / 2; |
| 1990 | m = nasm_stricmp(t->text, conditions[k]); |
| 1991 | if (m == 0) { |
| 1992 | i = k; |
| 1993 | j = -2; |
| 1994 | break; |
| 1995 | } else if (m < 0) { |
| 1996 | j = k; |
| 1997 | } else |
| 1998 | i = k; |
| 1999 | } |
| 2000 | if (j != -2) |
| 2001 | return -1; |
| 2002 | return i; |
| 2003 | } |
| 2004 | |
| 2005 | /* |
| 2006 | * Expand MMacro-local things: parameter references (%0, %n, %+n, |
| 2007 | * %-n) and MMacro-local identifiers (%%foo). |
| 2008 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2009 | static Token *expand_mmac_params (Token *tline) |
| 2010 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2011 | Token *t, *tt, *ttt, **tail, *thead; |
| 2012 | |
| 2013 | tail = &thead; |
| 2014 | thead = NULL; |
| 2015 | |
| 2016 | while (tline) { |
| 2017 | if (tline->type == TOK_PREPROC_ID && |
| 2018 | (tline->text[1] == '+' || tline->text[1] == '-' || |
| 2019 | tline->text[1] == '%' || |
| 2020 | (tline->text[1] >= '0' && tline->text[1] <= '9'))) { |
| 2021 | char *text = NULL; |
| 2022 | int type = 0, cc; /* type = 0 to placate optimisers */ |
| 2023 | char tmpbuf[30]; |
| 2024 | int n, i; |
| 2025 | MMacro *mac; |
| 2026 | |
| 2027 | t = tline; |
| 2028 | tline = tline->next; |
| 2029 | |
| 2030 | mac = istk->mstk; |
| 2031 | while (mac && !mac->name) /* avoid mistaking %reps for macros */ |
| 2032 | mac = mac->next_active; |
| 2033 | if (!mac) |
| 2034 | error(ERR_NONFATAL, "`%s': not in a macro call", t->text); |
| 2035 | else switch (t->text[1]) { |
| 2036 | /* |
| 2037 | * We have to make a substitution of one of the |
| 2038 | * forms %1, %-1, %+1, %%foo, %0. |
| 2039 | */ |
| 2040 | case '0': |
| 2041 | type = TOK_NUMBER; |
| 2042 | sprintf(tmpbuf, "%d", mac->nparam); |
| 2043 | text = nasm_strdup(tmpbuf); |
| 2044 | break; |
| 2045 | case '%': |
| 2046 | type = TOK_ID; |
| 2047 | sprintf(tmpbuf, "..@%lu.", mac->unique); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2048 | text = nasm_strcat(tmpbuf, t->text+2); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2049 | break; |
| 2050 | case '-': |
| 2051 | n = atoi(t->text+2)-1; |
| 2052 | if (n >= mac->nparam) |
| 2053 | tt = NULL; |
| 2054 | else { |
| 2055 | if (mac->nparam > 1) |
| 2056 | n = (n + mac->rotate) % mac->nparam; |
| 2057 | tt = mac->params[n]; |
| 2058 | } |
| 2059 | cc = find_cc (tt); |
| 2060 | if (cc == -1) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2061 | error (ERR_NONFATAL, |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2062 | "macro parameter %d is not a condition code", |
| 2063 | n+1); |
| 2064 | text = NULL; |
| 2065 | } else { |
| 2066 | type = TOK_ID; |
| 2067 | if (inverse_ccs[cc] == -1) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2068 | error (ERR_NONFATAL, |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2069 | "condition code `%s' is not invertible", |
| 2070 | conditions[cc]); |
| 2071 | text = NULL; |
| 2072 | } else |
| 2073 | text = nasm_strdup(conditions[inverse_ccs[cc]]); |
| 2074 | } |
| 2075 | break; |
| 2076 | case '+': |
| 2077 | n = atoi(t->text+2)-1; |
| 2078 | if (n >= mac->nparam) |
| 2079 | tt = NULL; |
| 2080 | else { |
| 2081 | if (mac->nparam > 1) |
| 2082 | n = (n + mac->rotate) % mac->nparam; |
| 2083 | tt = mac->params[n]; |
| 2084 | } |
| 2085 | cc = find_cc (tt); |
| 2086 | if (cc == -1) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2087 | error (ERR_NONFATAL, |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2088 | "macro parameter %d is not a condition code", |
| 2089 | n+1); |
| 2090 | text = NULL; |
| 2091 | } else { |
| 2092 | type = TOK_ID; |
| 2093 | text = nasm_strdup(conditions[cc]); |
| 2094 | } |
| 2095 | break; |
| 2096 | default: |
| 2097 | n = atoi(t->text+1)-1; |
| 2098 | if (n >= mac->nparam) |
| 2099 | tt = NULL; |
| 2100 | else { |
| 2101 | if (mac->nparam > 1) |
| 2102 | n = (n + mac->rotate) % mac->nparam; |
| 2103 | tt = mac->params[n]; |
| 2104 | } |
| 2105 | if (tt) { |
| 2106 | for (i=0; i<mac->paramlen[n]; i++) { |
| 2107 | ttt = *tail = nasm_malloc(sizeof(Token)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2108 | tail = &ttt->next; |
| 2109 | ttt->type = tt->type; |
| 2110 | ttt->text = nasm_strdup(tt->text); |
| 2111 | ttt->mac = NULL; |
| 2112 | tt = tt->next; |
| 2113 | } |
| 2114 | } |
| 2115 | text = NULL; /* we've done it here */ |
| 2116 | break; |
| 2117 | } |
| 2118 | nasm_free (t->text); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2119 | if (!text) { |
| 2120 | nasm_free (t); |
| 2121 | } else { |
| 2122 | *tail = t; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2123 | tail = &t->next; |
| 2124 | t->type = type; |
| 2125 | t->text = text; |
| 2126 | t->mac = NULL; |
| 2127 | } |
| 2128 | continue; |
| 2129 | } else { |
| 2130 | t = *tail = tline; |
| 2131 | tline = tline->next; |
| 2132 | t->mac = NULL; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2133 | tail = &t->next; |
| 2134 | } |
| 2135 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2136 | *tail = NULL; |
| 2137 | t = thead; |
| 2138 | for (; t && (tt=t->next)!=NULL ; t = t->next) |
| 2139 | switch (t->type) { |
| 2140 | case TOK_WHITESPACE: |
| 2141 | if (tt->type == TOK_WHITESPACE) { |
| 2142 | t->next = tt->next; |
| 2143 | nasm_free(tt->text); |
| 2144 | nasm_free(tt); |
| 2145 | } |
| 2146 | break; |
| 2147 | case TOK_ID: |
| 2148 | if (tt->type == TOK_ID || tt->type == TOK_NUMBER) { |
| 2149 | char *tmp = nasm_strcat(t->text, tt->text); |
| 2150 | nasm_free(t->text); |
| 2151 | t->text = tmp; |
| 2152 | t->next = tt->next; |
| 2153 | nasm_free(tt->text); |
| 2154 | nasm_free(tt); |
| 2155 | } |
| 2156 | break; |
| 2157 | case TOK_NUMBER: |
| 2158 | if (tt->type == TOK_NUMBER) { |
| 2159 | char *tmp = nasm_strcat(t->text, tt->text); |
| 2160 | nasm_free(t->text); |
| 2161 | t->text = tmp; |
| 2162 | t->next = tt->next; |
| 2163 | nasm_free(tt->text); |
| 2164 | nasm_free(tt); |
| 2165 | } |
| 2166 | break; |
| 2167 | } |
| 2168 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2169 | return thead; |
| 2170 | } |
| 2171 | |
| 2172 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2173 | * Expand all single-line macro calls made in the given line. |
| 2174 | * Return the expanded version of the line. The original is deemed |
| 2175 | * to be destroyed in the process. (In reality we'll just move |
| 2176 | * Tokens from input to output a lot of the time, rather than |
| 2177 | * actually bothering to destroy and replicate.) |
| 2178 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2179 | static Token *expand_smacro (Token *tline) |
| 2180 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2181 | Token *t, *tt, *mstart, **tail, *thead; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2182 | SMacro *head = NULL, *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2183 | Token **params; |
| 2184 | int *paramsize; |
| 2185 | int nparam, sparam, brackets; |
| 2186 | char *p; |
| 2187 | |
| 2188 | tail = &thead; |
| 2189 | thead = NULL; |
| 2190 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2191 | while (tline) { /* main token loop */ |
| 2192 | p = NULL; |
| 2193 | if (tline->type == TOK_ID) { |
| 2194 | head = smacros[hash(tline->text)]; |
| 2195 | p = tline->text; |
| 2196 | } else if (tline->type == TOK_PREPROC_ID && tline->text[1] == '$') { |
| 2197 | Context *ctx = get_ctx (tline->text); |
| 2198 | if (ctx) { |
| 2199 | head = ctx->localmac; |
| 2200 | p = tline->text+2; |
| 2201 | p += strspn(p, "$"); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2202 | } |
| 2203 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2204 | if (p) { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2205 | /* |
| 2206 | * We've hit an identifier. As in is_mmacro below, we first |
| 2207 | * check whether the identifier is a single-line macro at |
| 2208 | * all, then think about checking for parameters if |
| 2209 | * necessary. |
| 2210 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2211 | for (m = head; m; m = m->next) |
| 2212 | if (!mstrcmp(m->name, p, m->casesense)) |
| 2213 | break; |
| 2214 | if (m) { |
| 2215 | mstart = tline; |
| 2216 | params = NULL; |
| 2217 | paramsize = NULL; |
| 2218 | if (m->nparam == 0) { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2219 | /* |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2220 | * Simple case: the macro is parameterless. Discard the |
| 2221 | * one token that the macro call took, and push the |
| 2222 | * expansion back on the to-do stack. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2223 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2224 | if (!m->expansion) |
| 2225 | { |
| 2226 | if (!strcmp("__FILE__", m->name)) { |
| 2227 | long num=0; |
| 2228 | src_get(&num, &(tline->text)); |
| 2229 | nasm_quote(&(tline->text)); |
| 2230 | tline->type = TOK_STRING; |
| 2231 | continue; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2232 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2233 | if (!strcmp("__LINE__", m->name)) { |
| 2234 | nasm_free(tline->text); |
| 2235 | make_tok_num(tline, src_get_linnum()); |
| 2236 | continue; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2237 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2238 | t = tline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2239 | tline = tline->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2240 | nasm_free (t->text); |
| 2241 | nasm_free (t); |
| 2242 | continue; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2243 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2244 | } |
| 2245 | else { |
| 2246 | /* |
| 2247 | * Complicated case: at least one macro with this name |
| 2248 | * exists and takes parameters. We must find the |
| 2249 | * parameters in the call, count them, find the SMacro |
| 2250 | * that corresponds to that form of the macro call, and |
| 2251 | * substitute for the parameters when we expand. What a |
| 2252 | * pain. |
| 2253 | */ |
| 2254 | tline = tline->next; |
| 2255 | skip_white_(tline); |
| 2256 | if (!tok_is_(tline, "(")) { |
| 2257 | /* |
| 2258 | * This macro wasn't called with parameters: ignore |
| 2259 | * the call. (Behaviour borrowed from gnu cpp.) |
| 2260 | */ |
| 2261 | tline = mstart; |
| 2262 | m = NULL; |
| 2263 | } |
| 2264 | else { |
| 2265 | int paren = 0; |
| 2266 | int white = 0; |
| 2267 | brackets = 0; |
| 2268 | nparam = 0; |
| 2269 | tline = tline->next; |
| 2270 | sparam = PARAM_DELTA; |
| 2271 | params = nasm_malloc (sparam*sizeof(Token *)); |
| 2272 | params[0] = tline; |
| 2273 | paramsize = nasm_malloc (sparam*sizeof(int)); |
| 2274 | paramsize[0] = 0; |
| 2275 | for (;;tline = tline->next) { /* parameter loop */ |
| 2276 | if (!tline) { |
| 2277 | error(ERR_NONFATAL, |
| 2278 | "macro call expects terminating `)'"); |
| 2279 | break; |
| 2280 | } |
| 2281 | if (tline->type == TOK_WHITESPACE && brackets<=0) { |
| 2282 | if (paramsize[nparam]) |
| 2283 | white++; |
| 2284 | else |
| 2285 | params[nparam] = tline->next; |
| 2286 | continue; /* parameter loop */ |
| 2287 | } |
| 2288 | if (tline->type == TOK_OTHER && tline->text[1]==0) { |
| 2289 | char ch = tline->text[0]; |
| 2290 | if (ch == ',' && !paren && brackets<=0) { |
| 2291 | if (++nparam >= sparam) { |
| 2292 | sparam += PARAM_DELTA; |
| 2293 | params = nasm_realloc (params, |
| 2294 | sparam*sizeof(Token *)); |
| 2295 | paramsize = nasm_realloc (paramsize, |
| 2296 | sparam*sizeof(int)); |
| 2297 | } |
| 2298 | params[nparam] = tline->next; |
| 2299 | paramsize[nparam] = 0; |
| 2300 | white = 0; |
| 2301 | continue; /* parameter loop */ |
| 2302 | } |
H. Peter Anvin | 4836e33 | 2002-04-30 20:56:43 +0000 | [diff] [blame^] | 2303 | if (ch == '{' && |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2304 | (brackets>0 || (brackets==0 && |
| 2305 | !paramsize[nparam]))) |
| 2306 | { |
| 2307 | if (!(brackets++)) |
| 2308 | { |
| 2309 | params[nparam] = tline->next; |
| 2310 | continue; /* parameter loop */ |
| 2311 | } |
| 2312 | } |
H. Peter Anvin | 4836e33 | 2002-04-30 20:56:43 +0000 | [diff] [blame^] | 2313 | if (ch == '}' && brackets>0) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2314 | if (--brackets == 0) { |
| 2315 | brackets = -1; |
| 2316 | continue; /* parameter loop */ |
| 2317 | } |
| 2318 | if (ch == '(' && !brackets) |
| 2319 | paren++; |
| 2320 | if (ch == ')' && brackets<=0) |
| 2321 | if (--paren < 0) |
| 2322 | break; |
| 2323 | } |
| 2324 | if (brackets<0) { |
| 2325 | brackets = 0; |
| 2326 | error (ERR_NONFATAL, "braces do not " |
| 2327 | "enclose all of macro parameter"); |
| 2328 | } |
| 2329 | paramsize[nparam] += white+1; |
| 2330 | white = 0; |
| 2331 | } /* parameter loop */ |
| 2332 | nparam++; |
| 2333 | while (m && (m->nparam != nparam || |
| 2334 | mstrcmp(m->name, p, m->casesense))) |
| 2335 | m = m->next; |
| 2336 | if (!m) |
| 2337 | error (ERR_WARNING|ERR_WARN_MNP, |
| 2338 | "macro `%s' exists, " |
| 2339 | "but not taking %d parameters", |
| 2340 | mstart->text, nparam); |
| 2341 | } |
| 2342 | } |
| 2343 | if (m && m->in_progress) |
| 2344 | m = NULL; |
| 2345 | if (!m) /* in progess or didn't find '(' or wrong nparam */ |
| 2346 | { |
| 2347 | /* |
| 2348 | * Design question: should we handle !tline, which |
| 2349 | * indicates missing ')' here, or expand those |
| 2350 | * macros anyway, which requires the (t) test a few |
| 2351 | * lines down? |
| 2352 | */ |
| 2353 | nasm_free (params); |
| 2354 | nasm_free (paramsize); |
| 2355 | tline = mstart; |
| 2356 | } |
| 2357 | else { |
| 2358 | /* |
| 2359 | * Expand the macro: we are placed on the last token of the |
| 2360 | * call, so that we can easily split the call from the |
| 2361 | * following tokens. We also start by pushing an SMAC_END |
| 2362 | * token for the cycle removal. |
| 2363 | */ |
| 2364 | t = tline; |
| 2365 | if (t) { |
| 2366 | tline = t->next; |
| 2367 | t->next = NULL; |
| 2368 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2369 | tt = nasm_malloc(sizeof(Token)); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2370 | tt->type = TOK_SMAC_END; |
| 2371 | tt->text = NULL; |
| 2372 | tt->mac = m; |
| 2373 | m->in_progress = TRUE; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2374 | tt->next = tline; |
| 2375 | tline = tt; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2376 | for (t = m->expansion; t; t = t->next) { |
| 2377 | if (t->type >= TOK_SMAC_PARAM) { |
| 2378 | Token *pcopy = tline, **ptail = &pcopy; |
| 2379 | Token *ttt, *pt; |
| 2380 | int i; |
| 2381 | |
| 2382 | ttt = params[t->type - TOK_SMAC_PARAM]; |
| 2383 | for (i=paramsize[t->type-TOK_SMAC_PARAM]; --i>=0;) { |
| 2384 | pt = *ptail = nasm_malloc(sizeof(Token)); |
| 2385 | pt->next = tline; |
| 2386 | ptail = &pt->next; |
| 2387 | pt->text = nasm_strdup(ttt->text); |
| 2388 | pt->type = ttt->type; |
| 2389 | pt->mac = NULL; |
| 2390 | ttt = ttt->next; |
| 2391 | } |
| 2392 | tline = pcopy; |
| 2393 | } else { |
| 2394 | tt = nasm_malloc(sizeof(Token)); |
| 2395 | tt->type = t->type; |
| 2396 | tt->text = nasm_strdup(t->text); |
| 2397 | tt->mac = NULL; |
| 2398 | tt->next = tline; |
| 2399 | tline = tt; |
| 2400 | } |
| 2401 | } |
| 2402 | |
| 2403 | /* |
| 2404 | * Having done that, get rid of the macro call, and clean |
| 2405 | * up the parameters. |
| 2406 | */ |
| 2407 | nasm_free (params); |
| 2408 | nasm_free (paramsize); |
| 2409 | free_tlist (mstart); |
| 2410 | continue; /* main token loop */ |
| 2411 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2412 | } |
| 2413 | } |
| 2414 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2415 | if (tline->type == TOK_SMAC_END) { |
| 2416 | tline->mac->in_progress = FALSE; |
| 2417 | t = tline; |
| 2418 | tline = tline->next; |
| 2419 | nasm_free (t); |
| 2420 | } else { |
| 2421 | t = *tail = tline; |
| 2422 | tline = tline->next; |
| 2423 | t->mac = NULL; |
| 2424 | t->next = NULL; |
| 2425 | tail = &t->next; |
| 2426 | if (t->type == TOK_PREPROC_ID && t->text[1] == '$') { |
| 2427 | Context *c = get_ctx (t->text); |
| 2428 | char *p, *q, buffer[40]; |
| 2429 | |
| 2430 | t->type = TOK_ID; |
| 2431 | if (c) { |
| 2432 | q = t->text+1; |
| 2433 | q += strspn(q, "$"); |
| 2434 | sprintf(buffer, "..@%lu.", c->number); |
| 2435 | p = nasm_strcat (buffer,q); |
| 2436 | nasm_free (t->text); |
| 2437 | t->text = p; |
| 2438 | } |
| 2439 | } |
| 2440 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2441 | } |
| 2442 | |
| 2443 | return thead; |
| 2444 | } |
| 2445 | |
| 2446 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2447 | * Determine whether the given line constitutes a multi-line macro |
| 2448 | * call, and return the MMacro structure called if so. Doesn't have |
| 2449 | * to check for an initial label - that's taken care of in |
| 2450 | * expand_mmacro - but must check numbers of parameters. Guaranteed |
| 2451 | * to be called with tline->type == TOK_ID, so the putative macro |
| 2452 | * name is easy to find. |
| 2453 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2454 | static MMacro *is_mmacro (Token *tline, Token ***params_array) |
| 2455 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2456 | MMacro *head, *m; |
| 2457 | Token **params; |
| 2458 | int nparam; |
| 2459 | |
| 2460 | head = mmacros[hash(tline->text)]; |
| 2461 | |
| 2462 | /* |
| 2463 | * Efficiency: first we see if any macro exists with the given |
| 2464 | * name. If not, we can return NULL immediately. _Then_ we |
| 2465 | * count the parameters, and then we look further along the |
| 2466 | * list if necessary to find the proper MMacro. |
| 2467 | */ |
| 2468 | for (m = head; m; m = m->next) |
| 2469 | if (!mstrcmp(m->name, tline->text, m->casesense)) |
| 2470 | break; |
| 2471 | if (!m) |
| 2472 | return NULL; |
| 2473 | |
| 2474 | /* |
| 2475 | * OK, we have a potential macro. Count and demarcate the |
| 2476 | * parameters. |
| 2477 | */ |
| 2478 | count_mmac_params (tline->next, &nparam, ¶ms); |
| 2479 | |
| 2480 | /* |
| 2481 | * So we know how many parameters we've got. Find the MMacro |
| 2482 | * structure that handles this number. |
| 2483 | */ |
| 2484 | while (m) { |
| 2485 | if (m->nparam_min <= nparam && (m->plus || nparam <= m->nparam_max)) { |
| 2486 | /* |
| 2487 | * This one is right. Just check if cycle removal |
| 2488 | * prohibits us using it before we actually celebrate... |
| 2489 | */ |
| 2490 | if (m->in_progress) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2491 | #if 0 |
| 2492 | error (ERR_NONFATAL, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2493 | "self-reference in multi-line macro `%s'", |
| 2494 | m->name); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2495 | #endif |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2496 | nasm_free (params); |
| 2497 | return NULL; |
| 2498 | } |
| 2499 | /* |
| 2500 | * It's right, and we can use it. Add its default |
| 2501 | * parameters to the end of our list if necessary. |
| 2502 | */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2503 | if (m->defaults && nparam < m->nparam_min + m->ndefs) { |
| 2504 | params = nasm_realloc (params, ((m->nparam_min+m->ndefs+1) * |
| 2505 | sizeof(*params))); |
| 2506 | while (nparam < m->nparam_min + m->ndefs) { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2507 | params[nparam] = m->defaults[nparam - m->nparam_min]; |
| 2508 | nparam++; |
| 2509 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2510 | } |
| 2511 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2512 | * If we've gone over the maximum parameter count (and |
| 2513 | * we're in Plus mode), ignore parameters beyond |
| 2514 | * nparam_max. |
| 2515 | */ |
| 2516 | if (m->plus && nparam > m->nparam_max) |
| 2517 | nparam = m->nparam_max; |
| 2518 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2519 | * Then terminate the parameter list, and leave. |
| 2520 | */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2521 | if (!params) { /* need this special case */ |
| 2522 | params = nasm_malloc(sizeof(*params)); |
| 2523 | nparam = 0; |
| 2524 | } |
| 2525 | params[nparam] = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2526 | *params_array = params; |
| 2527 | return m; |
| 2528 | } |
| 2529 | /* |
| 2530 | * This one wasn't right: look for the next one with the |
| 2531 | * same name. |
| 2532 | */ |
| 2533 | for (m = m->next; m; m = m->next) |
| 2534 | if (!mstrcmp(m->name, tline->text, m->casesense)) |
| 2535 | break; |
| 2536 | } |
| 2537 | |
| 2538 | /* |
| 2539 | * After all that, we didn't find one with the right number of |
| 2540 | * parameters. Issue a warning, and fail to expand the macro. |
| 2541 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2542 | error (ERR_WARNING|ERR_WARN_MNP, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2543 | "macro `%s' exists, but not taking %d parameters", |
| 2544 | tline->text, nparam); |
| 2545 | nasm_free (params); |
| 2546 | return NULL; |
| 2547 | } |
| 2548 | |
| 2549 | /* |
| 2550 | * Expand the multi-line macro call made by the given line, if |
| 2551 | * there is one to be expanded. If there is, push the expansion on |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2552 | * istk->expansion and return 1. Otherwise return 0. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2553 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2554 | static int expand_mmacro (Token *tline) |
| 2555 | { |
| 2556 | Token *startline = tline; |
| 2557 | Token *label = NULL; |
| 2558 | int dont_prepend = 0; |
| 2559 | Token **params, *t, *tt; |
| 2560 | MMacro *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2561 | Line *l, *ll; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2562 | int i, nparam, *paramlen; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2563 | |
| 2564 | t = tline; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2565 | skip_white_(t); |
| 2566 | if (!tok_type_(t, TOK_ID)) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2567 | return 0; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2568 | m = is_mmacro (t, ¶ms); |
| 2569 | if (!m) { |
| 2570 | Token *last; |
| 2571 | /* |
| 2572 | * We have an id which isn't a macro call. We'll assume |
| 2573 | * it might be a label; we'll also check to see if a |
| 2574 | * colon follows it. Then, if there's another id after |
| 2575 | * that lot, we'll check it again for macro-hood. |
| 2576 | */ |
| 2577 | label = last = t; |
| 2578 | t = t->next; |
| 2579 | if (tok_type_(t, TOK_WHITESPACE)) |
| 2580 | last = t, t = t->next; |
| 2581 | if (tok_is_(t, ":")) { |
| 2582 | dont_prepend = 1; |
| 2583 | last = t, t = t->next; |
| 2584 | if (tok_type_(t, TOK_WHITESPACE)) |
| 2585 | last = t, t = t->next; |
| 2586 | } |
| 2587 | if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, ¶ms)) == NULL) |
| 2588 | return 0; |
| 2589 | last->next = NULL; |
| 2590 | tline = t; |
| 2591 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2592 | |
| 2593 | /* |
| 2594 | * Fix up the parameters: this involves stripping leading and |
| 2595 | * trailing whitespace, then stripping braces if they are |
| 2596 | * present. |
| 2597 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2598 | for (nparam = 0; params[nparam]; nparam++) |
| 2599 | ; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2600 | paramlen = nparam ? nasm_malloc(nparam*sizeof(*paramlen)) : NULL; |
| 2601 | |
| 2602 | for (i = 0; params[i]; i++) { |
| 2603 | int brace = FALSE; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 2604 | int comma = (!m->plus || i < nparam-1); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2605 | |
| 2606 | t = params[i]; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2607 | skip_white_(t); |
| 2608 | if (tok_is_(t, "{")) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2609 | t = t->next, brace = TRUE, comma = FALSE; |
| 2610 | params[i] = t; |
| 2611 | paramlen[i] = 0; |
| 2612 | while (t) { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2613 | if (comma && t->type == TOK_OTHER && !strcmp(t->text, ",")) |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 2614 | break; /* ... because we have hit a comma */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2615 | if (comma && t->type == TOK_WHITESPACE && tok_is_(t->next, ",")) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2616 | break; /* ... or a space then a comma */ |
| 2617 | if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}")) |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 2618 | break; /* ... or a brace */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2619 | t = t->next; |
| 2620 | paramlen[i]++; |
| 2621 | } |
| 2622 | } |
| 2623 | |
| 2624 | /* |
| 2625 | * OK, we have a MMacro structure together with a set of |
| 2626 | * parameters. We must now go through the expansion and push |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2627 | * copies of each Line on to istk->expansion. Substitution of |
| 2628 | * parameter tokens and macro-local tokens doesn't get done |
| 2629 | * until the single-line macro substitution process; this is |
| 2630 | * because delaying them allows us to change the semantics |
| 2631 | * later through %rotate. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2632 | * |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2633 | * First, push an end marker on to istk->expansion, mark this |
| 2634 | * macro as in progress, and set up its invocation-specific |
| 2635 | * variables. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2636 | */ |
| 2637 | ll = nasm_malloc(sizeof(Line)); |
| 2638 | ll->next = istk->expansion; |
| 2639 | ll->finishes = m; |
| 2640 | ll->first = NULL; |
| 2641 | istk->expansion = ll; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2642 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2643 | m->in_progress = TRUE; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2644 | m->params = params; |
| 2645 | m->iline = tline; |
| 2646 | m->nparam = nparam; |
| 2647 | m->rotate = 0; |
| 2648 | m->paramlen = paramlen; |
| 2649 | m->unique = unique++; |
| 2650 | |
| 2651 | m->next_active = istk->mstk; |
| 2652 | istk->mstk = m; |
| 2653 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2654 | for (l = m->expansion; l; l = l->next) { |
| 2655 | Token **tail; |
| 2656 | |
| 2657 | ll = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2658 | ll->finishes = NULL; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2659 | ll->next = istk->expansion; |
| 2660 | istk->expansion = ll; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2661 | tail = &ll->first; |
| 2662 | |
| 2663 | for (t = l->first; t; t = t->next) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2664 | Token *x = t; |
| 2665 | if (t->type == TOK_PREPROC_ID && |
| 2666 | t->text[1]=='0' && t->text[2]=='0') |
| 2667 | { |
| 2668 | dont_prepend = -1; |
| 2669 | x = label; |
| 2670 | if (!x) |
| 2671 | continue; |
| 2672 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2673 | tt = *tail = nasm_malloc(sizeof(Token)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2674 | tail = &tt->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2675 | tt->type = x->type; |
| 2676 | tt->text = nasm_strdup(x->text); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2677 | tt->mac = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2678 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2679 | *tail = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2680 | } |
| 2681 | |
| 2682 | /* |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2683 | * If we had a label, push it on as the first line of |
| 2684 | * the macro expansion. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2685 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2686 | if (label) |
| 2687 | if (dont_prepend<0) |
| 2688 | free_tlist(startline); |
| 2689 | else { |
| 2690 | ll = nasm_malloc(sizeof(Line)); |
| 2691 | ll->finishes = NULL; |
| 2692 | ll->next = istk->expansion; |
| 2693 | istk->expansion = ll; |
| 2694 | ll->first = startline; |
| 2695 | if (!dont_prepend) { |
| 2696 | while (label->next) |
| 2697 | label = label->next; |
| 2698 | label->next = tt = nasm_malloc(sizeof(Token)); |
| 2699 | tt->next = NULL; |
| 2700 | tt->mac = NULL; |
| 2701 | tt->type = TOK_OTHER; |
| 2702 | tt->text = nasm_strdup(":"); |
| 2703 | } |
H. Peter Anvin | 87bc619 | 2002-04-30 20:53:16 +0000 | [diff] [blame] | 2704 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2705 | |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 2706 | list->uplevel (m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); |
| 2707 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2708 | return 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2709 | } |
| 2710 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2711 | static void pp_reset (char *file, int apass, efunc errfunc, evalfunc eval, |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2712 | ListGen *listgen) |
| 2713 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2714 | int h; |
| 2715 | |
| 2716 | error = errfunc; |
| 2717 | cstk = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2718 | istk = nasm_malloc(sizeof(Include)); |
| 2719 | istk->next = NULL; |
| 2720 | istk->conds = NULL; |
| 2721 | istk->expansion = NULL; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2722 | istk->mstk = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2723 | istk->fp = fopen(file, "r"); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2724 | istk->fname = NULL; |
| 2725 | src_set_fname(nasm_strdup(file)); |
| 2726 | src_set_linnum(0); |
| 2727 | istk->lineinc = 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2728 | if (!istk->fp) |
| 2729 | error (ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'", file); |
| 2730 | defining = NULL; |
| 2731 | for (h=0; h<NHASH; h++) { |
| 2732 | mmacros[h] = NULL; |
| 2733 | smacros[h] = NULL; |
| 2734 | } |
| 2735 | unique = 0; |
| 2736 | stdmacpos = stdmac; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2737 | any_extrastdmac = (extrastdmac != NULL); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 2738 | list = listgen; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2739 | evaluate = eval; |
| 2740 | pass = apass; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2741 | } |
| 2742 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2743 | static char *pp_getline (void) |
| 2744 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2745 | char *line; |
| 2746 | Token *tline; |
| 2747 | int ret; |
| 2748 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2749 | while (1) { |
| 2750 | /* |
| 2751 | * Fetch a tokenised line, either from the macro-expansion |
| 2752 | * buffer or from the input file. |
| 2753 | */ |
| 2754 | tline = NULL; |
| 2755 | while (istk->expansion && istk->expansion->finishes) { |
| 2756 | Line *l = istk->expansion; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2757 | if (!l->finishes->name && l->finishes->in_progress > 1) { |
| 2758 | Line *ll; |
| 2759 | |
| 2760 | /* |
| 2761 | * This is a macro-end marker for a macro with no |
| 2762 | * name, which means it's not really a macro at all |
| 2763 | * but a %rep block, and the `in_progress' field is |
| 2764 | * more than 1, meaning that we still need to |
| 2765 | * repeat. (1 means the natural last repetition; 0 |
| 2766 | * means termination by %exitrep.) We have |
| 2767 | * therefore expanded up to the %endrep, and must |
| 2768 | * push the whole block on to the expansion buffer |
| 2769 | * again. We don't bother to remove the macro-end |
| 2770 | * marker: we'd only have to generate another one |
| 2771 | * if we did. |
| 2772 | */ |
| 2773 | l->finishes->in_progress--; |
| 2774 | for (l = l->finishes->expansion; l; l = l->next) { |
| 2775 | Token *t, *tt, **tail; |
| 2776 | |
| 2777 | ll = nasm_malloc(sizeof(Line)); |
| 2778 | ll->next = istk->expansion; |
| 2779 | ll->finishes = NULL; |
| 2780 | ll->first = NULL; |
| 2781 | tail = &ll->first; |
| 2782 | |
| 2783 | for (t = l->first; t; t = t->next) { |
| 2784 | if (t->text) { |
| 2785 | tt = *tail = nasm_malloc(sizeof(Token)); |
| 2786 | tt->next = NULL; |
| 2787 | tail = &tt->next; |
| 2788 | tt->type = t->type; |
| 2789 | tt->text = nasm_strdup(t->text); |
| 2790 | tt->mac = NULL; |
| 2791 | } |
| 2792 | } |
| 2793 | |
| 2794 | istk->expansion = ll; |
| 2795 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2796 | } else { |
H. Peter Anvin | 87bc619 | 2002-04-30 20:53:16 +0000 | [diff] [blame] | 2797 | /* |
| 2798 | * Check whether a `%rep' was started and not ended |
| 2799 | * within this macro expansion. This can happen and |
| 2800 | * should be detected. It's a fatal error because |
| 2801 | * I'm too confused to work out how to recover |
| 2802 | * sensibly from it. |
| 2803 | */ |
| 2804 | if (defining) { |
| 2805 | if (defining->name) |
| 2806 | error (ERR_PANIC, |
| 2807 | "defining with name in expansion"); |
H. Peter Anvin | b64535f | 2002-04-30 20:55:37 +0000 | [diff] [blame] | 2808 | else if (istk->mstk->name) |
H. Peter Anvin | 87bc619 | 2002-04-30 20:53:16 +0000 | [diff] [blame] | 2809 | error (ERR_FATAL, "`%%rep' without `%%endrep' within" |
| 2810 | " expansion of macro `%s'", istk->mstk->name); |
| 2811 | } |
| 2812 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2813 | /* |
| 2814 | * FIXME: investigate the relationship at this point between |
| 2815 | * istk->mstk and l->finishes |
| 2816 | */ |
| 2817 | { |
| 2818 | MMacro *m = istk->mstk; |
| 2819 | istk->mstk = m->next_active; |
| 2820 | if (m->name) { |
| 2821 | /* |
| 2822 | * This was a real macro call, not a %rep, and |
| 2823 | * therefore the parameter information needs to |
| 2824 | * be freed. |
| 2825 | */ |
| 2826 | nasm_free(m->params); |
| 2827 | free_tlist(m->iline); |
| 2828 | nasm_free(m->paramlen); |
| 2829 | l->finishes->in_progress = FALSE; |
| 2830 | } |
| 2831 | else |
| 2832 | free_mmacro(m); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2833 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2834 | istk->expansion = l->next; |
| 2835 | nasm_free (l); |
| 2836 | list->downlevel (LIST_MACRO); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2837 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2838 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2839 | while (1) { /* until we get a line we can use */ |
| 2840 | |
| 2841 | if (istk->expansion) { /* from a macro expansion */ |
| 2842 | char *p; |
| 2843 | Line *l = istk->expansion; |
| 2844 | tline = l->first; |
| 2845 | istk->expansion = l->next; |
| 2846 | nasm_free (l); |
| 2847 | p = detoken(tline); |
| 2848 | list->line (LIST_MACRO, p); |
| 2849 | nasm_free(p); |
| 2850 | break; |
| 2851 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2852 | line = read_line(); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2853 | if (line) { /* from the current input file */ |
| 2854 | line = prepreproc(line); |
| 2855 | tline = tokenise(line); |
| 2856 | nasm_free (line); |
| 2857 | break; |
| 2858 | } |
| 2859 | /* |
| 2860 | * The current file has ended; work down the istk |
| 2861 | */ |
| 2862 | { |
| 2863 | Include *i = istk; |
| 2864 | fclose(i->fp); |
| 2865 | if (i->conds) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2866 | error(ERR_FATAL, "expected `%%endif' before end of file"); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2867 | istk = i->next; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 2868 | list->downlevel (LIST_INCLUDE); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2869 | src_set_linnum(i->lineno); |
| 2870 | nasm_free ( src_set_fname(i->fname) ); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2871 | nasm_free (i); |
| 2872 | if (!istk) |
| 2873 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2874 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2875 | } |
| 2876 | |
| 2877 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2878 | * We must expand MMacro parameters and MMacro-local labels |
| 2879 | * _before_ we plunge into directive processing, to cope |
| 2880 | * with things like `%define something %1' such as STRUC |
| 2881 | * uses. Unless we're _defining_ a MMacro, in which case |
| 2882 | * those tokens should be left alone to go into the |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2883 | * definition; and unless we're in a non-emitting |
| 2884 | * condition, in which case we don't want to meddle with |
| 2885 | * anything. |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2886 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2887 | if (!defining && !(istk->conds && !emitting(istk->conds->state))) |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2888 | tline = expand_mmac_params(tline); |
| 2889 | |
| 2890 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2891 | * Check the line to see if it's a preprocessor directive. |
| 2892 | */ |
| 2893 | ret = do_directive(tline); |
| 2894 | if (ret & 1) { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2895 | continue; |
| 2896 | } else if (defining) { |
| 2897 | /* |
| 2898 | * We're defining a multi-line macro. We emit nothing |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2899 | * at all, and just |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2900 | * shove the tokenised line on to the macro definition. |
| 2901 | */ |
| 2902 | Line *l = nasm_malloc(sizeof(Line)); |
| 2903 | l->next = defining->expansion; |
| 2904 | l->first = tline; |
| 2905 | l->finishes = FALSE; |
| 2906 | defining->expansion = l; |
| 2907 | continue; |
| 2908 | } else if (istk->conds && !emitting(istk->conds->state)) { |
| 2909 | /* |
| 2910 | * We're in a non-emitting branch of a condition block. |
| 2911 | * Emit nothing at all, not even a blank line: when we |
| 2912 | * emerge from the condition we'll give a line-number |
| 2913 | * directive so we keep our place correctly. |
| 2914 | */ |
| 2915 | free_tlist(tline); |
| 2916 | continue; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2917 | } else if (istk->mstk && !istk->mstk->in_progress) { |
| 2918 | /* |
| 2919 | * We're in a %rep block which has been terminated, so |
| 2920 | * we're walking through to the %endrep without |
| 2921 | * emitting anything. Emit nothing at all, not even a |
| 2922 | * blank line: when we emerge from the %rep block we'll |
| 2923 | * give a line-number directive so we keep our place |
| 2924 | * correctly. |
| 2925 | */ |
| 2926 | free_tlist(tline); |
| 2927 | continue; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2928 | } else { |
| 2929 | tline = expand_smacro(tline); |
| 2930 | ret = expand_mmacro(tline); |
| 2931 | if (!ret) { |
| 2932 | /* |
| 2933 | * De-tokenise the line again, and emit it. |
| 2934 | */ |
| 2935 | line = detoken(tline); |
| 2936 | free_tlist (tline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2937 | break; |
| 2938 | } else { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2939 | continue; /* expand_mmacro calls free_tlist */ |
| 2940 | } |
| 2941 | } |
| 2942 | } |
| 2943 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2944 | return line; |
| 2945 | } |
| 2946 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2947 | static void pp_cleanup (void) |
| 2948 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2949 | int h; |
| 2950 | |
| 2951 | if (defining) { |
| 2952 | error (ERR_NONFATAL, "end of file while still defining macro `%s'", |
| 2953 | defining->name); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2954 | free_mmacro (defining); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2955 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2956 | while (cstk) |
| 2957 | ctx_pop(); |
| 2958 | for (h=0; h<NHASH; h++) { |
| 2959 | while (mmacros[h]) { |
| 2960 | MMacro *m = mmacros[h]; |
| 2961 | mmacros[h] = mmacros[h]->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2962 | free_mmacro(m); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2963 | } |
| 2964 | while (smacros[h]) { |
| 2965 | SMacro *s = smacros[h]; |
| 2966 | smacros[h] = smacros[h]->next; |
| 2967 | nasm_free (s->name); |
| 2968 | free_tlist (s->expansion); |
| 2969 | nasm_free (s); |
| 2970 | } |
| 2971 | } |
| 2972 | while (istk) { |
| 2973 | Include *i = istk; |
| 2974 | istk = istk->next; |
| 2975 | fclose(i->fp); |
| 2976 | nasm_free (i->fname); |
| 2977 | nasm_free (i); |
| 2978 | } |
| 2979 | while (cstk) |
| 2980 | ctx_pop(); |
| 2981 | } |
| 2982 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2983 | void pp_include_path (char *path) |
| 2984 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 2985 | IncPath *i; |
| 2986 | |
| 2987 | i = nasm_malloc(sizeof(IncPath)); |
| 2988 | i->path = nasm_strdup(path); |
| 2989 | i->next = ipath; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 2990 | ipath = i; |
| 2991 | } |
| 2992 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2993 | void pp_pre_include (char *fname) |
| 2994 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 2995 | Token *inc, *space, *name; |
| 2996 | Line *l; |
| 2997 | |
| 2998 | inc = nasm_malloc(sizeof(Token)); |
| 2999 | inc->next = space = nasm_malloc(sizeof(Token)); |
| 3000 | space->next = name = nasm_malloc(sizeof(Token)); |
| 3001 | name->next = NULL; |
| 3002 | |
| 3003 | inc->type = TOK_PREPROC_ID; |
| 3004 | inc->text = nasm_strdup("%include"); |
| 3005 | space->type = TOK_WHITESPACE; |
| 3006 | space->text = nasm_strdup(" "); |
| 3007 | name->type = TOK_INTERNAL_STRING; |
| 3008 | name->text = nasm_strdup(fname); |
| 3009 | |
| 3010 | inc->mac = space->mac = name->mac = NULL; |
| 3011 | |
| 3012 | l = nasm_malloc(sizeof(Line)); |
| 3013 | l->next = predef; |
| 3014 | l->first = inc; |
| 3015 | l->finishes = FALSE; |
| 3016 | predef = l; |
| 3017 | } |
| 3018 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3019 | void pp_pre_define (char *definition) |
| 3020 | { |
| 3021 | Token *def, *space; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 3022 | Line *l; |
| 3023 | char *equals; |
| 3024 | |
| 3025 | equals = strchr(definition, '='); |
| 3026 | |
| 3027 | def = nasm_malloc(sizeof(Token)); |
| 3028 | def->next = space = nasm_malloc(sizeof(Token)); |
| 3029 | if (equals) |
| 3030 | *equals = ' '; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3031 | space->next = tokenise(definition); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 3032 | if (equals) |
| 3033 | *equals = '='; |
| 3034 | |
| 3035 | def->type = TOK_PREPROC_ID; |
| 3036 | def->text = nasm_strdup("%define"); |
| 3037 | space->type = TOK_WHITESPACE; |
| 3038 | space->text = nasm_strdup(" "); |
| 3039 | |
| 3040 | def->mac = space->mac = NULL; |
| 3041 | |
| 3042 | l = nasm_malloc(sizeof(Line)); |
| 3043 | l->next = predef; |
| 3044 | l->first = def; |
| 3045 | l->finishes = FALSE; |
| 3046 | predef = l; |
| 3047 | } |
| 3048 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3049 | void pp_extra_stdmac (char **macros) |
| 3050 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3051 | extrastdmac = macros; |
| 3052 | } |
| 3053 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3054 | static void make_tok_num(Token *tok, long val) |
| 3055 | { |
| 3056 | char numbuf[20]; |
| 3057 | sprintf(numbuf, "%ld", val); |
| 3058 | tok->text = nasm_strdup(numbuf); |
| 3059 | tok->type = TOK_NUMBER; |
| 3060 | } |
| 3061 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3062 | Preproc nasmpp = { |
| 3063 | pp_reset, |
| 3064 | pp_getline, |
| 3065 | pp_cleanup |
| 3066 | }; |