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", |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 271 | "%macro", "%pop", "%push", "%rep", "%repl", "%rotate", "%undef" |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 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, |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 282 | PP_MACRO, PP_POP, PP_PUSH, PP_REP, PP_REPL, PP_ROTATE, PP_UNDEF |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 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 | |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 293 | static int pass; /* HACK: pass 0 = generate dependencies only */ |
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 | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 881 | static int namelen = 0; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 882 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 883 | while (1) { |
| 884 | combine = nasm_strcat(prefix,file); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 885 | fp = fopen(combine, "r"); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 886 | if (pass == 0 && fp) |
| 887 | { |
| 888 | namelen += strlen(combine) + 1; |
| 889 | if (namelen > 62) |
| 890 | { |
| 891 | printf(" \\\n "); |
| 892 | namelen = 2; |
| 893 | } |
| 894 | printf(" %s", combine); |
| 895 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 896 | nasm_free (combine); |
| 897 | if (fp) |
| 898 | return fp; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 899 | if (!ip) |
| 900 | break; |
| 901 | prefix = ip->path; |
| 902 | ip = ip->next; |
| 903 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 904 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 905 | error (ERR_FATAL, |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 906 | "unable to open include file `%s'", file); |
| 907 | return NULL; /* never reached - placate compilers */ |
| 908 | } |
| 909 | |
| 910 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 911 | * Determine if we should warn on defining a single-line macro of |
H. Peter Anvin | ef7468f | 2002-04-30 20:57:59 +0000 | [diff] [blame] | 912 | * name `name', with `nparam' parameters. If nparam is 0 or -1, will |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 913 | * return TRUE if _any_ single-line macro of that name is defined. |
| 914 | * Otherwise, will return TRUE if a single-line macro with either |
| 915 | * `nparam' or no parameters is defined. |
| 916 | * |
| 917 | * If a macro with precisely the right number of parameters is |
H. Peter Anvin | ef7468f | 2002-04-30 20:57:59 +0000 | [diff] [blame] | 918 | * defined, or nparam is -1, the address of the definition structure |
| 919 | * will be returned in `defn'; otherwise NULL will be returned. If `defn' |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 920 | * is NULL, no action will be taken regarding its contents, and no |
| 921 | * error will occur. |
| 922 | * |
| 923 | * Note that this is also called with nparam zero to resolve |
| 924 | * `ifdef'. |
| 925 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 926 | static int smacro_defined (char *name, int nparam, SMacro **defn, int nocase) |
| 927 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 928 | SMacro *m; |
| 929 | Context *ctx; |
| 930 | char *p; |
| 931 | |
| 932 | if (name[0] == '%' && name[1] == '$') { |
| 933 | ctx = get_ctx (name); |
| 934 | if (!ctx) |
| 935 | return FALSE; /* got to return _something_ */ |
| 936 | m = ctx->localmac; |
| 937 | p = name+1; |
| 938 | p += strspn(p, "$"); |
| 939 | } else { |
| 940 | m = smacros[hash(name)]; |
| 941 | p = name; |
| 942 | } |
| 943 | |
| 944 | while (m) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 945 | if (!mstrcmp(m->name, p, m->casesense & nocase) && |
H. Peter Anvin | ef7468f | 2002-04-30 20:57:59 +0000 | [diff] [blame] | 946 | (nparam <= 0 || m->nparam == 0 || nparam == m->nparam)) { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 947 | if (defn) { |
H. Peter Anvin | ef7468f | 2002-04-30 20:57:59 +0000 | [diff] [blame] | 948 | if (nparam == m->nparam || nparam == -1) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 949 | *defn = m; |
| 950 | else |
| 951 | *defn = NULL; |
| 952 | } |
| 953 | return TRUE; |
| 954 | } |
| 955 | m = m->next; |
| 956 | } |
| 957 | return FALSE; |
| 958 | } |
| 959 | |
| 960 | /* |
| 961 | * Count and mark off the parameters in a multi-line macro call. |
| 962 | * This is called both from within the multi-line macro expansion |
| 963 | * code, and also to mark off the default parameters when provided |
| 964 | * in a %macro definition line. |
| 965 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 966 | static void count_mmac_params (Token *t, int *nparam, Token ***params) |
| 967 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 968 | int paramsize, brace; |
| 969 | |
| 970 | *nparam = paramsize = 0; |
| 971 | *params = NULL; |
| 972 | while (t) { |
| 973 | if (*nparam >= paramsize) { |
| 974 | paramsize += PARAM_DELTA; |
| 975 | *params = nasm_realloc(*params, sizeof(**params) * paramsize); |
| 976 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 977 | skip_white_(t); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 978 | brace = FALSE; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 979 | if (tok_is_(t, "{")) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 980 | brace = TRUE; |
| 981 | (*params)[(*nparam)++] = t; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 982 | while (tok_isnt_(t, brace ? "}" : ",")) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 983 | t = t->next; |
| 984 | if (t) { /* got a comma/brace */ |
| 985 | t = t->next; |
| 986 | if (brace) { |
| 987 | /* |
| 988 | * Now we've found the closing brace, look further |
| 989 | * for the comma. |
| 990 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 991 | skip_white_(t); |
| 992 | if (tok_isnt_(t, ",")) { |
| 993 | error (ERR_NONFATAL, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 994 | "braces do not enclose all of macro parameter"); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 995 | while (tok_isnt_(t, ",")) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 996 | t = t->next; |
| 997 | } |
| 998 | if (t) |
| 999 | t = t->next; /* eat the comma */ |
| 1000 | } |
| 1001 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1006 | * Determine whether one of the various `if' conditions is true or |
| 1007 | * not. |
| 1008 | * |
| 1009 | * We must free the tline we get passed. |
| 1010 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1011 | static int if_condition (Token *tline, int i) |
| 1012 | { |
| 1013 | int j, casesense; |
| 1014 | Token * t, * tt, ** tptr, * origline; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1015 | struct tokenval tokval; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1016 | expr * evalresult; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1017 | |
| 1018 | origline = tline; |
| 1019 | |
| 1020 | switch (i) { |
| 1021 | case PP_IFCTX: case PP_ELIFCTX: |
| 1022 | case PP_IFNCTX: case PP_ELIFNCTX: |
| 1023 | j = FALSE; /* have we matched yet? */ |
| 1024 | if (!cstk) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1025 | error(ERR_FATAL, |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1026 | "`%s': context stack is empty", directives[i]); |
| 1027 | else while (tline) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1028 | skip_white_(tline); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1029 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1030 | error(ERR_NONFATAL, |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1031 | "`%s' expects context identifiers", directives[i]); |
| 1032 | free_tlist (origline); |
| 1033 | return -1; |
| 1034 | } |
| 1035 | if (!nasm_stricmp(tline->text, cstk->name)) |
| 1036 | j = TRUE; |
| 1037 | tline = tline->next; |
| 1038 | } |
| 1039 | if (i == PP_IFNCTX || i == PP_ELIFNCTX) |
| 1040 | j = !j; |
| 1041 | free_tlist (origline); |
| 1042 | return j; |
| 1043 | |
| 1044 | case PP_IFDEF: case PP_ELIFDEF: |
| 1045 | case PP_IFNDEF: case PP_ELIFNDEF: |
| 1046 | j = FALSE; /* have we matched yet? */ |
| 1047 | while (tline) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1048 | skip_white_(tline); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1049 | if (!tline || (tline->type != TOK_ID && |
| 1050 | (tline->type != TOK_PREPROC_ID || |
| 1051 | tline->text[1] != '$'))) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1052 | error(ERR_NONFATAL, |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1053 | "`%%if%sdef' expects macro identifiers", |
| 1054 | (i==PP_ELIFNDEF ? "n" : "")); |
| 1055 | free_tlist (origline); |
| 1056 | return -1; |
| 1057 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1058 | if (smacro_defined(tline->text, 0, NULL, 1)) |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1059 | j = TRUE; |
| 1060 | tline = tline->next; |
| 1061 | } |
| 1062 | if (i == PP_IFNDEF || i == PP_ELIFNDEF) |
| 1063 | j = !j; |
| 1064 | free_tlist (origline); |
| 1065 | return j; |
| 1066 | |
| 1067 | case PP_IFIDN: case PP_ELIFIDN: case PP_IFNIDN: case PP_ELIFNIDN: |
| 1068 | case PP_IFIDNI: case PP_ELIFIDNI: case PP_IFNIDNI: case PP_ELIFNIDNI: |
| 1069 | tline = expand_smacro(tline); |
| 1070 | t = tt = tline; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1071 | while (tok_isnt_(tt, ",")) |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1072 | tt = tt->next; |
| 1073 | if (!tt) { |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 1074 | error(ERR_NONFATAL, "`%s' expects two comma-separated arguments", |
| 1075 | directives[i]); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1076 | free_tlist (tline); |
| 1077 | return -1; |
| 1078 | } |
| 1079 | tt = tt->next; |
| 1080 | casesense = (i == PP_IFIDN || i == PP_ELIFIDN || |
| 1081 | i == PP_IFNIDN || i == PP_ELIFNIDN); |
| 1082 | j = TRUE; /* assume equality unless proved not */ |
| 1083 | while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) { |
| 1084 | if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) { |
| 1085 | error(ERR_NONFATAL, "`%s': more than one comma on line", |
| 1086 | directives[i]); |
| 1087 | free_tlist (tline); |
| 1088 | return -1; |
| 1089 | } |
| 1090 | if (t->type == TOK_WHITESPACE) { |
| 1091 | t = t->next; |
| 1092 | continue; |
| 1093 | } else if (tt->type == TOK_WHITESPACE) { |
| 1094 | tt = tt->next; |
| 1095 | continue; |
| 1096 | } else if (tt->type != t->type || |
| 1097 | (casesense ? strcmp(tt->text, t->text) : |
| 1098 | nasm_stricmp(tt->text, t->text))) { |
| 1099 | j = FALSE; /* found mismatching tokens */ |
| 1100 | break; |
| 1101 | } else { |
| 1102 | t = t->next; |
| 1103 | tt = tt->next; |
| 1104 | continue; |
| 1105 | } |
| 1106 | } |
| 1107 | if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt) |
| 1108 | j = FALSE; /* trailing gunk on one end or other */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1109 | if (i == PP_IFNIDN || i == PP_ELIFNIDN || |
| 1110 | i == PP_IFNIDNI || i == PP_ELIFNIDNI) |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1111 | j = !j; |
| 1112 | free_tlist (tline); |
| 1113 | return j; |
| 1114 | |
| 1115 | case PP_IFID: case PP_ELIFID: case PP_IFNID: case PP_ELIFNID: |
| 1116 | case PP_IFNUM: case PP_ELIFNUM: case PP_IFNNUM: case PP_ELIFNNUM: |
| 1117 | case PP_IFSTR: case PP_ELIFSTR: case PP_IFNSTR: case PP_ELIFNSTR: |
| 1118 | tline = expand_smacro(tline); |
| 1119 | t = tline; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1120 | while (tok_type_(t, TOK_WHITESPACE)) |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1121 | t = t->next; |
| 1122 | j = FALSE; /* placate optimiser */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1123 | if (t) switch (i) { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1124 | case PP_IFID: case PP_ELIFID: case PP_IFNID: case PP_ELIFNID: |
| 1125 | j = (t->type == TOK_ID); |
| 1126 | break; |
| 1127 | case PP_IFNUM: case PP_ELIFNUM: case PP_IFNNUM: case PP_ELIFNNUM: |
| 1128 | j = (t->type == TOK_NUMBER); |
| 1129 | break; |
| 1130 | case PP_IFSTR: case PP_ELIFSTR: case PP_IFNSTR: case PP_ELIFNSTR: |
| 1131 | j = (t->type == TOK_STRING); |
| 1132 | break; |
| 1133 | } |
| 1134 | if (i == PP_IFNID || i == PP_ELIFNID || |
| 1135 | i == PP_IFNNUM || i == PP_ELIFNNUM || |
| 1136 | i == PP_IFNSTR || i == PP_ELIFNSTR) |
| 1137 | j = !j; |
| 1138 | free_tlist (tline); |
| 1139 | return j; |
| 1140 | |
| 1141 | case PP_IF: case PP_ELIF: |
| 1142 | t = tline = expand_smacro(tline); |
| 1143 | tptr = &t; |
| 1144 | tokval.t_type = TOKEN_INVALID; |
| 1145 | evalresult = evaluate (ppscan, tptr, &tokval, |
| 1146 | NULL, pass | 0x10, error, NULL); |
| 1147 | free_tlist (tline); |
| 1148 | if (!evalresult) |
| 1149 | return -1; |
| 1150 | if (tokval.t_type) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1151 | error(ERR_WARNING, |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1152 | "trailing garbage after expression ignored"); |
| 1153 | if (!is_simple(evalresult)) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1154 | error(ERR_NONFATAL, |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1155 | "non-constant value given to `%s'", directives[i]); |
| 1156 | return -1; |
| 1157 | } |
| 1158 | return reloc_value(evalresult) != 0; |
| 1159 | |
| 1160 | default: |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1161 | error(ERR_FATAL, |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1162 | "preprocessor directive `%s' not yet implemented", |
| 1163 | directives[i]); |
| 1164 | free_tlist (origline); |
| 1165 | return -1; /* yeah, right */ |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1170 | * Find out if a line contains a preprocessor directive, and deal |
| 1171 | * with it if so. |
| 1172 | * |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1173 | * If a directive _is_ found, we are expected to free_tlist() the |
| 1174 | * line. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1175 | * |
| 1176 | * Return values go like this: |
| 1177 | * |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1178 | * 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] | 1179 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1180 | static int do_directive (Token *tline) |
| 1181 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1182 | int i, j, k, m, nparam, nolist; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1183 | char *p, *mname; |
| 1184 | Include *inc; |
| 1185 | Context *ctx; |
| 1186 | Cond *cond; |
| 1187 | SMacro *smac, **smhead; |
| 1188 | MMacro *mmac; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1189 | Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline; |
| 1190 | Line *l; |
| 1191 | struct tokenval tokval; |
| 1192 | expr *evalresult; |
H. Peter Anvin | b64535f | 2002-04-30 20:55:37 +0000 | [diff] [blame] | 1193 | MMacro *tmp_defining; /* Used when manipulating rep_nest */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1194 | |
| 1195 | origline = tline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1196 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1197 | skip_white_(tline); |
| 1198 | if (!tok_type_(tline, TOK_PREPROC_ID) || |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1199 | (tline->text[1]=='%' || tline->text[1]=='$' || tline->text[1]=='!')) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1200 | return 0; |
| 1201 | |
| 1202 | i = -1; |
| 1203 | j = sizeof(directives)/sizeof(*directives); |
| 1204 | while (j-i > 1) { |
| 1205 | k = (j+i) / 2; |
| 1206 | m = nasm_stricmp(tline->text, directives[k]); |
| 1207 | if (m == 0) { |
| 1208 | i = k; |
| 1209 | j = -2; |
| 1210 | break; |
| 1211 | } else if (m < 0) { |
| 1212 | j = k; |
| 1213 | } else |
| 1214 | i = k; |
| 1215 | } |
| 1216 | |
| 1217 | /* |
| 1218 | * 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] | 1219 | * or walking to the end of an already terminated %rep block, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1220 | * we should ignore all directives except for condition |
| 1221 | * directives. |
| 1222 | */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1223 | if (((istk->conds && !emitting(istk->conds->state)) || |
| 1224 | (istk->mstk && !istk->mstk->in_progress)) && |
| 1225 | i != PP_IF && i != PP_ELIF && |
| 1226 | i != PP_IFCTX && i != PP_ELIFCTX && |
| 1227 | i != PP_IFDEF && i != PP_ELIFDEF && |
| 1228 | i != PP_IFID && i != PP_ELIFID && |
| 1229 | i != PP_IFIDN && i != PP_ELIFIDN && |
| 1230 | i != PP_IFIDNI && i != PP_ELIFIDNI && |
| 1231 | i != PP_IFNCTX && i != PP_ELIFNCTX && |
| 1232 | i != PP_IFNDEF && i != PP_ELIFNDEF && |
| 1233 | i != PP_IFNID && i != PP_ELIFNID && |
| 1234 | i != PP_IFNIDN && i != PP_ELIFNIDN && |
| 1235 | i != PP_IFNIDNI && i != PP_ELIFNIDNI && |
| 1236 | i != PP_IFNNUM && i != PP_ELIFNNUM && |
| 1237 | i != PP_IFNSTR && i != PP_ELIFNSTR && |
| 1238 | i != PP_IFNUM && i != PP_ELIFNUM && |
| 1239 | i != PP_IFSTR && i != PP_ELIFSTR && |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1240 | i != PP_ELSE && i != PP_ENDIF) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1241 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1242 | return 0; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1243 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1244 | |
| 1245 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1246 | * If we're defining a macro or reading a %rep block, we should |
| 1247 | * ignore all directives except for %macro/%imacro (which |
| 1248 | * 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] | 1249 | * %rep block) %endrep. If we're in a %rep block, another %rep |
| 1250 | * causes an error, so should be let through. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1251 | */ |
| 1252 | if (defining && i != PP_MACRO && i != PP_IMACRO && |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1253 | i != PP_ENDMACRO && i != PP_ENDM && |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1254 | (defining->name || (i != PP_ENDREP && i != PP_REP))) |
| 1255 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1256 | return 0; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1257 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1258 | |
| 1259 | if (j != -2) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1260 | error(ERR_NONFATAL, "unknown preprocessor directive `%s'", |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1261 | tline->text); |
| 1262 | return 0; /* didn't get it */ |
| 1263 | } |
| 1264 | |
| 1265 | switch (i) { |
| 1266 | |
| 1267 | case PP_CLEAR: |
| 1268 | if (tline->next) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1269 | error(ERR_WARNING, |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1270 | "trailing garbage after `%%clear' ignored"); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1271 | for (j=0; j<NHASH; j++) { |
| 1272 | while (mmacros[j]) { |
| 1273 | MMacro *m = mmacros[j]; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1274 | mmacros[j] = m->next; |
| 1275 | free_mmacro(m); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1276 | } |
| 1277 | while (smacros[j]) { |
| 1278 | SMacro *s = smacros[j]; |
| 1279 | smacros[j] = smacros[j]->next; |
| 1280 | nasm_free (s->name); |
| 1281 | free_tlist (s->expansion); |
| 1282 | nasm_free (s); |
| 1283 | } |
| 1284 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1285 | free_tlist (origline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1286 | return 3; |
| 1287 | |
| 1288 | case PP_INCLUDE: |
| 1289 | tline = tline->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1290 | skip_white_(tline); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1291 | if (!tline || (tline->type != TOK_STRING && |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1292 | tline->type != TOK_INTERNAL_STRING)) |
| 1293 | { |
| 1294 | error(ERR_NONFATAL, "`%%include' expects a file name"); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1295 | free_tlist (origline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1296 | return 3; /* but we did _something_ */ |
| 1297 | } |
| 1298 | if (tline->next) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1299 | error(ERR_WARNING, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1300 | "trailing garbage after `%%include' ignored"); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1301 | if (tline->type != TOK_INTERNAL_STRING) { |
| 1302 | p = tline->text+1; /* point past the quote to the name */ |
| 1303 | p[strlen(p)-1] = '\0'; /* remove the trailing quote */ |
| 1304 | } else |
| 1305 | p = tline->text; /* internal_string is easier */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1306 | inc = nasm_malloc(sizeof(Include)); |
| 1307 | inc->next = istk; |
| 1308 | inc->conds = NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1309 | inc->fp = inc_fopen(p); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1310 | inc->fname = src_set_fname(nasm_strdup(p)); |
| 1311 | inc->lineno = src_set_linnum(0); |
| 1312 | inc->lineinc = 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1313 | inc->expansion = NULL; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1314 | inc->mstk = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1315 | istk = inc; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1316 | list->uplevel (LIST_INCLUDE); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1317 | free_tlist (origline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1318 | return 5; |
| 1319 | |
| 1320 | case PP_PUSH: |
| 1321 | tline = tline->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1322 | skip_white_(tline); |
| 1323 | if (!tok_type_(tline, TOK_ID)) { |
| 1324 | error(ERR_NONFATAL, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1325 | "`%%push' expects a context identifier"); |
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 | return 3; /* but we did _something_ */ |
| 1328 | } |
| 1329 | if (tline->next) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1330 | error(ERR_WARNING, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1331 | "trailing garbage after `%%push' ignored"); |
| 1332 | ctx = nasm_malloc(sizeof(Context)); |
| 1333 | ctx->next = cstk; |
| 1334 | ctx->localmac = NULL; |
| 1335 | ctx->name = nasm_strdup(tline->text); |
| 1336 | ctx->number = unique++; |
| 1337 | cstk = ctx; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1338 | free_tlist (origline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1339 | break; |
| 1340 | |
| 1341 | case PP_REPL: |
| 1342 | tline = tline->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1343 | skip_white_(tline); |
| 1344 | if (!tok_type_(tline, TOK_ID)) { |
| 1345 | error(ERR_NONFATAL, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1346 | "`%%repl' expects a context identifier"); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1347 | free_tlist (origline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1348 | return 3; /* but we did _something_ */ |
| 1349 | } |
| 1350 | if (tline->next) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1351 | error(ERR_WARNING, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1352 | "trailing garbage after `%%repl' ignored"); |
| 1353 | if (!cstk) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1354 | error(ERR_NONFATAL, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1355 | "`%%repl': context stack is empty"); |
| 1356 | else { |
| 1357 | nasm_free (cstk->name); |
| 1358 | cstk->name = nasm_strdup(tline->text); |
| 1359 | } |
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_POP: |
| 1364 | if (tline->next) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1365 | error(ERR_WARNING, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1366 | "trailing garbage after `%%pop' ignored"); |
| 1367 | if (!cstk) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1368 | error(ERR_NONFATAL, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1369 | "`%%pop': context stack is already empty"); |
| 1370 | else |
| 1371 | ctx_pop(); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1372 | free_tlist (origline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1373 | break; |
| 1374 | |
| 1375 | case PP_ERROR: |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1376 | tline->next = expand_smacro (tline->next); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1377 | tline = tline->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1378 | skip_white_(tline); |
| 1379 | if (tok_type_(tline, TOK_STRING)) { |
| 1380 | p = tline->text+1; /* point past the quote to the name */ |
| 1381 | p[strlen(p)-1] = '\0'; /* remove the trailing quote */ |
| 1382 | error(ERR_NONFATAL, "user error: %s", p); |
| 1383 | } else { |
| 1384 | p = detoken(tline); |
| 1385 | error(ERR_WARNING, "user error: %s", p); |
| 1386 | nasm_free(p); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1387 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1388 | free_tlist (origline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1389 | break; |
| 1390 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1391 | case PP_IF: |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1392 | case PP_IFCTX: |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1393 | case PP_IFDEF: |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1394 | case PP_IFID: |
| 1395 | case PP_IFIDN: |
| 1396 | case PP_IFIDNI: |
| 1397 | case PP_IFNCTX: |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1398 | case PP_IFNDEF: |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1399 | case PP_IFNID: |
| 1400 | case PP_IFNIDN: |
| 1401 | case PP_IFNIDNI: |
| 1402 | case PP_IFNNUM: |
| 1403 | case PP_IFNSTR: |
| 1404 | case PP_IFNUM: |
| 1405 | case PP_IFSTR: |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1406 | if (istk->conds && !emitting(istk->conds->state)) |
| 1407 | j = COND_NEVER; |
| 1408 | else { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1409 | j = if_condition(tline->next, i); |
| 1410 | tline->next = NULL; /* it got freed */ |
| 1411 | free_tlist (origline); |
| 1412 | if (j < 0) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1413 | /* |
| 1414 | * Bogus expression in %if, but we should pretend |
| 1415 | * it was OK anyway, so that we don't get an error |
| 1416 | * cascade on the subsequent %else / %endif. |
| 1417 | */ |
| 1418 | j = COND_NEVER; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1419 | else |
| 1420 | j = j ? COND_IF_TRUE : COND_IF_FALSE; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1421 | } |
| 1422 | cond = nasm_malloc(sizeof(Cond)); |
| 1423 | cond->next = istk->conds; |
| 1424 | cond->state = j; |
| 1425 | istk->conds = cond; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1426 | return (j == COND_IF_TRUE ? 3 : 1); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1427 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1428 | case PP_ELIF: |
| 1429 | case PP_ELIFCTX: |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1430 | case PP_ELIFDEF: |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1431 | case PP_ELIFID: |
| 1432 | case PP_ELIFIDN: |
| 1433 | case PP_ELIFIDNI: |
| 1434 | case PP_ELIFNCTX: |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1435 | case PP_ELIFNDEF: |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1436 | case PP_ELIFNID: |
| 1437 | case PP_ELIFNIDN: |
| 1438 | case PP_ELIFNIDNI: |
| 1439 | case PP_ELIFNNUM: |
| 1440 | case PP_ELIFNSTR: |
| 1441 | case PP_ELIFNUM: |
| 1442 | case PP_ELIFSTR: |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1443 | if (!istk->conds) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1444 | error(ERR_FATAL, "`%s': no matching `%%if'", |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1445 | directives[i]); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1446 | if (emitting(istk->conds->state) || istk->conds->state == COND_NEVER) |
| 1447 | istk->conds->state = COND_NEVER; |
| 1448 | else { |
H. Peter Anvin | 4836e33 | 2002-04-30 20:56:43 +0000 | [diff] [blame] | 1449 | j = if_condition(expand_mmac_params(tline->next), i); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1450 | tline->next = NULL; /* it got freed */ |
| 1451 | free_tlist (origline); |
| 1452 | if (j < 0) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1453 | /* |
| 1454 | * The expression was bogus, but let's make |
| 1455 | * %endif not complain about missing %if |
| 1456 | */ |
| 1457 | j = COND_NEVER; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1458 | else |
| 1459 | istk->conds->state = j ? COND_IF_TRUE : COND_IF_FALSE; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1460 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1461 | return (istk->conds->state == COND_IF_TRUE ? 5 : 1); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1462 | |
| 1463 | case PP_ELSE: |
| 1464 | if (tline->next) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1465 | error(ERR_WARNING, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1466 | "trailing garbage after `%%else' ignored"); |
| 1467 | if (!istk->conds) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1468 | error(ERR_FATAL, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1469 | "`%%else': no matching `%%if'"); |
| 1470 | if (emitting(istk->conds->state) || istk->conds->state == COND_NEVER) |
| 1471 | istk->conds->state = COND_ELSE_FALSE; |
| 1472 | else |
| 1473 | istk->conds->state = COND_ELSE_TRUE; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1474 | free_tlist (origline); |
| 1475 | return 5; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1476 | |
| 1477 | case PP_ENDIF: |
| 1478 | if (tline->next) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1479 | error(ERR_WARNING, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1480 | "trailing garbage after `%%endif' ignored"); |
| 1481 | if (!istk->conds) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1482 | error(ERR_FATAL, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1483 | "`%%endif': no matching `%%if'"); |
| 1484 | cond = istk->conds; |
| 1485 | istk->conds = cond->next; |
| 1486 | nasm_free (cond); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1487 | free_tlist (origline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1488 | return 5; |
| 1489 | |
| 1490 | case PP_MACRO: |
| 1491 | case PP_IMACRO: |
| 1492 | if (defining) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1493 | error (ERR_FATAL, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1494 | "`%%%smacro': already defining a macro", |
| 1495 | (i == PP_IMACRO ? "i" : "")); |
| 1496 | tline = tline->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1497 | skip_white_(tline); |
| 1498 | if (!tok_type_(tline, TOK_ID)) { |
| 1499 | error (ERR_NONFATAL, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1500 | "`%%%smacro' expects a macro name", |
| 1501 | (i == PP_IMACRO ? "i" : "")); |
| 1502 | return 3; |
| 1503 | } |
| 1504 | defining = nasm_malloc(sizeof(MMacro)); |
| 1505 | defining->name = nasm_strdup(tline->text); |
| 1506 | defining->casesense = (i == PP_MACRO); |
| 1507 | defining->plus = FALSE; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1508 | defining->nolist = FALSE; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1509 | defining->in_progress = FALSE; |
H. Peter Anvin | b64535f | 2002-04-30 20:55:37 +0000 | [diff] [blame] | 1510 | defining->rep_nest = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1511 | tline = tline->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1512 | skip_white_(tline); |
| 1513 | if (!tok_type_(tline, TOK_NUMBER)) { |
| 1514 | error (ERR_NONFATAL, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1515 | "`%%%smacro' expects a parameter count", |
| 1516 | (i == PP_IMACRO ? "i" : "")); |
| 1517 | defining->nparam_min = defining->nparam_max = 0; |
| 1518 | } else { |
| 1519 | defining->nparam_min = defining->nparam_max = |
| 1520 | readnum(tline->text, &j); |
| 1521 | if (j) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1522 | error (ERR_NONFATAL, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1523 | "unable to parse parameter count `%s'", tline->text); |
| 1524 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1525 | if (tline && tok_is_(tline->next, "-")) { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1526 | tline = tline->next->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1527 | if (tok_is_(tline, "*")) |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1528 | defining->nparam_max = INT_MAX; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1529 | else if (!tok_type_(tline, TOK_NUMBER)) |
| 1530 | error (ERR_NONFATAL, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1531 | "`%%%smacro' expects a parameter count after `-'", |
| 1532 | (i == PP_IMACRO ? "i" : "")); |
| 1533 | else { |
| 1534 | defining->nparam_max = readnum(tline->text, &j); |
| 1535 | if (j) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1536 | error (ERR_NONFATAL, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1537 | "unable to parse parameter count `%s'", |
| 1538 | tline->text); |
| 1539 | if (defining->nparam_min > defining->nparam_max) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1540 | error (ERR_NONFATAL, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1541 | "minimum parameter count exceeds maximum"); |
| 1542 | } |
| 1543 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1544 | if (tline && tok_is_(tline->next, "+")) { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1545 | tline = tline->next; |
| 1546 | defining->plus = TRUE; |
| 1547 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1548 | if (tline && tok_type_(tline->next, TOK_ID) && |
| 1549 | !nasm_stricmp(tline->next->text, ".nolist")) |
| 1550 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1551 | tline = tline->next; |
| 1552 | defining->nolist = TRUE; |
| 1553 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1554 | mmac = mmacros[hash(defining->name)]; |
| 1555 | while (mmac) { |
| 1556 | if (!strcmp(mmac->name, defining->name) && |
| 1557 | (mmac->nparam_min<=defining->nparam_max || defining->plus) && |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1558 | (defining->nparam_min<=mmac->nparam_max || mmac->plus)) |
| 1559 | { |
| 1560 | error (ERR_WARNING, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1561 | "redefining multi-line macro `%s'", defining->name); |
| 1562 | break; |
| 1563 | } |
| 1564 | mmac = mmac->next; |
| 1565 | } |
| 1566 | /* |
| 1567 | * Handle default parameters. |
| 1568 | */ |
| 1569 | if (tline && tline->next) { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1570 | defining->dlist = tline->next; |
| 1571 | tline->next = NULL; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1572 | count_mmac_params (defining->dlist, &defining->ndefs, |
| 1573 | &defining->defaults); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1574 | } else { |
| 1575 | defining->dlist = NULL; |
| 1576 | defining->defaults = NULL; |
| 1577 | } |
| 1578 | defining->expansion = NULL; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1579 | free_tlist (origline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1580 | return 1; |
| 1581 | |
| 1582 | case PP_ENDM: |
| 1583 | case PP_ENDMACRO: |
| 1584 | if (!defining) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1585 | error (ERR_NONFATAL, "`%s': not defining a macro", |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1586 | tline->text); |
| 1587 | return 3; |
| 1588 | } |
| 1589 | k = hash(defining->name); |
| 1590 | defining->next = mmacros[k]; |
| 1591 | mmacros[k] = defining; |
| 1592 | defining = NULL; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1593 | free_tlist (origline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1594 | return 5; |
| 1595 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1596 | case PP_ROTATE: |
| 1597 | if (tline->next && tline->next->type == TOK_WHITESPACE) |
| 1598 | tline = tline->next; |
| 1599 | t = expand_smacro(tline->next); |
| 1600 | tline->next = NULL; |
| 1601 | free_tlist (origline); |
| 1602 | tline = t; |
| 1603 | tptr = &t; |
| 1604 | tokval.t_type = TOKEN_INVALID; |
| 1605 | evalresult = evaluate (ppscan, tptr, &tokval, NULL, pass, error, NULL); |
| 1606 | free_tlist (tline); |
| 1607 | if (!evalresult) |
| 1608 | return 3; |
| 1609 | if (tokval.t_type) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1610 | error(ERR_WARNING, |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1611 | "trailing garbage after expression ignored"); |
| 1612 | if (!is_simple(evalresult)) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1613 | error(ERR_NONFATAL, |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1614 | "non-constant value given to `%%rotate'"); |
| 1615 | return 3; |
| 1616 | } |
| 1617 | mmac = istk->mstk; |
| 1618 | while (mmac && !mmac->name) /* avoid mistaking %reps for macros */ |
| 1619 | mmac = mmac->next_active; |
| 1620 | if (!mmac) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1621 | error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call"); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1622 | mmac->rotate = mmac->rotate + reloc_value(evalresult); |
| 1623 | if (mmac->rotate < 0) |
| 1624 | mmac->rotate = mmac->nparam - (-mmac->rotate) % mmac->nparam; |
| 1625 | mmac->rotate %= mmac->nparam; |
| 1626 | return 1; |
| 1627 | |
| 1628 | case PP_REP: |
| 1629 | nolist = FALSE; |
| 1630 | tline = tline->next; |
| 1631 | if (tline->next && tline->next->type == TOK_WHITESPACE) |
| 1632 | tline = tline->next; |
| 1633 | if (tline->next && tline->next->type == TOK_ID && |
| 1634 | !nasm_stricmp(tline->next->text, ".nolist")) { |
| 1635 | tline = tline->next; |
| 1636 | nolist = TRUE; |
| 1637 | } |
| 1638 | t = expand_smacro(tline->next); |
| 1639 | tline->next = NULL; |
| 1640 | free_tlist (origline); |
| 1641 | tline = t; |
| 1642 | tptr = &t; |
| 1643 | tokval.t_type = TOKEN_INVALID; |
| 1644 | evalresult = evaluate (ppscan, tptr, &tokval, NULL, pass, error, NULL); |
| 1645 | free_tlist (tline); |
| 1646 | if (!evalresult) |
| 1647 | return 3; |
| 1648 | if (tokval.t_type) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1649 | error(ERR_WARNING, |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1650 | "trailing garbage after expression ignored"); |
| 1651 | if (!is_simple(evalresult)) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1652 | error(ERR_NONFATAL, |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1653 | "non-constant value given to `%%rep'"); |
| 1654 | return 3; |
| 1655 | } |
H. Peter Anvin | b64535f | 2002-04-30 20:55:37 +0000 | [diff] [blame] | 1656 | tmp_defining = defining; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1657 | defining = nasm_malloc(sizeof(MMacro)); |
| 1658 | defining->name = NULL; /* flags this macro as a %rep block */ |
| 1659 | defining->casesense = 0; |
| 1660 | defining->plus = FALSE; |
| 1661 | defining->nolist = nolist; |
| 1662 | defining->in_progress = reloc_value(evalresult) + 1; |
| 1663 | defining->nparam_min = defining->nparam_max = 0; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1664 | defining->defaults = NULL; |
| 1665 | defining->dlist = NULL; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1666 | defining->expansion = NULL; |
| 1667 | defining->next_active = istk->mstk; |
H. Peter Anvin | b64535f | 2002-04-30 20:55:37 +0000 | [diff] [blame] | 1668 | defining->rep_nest = tmp_defining; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1669 | return 1; |
| 1670 | |
| 1671 | case PP_ENDREP: |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1672 | if (!defining || defining->name) { |
| 1673 | error (ERR_NONFATAL, |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1674 | "`%%endrep': no matching `%%rep'"); |
| 1675 | return 3; |
| 1676 | } |
| 1677 | |
| 1678 | /* |
| 1679 | * Now we have a "macro" defined - although it has no name |
| 1680 | * and we won't be entering it in the hash tables - we must |
| 1681 | * push a macro-end marker for it on to istk->expansion. |
| 1682 | * After that, it will take care of propagating itself (a |
| 1683 | * macro-end marker line for a macro which is really a %rep |
| 1684 | * block will cause the macro to be re-expanded, complete |
| 1685 | * with another macro-end marker to ensure the process |
| 1686 | * continues) until the whole expansion is forcibly removed |
| 1687 | * from istk->expansion by a %exitrep. |
| 1688 | */ |
| 1689 | l = nasm_malloc(sizeof(Line)); |
| 1690 | l->next = istk->expansion; |
| 1691 | l->finishes = defining; |
| 1692 | l->first = NULL; |
| 1693 | istk->expansion = l; |
| 1694 | |
| 1695 | istk->mstk = defining; |
| 1696 | |
| 1697 | list->uplevel (defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); |
H. Peter Anvin | b64535f | 2002-04-30 20:55:37 +0000 | [diff] [blame] | 1698 | tmp_defining = defining; |
| 1699 | defining = defining->rep_nest; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1700 | free_tlist (origline); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1701 | return 1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1702 | |
| 1703 | case PP_EXITREP: |
| 1704 | /* |
| 1705 | * We must search along istk->expansion until we hit a |
| 1706 | * macro-end marker for a macro with no name. Then we set |
| 1707 | * its `in_progress' flag to 0. |
| 1708 | */ |
| 1709 | for (l = istk->expansion; l; l = l->next) |
| 1710 | if (l->finishes && !l->finishes->name) |
| 1711 | break; |
| 1712 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1713 | if (l) |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1714 | l->finishes->in_progress = 0; |
| 1715 | else |
| 1716 | error (ERR_NONFATAL, "`%%exitrep' not within `%%rep' block"); |
| 1717 | free_tlist (origline); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1718 | return 1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1719 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1720 | case PP_DEFINE: |
| 1721 | case PP_IDEFINE: |
| 1722 | tline = tline->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1723 | skip_white_(tline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1724 | if (!tline || (tline->type != TOK_ID && |
| 1725 | (tline->type != TOK_PREPROC_ID || |
| 1726 | tline->text[1] != '$'))) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1727 | error (ERR_NONFATAL, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1728 | "`%%%sdefine' expects a macro identifier", |
| 1729 | (i == PP_IDEFINE ? "i" : "")); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1730 | free_tlist (origline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1731 | return 3; |
| 1732 | } |
| 1733 | mname = tline->text; |
| 1734 | if (tline->type == TOK_ID) { |
| 1735 | p = tline->text; |
| 1736 | smhead = &smacros[hash(mname)]; |
| 1737 | } else { |
| 1738 | ctx = get_ctx (tline->text); |
| 1739 | if (ctx == NULL) |
| 1740 | return 3; |
| 1741 | else { |
| 1742 | p = tline->text+1; |
| 1743 | p += strspn(p, "$"); |
| 1744 | smhead = &ctx->localmac; |
| 1745 | } |
| 1746 | } |
| 1747 | last = tline; |
| 1748 | param_start = tline = tline->next; |
| 1749 | nparam = 0; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1750 | if (tok_is_(tline, "(")) { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1751 | /* |
| 1752 | * This macro has parameters. |
| 1753 | */ |
| 1754 | |
| 1755 | tline = tline->next; |
| 1756 | while (1) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1757 | skip_white_(tline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1758 | if (!tline) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1759 | error (ERR_NONFATAL, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1760 | "parameter identifier expected"); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1761 | free_tlist (origline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1762 | return 3; |
| 1763 | } |
| 1764 | if (tline->type != TOK_ID) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1765 | error (ERR_NONFATAL, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1766 | "`%s': parameter identifier expected", |
| 1767 | tline->text); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1768 | free_tlist (origline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1769 | return 3; |
| 1770 | } |
| 1771 | tline->type = TOK_SMAC_PARAM + nparam++; |
| 1772 | tline = tline->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1773 | skip_white_(tline); |
| 1774 | if (tok_is_(tline, ",")) { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1775 | tline = tline->next; |
| 1776 | continue; |
| 1777 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1778 | if (!tok_is_(tline, ")")) { |
| 1779 | error (ERR_NONFATAL, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1780 | "`)' expected to terminate macro template"); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1781 | free_tlist (origline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1782 | return 3; |
| 1783 | } |
| 1784 | break; |
| 1785 | } |
| 1786 | last = tline; |
| 1787 | tline = tline->next; |
| 1788 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1789 | if (tok_type_(tline, TOK_WHITESPACE)) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1790 | last = tline, tline = tline->next; |
| 1791 | macro_start = NULL; |
| 1792 | last->next = NULL; |
| 1793 | t = tline; |
| 1794 | while (t) { |
| 1795 | if (t->type == TOK_ID) { |
| 1796 | for (tt = param_start; tt; tt = tt->next) |
| 1797 | if (tt->type >= TOK_SMAC_PARAM && |
| 1798 | !strcmp(tt->text, t->text)) |
| 1799 | t->type = tt->type; |
| 1800 | } |
| 1801 | tt = t->next; |
| 1802 | t->next = macro_start; |
| 1803 | macro_start = t; |
| 1804 | t = tt; |
| 1805 | } |
| 1806 | /* |
| 1807 | * Good. We now have a macro name, a parameter count, and a |
| 1808 | * token list (in reverse order) for an expansion. We ought |
| 1809 | * 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] | 1810 | * free_tlist have the rest of the line (which we have |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1811 | * carefully re-terminated after chopping off the expansion |
| 1812 | * from the end). |
| 1813 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1814 | if (smacro_defined (mname, nparam, &smac, i==PP_DEFINE)) { |
| 1815 | if (!smac) { |
| 1816 | error (ERR_WARNING, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1817 | "single-line macro `%s' defined both with and" |
| 1818 | " without parameters", mname); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1819 | free_tlist (origline); |
| 1820 | free_tlist (macro_start); |
| 1821 | return 3; |
| 1822 | } else { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1823 | /* |
| 1824 | * We're redefining, so we have to take over an |
| 1825 | * existing SMacro structure. This means freeing |
| 1826 | * what was already in it. |
| 1827 | */ |
| 1828 | nasm_free (smac->name); |
| 1829 | free_tlist (smac->expansion); |
| 1830 | } |
| 1831 | } else { |
| 1832 | smac = nasm_malloc(sizeof(SMacro)); |
| 1833 | smac->next = *smhead; |
| 1834 | *smhead = smac; |
| 1835 | } |
| 1836 | smac->name = nasm_strdup(p); |
| 1837 | smac->casesense = (i == PP_DEFINE); |
| 1838 | smac->nparam = nparam; |
| 1839 | smac->expansion = macro_start; |
| 1840 | smac->in_progress = FALSE; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1841 | free_tlist (origline); |
| 1842 | return 3; |
| 1843 | |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 1844 | case PP_UNDEF: |
| 1845 | tline = tline->next; |
| 1846 | skip_white_(tline); |
| 1847 | if (!tline || (tline->type != TOK_ID && |
| 1848 | (tline->type != TOK_PREPROC_ID || |
| 1849 | tline->text[1] != '$'))) { |
| 1850 | error (ERR_NONFATAL, |
| 1851 | "`%%undef' expects a macro identifier"); |
| 1852 | free_tlist (origline); |
| 1853 | return 3; |
| 1854 | } |
| 1855 | mname = tline->text; |
| 1856 | if (tline->type == TOK_ID) { |
| 1857 | p = tline->text; |
| 1858 | smhead = &smacros[hash(mname)]; |
| 1859 | } else { |
| 1860 | ctx = get_ctx (tline->text); |
| 1861 | if (ctx == NULL) { |
| 1862 | free_tlist (origline); |
| 1863 | return 3; |
| 1864 | } else { |
| 1865 | p = tline->text+1; |
| 1866 | p += strspn(p, "$"); |
| 1867 | smhead = &ctx->localmac; |
| 1868 | } |
| 1869 | } |
| 1870 | last = tline; |
| 1871 | tline = tline->next; |
| 1872 | last->next = NULL; |
| 1873 | |
| 1874 | if (tline) |
| 1875 | error(ERR_WARNING, |
| 1876 | "trailing garbage after macro name ignored"); |
| 1877 | |
| 1878 | /* |
| 1879 | * We now have a macro name... go hunt for it. |
| 1880 | */ |
H. Peter Anvin | ef7468f | 2002-04-30 20:57:59 +0000 | [diff] [blame] | 1881 | while (smacro_defined (mname, -1, &smac, 1)) { |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 1882 | /* Defined, so we need to find its predecessor and nuke it */ |
| 1883 | SMacro **s; |
| 1884 | for ( s = smhead ; *s && *s != smac ; s = &(*s)->next ); |
| 1885 | if ( *s ) { |
| 1886 | *s = smac->next; |
| 1887 | nasm_free(smac->name); |
| 1888 | free_tlist(smac->expansion); |
| 1889 | nasm_free(smac); |
| 1890 | } |
| 1891 | } |
| 1892 | return 3; |
| 1893 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1894 | case PP_ASSIGN: |
| 1895 | case PP_IASSIGN: |
| 1896 | tline = tline->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1897 | skip_white_(tline); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1898 | if (!tline || (tline->type != TOK_ID && |
| 1899 | (tline->type != TOK_PREPROC_ID || |
| 1900 | tline->text[1] != '$'))) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1901 | error (ERR_NONFATAL, |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1902 | "`%%%sassign' expects a macro identifier", |
| 1903 | (i == PP_IASSIGN ? "i" : "")); |
| 1904 | free_tlist (origline); |
| 1905 | return 3; |
| 1906 | } |
| 1907 | mname = tline->text; |
| 1908 | if (tline->type == TOK_ID) { |
| 1909 | p = tline->text; |
| 1910 | smhead = &smacros[hash(mname)]; |
| 1911 | } else { |
| 1912 | ctx = get_ctx (tline->text); |
| 1913 | if (ctx == NULL) { |
| 1914 | free_tlist (origline); |
| 1915 | return 3; |
| 1916 | } else { |
| 1917 | p = tline->text+1; |
| 1918 | p += strspn(p, "$"); |
| 1919 | smhead = &ctx->localmac; |
| 1920 | } |
| 1921 | } |
| 1922 | last = tline; |
| 1923 | tline = tline->next; |
| 1924 | last->next = NULL; |
| 1925 | |
| 1926 | tline = expand_smacro (tline); |
| 1927 | t = tline; |
| 1928 | tptr = &t; |
| 1929 | tokval.t_type = TOKEN_INVALID; |
| 1930 | evalresult = evaluate (ppscan, tptr, &tokval, NULL, pass, error, NULL); |
| 1931 | free_tlist (tline); |
| 1932 | if (!evalresult) { |
| 1933 | free_tlist (origline); |
| 1934 | return 3; |
| 1935 | } |
| 1936 | |
| 1937 | if (tokval.t_type) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1938 | error(ERR_WARNING, |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1939 | "trailing garbage after expression ignored"); |
| 1940 | |
| 1941 | if (!is_simple(evalresult)) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1942 | error(ERR_NONFATAL, |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1943 | "non-constant value given to `%%%sassign'", |
| 1944 | (i == PP_IASSIGN ? "i" : "")); |
| 1945 | free_tlist (origline); |
| 1946 | return 3; |
| 1947 | } |
| 1948 | |
| 1949 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 1950 | macro_start->next = NULL; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1951 | make_tok_num(macro_start, reloc_value(evalresult)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1952 | macro_start->mac = NULL; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1953 | |
| 1954 | /* |
| 1955 | * We now have a macro name, an implicit parameter count of |
| 1956 | * zero, and a numeric token to use as an expansion. Create |
| 1957 | * and store an SMacro. |
| 1958 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1959 | if (smacro_defined (mname, 0, &smac, i==PP_ASSIGN)) { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1960 | if (!smac) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1961 | error (ERR_WARNING, |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1962 | "single-line macro `%s' defined both with and" |
| 1963 | " without parameters", mname); |
| 1964 | else { |
| 1965 | /* |
| 1966 | * We're redefining, so we have to take over an |
| 1967 | * existing SMacro structure. This means freeing |
| 1968 | * what was already in it. |
| 1969 | */ |
| 1970 | nasm_free (smac->name); |
| 1971 | free_tlist (smac->expansion); |
| 1972 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1973 | } |
| 1974 | else { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1975 | smac = nasm_malloc(sizeof(SMacro)); |
| 1976 | smac->next = *smhead; |
| 1977 | *smhead = smac; |
| 1978 | } |
| 1979 | smac->name = nasm_strdup(p); |
| 1980 | smac->casesense = (i == PP_ASSIGN); |
| 1981 | smac->nparam = 0; |
| 1982 | smac->expansion = macro_start; |
| 1983 | smac->in_progress = FALSE; |
| 1984 | free_tlist (origline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1985 | return 3; |
| 1986 | |
| 1987 | case PP_LINE: |
| 1988 | /* |
| 1989 | * Syntax is `%line nnn[+mmm] [filename]' |
| 1990 | */ |
| 1991 | tline = tline->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1992 | skip_white_(tline); |
| 1993 | if (!tok_type_(tline, TOK_NUMBER)) { |
| 1994 | error (ERR_NONFATAL, "`%%line' expects line number"); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1995 | free_tlist (origline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1996 | return 3; |
| 1997 | } |
| 1998 | k = readnum(tline->text, &j); |
| 1999 | m = 1; |
| 2000 | tline = tline->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2001 | if (tok_is_(tline, "+")) { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2002 | tline = tline->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2003 | if (!tok_type_(tline, TOK_NUMBER)) { |
| 2004 | error (ERR_NONFATAL, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2005 | "`%%line' expects line increment"); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2006 | free_tlist (origline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2007 | return 3; |
| 2008 | } |
| 2009 | m = readnum(tline->text, &j); |
| 2010 | tline = tline->next; |
| 2011 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2012 | skip_white_(tline); |
| 2013 | src_set_linnum(k); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2014 | istk->lineinc = m; |
| 2015 | if (tline) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2016 | nasm_free ( src_set_fname ( detoken(tline) ) ); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2017 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2018 | free_tlist (origline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2019 | return 5; |
| 2020 | |
| 2021 | default: |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2022 | error(ERR_FATAL, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2023 | "preprocessor directive `%s' not yet implemented", |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2024 | directives[i]); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2025 | break; |
| 2026 | } |
| 2027 | return 3; |
| 2028 | } |
| 2029 | |
| 2030 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2031 | * Ensure that a macro parameter contains a condition code and |
| 2032 | * nothing else. Return the condition code index if so, or -1 |
| 2033 | * otherwise. |
| 2034 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2035 | static int find_cc (Token *t) |
| 2036 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2037 | Token *tt; |
| 2038 | int i, j, k, m; |
| 2039 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2040 | skip_white_(t); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2041 | if (t->type != TOK_ID) |
| 2042 | return -1; |
| 2043 | tt = t->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2044 | skip_white_(tt); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2045 | if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ","))) |
| 2046 | return -1; |
| 2047 | |
| 2048 | i = -1; |
| 2049 | j = sizeof(conditions)/sizeof(*conditions); |
| 2050 | while (j-i > 1) { |
| 2051 | k = (j+i) / 2; |
| 2052 | m = nasm_stricmp(t->text, conditions[k]); |
| 2053 | if (m == 0) { |
| 2054 | i = k; |
| 2055 | j = -2; |
| 2056 | break; |
| 2057 | } else if (m < 0) { |
| 2058 | j = k; |
| 2059 | } else |
| 2060 | i = k; |
| 2061 | } |
| 2062 | if (j != -2) |
| 2063 | return -1; |
| 2064 | return i; |
| 2065 | } |
| 2066 | |
| 2067 | /* |
| 2068 | * Expand MMacro-local things: parameter references (%0, %n, %+n, |
| 2069 | * %-n) and MMacro-local identifiers (%%foo). |
| 2070 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2071 | static Token *expand_mmac_params (Token *tline) |
| 2072 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2073 | Token *t, *tt, *ttt, **tail, *thead; |
| 2074 | |
| 2075 | tail = &thead; |
| 2076 | thead = NULL; |
| 2077 | |
| 2078 | while (tline) { |
| 2079 | if (tline->type == TOK_PREPROC_ID && |
| 2080 | (tline->text[1] == '+' || tline->text[1] == '-' || |
| 2081 | tline->text[1] == '%' || |
| 2082 | (tline->text[1] >= '0' && tline->text[1] <= '9'))) { |
| 2083 | char *text = NULL; |
| 2084 | int type = 0, cc; /* type = 0 to placate optimisers */ |
| 2085 | char tmpbuf[30]; |
| 2086 | int n, i; |
| 2087 | MMacro *mac; |
| 2088 | |
| 2089 | t = tline; |
| 2090 | tline = tline->next; |
| 2091 | |
| 2092 | mac = istk->mstk; |
| 2093 | while (mac && !mac->name) /* avoid mistaking %reps for macros */ |
| 2094 | mac = mac->next_active; |
| 2095 | if (!mac) |
| 2096 | error(ERR_NONFATAL, "`%s': not in a macro call", t->text); |
| 2097 | else switch (t->text[1]) { |
| 2098 | /* |
| 2099 | * We have to make a substitution of one of the |
| 2100 | * forms %1, %-1, %+1, %%foo, %0. |
| 2101 | */ |
| 2102 | case '0': |
| 2103 | type = TOK_NUMBER; |
| 2104 | sprintf(tmpbuf, "%d", mac->nparam); |
| 2105 | text = nasm_strdup(tmpbuf); |
| 2106 | break; |
| 2107 | case '%': |
| 2108 | type = TOK_ID; |
| 2109 | sprintf(tmpbuf, "..@%lu.", mac->unique); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2110 | text = nasm_strcat(tmpbuf, t->text+2); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2111 | break; |
| 2112 | case '-': |
| 2113 | n = atoi(t->text+2)-1; |
| 2114 | if (n >= mac->nparam) |
| 2115 | tt = NULL; |
| 2116 | else { |
| 2117 | if (mac->nparam > 1) |
| 2118 | n = (n + mac->rotate) % mac->nparam; |
| 2119 | tt = mac->params[n]; |
| 2120 | } |
| 2121 | cc = find_cc (tt); |
| 2122 | if (cc == -1) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2123 | error (ERR_NONFATAL, |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2124 | "macro parameter %d is not a condition code", |
| 2125 | n+1); |
| 2126 | text = NULL; |
| 2127 | } else { |
| 2128 | type = TOK_ID; |
| 2129 | if (inverse_ccs[cc] == -1) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2130 | error (ERR_NONFATAL, |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2131 | "condition code `%s' is not invertible", |
| 2132 | conditions[cc]); |
| 2133 | text = NULL; |
| 2134 | } else |
| 2135 | text = nasm_strdup(conditions[inverse_ccs[cc]]); |
| 2136 | } |
| 2137 | break; |
| 2138 | case '+': |
| 2139 | n = atoi(t->text+2)-1; |
| 2140 | if (n >= mac->nparam) |
| 2141 | tt = NULL; |
| 2142 | else { |
| 2143 | if (mac->nparam > 1) |
| 2144 | n = (n + mac->rotate) % mac->nparam; |
| 2145 | tt = mac->params[n]; |
| 2146 | } |
| 2147 | cc = find_cc (tt); |
| 2148 | if (cc == -1) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2149 | error (ERR_NONFATAL, |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2150 | "macro parameter %d is not a condition code", |
| 2151 | n+1); |
| 2152 | text = NULL; |
| 2153 | } else { |
| 2154 | type = TOK_ID; |
| 2155 | text = nasm_strdup(conditions[cc]); |
| 2156 | } |
| 2157 | break; |
| 2158 | default: |
| 2159 | n = atoi(t->text+1)-1; |
| 2160 | if (n >= mac->nparam) |
| 2161 | tt = NULL; |
| 2162 | else { |
| 2163 | if (mac->nparam > 1) |
| 2164 | n = (n + mac->rotate) % mac->nparam; |
| 2165 | tt = mac->params[n]; |
| 2166 | } |
| 2167 | if (tt) { |
| 2168 | for (i=0; i<mac->paramlen[n]; i++) { |
| 2169 | ttt = *tail = nasm_malloc(sizeof(Token)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2170 | tail = &ttt->next; |
| 2171 | ttt->type = tt->type; |
| 2172 | ttt->text = nasm_strdup(tt->text); |
| 2173 | ttt->mac = NULL; |
| 2174 | tt = tt->next; |
| 2175 | } |
| 2176 | } |
| 2177 | text = NULL; /* we've done it here */ |
| 2178 | break; |
| 2179 | } |
| 2180 | nasm_free (t->text); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2181 | if (!text) { |
| 2182 | nasm_free (t); |
| 2183 | } else { |
| 2184 | *tail = t; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2185 | tail = &t->next; |
| 2186 | t->type = type; |
| 2187 | t->text = text; |
| 2188 | t->mac = NULL; |
| 2189 | } |
| 2190 | continue; |
| 2191 | } else { |
| 2192 | t = *tail = tline; |
| 2193 | tline = tline->next; |
| 2194 | t->mac = NULL; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2195 | tail = &t->next; |
| 2196 | } |
| 2197 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2198 | *tail = NULL; |
| 2199 | t = thead; |
| 2200 | for (; t && (tt=t->next)!=NULL ; t = t->next) |
| 2201 | switch (t->type) { |
| 2202 | case TOK_WHITESPACE: |
| 2203 | if (tt->type == TOK_WHITESPACE) { |
| 2204 | t->next = tt->next; |
| 2205 | nasm_free(tt->text); |
| 2206 | nasm_free(tt); |
| 2207 | } |
| 2208 | break; |
| 2209 | case TOK_ID: |
| 2210 | if (tt->type == TOK_ID || tt->type == TOK_NUMBER) { |
| 2211 | char *tmp = nasm_strcat(t->text, tt->text); |
| 2212 | nasm_free(t->text); |
| 2213 | t->text = tmp; |
| 2214 | t->next = tt->next; |
| 2215 | nasm_free(tt->text); |
| 2216 | nasm_free(tt); |
| 2217 | } |
| 2218 | break; |
| 2219 | case TOK_NUMBER: |
| 2220 | if (tt->type == TOK_NUMBER) { |
| 2221 | char *tmp = nasm_strcat(t->text, tt->text); |
| 2222 | nasm_free(t->text); |
| 2223 | t->text = tmp; |
| 2224 | t->next = tt->next; |
| 2225 | nasm_free(tt->text); |
| 2226 | nasm_free(tt); |
| 2227 | } |
| 2228 | break; |
| 2229 | } |
| 2230 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2231 | return thead; |
| 2232 | } |
| 2233 | |
| 2234 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2235 | * Expand all single-line macro calls made in the given line. |
| 2236 | * Return the expanded version of the line. The original is deemed |
| 2237 | * to be destroyed in the process. (In reality we'll just move |
| 2238 | * Tokens from input to output a lot of the time, rather than |
| 2239 | * actually bothering to destroy and replicate.) |
| 2240 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2241 | static Token *expand_smacro (Token *tline) |
| 2242 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2243 | Token *t, *tt, *mstart, **tail, *thead; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2244 | SMacro *head = NULL, *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2245 | Token **params; |
| 2246 | int *paramsize; |
| 2247 | int nparam, sparam, brackets; |
| 2248 | char *p; |
| 2249 | |
| 2250 | tail = &thead; |
| 2251 | thead = NULL; |
| 2252 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2253 | while (tline) { /* main token loop */ |
| 2254 | p = NULL; |
| 2255 | if (tline->type == TOK_ID) { |
| 2256 | head = smacros[hash(tline->text)]; |
| 2257 | p = tline->text; |
| 2258 | } else if (tline->type == TOK_PREPROC_ID && tline->text[1] == '$') { |
| 2259 | Context *ctx = get_ctx (tline->text); |
| 2260 | if (ctx) { |
| 2261 | head = ctx->localmac; |
| 2262 | p = tline->text+2; |
| 2263 | p += strspn(p, "$"); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2264 | } |
| 2265 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2266 | if (p) { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2267 | /* |
| 2268 | * We've hit an identifier. As in is_mmacro below, we first |
| 2269 | * check whether the identifier is a single-line macro at |
| 2270 | * all, then think about checking for parameters if |
| 2271 | * necessary. |
| 2272 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2273 | for (m = head; m; m = m->next) |
| 2274 | if (!mstrcmp(m->name, p, m->casesense)) |
| 2275 | break; |
| 2276 | if (m) { |
| 2277 | mstart = tline; |
| 2278 | params = NULL; |
| 2279 | paramsize = NULL; |
| 2280 | if (m->nparam == 0) { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2281 | /* |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2282 | * Simple case: the macro is parameterless. Discard the |
| 2283 | * one token that the macro call took, and push the |
| 2284 | * expansion back on the to-do stack. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2285 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2286 | if (!m->expansion) |
| 2287 | { |
| 2288 | if (!strcmp("__FILE__", m->name)) { |
| 2289 | long num=0; |
| 2290 | src_get(&num, &(tline->text)); |
| 2291 | nasm_quote(&(tline->text)); |
| 2292 | tline->type = TOK_STRING; |
| 2293 | continue; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2294 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2295 | if (!strcmp("__LINE__", m->name)) { |
| 2296 | nasm_free(tline->text); |
| 2297 | make_tok_num(tline, src_get_linnum()); |
| 2298 | continue; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2299 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2300 | t = tline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2301 | tline = tline->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2302 | nasm_free (t->text); |
| 2303 | nasm_free (t); |
| 2304 | continue; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2305 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2306 | } |
| 2307 | else { |
| 2308 | /* |
| 2309 | * Complicated case: at least one macro with this name |
| 2310 | * exists and takes parameters. We must find the |
| 2311 | * parameters in the call, count them, find the SMacro |
| 2312 | * that corresponds to that form of the macro call, and |
| 2313 | * substitute for the parameters when we expand. What a |
| 2314 | * pain. |
| 2315 | */ |
| 2316 | tline = tline->next; |
| 2317 | skip_white_(tline); |
| 2318 | if (!tok_is_(tline, "(")) { |
| 2319 | /* |
| 2320 | * This macro wasn't called with parameters: ignore |
| 2321 | * the call. (Behaviour borrowed from gnu cpp.) |
| 2322 | */ |
| 2323 | tline = mstart; |
| 2324 | m = NULL; |
| 2325 | } |
| 2326 | else { |
| 2327 | int paren = 0; |
| 2328 | int white = 0; |
| 2329 | brackets = 0; |
| 2330 | nparam = 0; |
| 2331 | tline = tline->next; |
| 2332 | sparam = PARAM_DELTA; |
| 2333 | params = nasm_malloc (sparam*sizeof(Token *)); |
| 2334 | params[0] = tline; |
| 2335 | paramsize = nasm_malloc (sparam*sizeof(int)); |
| 2336 | paramsize[0] = 0; |
| 2337 | for (;;tline = tline->next) { /* parameter loop */ |
| 2338 | if (!tline) { |
| 2339 | error(ERR_NONFATAL, |
| 2340 | "macro call expects terminating `)'"); |
| 2341 | break; |
| 2342 | } |
| 2343 | if (tline->type == TOK_WHITESPACE && brackets<=0) { |
| 2344 | if (paramsize[nparam]) |
| 2345 | white++; |
| 2346 | else |
| 2347 | params[nparam] = tline->next; |
| 2348 | continue; /* parameter loop */ |
| 2349 | } |
| 2350 | if (tline->type == TOK_OTHER && tline->text[1]==0) { |
| 2351 | char ch = tline->text[0]; |
| 2352 | if (ch == ',' && !paren && brackets<=0) { |
| 2353 | if (++nparam >= sparam) { |
| 2354 | sparam += PARAM_DELTA; |
| 2355 | params = nasm_realloc (params, |
| 2356 | sparam*sizeof(Token *)); |
| 2357 | paramsize = nasm_realloc (paramsize, |
| 2358 | sparam*sizeof(int)); |
| 2359 | } |
| 2360 | params[nparam] = tline->next; |
| 2361 | paramsize[nparam] = 0; |
| 2362 | white = 0; |
| 2363 | continue; /* parameter loop */ |
| 2364 | } |
H. Peter Anvin | 4836e33 | 2002-04-30 20:56:43 +0000 | [diff] [blame] | 2365 | if (ch == '{' && |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2366 | (brackets>0 || (brackets==0 && |
| 2367 | !paramsize[nparam]))) |
| 2368 | { |
| 2369 | if (!(brackets++)) |
| 2370 | { |
| 2371 | params[nparam] = tline->next; |
| 2372 | continue; /* parameter loop */ |
| 2373 | } |
| 2374 | } |
H. Peter Anvin | 4836e33 | 2002-04-30 20:56:43 +0000 | [diff] [blame] | 2375 | if (ch == '}' && brackets>0) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2376 | if (--brackets == 0) { |
| 2377 | brackets = -1; |
| 2378 | continue; /* parameter loop */ |
| 2379 | } |
| 2380 | if (ch == '(' && !brackets) |
| 2381 | paren++; |
| 2382 | if (ch == ')' && brackets<=0) |
| 2383 | if (--paren < 0) |
| 2384 | break; |
| 2385 | } |
| 2386 | if (brackets<0) { |
| 2387 | brackets = 0; |
| 2388 | error (ERR_NONFATAL, "braces do not " |
| 2389 | "enclose all of macro parameter"); |
| 2390 | } |
| 2391 | paramsize[nparam] += white+1; |
| 2392 | white = 0; |
| 2393 | } /* parameter loop */ |
| 2394 | nparam++; |
| 2395 | while (m && (m->nparam != nparam || |
| 2396 | mstrcmp(m->name, p, m->casesense))) |
| 2397 | m = m->next; |
| 2398 | if (!m) |
| 2399 | error (ERR_WARNING|ERR_WARN_MNP, |
| 2400 | "macro `%s' exists, " |
| 2401 | "but not taking %d parameters", |
| 2402 | mstart->text, nparam); |
| 2403 | } |
| 2404 | } |
| 2405 | if (m && m->in_progress) |
| 2406 | m = NULL; |
| 2407 | if (!m) /* in progess or didn't find '(' or wrong nparam */ |
| 2408 | { |
| 2409 | /* |
| 2410 | * Design question: should we handle !tline, which |
| 2411 | * indicates missing ')' here, or expand those |
| 2412 | * macros anyway, which requires the (t) test a few |
| 2413 | * lines down? |
| 2414 | */ |
| 2415 | nasm_free (params); |
| 2416 | nasm_free (paramsize); |
| 2417 | tline = mstart; |
| 2418 | } |
| 2419 | else { |
| 2420 | /* |
| 2421 | * Expand the macro: we are placed on the last token of the |
| 2422 | * call, so that we can easily split the call from the |
| 2423 | * following tokens. We also start by pushing an SMAC_END |
| 2424 | * token for the cycle removal. |
| 2425 | */ |
| 2426 | t = tline; |
| 2427 | if (t) { |
| 2428 | tline = t->next; |
| 2429 | t->next = NULL; |
| 2430 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2431 | tt = nasm_malloc(sizeof(Token)); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2432 | tt->type = TOK_SMAC_END; |
| 2433 | tt->text = NULL; |
| 2434 | tt->mac = m; |
| 2435 | m->in_progress = TRUE; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2436 | tt->next = tline; |
| 2437 | tline = tt; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2438 | for (t = m->expansion; t; t = t->next) { |
| 2439 | if (t->type >= TOK_SMAC_PARAM) { |
| 2440 | Token *pcopy = tline, **ptail = &pcopy; |
| 2441 | Token *ttt, *pt; |
| 2442 | int i; |
| 2443 | |
| 2444 | ttt = params[t->type - TOK_SMAC_PARAM]; |
| 2445 | for (i=paramsize[t->type-TOK_SMAC_PARAM]; --i>=0;) { |
| 2446 | pt = *ptail = nasm_malloc(sizeof(Token)); |
| 2447 | pt->next = tline; |
| 2448 | ptail = &pt->next; |
| 2449 | pt->text = nasm_strdup(ttt->text); |
| 2450 | pt->type = ttt->type; |
| 2451 | pt->mac = NULL; |
| 2452 | ttt = ttt->next; |
| 2453 | } |
| 2454 | tline = pcopy; |
| 2455 | } else { |
| 2456 | tt = nasm_malloc(sizeof(Token)); |
| 2457 | tt->type = t->type; |
| 2458 | tt->text = nasm_strdup(t->text); |
| 2459 | tt->mac = NULL; |
| 2460 | tt->next = tline; |
| 2461 | tline = tt; |
| 2462 | } |
| 2463 | } |
| 2464 | |
| 2465 | /* |
| 2466 | * Having done that, get rid of the macro call, and clean |
| 2467 | * up the parameters. |
| 2468 | */ |
| 2469 | nasm_free (params); |
| 2470 | nasm_free (paramsize); |
| 2471 | free_tlist (mstart); |
| 2472 | continue; /* main token loop */ |
| 2473 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2474 | } |
| 2475 | } |
| 2476 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2477 | if (tline->type == TOK_SMAC_END) { |
| 2478 | tline->mac->in_progress = FALSE; |
| 2479 | t = tline; |
| 2480 | tline = tline->next; |
| 2481 | nasm_free (t); |
| 2482 | } else { |
| 2483 | t = *tail = tline; |
| 2484 | tline = tline->next; |
| 2485 | t->mac = NULL; |
| 2486 | t->next = NULL; |
| 2487 | tail = &t->next; |
| 2488 | if (t->type == TOK_PREPROC_ID && t->text[1] == '$') { |
| 2489 | Context *c = get_ctx (t->text); |
| 2490 | char *p, *q, buffer[40]; |
| 2491 | |
| 2492 | t->type = TOK_ID; |
| 2493 | if (c) { |
| 2494 | q = t->text+1; |
| 2495 | q += strspn(q, "$"); |
| 2496 | sprintf(buffer, "..@%lu.", c->number); |
| 2497 | p = nasm_strcat (buffer,q); |
| 2498 | nasm_free (t->text); |
| 2499 | t->text = p; |
| 2500 | } |
| 2501 | } |
| 2502 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2503 | } |
| 2504 | |
| 2505 | return thead; |
| 2506 | } |
| 2507 | |
| 2508 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2509 | * Determine whether the given line constitutes a multi-line macro |
| 2510 | * call, and return the MMacro structure called if so. Doesn't have |
| 2511 | * to check for an initial label - that's taken care of in |
| 2512 | * expand_mmacro - but must check numbers of parameters. Guaranteed |
| 2513 | * to be called with tline->type == TOK_ID, so the putative macro |
| 2514 | * name is easy to find. |
| 2515 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2516 | static MMacro *is_mmacro (Token *tline, Token ***params_array) |
| 2517 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2518 | MMacro *head, *m; |
| 2519 | Token **params; |
| 2520 | int nparam; |
| 2521 | |
| 2522 | head = mmacros[hash(tline->text)]; |
| 2523 | |
| 2524 | /* |
| 2525 | * Efficiency: first we see if any macro exists with the given |
| 2526 | * name. If not, we can return NULL immediately. _Then_ we |
| 2527 | * count the parameters, and then we look further along the |
| 2528 | * list if necessary to find the proper MMacro. |
| 2529 | */ |
| 2530 | for (m = head; m; m = m->next) |
| 2531 | if (!mstrcmp(m->name, tline->text, m->casesense)) |
| 2532 | break; |
| 2533 | if (!m) |
| 2534 | return NULL; |
| 2535 | |
| 2536 | /* |
| 2537 | * OK, we have a potential macro. Count and demarcate the |
| 2538 | * parameters. |
| 2539 | */ |
| 2540 | count_mmac_params (tline->next, &nparam, ¶ms); |
| 2541 | |
| 2542 | /* |
| 2543 | * So we know how many parameters we've got. Find the MMacro |
| 2544 | * structure that handles this number. |
| 2545 | */ |
| 2546 | while (m) { |
| 2547 | if (m->nparam_min <= nparam && (m->plus || nparam <= m->nparam_max)) { |
| 2548 | /* |
| 2549 | * This one is right. Just check if cycle removal |
| 2550 | * prohibits us using it before we actually celebrate... |
| 2551 | */ |
| 2552 | if (m->in_progress) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2553 | #if 0 |
| 2554 | error (ERR_NONFATAL, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2555 | "self-reference in multi-line macro `%s'", |
| 2556 | m->name); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2557 | #endif |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2558 | nasm_free (params); |
| 2559 | return NULL; |
| 2560 | } |
| 2561 | /* |
| 2562 | * It's right, and we can use it. Add its default |
| 2563 | * parameters to the end of our list if necessary. |
| 2564 | */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2565 | if (m->defaults && nparam < m->nparam_min + m->ndefs) { |
| 2566 | params = nasm_realloc (params, ((m->nparam_min+m->ndefs+1) * |
| 2567 | sizeof(*params))); |
| 2568 | while (nparam < m->nparam_min + m->ndefs) { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2569 | params[nparam] = m->defaults[nparam - m->nparam_min]; |
| 2570 | nparam++; |
| 2571 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2572 | } |
| 2573 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2574 | * If we've gone over the maximum parameter count (and |
| 2575 | * we're in Plus mode), ignore parameters beyond |
| 2576 | * nparam_max. |
| 2577 | */ |
| 2578 | if (m->plus && nparam > m->nparam_max) |
| 2579 | nparam = m->nparam_max; |
| 2580 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2581 | * Then terminate the parameter list, and leave. |
| 2582 | */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2583 | if (!params) { /* need this special case */ |
| 2584 | params = nasm_malloc(sizeof(*params)); |
| 2585 | nparam = 0; |
| 2586 | } |
| 2587 | params[nparam] = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2588 | *params_array = params; |
| 2589 | return m; |
| 2590 | } |
| 2591 | /* |
| 2592 | * This one wasn't right: look for the next one with the |
| 2593 | * same name. |
| 2594 | */ |
| 2595 | for (m = m->next; m; m = m->next) |
| 2596 | if (!mstrcmp(m->name, tline->text, m->casesense)) |
| 2597 | break; |
| 2598 | } |
| 2599 | |
| 2600 | /* |
| 2601 | * After all that, we didn't find one with the right number of |
| 2602 | * parameters. Issue a warning, and fail to expand the macro. |
| 2603 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2604 | error (ERR_WARNING|ERR_WARN_MNP, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2605 | "macro `%s' exists, but not taking %d parameters", |
| 2606 | tline->text, nparam); |
| 2607 | nasm_free (params); |
| 2608 | return NULL; |
| 2609 | } |
| 2610 | |
| 2611 | /* |
| 2612 | * Expand the multi-line macro call made by the given line, if |
| 2613 | * 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] | 2614 | * istk->expansion and return 1. Otherwise return 0. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2615 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2616 | static int expand_mmacro (Token *tline) |
| 2617 | { |
| 2618 | Token *startline = tline; |
| 2619 | Token *label = NULL; |
| 2620 | int dont_prepend = 0; |
| 2621 | Token **params, *t, *tt; |
| 2622 | MMacro *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2623 | Line *l, *ll; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2624 | int i, nparam, *paramlen; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2625 | |
| 2626 | t = tline; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2627 | skip_white_(t); |
| 2628 | if (!tok_type_(t, TOK_ID)) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2629 | return 0; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2630 | m = is_mmacro (t, ¶ms); |
| 2631 | if (!m) { |
| 2632 | Token *last; |
| 2633 | /* |
| 2634 | * We have an id which isn't a macro call. We'll assume |
| 2635 | * it might be a label; we'll also check to see if a |
| 2636 | * colon follows it. Then, if there's another id after |
| 2637 | * that lot, we'll check it again for macro-hood. |
| 2638 | */ |
| 2639 | label = last = t; |
| 2640 | t = t->next; |
| 2641 | if (tok_type_(t, TOK_WHITESPACE)) |
| 2642 | last = t, t = t->next; |
| 2643 | if (tok_is_(t, ":")) { |
| 2644 | dont_prepend = 1; |
| 2645 | last = t, t = t->next; |
| 2646 | if (tok_type_(t, TOK_WHITESPACE)) |
| 2647 | last = t, t = t->next; |
| 2648 | } |
| 2649 | if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, ¶ms)) == NULL) |
| 2650 | return 0; |
| 2651 | last->next = NULL; |
| 2652 | tline = t; |
| 2653 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2654 | |
| 2655 | /* |
| 2656 | * Fix up the parameters: this involves stripping leading and |
| 2657 | * trailing whitespace, then stripping braces if they are |
| 2658 | * present. |
| 2659 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2660 | for (nparam = 0; params[nparam]; nparam++) |
| 2661 | ; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2662 | paramlen = nparam ? nasm_malloc(nparam*sizeof(*paramlen)) : NULL; |
| 2663 | |
| 2664 | for (i = 0; params[i]; i++) { |
| 2665 | int brace = FALSE; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 2666 | int comma = (!m->plus || i < nparam-1); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2667 | |
| 2668 | t = params[i]; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2669 | skip_white_(t); |
| 2670 | if (tok_is_(t, "{")) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2671 | t = t->next, brace = TRUE, comma = FALSE; |
| 2672 | params[i] = t; |
| 2673 | paramlen[i] = 0; |
| 2674 | while (t) { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2675 | if (comma && t->type == TOK_OTHER && !strcmp(t->text, ",")) |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 2676 | break; /* ... because we have hit a comma */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2677 | if (comma && t->type == TOK_WHITESPACE && tok_is_(t->next, ",")) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2678 | break; /* ... or a space then a comma */ |
| 2679 | if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}")) |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 2680 | break; /* ... or a brace */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2681 | t = t->next; |
| 2682 | paramlen[i]++; |
| 2683 | } |
| 2684 | } |
| 2685 | |
| 2686 | /* |
| 2687 | * OK, we have a MMacro structure together with a set of |
| 2688 | * parameters. We must now go through the expansion and push |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2689 | * copies of each Line on to istk->expansion. Substitution of |
| 2690 | * parameter tokens and macro-local tokens doesn't get done |
| 2691 | * until the single-line macro substitution process; this is |
| 2692 | * because delaying them allows us to change the semantics |
| 2693 | * later through %rotate. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2694 | * |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2695 | * First, push an end marker on to istk->expansion, mark this |
| 2696 | * macro as in progress, and set up its invocation-specific |
| 2697 | * variables. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2698 | */ |
| 2699 | ll = nasm_malloc(sizeof(Line)); |
| 2700 | ll->next = istk->expansion; |
| 2701 | ll->finishes = m; |
| 2702 | ll->first = NULL; |
| 2703 | istk->expansion = ll; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2704 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2705 | m->in_progress = TRUE; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2706 | m->params = params; |
| 2707 | m->iline = tline; |
| 2708 | m->nparam = nparam; |
| 2709 | m->rotate = 0; |
| 2710 | m->paramlen = paramlen; |
| 2711 | m->unique = unique++; |
| 2712 | |
| 2713 | m->next_active = istk->mstk; |
| 2714 | istk->mstk = m; |
| 2715 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2716 | for (l = m->expansion; l; l = l->next) { |
| 2717 | Token **tail; |
| 2718 | |
| 2719 | ll = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2720 | ll->finishes = NULL; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2721 | ll->next = istk->expansion; |
| 2722 | istk->expansion = ll; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2723 | tail = &ll->first; |
| 2724 | |
| 2725 | for (t = l->first; t; t = t->next) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2726 | Token *x = t; |
| 2727 | if (t->type == TOK_PREPROC_ID && |
| 2728 | t->text[1]=='0' && t->text[2]=='0') |
| 2729 | { |
| 2730 | dont_prepend = -1; |
| 2731 | x = label; |
| 2732 | if (!x) |
| 2733 | continue; |
| 2734 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2735 | tt = *tail = nasm_malloc(sizeof(Token)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2736 | tail = &tt->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2737 | tt->type = x->type; |
| 2738 | tt->text = nasm_strdup(x->text); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2739 | tt->mac = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2740 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2741 | *tail = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2742 | } |
| 2743 | |
| 2744 | /* |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2745 | * If we had a label, push it on as the first line of |
| 2746 | * the macro expansion. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2747 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2748 | if (label) |
| 2749 | if (dont_prepend<0) |
| 2750 | free_tlist(startline); |
| 2751 | else { |
| 2752 | ll = nasm_malloc(sizeof(Line)); |
| 2753 | ll->finishes = NULL; |
| 2754 | ll->next = istk->expansion; |
| 2755 | istk->expansion = ll; |
| 2756 | ll->first = startline; |
| 2757 | if (!dont_prepend) { |
| 2758 | while (label->next) |
| 2759 | label = label->next; |
| 2760 | label->next = tt = nasm_malloc(sizeof(Token)); |
| 2761 | tt->next = NULL; |
| 2762 | tt->mac = NULL; |
| 2763 | tt->type = TOK_OTHER; |
| 2764 | tt->text = nasm_strdup(":"); |
| 2765 | } |
H. Peter Anvin | 87bc619 | 2002-04-30 20:53:16 +0000 | [diff] [blame] | 2766 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2767 | |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 2768 | list->uplevel (m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); |
| 2769 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2770 | return 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2771 | } |
| 2772 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2773 | 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] | 2774 | ListGen *listgen) |
| 2775 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2776 | int h; |
| 2777 | |
| 2778 | error = errfunc; |
| 2779 | cstk = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2780 | istk = nasm_malloc(sizeof(Include)); |
| 2781 | istk->next = NULL; |
| 2782 | istk->conds = NULL; |
| 2783 | istk->expansion = NULL; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2784 | istk->mstk = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2785 | istk->fp = fopen(file, "r"); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2786 | istk->fname = NULL; |
| 2787 | src_set_fname(nasm_strdup(file)); |
| 2788 | src_set_linnum(0); |
| 2789 | istk->lineinc = 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2790 | if (!istk->fp) |
| 2791 | error (ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'", file); |
| 2792 | defining = NULL; |
| 2793 | for (h=0; h<NHASH; h++) { |
| 2794 | mmacros[h] = NULL; |
| 2795 | smacros[h] = NULL; |
| 2796 | } |
| 2797 | unique = 0; |
| 2798 | stdmacpos = stdmac; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2799 | any_extrastdmac = (extrastdmac != NULL); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 2800 | list = listgen; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2801 | evaluate = eval; |
| 2802 | pass = apass; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2803 | } |
| 2804 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2805 | static char *pp_getline (void) |
| 2806 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2807 | char *line; |
| 2808 | Token *tline; |
| 2809 | int ret; |
| 2810 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2811 | while (1) { |
| 2812 | /* |
| 2813 | * Fetch a tokenised line, either from the macro-expansion |
| 2814 | * buffer or from the input file. |
| 2815 | */ |
| 2816 | tline = NULL; |
| 2817 | while (istk->expansion && istk->expansion->finishes) { |
| 2818 | Line *l = istk->expansion; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2819 | if (!l->finishes->name && l->finishes->in_progress > 1) { |
| 2820 | Line *ll; |
| 2821 | |
| 2822 | /* |
| 2823 | * This is a macro-end marker for a macro with no |
| 2824 | * name, which means it's not really a macro at all |
| 2825 | * but a %rep block, and the `in_progress' field is |
| 2826 | * more than 1, meaning that we still need to |
| 2827 | * repeat. (1 means the natural last repetition; 0 |
| 2828 | * means termination by %exitrep.) We have |
| 2829 | * therefore expanded up to the %endrep, and must |
| 2830 | * push the whole block on to the expansion buffer |
| 2831 | * again. We don't bother to remove the macro-end |
| 2832 | * marker: we'd only have to generate another one |
| 2833 | * if we did. |
| 2834 | */ |
| 2835 | l->finishes->in_progress--; |
| 2836 | for (l = l->finishes->expansion; l; l = l->next) { |
| 2837 | Token *t, *tt, **tail; |
| 2838 | |
| 2839 | ll = nasm_malloc(sizeof(Line)); |
| 2840 | ll->next = istk->expansion; |
| 2841 | ll->finishes = NULL; |
| 2842 | ll->first = NULL; |
| 2843 | tail = &ll->first; |
| 2844 | |
| 2845 | for (t = l->first; t; t = t->next) { |
| 2846 | if (t->text) { |
| 2847 | tt = *tail = nasm_malloc(sizeof(Token)); |
| 2848 | tt->next = NULL; |
| 2849 | tail = &tt->next; |
| 2850 | tt->type = t->type; |
| 2851 | tt->text = nasm_strdup(t->text); |
| 2852 | tt->mac = NULL; |
| 2853 | } |
| 2854 | } |
| 2855 | |
| 2856 | istk->expansion = ll; |
| 2857 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2858 | } else { |
H. Peter Anvin | 87bc619 | 2002-04-30 20:53:16 +0000 | [diff] [blame] | 2859 | /* |
| 2860 | * Check whether a `%rep' was started and not ended |
| 2861 | * within this macro expansion. This can happen and |
| 2862 | * should be detected. It's a fatal error because |
| 2863 | * I'm too confused to work out how to recover |
| 2864 | * sensibly from it. |
| 2865 | */ |
| 2866 | if (defining) { |
| 2867 | if (defining->name) |
| 2868 | error (ERR_PANIC, |
| 2869 | "defining with name in expansion"); |
H. Peter Anvin | b64535f | 2002-04-30 20:55:37 +0000 | [diff] [blame] | 2870 | else if (istk->mstk->name) |
H. Peter Anvin | 87bc619 | 2002-04-30 20:53:16 +0000 | [diff] [blame] | 2871 | error (ERR_FATAL, "`%%rep' without `%%endrep' within" |
| 2872 | " expansion of macro `%s'", istk->mstk->name); |
| 2873 | } |
| 2874 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2875 | /* |
| 2876 | * FIXME: investigate the relationship at this point between |
| 2877 | * istk->mstk and l->finishes |
| 2878 | */ |
| 2879 | { |
| 2880 | MMacro *m = istk->mstk; |
| 2881 | istk->mstk = m->next_active; |
| 2882 | if (m->name) { |
| 2883 | /* |
| 2884 | * This was a real macro call, not a %rep, and |
| 2885 | * therefore the parameter information needs to |
| 2886 | * be freed. |
| 2887 | */ |
| 2888 | nasm_free(m->params); |
| 2889 | free_tlist(m->iline); |
| 2890 | nasm_free(m->paramlen); |
| 2891 | l->finishes->in_progress = FALSE; |
| 2892 | } |
| 2893 | else |
| 2894 | free_mmacro(m); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2895 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2896 | istk->expansion = l->next; |
| 2897 | nasm_free (l); |
| 2898 | list->downlevel (LIST_MACRO); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2899 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2900 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2901 | while (1) { /* until we get a line we can use */ |
| 2902 | |
| 2903 | if (istk->expansion) { /* from a macro expansion */ |
| 2904 | char *p; |
| 2905 | Line *l = istk->expansion; |
| 2906 | tline = l->first; |
| 2907 | istk->expansion = l->next; |
| 2908 | nasm_free (l); |
| 2909 | p = detoken(tline); |
| 2910 | list->line (LIST_MACRO, p); |
| 2911 | nasm_free(p); |
| 2912 | break; |
| 2913 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2914 | line = read_line(); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2915 | if (line) { /* from the current input file */ |
| 2916 | line = prepreproc(line); |
| 2917 | tline = tokenise(line); |
| 2918 | nasm_free (line); |
| 2919 | break; |
| 2920 | } |
| 2921 | /* |
| 2922 | * The current file has ended; work down the istk |
| 2923 | */ |
| 2924 | { |
| 2925 | Include *i = istk; |
| 2926 | fclose(i->fp); |
| 2927 | if (i->conds) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2928 | error(ERR_FATAL, "expected `%%endif' before end of file"); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2929 | istk = i->next; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 2930 | list->downlevel (LIST_INCLUDE); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2931 | src_set_linnum(i->lineno); |
| 2932 | nasm_free ( src_set_fname(i->fname) ); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2933 | nasm_free (i); |
| 2934 | if (!istk) |
| 2935 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2936 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2937 | } |
| 2938 | |
| 2939 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2940 | * We must expand MMacro parameters and MMacro-local labels |
| 2941 | * _before_ we plunge into directive processing, to cope |
| 2942 | * with things like `%define something %1' such as STRUC |
| 2943 | * uses. Unless we're _defining_ a MMacro, in which case |
| 2944 | * those tokens should be left alone to go into the |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2945 | * definition; and unless we're in a non-emitting |
| 2946 | * condition, in which case we don't want to meddle with |
| 2947 | * anything. |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2948 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2949 | if (!defining && !(istk->conds && !emitting(istk->conds->state))) |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2950 | tline = expand_mmac_params(tline); |
| 2951 | |
| 2952 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2953 | * Check the line to see if it's a preprocessor directive. |
| 2954 | */ |
| 2955 | ret = do_directive(tline); |
| 2956 | if (ret & 1) { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2957 | continue; |
| 2958 | } else if (defining) { |
| 2959 | /* |
| 2960 | * We're defining a multi-line macro. We emit nothing |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2961 | * at all, and just |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2962 | * shove the tokenised line on to the macro definition. |
| 2963 | */ |
| 2964 | Line *l = nasm_malloc(sizeof(Line)); |
| 2965 | l->next = defining->expansion; |
| 2966 | l->first = tline; |
| 2967 | l->finishes = FALSE; |
| 2968 | defining->expansion = l; |
| 2969 | continue; |
| 2970 | } else if (istk->conds && !emitting(istk->conds->state)) { |
| 2971 | /* |
| 2972 | * We're in a non-emitting branch of a condition block. |
| 2973 | * Emit nothing at all, not even a blank line: when we |
| 2974 | * emerge from the condition we'll give a line-number |
| 2975 | * directive so we keep our place correctly. |
| 2976 | */ |
| 2977 | free_tlist(tline); |
| 2978 | continue; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2979 | } else if (istk->mstk && !istk->mstk->in_progress) { |
| 2980 | /* |
| 2981 | * We're in a %rep block which has been terminated, so |
| 2982 | * we're walking through to the %endrep without |
| 2983 | * emitting anything. Emit nothing at all, not even a |
| 2984 | * blank line: when we emerge from the %rep block we'll |
| 2985 | * give a line-number directive so we keep our place |
| 2986 | * correctly. |
| 2987 | */ |
| 2988 | free_tlist(tline); |
| 2989 | continue; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2990 | } else { |
| 2991 | tline = expand_smacro(tline); |
| 2992 | ret = expand_mmacro(tline); |
| 2993 | if (!ret) { |
| 2994 | /* |
| 2995 | * De-tokenise the line again, and emit it. |
| 2996 | */ |
| 2997 | line = detoken(tline); |
| 2998 | free_tlist (tline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2999 | break; |
| 3000 | } else { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3001 | continue; /* expand_mmacro calls free_tlist */ |
| 3002 | } |
| 3003 | } |
| 3004 | } |
| 3005 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3006 | return line; |
| 3007 | } |
| 3008 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3009 | static void pp_cleanup (void) |
| 3010 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3011 | int h; |
| 3012 | |
| 3013 | if (defining) { |
| 3014 | error (ERR_NONFATAL, "end of file while still defining macro `%s'", |
| 3015 | defining->name); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3016 | free_mmacro (defining); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3017 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3018 | while (cstk) |
| 3019 | ctx_pop(); |
| 3020 | for (h=0; h<NHASH; h++) { |
| 3021 | while (mmacros[h]) { |
| 3022 | MMacro *m = mmacros[h]; |
| 3023 | mmacros[h] = mmacros[h]->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3024 | free_mmacro(m); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3025 | } |
| 3026 | while (smacros[h]) { |
| 3027 | SMacro *s = smacros[h]; |
| 3028 | smacros[h] = smacros[h]->next; |
| 3029 | nasm_free (s->name); |
| 3030 | free_tlist (s->expansion); |
| 3031 | nasm_free (s); |
| 3032 | } |
| 3033 | } |
| 3034 | while (istk) { |
| 3035 | Include *i = istk; |
| 3036 | istk = istk->next; |
| 3037 | fclose(i->fp); |
| 3038 | nasm_free (i->fname); |
| 3039 | nasm_free (i); |
| 3040 | } |
| 3041 | while (cstk) |
| 3042 | ctx_pop(); |
| 3043 | } |
| 3044 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3045 | void pp_include_path (char *path) |
| 3046 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 3047 | IncPath *i; |
| 3048 | |
| 3049 | i = nasm_malloc(sizeof(IncPath)); |
| 3050 | i->path = nasm_strdup(path); |
| 3051 | i->next = ipath; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 3052 | ipath = i; |
| 3053 | } |
| 3054 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3055 | void pp_pre_include (char *fname) |
| 3056 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 3057 | Token *inc, *space, *name; |
| 3058 | Line *l; |
| 3059 | |
| 3060 | inc = nasm_malloc(sizeof(Token)); |
| 3061 | inc->next = space = nasm_malloc(sizeof(Token)); |
| 3062 | space->next = name = nasm_malloc(sizeof(Token)); |
| 3063 | name->next = NULL; |
| 3064 | |
| 3065 | inc->type = TOK_PREPROC_ID; |
| 3066 | inc->text = nasm_strdup("%include"); |
| 3067 | space->type = TOK_WHITESPACE; |
| 3068 | space->text = nasm_strdup(" "); |
| 3069 | name->type = TOK_INTERNAL_STRING; |
| 3070 | name->text = nasm_strdup(fname); |
| 3071 | |
| 3072 | inc->mac = space->mac = name->mac = NULL; |
| 3073 | |
| 3074 | l = nasm_malloc(sizeof(Line)); |
| 3075 | l->next = predef; |
| 3076 | l->first = inc; |
| 3077 | l->finishes = FALSE; |
| 3078 | predef = l; |
| 3079 | } |
| 3080 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3081 | void pp_pre_define (char *definition) |
| 3082 | { |
| 3083 | Token *def, *space; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 3084 | Line *l; |
| 3085 | char *equals; |
| 3086 | |
| 3087 | equals = strchr(definition, '='); |
| 3088 | |
| 3089 | def = nasm_malloc(sizeof(Token)); |
| 3090 | def->next = space = nasm_malloc(sizeof(Token)); |
| 3091 | if (equals) |
| 3092 | *equals = ' '; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3093 | space->next = tokenise(definition); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 3094 | if (equals) |
| 3095 | *equals = '='; |
| 3096 | |
| 3097 | def->type = TOK_PREPROC_ID; |
| 3098 | def->text = nasm_strdup("%define"); |
| 3099 | space->type = TOK_WHITESPACE; |
| 3100 | space->text = nasm_strdup(" "); |
| 3101 | |
| 3102 | def->mac = space->mac = NULL; |
| 3103 | |
| 3104 | l = nasm_malloc(sizeof(Line)); |
| 3105 | l->next = predef; |
| 3106 | l->first = def; |
| 3107 | l->finishes = FALSE; |
| 3108 | predef = l; |
| 3109 | } |
| 3110 | |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 3111 | void pp_pre_undefine (char *definition) |
| 3112 | { |
| 3113 | Token *def, *space; |
| 3114 | Line *l; |
| 3115 | |
| 3116 | def = nasm_malloc(sizeof(Token)); |
| 3117 | def->next = space = nasm_malloc(sizeof(Token)); |
| 3118 | space->next = tokenise(definition); |
| 3119 | |
| 3120 | def->type = TOK_PREPROC_ID; |
| 3121 | def->text = nasm_strdup("%undef"); |
| 3122 | space->type = TOK_WHITESPACE; |
| 3123 | space->text = nasm_strdup(" "); |
| 3124 | |
| 3125 | def->mac = space->mac = NULL; |
| 3126 | |
| 3127 | l = nasm_malloc(sizeof(Line)); |
| 3128 | l->next = predef; |
| 3129 | l->first = def; |
| 3130 | l->finishes = FALSE; |
| 3131 | predef = l; |
| 3132 | } |
| 3133 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3134 | void pp_extra_stdmac (char **macros) |
| 3135 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3136 | extrastdmac = macros; |
| 3137 | } |
| 3138 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3139 | static void make_tok_num(Token *tok, long val) |
| 3140 | { |
| 3141 | char numbuf[20]; |
| 3142 | sprintf(numbuf, "%ld", val); |
| 3143 | tok->text = nasm_strdup(numbuf); |
| 3144 | tok->type = TOK_NUMBER; |
| 3145 | } |
| 3146 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3147 | Preproc nasmpp = { |
| 3148 | pp_reset, |
| 3149 | pp_getline, |
| 3150 | pp_cleanup |
| 3151 | }; |