H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 1 | /* -*- mode: c; c-file-style: "bsd" -*- */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2 | /* preproc.c macro preprocessor for the Netwide Assembler |
| 3 | * |
| 4 | * The Netwide Assembler is copyright (C) 1996 Simon Tatham and |
| 5 | * Julian Hall. All rights reserved. The software is |
| 6 | * redistributable under the licence given in the file "Licence" |
| 7 | * distributed in the NASM archive. |
| 8 | * |
| 9 | * initial version 18/iii/97 by Simon Tatham |
| 10 | */ |
| 11 | |
H. Peter Anvin | 4836e33 | 2002-04-30 20:56:43 +0000 | [diff] [blame] | 12 | /* Typical flow of text through preproc |
| 13 | * |
| 14 | * pp_getline gets tokenised lines, either |
| 15 | * |
| 16 | * from a macro expansion |
| 17 | * |
| 18 | * or |
| 19 | * { |
| 20 | * read_line gets raw text from stdmacpos, or predef, or current input file |
| 21 | * tokenise converts to tokens |
| 22 | * } |
| 23 | * |
| 24 | * expand_mmac_params is used to expand %1 etc., unless a macro is being |
| 25 | * defined or a false conditional is being processed |
| 26 | * (%0, %1, %+1, %-1, %%foo |
| 27 | * |
| 28 | * do_directive checks for directives |
| 29 | * |
| 30 | * expand_smacro is used to expand single line macros |
| 31 | * |
| 32 | * expand_mmacro is used to expand multi-line macros |
| 33 | * |
| 34 | * detoken is used to convert the line back to text |
| 35 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 36 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 37 | #include <stdio.h> |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 38 | #include <stdarg.h> |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 39 | #include <stdlib.h> |
| 40 | #include <stddef.h> |
| 41 | #include <string.h> |
| 42 | #include <ctype.h> |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 43 | #include <limits.h> |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 44 | |
| 45 | #include "nasm.h" |
| 46 | #include "nasmlib.h" |
| 47 | |
| 48 | typedef struct SMacro SMacro; |
| 49 | typedef struct MMacro MMacro; |
| 50 | typedef struct Context Context; |
| 51 | typedef struct Token Token; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 52 | typedef struct Blocks Blocks; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 53 | typedef struct Line Line; |
| 54 | typedef struct Include Include; |
| 55 | typedef struct Cond Cond; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 56 | typedef struct IncPath IncPath; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 57 | |
| 58 | /* |
| 59 | * Store the definition of a single-line macro. |
| 60 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 61 | struct SMacro |
| 62 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 63 | SMacro *next; |
| 64 | char *name; |
| 65 | int casesense; |
| 66 | int nparam; |
| 67 | int in_progress; |
| 68 | Token *expansion; |
| 69 | }; |
| 70 | |
| 71 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 72 | * Store the definition of a multi-line macro. This is also used to |
| 73 | * store the interiors of `%rep...%endrep' blocks, which are |
| 74 | * effectively self-re-invoking multi-line macros which simply |
| 75 | * don't have a name or bother to appear in the hash tables. %rep |
| 76 | * blocks are signified by having a NULL `name' field. |
| 77 | * |
| 78 | * In a MMacro describing a `%rep' block, the `in_progress' field |
| 79 | * isn't merely boolean, but gives the number of repeats left to |
| 80 | * run. |
| 81 | * |
| 82 | * The `next' field is used for storing MMacros in hash tables; the |
| 83 | * `next_active' field is for stacking them on istk entries. |
| 84 | * |
| 85 | * When a MMacro is being expanded, `params', `iline', `nparam', |
| 86 | * `paramlen', `rotate' and `unique' are local to the invocation. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 87 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 88 | struct MMacro |
| 89 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 90 | MMacro *next; |
| 91 | char *name; |
| 92 | int casesense; |
| 93 | int nparam_min, nparam_max; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 94 | int plus; /* is the last parameter greedy? */ |
| 95 | int nolist; /* is this macro listing-inhibited? */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 96 | int in_progress; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 97 | Token *dlist; /* All defaults as one list */ |
| 98 | Token **defaults; /* Parameter default pointers */ |
| 99 | int ndefs; /* number of default parameters */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 100 | Line *expansion; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 101 | |
| 102 | MMacro *next_active; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 103 | MMacro *rep_nest; /* used for nesting %rep */ |
| 104 | Token **params; /* actual parameters */ |
| 105 | Token *iline; /* invocation line */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 106 | int nparam, rotate, *paramlen; |
| 107 | unsigned long unique; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 108 | int lineno; /* Current line number on expansion */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | /* |
| 112 | * The context stack is composed of a linked list of these. |
| 113 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 114 | struct Context |
| 115 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 116 | Context *next; |
| 117 | SMacro *localmac; |
| 118 | char *name; |
| 119 | unsigned long number; |
| 120 | }; |
| 121 | |
| 122 | /* |
| 123 | * This is the internal form which we break input lines up into. |
| 124 | * Typically stored in linked lists. |
| 125 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 126 | * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not |
| 127 | * necessarily used as-is, but is intended to denote the number of |
| 128 | * the substituted parameter. So in the definition |
| 129 | * |
| 130 | * %define a(x,y) ( (x) & ~(y) ) |
| 131 | * |
| 132 | * the token representing `x' will have its type changed to |
| 133 | * TOK_SMAC_PARAM, but the one representing `y' will be |
| 134 | * TOK_SMAC_PARAM+1. |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 135 | * |
| 136 | * TOK_INTERNAL_STRING is a dirty hack: it's a single string token |
| 137 | * which doesn't need quotes around it. Used in the pre-include |
| 138 | * mechanism as an alternative to trying to find a sensible type of |
| 139 | * quote to use on the filename we were passed. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 140 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 141 | struct Token |
| 142 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 143 | Token *next; |
| 144 | char *text; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 145 | SMacro *mac; /* associated macro for TOK_SMAC_END */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 146 | int type; |
| 147 | }; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 148 | enum |
| 149 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 150 | 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] | 151 | TOK_NUMBER, TOK_SMAC_END, TOK_OTHER, TOK_SMAC_PARAM, |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 152 | TOK_INTERNAL_STRING |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | /* |
| 156 | * Multi-line macro definitions are stored as a linked list of |
| 157 | * these, which is essentially a container to allow several linked |
| 158 | * lists of Tokens. |
| 159 | * |
| 160 | * Note that in this module, linked lists are treated as stacks |
| 161 | * wherever possible. For this reason, Lines are _pushed_ on to the |
| 162 | * `expansion' field in MMacro structures, so that the linked list, |
| 163 | * if walked, would give the macro lines in reverse order; this |
| 164 | * means that we can walk the list when expanding a macro, and thus |
| 165 | * push the lines on to the `expansion' field in _istk_ in reverse |
| 166 | * order (so that when popped back off they are in the right |
| 167 | * order). It may seem cockeyed, and it relies on my design having |
| 168 | * an even number of steps in, but it works... |
| 169 | * |
| 170 | * Some of these structures, rather than being actual lines, are |
| 171 | * markers delimiting the end of the expansion of a given macro. |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 172 | * This is for use in the cycle-tracking and %rep-handling code. |
| 173 | * Such structures have `finishes' non-NULL, and `first' NULL. All |
| 174 | * others have `finishes' NULL, but `first' may still be NULL if |
| 175 | * the line is blank. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 176 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 177 | struct Line |
| 178 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 179 | Line *next; |
| 180 | MMacro *finishes; |
| 181 | Token *first; |
| 182 | }; |
| 183 | |
| 184 | /* |
| 185 | * To handle an arbitrary level of file inclusion, we maintain a |
| 186 | * stack (ie linked list) of these things. |
| 187 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 188 | struct Include |
| 189 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 190 | Include *next; |
| 191 | FILE *fp; |
| 192 | Cond *conds; |
| 193 | Line *expansion; |
| 194 | char *fname; |
| 195 | int lineno, lineinc; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 196 | MMacro *mstk; /* stack of active macros/reps */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 197 | }; |
| 198 | |
| 199 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 200 | * Include search path. This is simply a list of strings which get |
| 201 | * prepended, in turn, to the name of an include file, in an |
| 202 | * attempt to find the file if it's not in the current directory. |
| 203 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 204 | struct IncPath |
| 205 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 206 | IncPath *next; |
| 207 | char *path; |
| 208 | }; |
| 209 | |
| 210 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 211 | * Conditional assembly: we maintain a separate stack of these for |
| 212 | * each level of file inclusion. (The only reason we keep the |
| 213 | * stacks separate is to ensure that a stray `%endif' in a file |
| 214 | * included from within the true branch of a `%if' won't terminate |
| 215 | * it and cause confusion: instead, rightly, it'll cause an error.) |
| 216 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 217 | struct Cond |
| 218 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 219 | Cond *next; |
| 220 | int state; |
| 221 | }; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 222 | enum |
| 223 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 224 | /* |
| 225 | * These states are for use just after %if or %elif: IF_TRUE |
| 226 | * means the condition has evaluated to truth so we are |
| 227 | * currently emitting, whereas IF_FALSE means we are not |
| 228 | * currently emitting but will start doing so if a %else comes |
| 229 | * up. In these states, all directives are admissible: %elif, |
| 230 | * %else and %endif. (And of course %if.) |
| 231 | */ |
| 232 | COND_IF_TRUE, COND_IF_FALSE, |
| 233 | /* |
| 234 | * These states come up after a %else: ELSE_TRUE means we're |
| 235 | * emitting, and ELSE_FALSE means we're not. In ELSE_* states, |
| 236 | * any %elif or %else will cause an error. |
| 237 | */ |
| 238 | COND_ELSE_TRUE, COND_ELSE_FALSE, |
| 239 | /* |
| 240 | * This state means that we're not emitting now, and also that |
| 241 | * nothing until %endif will be emitted at all. It's for use in |
| 242 | * two circumstances: (i) when we've had our moment of emission |
| 243 | * and have now started seeing %elifs, and (ii) when the |
| 244 | * condition construct in question is contained within a |
| 245 | * non-emitting branch of a larger condition construct. |
| 246 | */ |
| 247 | COND_NEVER |
| 248 | }; |
| 249 | #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE ) |
| 250 | |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 251 | /* |
| 252 | * These defines are used as the possible return values for do_directive |
| 253 | */ |
| 254 | #define NO_DIRECTIVE_FOUND 0 |
| 255 | #define DIRECTIVE_FOUND 1 |
| 256 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 257 | /* |
| 258 | * Condition codes. Note that we use c_ prefix not C_ because C_ is |
| 259 | * used in nasm.h for the "real" condition codes. At _this_ level, |
| 260 | * we treat CXZ and ECXZ as condition codes, albeit non-invertible |
| 261 | * ones, so we need a different enum... |
| 262 | */ |
H. Peter Anvin | 0a7a3b4 | 2002-05-14 23:54:46 +0000 | [diff] [blame] | 263 | static const char *conditions[] = { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 264 | "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le", |
| 265 | "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no", |
| 266 | "np", "ns", "nz", "o", "p", "pe", "po", "s", "z" |
| 267 | }; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 268 | enum |
| 269 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 270 | c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE, |
| 271 | c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO, |
| 272 | c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_S, c_Z |
| 273 | }; |
| 274 | static int inverse_ccs[] = { |
| 275 | c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE, |
| 276 | c_A, c_AE, c_B, c_BE, c_C, c_E, c_G, c_GE, c_L, c_LE, c_O, c_P, c_S, |
| 277 | c_Z, c_NO, c_NP, c_PO, c_PE, c_NS, c_NZ |
| 278 | }; |
| 279 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 280 | /* |
| 281 | * Directive names. |
| 282 | */ |
H. Peter Anvin | 0a7a3b4 | 2002-05-14 23:54:46 +0000 | [diff] [blame] | 283 | static const char *directives[] = { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 284 | "%arg", |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 285 | "%assign", "%clear", "%define", "%elif", "%elifctx", "%elifdef", |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 286 | "%elifid", "%elifidn", "%elifidni", "%elifmacro", "%elifnctx", "%elifndef", |
| 287 | "%elifnid", "%elifnidn", "%elifnidni", "%elifnmacro", "%elifnnum", "%elifnstr", |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 288 | "%elifnum", "%elifstr", "%else", "%endif", "%endm", "%endmacro", |
| 289 | "%endrep", "%error", "%exitrep", "%iassign", "%idefine", "%if", |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 290 | "%ifctx", "%ifdef", "%ifid", "%ifidn", "%ifidni", "%ifmacro", "%ifnctx", |
| 291 | "%ifndef", "%ifnid", "%ifnidn", "%ifnidni", "%ifnmacro", "%ifnnum", |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 292 | "%ifnstr", "%ifnum", "%ifstr", "%imacro", "%include", |
| 293 | "%ixdefine", "%line", |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 294 | "%local", |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 295 | "%macro", "%pop", "%push", "%rep", "%repl", "%rotate", |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 296 | "%stacksize", |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 297 | "%strlen", "%substr", "%undef", "%xdefine" |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 298 | }; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 299 | enum |
| 300 | { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 301 | PP_ARG, |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 302 | PP_ASSIGN, PP_CLEAR, PP_DEFINE, PP_ELIF, PP_ELIFCTX, PP_ELIFDEF, |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 303 | PP_ELIFID, PP_ELIFIDN, PP_ELIFIDNI, PP_ELIFMACRO, PP_ELIFNCTX, PP_ELIFNDEF, |
| 304 | PP_ELIFNID, PP_ELIFNIDN, PP_ELIFNIDNI, PP_ELIFNMACRO, PP_ELIFNNUM, PP_ELIFNSTR, |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 305 | PP_ELIFNUM, PP_ELIFSTR, PP_ELSE, PP_ENDIF, PP_ENDM, PP_ENDMACRO, |
| 306 | PP_ENDREP, PP_ERROR, PP_EXITREP, PP_IASSIGN, PP_IDEFINE, PP_IF, |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 307 | PP_IFCTX, PP_IFDEF, PP_IFID, PP_IFIDN, PP_IFIDNI, PP_IFMACRO, PP_IFNCTX, |
| 308 | PP_IFNDEF, PP_IFNID, PP_IFNIDN, PP_IFNIDNI, PP_IFNMACRO, PP_IFNNUM, |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 309 | PP_IFNSTR, PP_IFNUM, PP_IFSTR, PP_IMACRO, PP_INCLUDE, |
| 310 | PP_IXDEFINE, PP_LINE, |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 311 | PP_LOCAL, |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 312 | PP_MACRO, PP_POP, PP_PUSH, PP_REP, PP_REPL, PP_ROTATE, |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 313 | PP_STACKSIZE, |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 314 | PP_STRLEN, PP_SUBSTR, PP_UNDEF, PP_XDEFINE |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 315 | }; |
| 316 | |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 317 | /* If this is a an IF, ELIF, ELSE or ENDIF keyword */ |
| 318 | static int is_condition(int arg) |
| 319 | { |
| 320 | return ((arg >= PP_ELIF) && (arg <= PP_ENDIF)) || |
| 321 | ((arg >= PP_IF) && (arg <= PP_IFSTR)); |
| 322 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 323 | |
| 324 | /* For TASM compatibility we need to be able to recognise TASM compatible |
| 325 | * conditional compilation directives. Using the NASM pre-processor does |
| 326 | * not work, so we look for them specifically from the following list and |
| 327 | * then jam in the equivalent NASM directive into the input stream. |
| 328 | */ |
| 329 | |
| 330 | #ifndef MAX |
| 331 | # define MAX(a,b) ( ((a) > (b)) ? (a) : (b)) |
| 332 | #endif |
| 333 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 334 | enum |
| 335 | { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 336 | TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI, |
| 337 | TM_IFNDEF, TM_INCLUDE, TM_LOCAL |
| 338 | }; |
| 339 | |
H. Peter Anvin | 0a7a3b4 | 2002-05-14 23:54:46 +0000 | [diff] [blame] | 340 | static const char *tasm_directives[] = { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 341 | "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi", |
| 342 | "ifndef", "include", "local" |
| 343 | }; |
| 344 | |
| 345 | static int StackSize = 4; |
| 346 | static char *StackPointer = "ebp"; |
| 347 | static int ArgOffset = 8; |
| 348 | static int LocalOffset = 4; |
| 349 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 350 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 351 | static Context *cstk; |
| 352 | static Include *istk; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 353 | static IncPath *ipath = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 354 | |
H. Peter Anvin | 99941bf | 2002-05-14 17:44:03 +0000 | [diff] [blame] | 355 | static efunc _error; /* Pointer to client-provided error reporting function */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 356 | static evalfunc evaluate; |
| 357 | |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 358 | static int pass; /* HACK: pass 0 = generate dependencies only */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 359 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 360 | static unsigned long unique; /* unique identifier numbers */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 361 | |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 362 | static Line *predef = NULL; |
| 363 | |
| 364 | static ListGen *list; |
| 365 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 366 | /* |
| 367 | * 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] | 368 | * FIXME: We should *really* be able to configure this at run time, |
| 369 | * or even have the hash table automatically expanding when necessary. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 370 | */ |
| 371 | #define NHASH 31 |
| 372 | |
| 373 | /* |
| 374 | * The current set of multi-line macros we have defined. |
| 375 | */ |
| 376 | static MMacro *mmacros[NHASH]; |
| 377 | |
| 378 | /* |
| 379 | * The current set of single-line macros we have defined. |
| 380 | */ |
| 381 | static SMacro *smacros[NHASH]; |
| 382 | |
| 383 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 384 | * The multi-line macro we are currently defining, or the %rep |
| 385 | * block we are currently reading, if any. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 386 | */ |
| 387 | static MMacro *defining; |
| 388 | |
| 389 | /* |
| 390 | * The number of macro parameters to allocate space for at a time. |
| 391 | */ |
| 392 | #define PARAM_DELTA 16 |
| 393 | |
| 394 | /* |
| 395 | * The standard macro set: defined as `static char *stdmac[]'. Also |
| 396 | * gives our position in the macro set, when we're processing it. |
| 397 | */ |
| 398 | #include "macros.c" |
Ed Beroset | 168c9c0 | 2002-05-17 03:10:13 +0000 | [diff] [blame] | 399 | static const char **stdmacpos; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 400 | |
| 401 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 402 | * The extra standard macros that come from the object format, if |
| 403 | * any. |
| 404 | */ |
H. Peter Anvin | bfebdb0 | 2002-09-12 02:23:54 +0000 | [diff] [blame] | 405 | static const char **extrastdmac = NULL; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 406 | int any_extrastdmac; |
| 407 | |
| 408 | /* |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 409 | * Tokens are allocated in blocks to improve speed |
| 410 | */ |
| 411 | #define TOKEN_BLOCKSIZE 4096 |
| 412 | static Token *freeTokens = NULL; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 413 | struct Blocks { |
| 414 | Blocks *next; |
| 415 | void *chunk; |
| 416 | }; |
| 417 | |
| 418 | static Blocks blocks = { NULL, NULL }; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 419 | |
| 420 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 421 | * Forward declarations. |
| 422 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 423 | static Token *expand_mmac_params(Token * tline); |
| 424 | static Token *expand_smacro(Token * tline); |
| 425 | static Token *expand_id(Token * tline); |
| 426 | static Context *get_ctx(char *name, int all_contexts); |
| 427 | static void make_tok_num(Token * tok, long val); |
Ed Beroset | 168c9c0 | 2002-05-17 03:10:13 +0000 | [diff] [blame] | 428 | static void error(int severity, const char *fmt, ...); |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 429 | static void *new_Block(size_t size); |
| 430 | static void delete_Blocks(void); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 431 | static Token *new_Token(Token * next, int type, char *text, int txtlen); |
| 432 | static Token *delete_Token(Token * t); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 433 | |
| 434 | /* |
| 435 | * Macros for safe checking of token pointers, avoid *(NULL) |
| 436 | */ |
| 437 | #define tok_type_(x,t) ((x) && (x)->type == (t)) |
| 438 | #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next |
| 439 | #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v))) |
| 440 | #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v)))) |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 441 | |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 442 | /* Handle TASM specific directives, which do not contain a % in |
| 443 | * front of them. We do it here because I could not find any other |
| 444 | * place to do it for the moment, and it is a hack (ideally it would |
| 445 | * be nice to be able to use the NASM pre-processor to do it). |
| 446 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 447 | static char * |
| 448 | check_tasm_directive(char *line) |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 449 | { |
| 450 | int i, j, k, m, len; |
| 451 | char *p = line, *oldline, oldchar; |
| 452 | |
| 453 | /* Skip whitespace */ |
| 454 | while (isspace(*p) && *p != 0) |
| 455 | p++; |
| 456 | |
| 457 | /* Binary search for the directive name */ |
| 458 | i = -1; |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 459 | j = elements(tasm_directives); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 460 | len = 0; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 461 | while (!isspace(p[len]) && p[len] != 0) |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 462 | len++; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 463 | if (len) |
| 464 | { |
| 465 | oldchar = p[len]; |
| 466 | p[len] = 0; |
| 467 | while (j - i > 1) |
| 468 | { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 469 | k = (j + i) / 2; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 470 | m = nasm_stricmp(p, tasm_directives[k]); |
| 471 | if (m == 0) |
| 472 | { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 473 | /* We have found a directive, so jam a % in front of it |
| 474 | * so that NASM will then recognise it as one if it's own. |
| 475 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 476 | p[len] = oldchar; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 477 | len = strlen(p); |
| 478 | oldline = line; |
| 479 | line = nasm_malloc(len + 2); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 480 | line[0] = '%'; |
| 481 | if (k == TM_IFDIFI) |
| 482 | { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 483 | /* NASM does not recognise IFDIFI, so we convert it to |
| 484 | * %ifdef BOGUS. This is not used in NASM comaptible |
| 485 | * code, but does need to parse for the TASM macro |
| 486 | * package. |
| 487 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 488 | strcpy(line + 1, "ifdef BOGUS"); |
| 489 | } |
| 490 | else |
| 491 | { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 492 | memcpy(line + 1, p, len + 1); |
| 493 | } |
| 494 | nasm_free(oldline); |
| 495 | return line; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 496 | } |
| 497 | else if (m < 0) |
| 498 | { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 499 | j = k; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 500 | } |
| 501 | else |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 502 | i = k; |
| 503 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 504 | p[len] = oldchar; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 505 | } |
| 506 | return line; |
| 507 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 508 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 509 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 510 | * The pre-preprocessing stage... This function translates line |
| 511 | * number indications as they emerge from GNU cpp (`# lineno "file" |
| 512 | * flags') into NASM preprocessor line number indications (`%line |
| 513 | * lineno file'). |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 514 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 515 | static char * |
| 516 | prepreproc(char *line) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 517 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 518 | int lineno, fnlen; |
| 519 | char *fname, *oldline; |
| 520 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 521 | if (line[0] == '#' && line[1] == ' ') |
| 522 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 523 | oldline = line; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 524 | fname = oldline + 2; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 525 | lineno = atoi(fname); |
| 526 | fname += strspn(fname, "0123456789 "); |
| 527 | if (*fname == '"') |
| 528 | fname++; |
| 529 | fnlen = strcspn(fname, "\""); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 530 | line = nasm_malloc(20 + fnlen); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 531 | sprintf(line, "%%line %d %.*s", lineno, fnlen, fname); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 532 | nasm_free(oldline); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 533 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 534 | if (tasm_compatible_mode) |
| 535 | return check_tasm_directive(line); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 536 | return line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | /* |
| 540 | * The hash function for macro lookups. Note that due to some |
| 541 | * macros having case-insensitive names, the hash function must be |
| 542 | * invariant under case changes. We implement this by applying a |
| 543 | * perfectly normal hash function to the uppercase of the string. |
| 544 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 545 | static int |
| 546 | hash(char *s) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 547 | { |
| 548 | unsigned int h = 0; |
| 549 | int i = 0; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 550 | /* |
| 551 | * Powers of three, mod 31. |
| 552 | */ |
| 553 | static const int multipliers[] = { |
| 554 | 1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, |
| 555 | 30, 28, 22, 4, 12, 5, 15, 14, 11, 2, 6, 18, 23, 7, 21 |
| 556 | }; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 557 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 558 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 559 | while (*s) |
| 560 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 561 | h += multipliers[i] * (unsigned char) (toupper(*s)); |
| 562 | s++; |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 563 | if (++i >= elements(multipliers)) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 564 | i = 0; |
| 565 | } |
| 566 | h %= NHASH; |
| 567 | return h; |
| 568 | } |
| 569 | |
| 570 | /* |
| 571 | * Free a linked list of tokens. |
| 572 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 573 | static void |
| 574 | free_tlist(Token * list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 575 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 576 | while (list) |
| 577 | { |
| 578 | list = delete_Token(list); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 579 | } |
| 580 | } |
| 581 | |
| 582 | /* |
| 583 | * Free a linked list of lines. |
| 584 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 585 | static void |
| 586 | free_llist(Line * list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 587 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 588 | Line *l; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 589 | while (list) |
| 590 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 591 | l = list; |
| 592 | list = list->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 593 | free_tlist(l->first); |
| 594 | nasm_free(l); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 595 | } |
| 596 | } |
| 597 | |
| 598 | /* |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 599 | * Free an MMacro |
| 600 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 601 | static void |
| 602 | free_mmacro(MMacro * m) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 603 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 604 | nasm_free(m->name); |
| 605 | free_tlist(m->dlist); |
| 606 | nasm_free(m->defaults); |
| 607 | free_llist(m->expansion); |
| 608 | nasm_free(m); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 612 | * Pop the context stack. |
| 613 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 614 | static void |
| 615 | ctx_pop(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 616 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 617 | Context *c = cstk; |
| 618 | SMacro *smac, *s; |
| 619 | |
| 620 | cstk = cstk->next; |
| 621 | smac = c->localmac; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 622 | while (smac) |
| 623 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 624 | s = smac; |
| 625 | smac = smac->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 626 | nasm_free(s->name); |
| 627 | free_tlist(s->expansion); |
| 628 | nasm_free(s); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 629 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 630 | nasm_free(c->name); |
| 631 | nasm_free(c); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 632 | } |
| 633 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 634 | #define BUF_DELTA 512 |
| 635 | /* |
| 636 | * Read a line from the top file in istk, handling multiple CR/LFs |
| 637 | * at the end of the line read, and handling spurious ^Zs. Will |
| 638 | * return lines from the standard macro set if this has not already |
| 639 | * been done. |
| 640 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 641 | static char * |
| 642 | read_line(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 643 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 644 | char *buffer, *p, *q; |
H. Peter Anvin | 9f39464 | 2002-04-30 21:07:51 +0000 | [diff] [blame] | 645 | int bufsize, continued_count; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 646 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 647 | if (stdmacpos) |
| 648 | { |
| 649 | if (*stdmacpos) |
| 650 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 651 | char *ret = nasm_strdup(*stdmacpos++); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 652 | if (!*stdmacpos && any_extrastdmac) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 653 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 654 | stdmacpos = extrastdmac; |
| 655 | any_extrastdmac = FALSE; |
| 656 | return ret; |
| 657 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 658 | /* |
| 659 | * Nasty hack: here we push the contents of `predef' on |
| 660 | * to the top-level expansion stack, since this is the |
| 661 | * most convenient way to implement the pre-include and |
| 662 | * pre-define features. |
| 663 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 664 | if (!*stdmacpos) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 665 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 666 | Line *pd, *l; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 667 | Token *head, **tail, *t; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 668 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 669 | for (pd = predef; pd; pd = pd->next) |
| 670 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 671 | head = NULL; |
| 672 | tail = &head; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 673 | for (t = pd->first; t; t = t->next) |
| 674 | { |
| 675 | *tail = new_Token(NULL, t->type, t->text, 0); |
| 676 | tail = &(*tail)->next; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 677 | } |
| 678 | l = nasm_malloc(sizeof(Line)); |
| 679 | l->next = istk->expansion; |
| 680 | l->first = head; |
| 681 | l->finishes = FALSE; |
| 682 | istk->expansion = l; |
| 683 | } |
| 684 | } |
| 685 | return ret; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 686 | } |
| 687 | else |
| 688 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 689 | stdmacpos = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 690 | } |
| 691 | } |
| 692 | |
| 693 | bufsize = BUF_DELTA; |
| 694 | buffer = nasm_malloc(BUF_DELTA); |
| 695 | p = buffer; |
H. Peter Anvin | 9f39464 | 2002-04-30 21:07:51 +0000 | [diff] [blame] | 696 | continued_count = 0; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 697 | while (1) |
| 698 | { |
| 699 | q = fgets(p, bufsize - (p - buffer), istk->fp); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 700 | if (!q) |
| 701 | break; |
| 702 | p += strlen(p); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 703 | if (p > buffer && p[-1] == '\n') |
| 704 | { |
H. Peter Anvin | 9f39464 | 2002-04-30 21:07:51 +0000 | [diff] [blame] | 705 | /* Convert backslash-CRLF line continuation sequences into |
| 706 | nothing at all (for DOS and Windows) */ |
| 707 | if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) { |
| 708 | p -= 3; |
| 709 | *p = 0; |
| 710 | continued_count++; |
| 711 | } |
| 712 | /* Also convert backslash-LF line continuation sequences into |
| 713 | nothing at all (for Unix) */ |
| 714 | else if (((p - 1) > buffer) && (p[-2] == '\\')) { |
| 715 | p -= 2; |
| 716 | *p = 0; |
| 717 | continued_count++; |
| 718 | } |
| 719 | else { |
| 720 | break; |
| 721 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 722 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 723 | if (p - buffer > bufsize - 10) |
| 724 | { |
| 725 | long offset = p - buffer; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 726 | bufsize += BUF_DELTA; |
| 727 | buffer = nasm_realloc(buffer, bufsize); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 728 | p = buffer + offset; /* prevent stale-pointer problems */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 729 | } |
| 730 | } |
| 731 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 732 | if (!q && p == buffer) |
| 733 | { |
| 734 | nasm_free(buffer); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 735 | return NULL; |
| 736 | } |
| 737 | |
H. Peter Anvin | 9a633fa | 2002-04-30 21:08:11 +0000 | [diff] [blame] | 738 | src_set_linnum(src_get_linnum() + istk->lineinc + (continued_count * istk->lineinc)); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 739 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 740 | /* |
| 741 | * Play safe: remove CRs as well as LFs, if any of either are |
| 742 | * present at the end of the line. |
| 743 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 744 | while (--p >= buffer && (*p == '\n' || *p == '\r')) |
| 745 | *p = '\0'; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 746 | |
| 747 | /* |
| 748 | * Handle spurious ^Z, which may be inserted into source files |
| 749 | * by some file transfer utilities. |
| 750 | */ |
| 751 | buffer[strcspn(buffer, "\032")] = '\0'; |
| 752 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 753 | list->line(LIST_READ, buffer); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 754 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 755 | return buffer; |
| 756 | } |
| 757 | |
| 758 | /* |
| 759 | * Tokenise a line of text. This is a very simple process since we |
| 760 | * don't need to parse the value out of e.g. numeric tokens: we |
| 761 | * simply split one string into many. |
| 762 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 763 | static Token * |
| 764 | tokenise(char *line) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 765 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 766 | char *p = line; |
| 767 | int type; |
| 768 | Token *list = NULL; |
| 769 | Token *t, **tail = &list; |
| 770 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 771 | while (*line) |
| 772 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 773 | p = line; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 774 | if (*p == '%') |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 775 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 776 | p++; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 777 | if ( isdigit(*p) || |
| 778 | ((*p == '-' || *p == '+') && isdigit(p[1])) || |
| 779 | ((*p == '+') && (isspace(p[1]) || !p[1]))) |
| 780 | { |
| 781 | do |
| 782 | { |
| 783 | p++; |
| 784 | } |
| 785 | while (isdigit(*p)); |
| 786 | type = TOK_PREPROC_ID; |
| 787 | } |
| 788 | else if (*p == '{') |
| 789 | { |
| 790 | p++; |
| 791 | while (*p && *p != '}') |
| 792 | { |
| 793 | p[-1] = *p; |
| 794 | p++; |
| 795 | } |
| 796 | p[-1] = '\0'; |
| 797 | if (*p) |
| 798 | p++; |
| 799 | type = TOK_PREPROC_ID; |
| 800 | } |
| 801 | else if (isidchar(*p) || |
| 802 | ((*p == '!' || *p == '%' || *p == '$') && |
| 803 | isidchar(p[1]))) |
| 804 | { |
| 805 | do |
| 806 | { |
| 807 | p++; |
| 808 | } |
| 809 | while (isidchar(*p)); |
| 810 | type = TOK_PREPROC_ID; |
| 811 | } |
| 812 | else |
| 813 | { |
| 814 | type = TOK_OTHER; |
| 815 | if (*p == '%') |
| 816 | p++; |
| 817 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 818 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 819 | else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 820 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 821 | type = TOK_ID; |
| 822 | p++; |
| 823 | while (*p && isidchar(*p)) |
| 824 | p++; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 825 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 826 | else if (*p == '\'' || *p == '"') |
| 827 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 828 | /* |
| 829 | * A string token. |
| 830 | */ |
| 831 | char c = *p; |
| 832 | p++; |
| 833 | type = TOK_STRING; |
| 834 | while (*p && *p != c) |
| 835 | p++; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 836 | if (*p) |
| 837 | { |
| 838 | p++; |
| 839 | } |
| 840 | else |
| 841 | { |
| 842 | error(ERR_WARNING, "unterminated string"); |
| 843 | } |
| 844 | } |
| 845 | else if (isnumstart(*p)) |
| 846 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 847 | /* |
| 848 | * A number token. |
| 849 | */ |
| 850 | type = TOK_NUMBER; |
| 851 | p++; |
| 852 | while (*p && isnumchar(*p)) |
| 853 | p++; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 854 | } |
| 855 | else if (isspace(*p)) |
| 856 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 857 | type = TOK_WHITESPACE; |
| 858 | p++; |
| 859 | while (*p && isspace(*p)) |
| 860 | p++; |
| 861 | /* |
| 862 | * Whitespace just before end-of-line is discarded by |
| 863 | * pretending it's a comment; whitespace just before a |
| 864 | * comment gets lumped into the comment. |
| 865 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 866 | if (!*p || *p == ';') |
| 867 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 868 | type = TOK_COMMENT; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 869 | while (*p) |
| 870 | p++; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 871 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 872 | } |
| 873 | else if (*p == ';') |
| 874 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 875 | type = TOK_COMMENT; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 876 | while (*p) |
| 877 | p++; |
| 878 | } |
| 879 | else |
| 880 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 881 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 882 | * Anything else is an operator of some kind. We check |
| 883 | * for all the double-character operators (>>, <<, //, |
| 884 | * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything |
| 885 | * else is a single-character operator. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 886 | */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 887 | type = TOK_OTHER; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 888 | if ((p[0] == '>' && p[1] == '>') || |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 889 | (p[0] == '<' && p[1] == '<') || |
| 890 | (p[0] == '/' && p[1] == '/') || |
| 891 | (p[0] == '<' && p[1] == '=') || |
| 892 | (p[0] == '>' && p[1] == '=') || |
| 893 | (p[0] == '=' && p[1] == '=') || |
| 894 | (p[0] == '!' && p[1] == '=') || |
| 895 | (p[0] == '<' && p[1] == '>') || |
| 896 | (p[0] == '&' && p[1] == '&') || |
| 897 | (p[0] == '|' && p[1] == '|') || |
| 898 | (p[0] == '^' && p[1] == '^')) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 899 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 900 | p++; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 901 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 902 | p++; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 903 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 904 | if (type != TOK_COMMENT) |
| 905 | { |
| 906 | *tail = t = new_Token(NULL, type, line, p - line); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 907 | tail = &t->next; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 908 | } |
| 909 | line = p; |
| 910 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 911 | return list; |
| 912 | } |
| 913 | |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 914 | /* |
| 915 | * this function allocates a new managed block of memory and |
| 916 | * returns a pointer to the block. The managed blocks are |
| 917 | * deleted only all at once by the delete_Blocks function. |
| 918 | */ |
| 919 | static void * |
| 920 | new_Block(size_t size) |
| 921 | { |
| 922 | Blocks *b = &blocks; |
| 923 | |
| 924 | /* first, get to the end of the linked list */ |
| 925 | while (b->next) |
| 926 | b = b->next; |
| 927 | /* now allocate the requested chunk */ |
| 928 | b->chunk = nasm_malloc(size); |
| 929 | |
| 930 | /* now allocate a new block for the next request */ |
| 931 | b->next = nasm_malloc(sizeof(Blocks)); |
| 932 | /* and initialize the contents of the new block */ |
| 933 | b->next->next = NULL; |
| 934 | b->next->chunk = NULL; |
| 935 | return b->chunk; |
| 936 | } |
| 937 | |
| 938 | /* |
| 939 | * this function deletes all managed blocks of memory |
| 940 | */ |
| 941 | static void |
| 942 | delete_Blocks(void) |
| 943 | { |
| 944 | Blocks *a,*b = &blocks; |
| 945 | |
| 946 | /* |
| 947 | * keep in mind that the first block, pointed to by blocks |
| 948 | * is a static and not dynamically allocated, so we don't |
| 949 | * free it. |
| 950 | */ |
| 951 | while (b) |
| 952 | { |
| 953 | if (b->chunk) |
| 954 | nasm_free(b->chunk); |
| 955 | a = b; |
| 956 | b = b->next; |
H. Peter Anvin | 9eb185b | 2002-04-30 21:02:47 +0000 | [diff] [blame] | 957 | if (a != &blocks) |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 958 | nasm_free(a); |
| 959 | } |
| 960 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 961 | |
| 962 | /* |
| 963 | * this function creates a new Token and passes a pointer to it |
| 964 | * back to the caller. It sets the type and text elements, and |
| 965 | * also the mac and next elements to NULL. |
| 966 | */ |
| 967 | static Token * |
| 968 | new_Token(Token * next, int type, char *text, int txtlen) |
| 969 | { |
| 970 | Token *t; |
| 971 | int i; |
| 972 | |
| 973 | if (freeTokens == NULL) |
| 974 | { |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 975 | freeTokens = (Token *)new_Block(TOKEN_BLOCKSIZE * sizeof(Token)); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 976 | for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++) |
| 977 | freeTokens[i].next = &freeTokens[i + 1]; |
| 978 | freeTokens[i].next = NULL; |
| 979 | } |
| 980 | t = freeTokens; |
| 981 | freeTokens = t->next; |
| 982 | t->next = next; |
| 983 | t->mac = NULL; |
| 984 | t->type = type; |
| 985 | if (type == TOK_WHITESPACE || text == NULL) |
| 986 | { |
| 987 | t->text = NULL; |
| 988 | } |
| 989 | else |
| 990 | { |
| 991 | if (txtlen == 0) |
| 992 | txtlen = strlen(text); |
| 993 | t->text = nasm_malloc(1 + txtlen); |
| 994 | strncpy(t->text, text, txtlen); |
| 995 | t->text[txtlen] = '\0'; |
| 996 | } |
| 997 | return t; |
| 998 | } |
| 999 | |
| 1000 | static Token * |
| 1001 | delete_Token(Token * t) |
| 1002 | { |
| 1003 | Token *next = t->next; |
| 1004 | nasm_free(t->text); |
H. Peter Anvin | 788e6c1 | 2002-04-30 21:02:01 +0000 | [diff] [blame] | 1005 | t->next = freeTokens; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1006 | freeTokens = t; |
| 1007 | return next; |
| 1008 | } |
| 1009 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1010 | /* |
| 1011 | * Convert a line of tokens back into text. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1012 | * If expand_locals is not zero, identifiers of the form "%$*xxx" |
| 1013 | * will be transformed into ..@ctxnum.xxx |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1014 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1015 | static char * |
| 1016 | detoken(Token * tlist, int expand_locals) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1017 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1018 | Token *t; |
| 1019 | int len; |
| 1020 | char *line, *p; |
| 1021 | |
| 1022 | len = 0; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1023 | for (t = tlist; t; t = t->next) |
| 1024 | { |
| 1025 | if (t->type == TOK_PREPROC_ID && t->text[1] == '!') |
| 1026 | { |
| 1027 | char *p = getenv(t->text + 2); |
| 1028 | nasm_free(t->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1029 | if (p) |
| 1030 | t->text = nasm_strdup(p); |
| 1031 | else |
| 1032 | t->text = NULL; |
| 1033 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1034 | /* Expand local macros here and not during preprocessing */ |
| 1035 | if (expand_locals && |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1036 | t->type == TOK_PREPROC_ID && t->text && |
| 1037 | t->text[0] == '%' && t->text[1] == '$') |
| 1038 | { |
| 1039 | Context *ctx = get_ctx(t->text, FALSE); |
| 1040 | if (ctx) |
| 1041 | { |
| 1042 | char buffer[40]; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1043 | char *p, *q = t->text + 2; |
| 1044 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1045 | q += strspn(q, "$"); |
| 1046 | sprintf(buffer, "..@%lu.", ctx->number); |
| 1047 | p = nasm_strcat(buffer, q); |
| 1048 | nasm_free(t->text); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1049 | t->text = p; |
| 1050 | } |
| 1051 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1052 | if (t->type == TOK_WHITESPACE) |
| 1053 | { |
| 1054 | len++; |
| 1055 | } |
| 1056 | else if (t->text) |
| 1057 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1058 | len += strlen(t->text); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1059 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1060 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1061 | p = line = nasm_malloc(len + 1); |
| 1062 | for (t = tlist; t; t = t->next) |
| 1063 | { |
| 1064 | if (t->type == TOK_WHITESPACE) |
| 1065 | { |
| 1066 | *p = ' '; |
| 1067 | p++; |
| 1068 | *p = '\0'; |
| 1069 | } |
| 1070 | else if (t->text) |
| 1071 | { |
| 1072 | strcpy(p, t->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1073 | p += strlen(p); |
| 1074 | } |
| 1075 | } |
| 1076 | *p = '\0'; |
| 1077 | return line; |
| 1078 | } |
| 1079 | |
| 1080 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1081 | * A scanner, suitable for use by the expression evaluator, which |
| 1082 | * operates on a line of Tokens. Expects a pointer to a pointer to |
| 1083 | * the first token in the line to be passed in as its private_data |
| 1084 | * field. |
| 1085 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1086 | static int |
| 1087 | ppscan(void *private_data, struct tokenval *tokval) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1088 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1089 | Token **tlineptr = private_data; |
| 1090 | Token *tline; |
| 1091 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1092 | do |
| 1093 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1094 | tline = *tlineptr; |
| 1095 | *tlineptr = tline ? tline->next : NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1096 | } |
| 1097 | while (tline && (tline->type == TOK_WHITESPACE || |
| 1098 | tline->type == TOK_COMMENT)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1099 | |
| 1100 | if (!tline) |
| 1101 | return tokval->t_type = TOKEN_EOS; |
| 1102 | |
| 1103 | if (tline->text[0] == '$' && !tline->text[1]) |
| 1104 | return tokval->t_type = TOKEN_HERE; |
H. Peter Anvin | 7cf897e | 2002-05-30 21:30:33 +0000 | [diff] [blame] | 1105 | if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2]) |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1106 | return tokval->t_type = TOKEN_BASE; |
| 1107 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1108 | if (tline->type == TOK_ID) |
| 1109 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1110 | tokval->t_charptr = tline->text; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1111 | if (tline->text[0] == '$') |
| 1112 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1113 | tokval->t_charptr++; |
| 1114 | return tokval->t_type = TOKEN_ID; |
| 1115 | } |
| 1116 | |
| 1117 | /* |
| 1118 | * This is the only special case we actually need to worry |
| 1119 | * about in this restricted context. |
| 1120 | */ |
| 1121 | if (!nasm_stricmp(tline->text, "seg")) |
| 1122 | return tokval->t_type = TOKEN_SEG; |
| 1123 | |
| 1124 | return tokval->t_type = TOKEN_ID; |
| 1125 | } |
| 1126 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1127 | if (tline->type == TOK_NUMBER) |
| 1128 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1129 | int rn_error; |
| 1130 | |
| 1131 | tokval->t_integer = readnum(tline->text, &rn_error); |
| 1132 | if (rn_error) |
| 1133 | return tokval->t_type = TOKEN_ERRNUM; |
| 1134 | tokval->t_charptr = NULL; |
| 1135 | return tokval->t_type = TOKEN_NUM; |
| 1136 | } |
| 1137 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1138 | if (tline->type == TOK_STRING) |
| 1139 | { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1140 | int rn_warn; |
| 1141 | char q, *r; |
| 1142 | int l; |
| 1143 | |
| 1144 | r = tline->text; |
| 1145 | q = *r++; |
| 1146 | l = strlen(r); |
| 1147 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1148 | if (l == 0 || r[l - 1] != q) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1149 | return tokval->t_type = TOKEN_ERRNUM; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1150 | tokval->t_integer = readstrnum(r, l - 1, &rn_warn); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1151 | if (rn_warn) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1152 | error(ERR_WARNING | ERR_PASS1, "character constant too long"); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1153 | tokval->t_charptr = NULL; |
| 1154 | return tokval->t_type = TOKEN_NUM; |
| 1155 | } |
| 1156 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1157 | if (tline->type == TOK_OTHER) |
| 1158 | { |
| 1159 | if (!strcmp(tline->text, "<<")) |
| 1160 | return tokval->t_type = TOKEN_SHL; |
| 1161 | if (!strcmp(tline->text, ">>")) |
| 1162 | return tokval->t_type = TOKEN_SHR; |
| 1163 | if (!strcmp(tline->text, "//")) |
| 1164 | return tokval->t_type = TOKEN_SDIV; |
| 1165 | if (!strcmp(tline->text, "%%")) |
| 1166 | return tokval->t_type = TOKEN_SMOD; |
| 1167 | if (!strcmp(tline->text, "==")) |
| 1168 | return tokval->t_type = TOKEN_EQ; |
| 1169 | if (!strcmp(tline->text, "<>")) |
| 1170 | return tokval->t_type = TOKEN_NE; |
| 1171 | if (!strcmp(tline->text, "!=")) |
| 1172 | return tokval->t_type = TOKEN_NE; |
| 1173 | if (!strcmp(tline->text, "<=")) |
| 1174 | return tokval->t_type = TOKEN_LE; |
| 1175 | if (!strcmp(tline->text, ">=")) |
| 1176 | return tokval->t_type = TOKEN_GE; |
| 1177 | if (!strcmp(tline->text, "&&")) |
| 1178 | return tokval->t_type = TOKEN_DBL_AND; |
| 1179 | if (!strcmp(tline->text, "^^")) |
| 1180 | return tokval->t_type = TOKEN_DBL_XOR; |
| 1181 | if (!strcmp(tline->text, "||")) |
| 1182 | return tokval->t_type = TOKEN_DBL_OR; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | /* |
| 1186 | * We have no other options: just return the first character of |
| 1187 | * the token text. |
| 1188 | */ |
| 1189 | return tokval->t_type = tline->text[0]; |
| 1190 | } |
| 1191 | |
| 1192 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1193 | * Compare a string to the name of an existing macro; this is a |
| 1194 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 1195 | * depending on the value of the `casesense' parameter. |
| 1196 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1197 | static int |
| 1198 | mstrcmp(char *p, char *q, int casesense) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1199 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1200 | return casesense ? strcmp(p, q) : nasm_stricmp(p, q); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1204 | * Return the Context structure associated with a %$ token. Return |
| 1205 | * NULL, having _already_ reported an error condition, if the |
| 1206 | * context stack isn't deep enough for the supplied number of $ |
| 1207 | * signs. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1208 | * If all_contexts == TRUE, contexts that enclose current are |
| 1209 | * also scanned for such smacro, until it is found; if not - |
| 1210 | * only the context that directly results from the number of $'s |
| 1211 | * in variable's name. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1212 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1213 | static Context * |
| 1214 | get_ctx(char *name, int all_contexts) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1215 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1216 | Context *ctx; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1217 | SMacro *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1218 | int i; |
| 1219 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1220 | if (!name || name[0] != '%' || name[1] != '$') |
| 1221 | return NULL; |
| 1222 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1223 | if (!cstk) |
| 1224 | { |
| 1225 | error(ERR_NONFATAL, "`%s': context stack is empty", name); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1226 | return NULL; |
| 1227 | } |
| 1228 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1229 | for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--) |
| 1230 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1231 | ctx = ctx->next; |
H. Peter Anvin | dce1e2f | 2002-04-30 21:06:37 +0000 | [diff] [blame] | 1232 | /* i--; Lino - 02/25/02 */ |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1233 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1234 | if (!ctx) |
| 1235 | { |
| 1236 | error(ERR_NONFATAL, "`%s': context stack is only" |
| 1237 | " %d level%s deep", name, i - 1, (i == 2 ? "" : "s")); |
| 1238 | return NULL; |
| 1239 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1240 | if (!all_contexts) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1241 | return ctx; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1242 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1243 | do |
| 1244 | { |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1245 | /* Search for this smacro in found context */ |
| 1246 | m = ctx->localmac; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1247 | while (m) |
| 1248 | { |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1249 | if (!mstrcmp(m->name, name, m->casesense)) |
| 1250 | return ctx; |
| 1251 | m = m->next; |
| 1252 | } |
| 1253 | ctx = ctx->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1254 | } |
| 1255 | while (ctx); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1256 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1257 | } |
| 1258 | |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1259 | /* Add a slash to the end of a path if it is missing. We use the |
| 1260 | * forward slash to make it compatible with Unix systems. |
| 1261 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1262 | static void |
| 1263 | backslash(char *s) |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1264 | { |
| 1265 | int pos = strlen(s); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1266 | if (s[pos - 1] != '\\' && s[pos - 1] != '/') |
| 1267 | { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1268 | s[pos] = '/'; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1269 | s[pos + 1] = '\0'; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1270 | } |
| 1271 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1272 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1273 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1274 | * Open an include file. This routine must always return a valid |
| 1275 | * file pointer if it returns - it's responsible for throwing an |
| 1276 | * ERR_FATAL and bombing out completely if not. It should also try |
| 1277 | * the include path one by one until it finds the file or reaches |
| 1278 | * the end of the path. |
| 1279 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1280 | static FILE * |
| 1281 | inc_fopen(char *file) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1282 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1283 | FILE *fp; |
| 1284 | char *prefix = "", *combine; |
| 1285 | IncPath *ip = ipath; |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 1286 | static int namelen = 0; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1287 | int len = strlen(file); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1288 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1289 | while (1) |
| 1290 | { |
| 1291 | combine = nasm_malloc(strlen(prefix) + 1 + len + 1); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1292 | strcpy(combine, prefix); |
| 1293 | if (prefix[0] != 0) |
| 1294 | backslash(combine); |
| 1295 | strcat(combine, file); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1296 | fp = fopen(combine, "r"); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 1297 | if (pass == 0 && fp) |
| 1298 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1299 | namelen += strlen(combine) + 1; |
| 1300 | if (namelen > 62) |
| 1301 | { |
| 1302 | printf(" \\\n "); |
| 1303 | namelen = 2; |
| 1304 | } |
| 1305 | printf(" %s", combine); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 1306 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1307 | nasm_free(combine); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1308 | if (fp) |
| 1309 | return fp; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1310 | if (!ip) |
| 1311 | break; |
| 1312 | prefix = ip->path; |
| 1313 | ip = ip->next; |
| 1314 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1315 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1316 | error(ERR_FATAL, "unable to open include file `%s'", file); |
| 1317 | return NULL; /* never reached - placate compilers */ |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1321 | * 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] | 1322 | * 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] | 1323 | * return TRUE if _any_ single-line macro of that name is defined. |
| 1324 | * Otherwise, will return TRUE if a single-line macro with either |
| 1325 | * `nparam' or no parameters is defined. |
| 1326 | * |
| 1327 | * If a macro with precisely the right number of parameters is |
H. Peter Anvin | ef7468f | 2002-04-30 20:57:59 +0000 | [diff] [blame] | 1328 | * defined, or nparam is -1, the address of the definition structure |
| 1329 | * 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] | 1330 | * is NULL, no action will be taken regarding its contents, and no |
| 1331 | * error will occur. |
| 1332 | * |
| 1333 | * Note that this is also called with nparam zero to resolve |
| 1334 | * `ifdef'. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1335 | * |
| 1336 | * If you already know which context macro belongs to, you can pass |
| 1337 | * the context pointer as first parameter; if you won't but name begins |
| 1338 | * with %$ the context will be automatically computed. If all_contexts |
| 1339 | * is true, macro will be searched in outer contexts as well. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1340 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1341 | static int |
| 1342 | smacro_defined(Context * ctx, char *name, int nparam, SMacro ** defn, |
| 1343 | int nocase) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1344 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1345 | SMacro *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1346 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1347 | if (ctx) |
| 1348 | m = ctx->localmac; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1349 | else if (name[0] == '%' && name[1] == '$') |
| 1350 | { |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1351 | if (cstk) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1352 | ctx = get_ctx(name, FALSE); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1353 | if (!ctx) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1354 | return FALSE; /* got to return _something_ */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1355 | m = ctx->localmac; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1356 | } |
| 1357 | else |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1358 | m = smacros[hash(name)]; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1359 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1360 | while (m) |
| 1361 | { |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1362 | if (!mstrcmp(m->name, name, m->casesense && nocase) && |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1363 | (nparam <= 0 || m->nparam == 0 || nparam == m->nparam)) |
| 1364 | { |
| 1365 | if (defn) |
| 1366 | { |
H. Peter Anvin | ef7468f | 2002-04-30 20:57:59 +0000 | [diff] [blame] | 1367 | if (nparam == m->nparam || nparam == -1) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1368 | *defn = m; |
| 1369 | else |
| 1370 | *defn = NULL; |
| 1371 | } |
| 1372 | return TRUE; |
| 1373 | } |
| 1374 | m = m->next; |
| 1375 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1376 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1377 | return FALSE; |
| 1378 | } |
| 1379 | |
| 1380 | /* |
| 1381 | * Count and mark off the parameters in a multi-line macro call. |
| 1382 | * This is called both from within the multi-line macro expansion |
| 1383 | * code, and also to mark off the default parameters when provided |
| 1384 | * in a %macro definition line. |
| 1385 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1386 | static void |
| 1387 | count_mmac_params(Token * t, int *nparam, Token *** params) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1388 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1389 | int paramsize, brace; |
| 1390 | |
| 1391 | *nparam = paramsize = 0; |
| 1392 | *params = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1393 | while (t) |
| 1394 | { |
| 1395 | if (*nparam >= paramsize) |
| 1396 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1397 | paramsize += PARAM_DELTA; |
| 1398 | *params = nasm_realloc(*params, sizeof(**params) * paramsize); |
| 1399 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1400 | skip_white_(t); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1401 | brace = FALSE; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1402 | if (tok_is_(t, "{")) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1403 | brace = TRUE; |
| 1404 | (*params)[(*nparam)++] = t; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1405 | while (tok_isnt_(t, brace ? "}" : ",")) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1406 | t = t->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1407 | if (t) |
| 1408 | { /* got a comma/brace */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1409 | t = t->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1410 | if (brace) |
| 1411 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1412 | /* |
| 1413 | * Now we've found the closing brace, look further |
| 1414 | * for the comma. |
| 1415 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1416 | skip_white_(t); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1417 | if (tok_isnt_(t, ",")) |
| 1418 | { |
| 1419 | error(ERR_NONFATAL, |
| 1420 | "braces do not enclose all of macro parameter"); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1421 | while (tok_isnt_(t, ",")) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1422 | t = t->next; |
| 1423 | } |
| 1424 | if (t) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1425 | t = t->next; /* eat the comma */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1426 | } |
| 1427 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1428 | } |
| 1429 | } |
| 1430 | |
| 1431 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1432 | * Determine whether one of the various `if' conditions is true or |
| 1433 | * not. |
| 1434 | * |
| 1435 | * We must free the tline we get passed. |
| 1436 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1437 | static int |
| 1438 | if_condition(Token * tline, int i) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1439 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1440 | int j, casesense; |
| 1441 | Token *t, *tt, **tptr, *origline; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1442 | struct tokenval tokval; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1443 | expr *evalresult; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1444 | |
| 1445 | origline = tline; |
| 1446 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1447 | switch (i) |
| 1448 | { |
| 1449 | case PP_IFCTX: |
| 1450 | case PP_ELIFCTX: |
| 1451 | case PP_IFNCTX: |
| 1452 | case PP_ELIFNCTX: |
| 1453 | j = FALSE; /* have we matched yet? */ |
| 1454 | while (cstk && tline) |
| 1455 | { |
| 1456 | skip_white_(tline); |
| 1457 | if (!tline || tline->type != TOK_ID) |
| 1458 | { |
| 1459 | error(ERR_NONFATAL, |
| 1460 | "`%s' expects context identifiers", |
| 1461 | directives[i]); |
| 1462 | free_tlist(origline); |
| 1463 | return -1; |
| 1464 | } |
| 1465 | if (!nasm_stricmp(tline->text, cstk->name)) |
| 1466 | j = TRUE; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1467 | tline = tline->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1468 | } |
| 1469 | if (i == PP_IFNCTX || i == PP_ELIFNCTX) |
| 1470 | j = !j; |
| 1471 | free_tlist(origline); |
| 1472 | return j; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1473 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1474 | case PP_IFDEF: |
| 1475 | case PP_ELIFDEF: |
| 1476 | case PP_IFNDEF: |
| 1477 | case PP_ELIFNDEF: |
| 1478 | j = FALSE; /* have we matched yet? */ |
| 1479 | while (tline) |
| 1480 | { |
| 1481 | skip_white_(tline); |
| 1482 | if (!tline || (tline->type != TOK_ID && |
| 1483 | (tline->type != TOK_PREPROC_ID || |
| 1484 | tline->text[1] != '$'))) |
| 1485 | { |
| 1486 | error(ERR_NONFATAL, |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 1487 | "`%s' expects macro identifiers", |
| 1488 | directives[i]); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1489 | free_tlist(origline); |
| 1490 | return -1; |
| 1491 | } |
| 1492 | if (smacro_defined(NULL, tline->text, 0, NULL, 1)) |
| 1493 | j = TRUE; |
| 1494 | tline = tline->next; |
| 1495 | } |
| 1496 | if (i == PP_IFNDEF || i == PP_ELIFNDEF) |
| 1497 | j = !j; |
| 1498 | free_tlist(origline); |
| 1499 | return j; |
| 1500 | |
| 1501 | case PP_IFIDN: |
| 1502 | case PP_ELIFIDN: |
| 1503 | case PP_IFNIDN: |
| 1504 | case PP_ELIFNIDN: |
| 1505 | case PP_IFIDNI: |
| 1506 | case PP_ELIFIDNI: |
| 1507 | case PP_IFNIDNI: |
| 1508 | case PP_ELIFNIDNI: |
| 1509 | tline = expand_smacro(tline); |
| 1510 | t = tt = tline; |
| 1511 | while (tok_isnt_(tt, ",")) |
| 1512 | tt = tt->next; |
| 1513 | if (!tt) |
| 1514 | { |
| 1515 | error(ERR_NONFATAL, |
| 1516 | "`%s' expects two comma-separated arguments", |
| 1517 | directives[i]); |
| 1518 | free_tlist(tline); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1519 | return -1; |
| 1520 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1521 | tt = tt->next; |
| 1522 | casesense = (i == PP_IFIDN || i == PP_ELIFIDN || |
| 1523 | i == PP_IFNIDN || i == PP_ELIFNIDN); |
| 1524 | j = TRUE; /* assume equality unless proved not */ |
| 1525 | while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) |
| 1526 | { |
| 1527 | if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) |
| 1528 | { |
| 1529 | error(ERR_NONFATAL, "`%s': more than one comma on line", |
| 1530 | directives[i]); |
| 1531 | free_tlist(tline); |
| 1532 | return -1; |
| 1533 | } |
| 1534 | if (t->type == TOK_WHITESPACE) |
| 1535 | { |
| 1536 | t = t->next; |
| 1537 | continue; |
| 1538 | } |
| 1539 | else if (tt->type == TOK_WHITESPACE) |
| 1540 | { |
| 1541 | tt = tt->next; |
| 1542 | continue; |
| 1543 | } |
| 1544 | else if (tt->type != t->type || |
| 1545 | mstrcmp(tt->text, t->text, casesense)) |
| 1546 | { |
| 1547 | j = FALSE; /* found mismatching tokens */ |
| 1548 | break; |
| 1549 | } |
| 1550 | else |
| 1551 | { |
| 1552 | t = t->next; |
| 1553 | tt = tt->next; |
| 1554 | continue; |
| 1555 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1556 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1557 | if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt) |
| 1558 | j = FALSE; /* trailing gunk on one end or other */ |
| 1559 | if (i == PP_IFNIDN || i == PP_ELIFNIDN || |
| 1560 | i == PP_IFNIDNI || i == PP_ELIFNIDNI) |
| 1561 | j = !j; |
| 1562 | free_tlist(tline); |
| 1563 | return j; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1564 | |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 1565 | case PP_IFMACRO: |
| 1566 | case PP_ELIFMACRO: |
| 1567 | case PP_IFNMACRO: |
| 1568 | case PP_ELIFNMACRO: |
| 1569 | { |
| 1570 | int found = 0; |
| 1571 | MMacro searching, *mmac; |
| 1572 | |
| 1573 | tline = tline->next; |
| 1574 | skip_white_(tline); |
| 1575 | tline = expand_id(tline); |
| 1576 | if (!tok_type_(tline, TOK_ID)) |
| 1577 | { |
| 1578 | error(ERR_NONFATAL, |
| 1579 | "`%s' expects a macro name", |
| 1580 | directives[i]); |
| 1581 | return -1; |
| 1582 | } |
| 1583 | searching.name = nasm_strdup(tline->text); |
| 1584 | searching.casesense = (i == PP_MACRO); |
| 1585 | searching.plus = FALSE; |
| 1586 | searching.nolist = FALSE; |
| 1587 | searching.in_progress = FALSE; |
| 1588 | searching.rep_nest = NULL; |
| 1589 | searching.nparam_min = 0; |
| 1590 | searching.nparam_max = INT_MAX; |
| 1591 | tline = expand_smacro(tline->next); |
| 1592 | skip_white_(tline); |
| 1593 | if (!tline) |
| 1594 | { |
| 1595 | } else if (!tok_type_(tline, TOK_NUMBER)) |
| 1596 | { |
| 1597 | error(ERR_NONFATAL, |
| 1598 | "`%s' expects a parameter count or nothing", |
| 1599 | directives[i]); |
| 1600 | } |
| 1601 | else |
| 1602 | { |
| 1603 | searching.nparam_min = searching.nparam_max = |
| 1604 | readnum(tline->text, &j); |
| 1605 | if (j) |
| 1606 | error(ERR_NONFATAL, |
| 1607 | "unable to parse parameter count `%s'", |
| 1608 | tline->text); |
| 1609 | } |
| 1610 | if (tline && tok_is_(tline->next, "-")) |
| 1611 | { |
| 1612 | tline = tline->next->next; |
| 1613 | if (tok_is_(tline, "*")) |
| 1614 | searching.nparam_max = INT_MAX; |
| 1615 | else if (!tok_type_(tline, TOK_NUMBER)) |
| 1616 | error(ERR_NONFATAL, |
| 1617 | "`%s' expects a parameter count after `-'", |
| 1618 | directives[i]); |
| 1619 | else |
| 1620 | { |
| 1621 | searching.nparam_max = readnum(tline->text, &j); |
| 1622 | if (j) |
| 1623 | error(ERR_NONFATAL, |
| 1624 | "unable to parse parameter count `%s'", |
| 1625 | tline->text); |
| 1626 | if (searching.nparam_min > searching.nparam_max) |
| 1627 | error(ERR_NONFATAL, |
| 1628 | "minimum parameter count exceeds maximum"); |
| 1629 | } |
| 1630 | } |
| 1631 | if (tline && tok_is_(tline->next, "+")) |
| 1632 | { |
| 1633 | tline = tline->next; |
| 1634 | searching.plus = TRUE; |
| 1635 | } |
| 1636 | mmac = mmacros[hash(searching.name)]; |
| 1637 | while (mmac) |
| 1638 | { |
| 1639 | if (!strcmp(mmac->name, searching.name) && |
| 1640 | (mmac->nparam_min <= searching.nparam_max |
| 1641 | || searching.plus) |
| 1642 | && (searching.nparam_min <= mmac->nparam_max |
| 1643 | || mmac->plus)) |
| 1644 | { |
| 1645 | found = TRUE; |
| 1646 | break; |
| 1647 | } |
| 1648 | mmac = mmac->next; |
| 1649 | } |
| 1650 | nasm_free(searching.name); |
| 1651 | free_tlist(origline); |
| 1652 | if (i == PP_IFNMACRO || i == PP_ELIFNMACRO) |
| 1653 | found = !found; |
| 1654 | return found; |
| 1655 | } |
| 1656 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1657 | case PP_IFID: |
| 1658 | case PP_ELIFID: |
| 1659 | case PP_IFNID: |
| 1660 | case PP_ELIFNID: |
| 1661 | case PP_IFNUM: |
| 1662 | case PP_ELIFNUM: |
| 1663 | case PP_IFNNUM: |
| 1664 | case PP_ELIFNNUM: |
| 1665 | case PP_IFSTR: |
| 1666 | case PP_ELIFSTR: |
| 1667 | case PP_IFNSTR: |
| 1668 | case PP_ELIFNSTR: |
| 1669 | tline = expand_smacro(tline); |
| 1670 | t = tline; |
| 1671 | while (tok_type_(t, TOK_WHITESPACE)) |
| 1672 | t = t->next; |
| 1673 | j = FALSE; /* placate optimiser */ |
| 1674 | if (t) |
| 1675 | switch (i) |
| 1676 | { |
| 1677 | case PP_IFID: |
| 1678 | case PP_ELIFID: |
| 1679 | case PP_IFNID: |
| 1680 | case PP_ELIFNID: |
| 1681 | j = (t->type == TOK_ID); |
| 1682 | break; |
| 1683 | case PP_IFNUM: |
| 1684 | case PP_ELIFNUM: |
| 1685 | case PP_IFNNUM: |
| 1686 | case PP_ELIFNNUM: |
| 1687 | j = (t->type == TOK_NUMBER); |
| 1688 | break; |
| 1689 | case PP_IFSTR: |
| 1690 | case PP_ELIFSTR: |
| 1691 | case PP_IFNSTR: |
| 1692 | case PP_ELIFNSTR: |
| 1693 | j = (t->type == TOK_STRING); |
| 1694 | break; |
| 1695 | } |
| 1696 | if (i == PP_IFNID || i == PP_ELIFNID || |
| 1697 | i == PP_IFNNUM || i == PP_ELIFNNUM || |
| 1698 | i == PP_IFNSTR || i == PP_ELIFNSTR) |
| 1699 | j = !j; |
| 1700 | free_tlist(tline); |
| 1701 | return j; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1702 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1703 | case PP_IF: |
| 1704 | case PP_ELIF: |
| 1705 | t = tline = expand_smacro(tline); |
| 1706 | tptr = &t; |
| 1707 | tokval.t_type = TOKEN_INVALID; |
| 1708 | evalresult = evaluate(ppscan, tptr, &tokval, |
| 1709 | NULL, pass | CRITICAL, error, NULL); |
| 1710 | free_tlist(tline); |
| 1711 | if (!evalresult) |
| 1712 | return -1; |
| 1713 | if (tokval.t_type) |
| 1714 | error(ERR_WARNING, |
| 1715 | "trailing garbage after expression ignored"); |
| 1716 | if (!is_simple(evalresult)) |
| 1717 | { |
| 1718 | error(ERR_NONFATAL, |
| 1719 | "non-constant value given to `%s'", directives[i]); |
| 1720 | return -1; |
| 1721 | } |
| 1722 | return reloc_value(evalresult) != 0; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1723 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1724 | default: |
| 1725 | error(ERR_FATAL, |
| 1726 | "preprocessor directive `%s' not yet implemented", |
| 1727 | directives[i]); |
| 1728 | free_tlist(origline); |
| 1729 | return -1; /* yeah, right */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1730 | } |
| 1731 | } |
| 1732 | |
| 1733 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1734 | * Expand macros in a string. Used in %error and %include directives. |
| 1735 | * First tokenise the string, apply "expand_smacro" and then de-tokenise back. |
| 1736 | * The returned variable should ALWAYS be freed after usage. |
| 1737 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1738 | void |
| 1739 | expand_macros_in_string(char **p) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1740 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1741 | Token *line = tokenise(*p); |
| 1742 | line = expand_smacro(line); |
| 1743 | *p = detoken(line, FALSE); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1744 | } |
| 1745 | |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 1746 | /** |
| 1747 | * find and process preprocessor directive in passed line |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1748 | * Find out if a line contains a preprocessor directive, and deal |
| 1749 | * with it if so. |
| 1750 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 1751 | * If a directive _is_ found, it is the responsibility of this routine |
| 1752 | * (and not the caller) to free_tlist() the line. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1753 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 1754 | * @param tline a pointer to the current tokeninzed line linked list |
| 1755 | * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1756 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1757 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1758 | static int |
| 1759 | do_directive(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1760 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1761 | int i, j, k, m, nparam, nolist; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1762 | int offset; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1763 | char *p, *mname; |
| 1764 | Include *inc; |
| 1765 | Context *ctx; |
| 1766 | Cond *cond; |
| 1767 | SMacro *smac, **smhead; |
| 1768 | MMacro *mmac; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1769 | Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline; |
| 1770 | Line *l; |
| 1771 | struct tokenval tokval; |
| 1772 | expr *evalresult; |
H. Peter Anvin | b64535f | 2002-04-30 20:55:37 +0000 | [diff] [blame] | 1773 | MMacro *tmp_defining; /* Used when manipulating rep_nest */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1774 | |
| 1775 | origline = tline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1776 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1777 | skip_white_(tline); |
| 1778 | if (!tok_type_(tline, TOK_PREPROC_ID) || |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1779 | (tline->text[1] == '%' || tline->text[1] == '$' |
| 1780 | || tline->text[1] == '!')) |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 1781 | return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1782 | |
| 1783 | i = -1; |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 1784 | j = elements(directives); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1785 | while (j - i > 1) |
| 1786 | { |
| 1787 | k = (j + i) / 2; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1788 | m = nasm_stricmp(tline->text, directives[k]); |
| 1789 | if (m == 0) { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1790 | if (tasm_compatible_mode) { |
| 1791 | i = k; |
| 1792 | j = -2; |
| 1793 | } else if (k != PP_ARG && k != PP_LOCAL && k != PP_STACKSIZE) { |
| 1794 | i = k; |
| 1795 | j = -2; |
| 1796 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1797 | break; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1798 | } |
| 1799 | else if (m < 0) { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1800 | j = k; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1801 | } |
| 1802 | else |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1803 | i = k; |
| 1804 | } |
| 1805 | |
| 1806 | /* |
| 1807 | * 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] | 1808 | * or walking to the end of an already terminated %rep block, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1809 | * we should ignore all directives except for condition |
| 1810 | * directives. |
| 1811 | */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1812 | if (((istk->conds && !emitting(istk->conds->state)) || |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 1813 | (istk->mstk && !istk->mstk->in_progress)) && |
| 1814 | !is_condition(i)) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1815 | { |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 1816 | return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1817 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1818 | |
| 1819 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1820 | * If we're defining a macro or reading a %rep block, we should |
| 1821 | * ignore all directives except for %macro/%imacro (which |
| 1822 | * 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] | 1823 | * %rep block) %endrep. If we're in a %rep block, another %rep |
| 1824 | * causes an error, so should be let through. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1825 | */ |
| 1826 | if (defining && i != PP_MACRO && i != PP_IMACRO && |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1827 | i != PP_ENDMACRO && i != PP_ENDM && |
| 1828 | (defining->name || (i != PP_ENDREP && i != PP_REP))) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1829 | { |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 1830 | return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1831 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1832 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1833 | if (j != -2) |
| 1834 | { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1835 | error(ERR_NONFATAL, "unknown preprocessor directive `%s'", |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1836 | tline->text); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 1837 | return NO_DIRECTIVE_FOUND; /* didn't get it */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1838 | } |
| 1839 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1840 | switch (i) |
| 1841 | { |
| 1842 | case PP_STACKSIZE: |
| 1843 | /* Directive to tell NASM what the default stack size is. The |
| 1844 | * default is for a 16-bit stack, and this can be overriden with |
| 1845 | * %stacksize large. |
| 1846 | * the following form: |
| 1847 | * |
| 1848 | * ARG arg1:WORD, arg2:DWORD, arg4:QWORD |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1849 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1850 | tline = tline->next; |
| 1851 | if (tline && tline->type == TOK_WHITESPACE) |
| 1852 | tline = tline->next; |
| 1853 | if (!tline || tline->type != TOK_ID) |
| 1854 | { |
| 1855 | error(ERR_NONFATAL, "`%%stacksize' missing size parameter"); |
| 1856 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 1857 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1858 | } |
| 1859 | if (nasm_stricmp(tline->text, "flat") == 0) |
| 1860 | { |
| 1861 | /* All subsequent ARG directives are for a 32-bit stack */ |
| 1862 | StackSize = 4; |
| 1863 | StackPointer = "ebp"; |
| 1864 | ArgOffset = 8; |
| 1865 | LocalOffset = 4; |
| 1866 | } |
| 1867 | else if (nasm_stricmp(tline->text, "large") == 0) |
| 1868 | { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1869 | /* All subsequent ARG directives are for a 16-bit stack, |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1870 | * far function call. |
| 1871 | */ |
| 1872 | StackSize = 2; |
| 1873 | StackPointer = "bp"; |
| 1874 | ArgOffset = 4; |
| 1875 | LocalOffset = 2; |
| 1876 | } |
| 1877 | else if (nasm_stricmp(tline->text, "small") == 0) |
| 1878 | { |
| 1879 | /* All subsequent ARG directives are for a 16-bit stack, |
| 1880 | * far function call. We don't support near functions. |
| 1881 | */ |
| 1882 | StackSize = 2; |
| 1883 | StackPointer = "bp"; |
| 1884 | ArgOffset = 6; |
| 1885 | LocalOffset = 2; |
| 1886 | } |
| 1887 | else |
| 1888 | { |
| 1889 | error(ERR_NONFATAL, "`%%stacksize' invalid size type"); |
| 1890 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 1891 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1892 | } |
| 1893 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 1894 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1895 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1896 | case PP_ARG: |
| 1897 | /* TASM like ARG directive to define arguments to functions, in |
| 1898 | * the following form: |
| 1899 | * |
| 1900 | * ARG arg1:WORD, arg2:DWORD, arg4:QWORD |
| 1901 | */ |
| 1902 | offset = ArgOffset; |
| 1903 | do |
| 1904 | { |
| 1905 | char *arg, directive[256]; |
| 1906 | int size = StackSize; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1907 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1908 | /* Find the argument name */ |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1909 | tline = tline->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1910 | if (tline && tline->type == TOK_WHITESPACE) |
| 1911 | tline = tline->next; |
| 1912 | if (!tline || tline->type != TOK_ID) |
| 1913 | { |
| 1914 | error(ERR_NONFATAL, "`%%arg' missing argument parameter"); |
| 1915 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 1916 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1917 | } |
| 1918 | arg = tline->text; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1919 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1920 | /* Find the argument size type */ |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1921 | tline = tline->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1922 | if (!tline || tline->type != TOK_OTHER |
| 1923 | || tline->text[0] != ':') |
| 1924 | { |
| 1925 | error(ERR_NONFATAL, |
| 1926 | "Syntax error processing `%%arg' directive"); |
| 1927 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 1928 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1929 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1930 | tline = tline->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1931 | if (!tline || tline->type != TOK_ID) |
| 1932 | { |
| 1933 | error(ERR_NONFATAL, |
| 1934 | "`%%arg' missing size type parameter"); |
| 1935 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 1936 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1937 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1938 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1939 | /* Allow macro expansion of type parameter */ |
| 1940 | tt = tokenise(tline->text); |
| 1941 | tt = expand_smacro(tt); |
| 1942 | if (nasm_stricmp(tt->text, "byte") == 0) |
| 1943 | { |
| 1944 | size = MAX(StackSize, 1); |
| 1945 | } |
| 1946 | else if (nasm_stricmp(tt->text, "word") == 0) |
| 1947 | { |
| 1948 | size = MAX(StackSize, 2); |
| 1949 | } |
| 1950 | else if (nasm_stricmp(tt->text, "dword") == 0) |
| 1951 | { |
| 1952 | size = MAX(StackSize, 4); |
| 1953 | } |
| 1954 | else if (nasm_stricmp(tt->text, "qword") == 0) |
| 1955 | { |
| 1956 | size = MAX(StackSize, 8); |
| 1957 | } |
| 1958 | else if (nasm_stricmp(tt->text, "tword") == 0) |
| 1959 | { |
| 1960 | size = MAX(StackSize, 10); |
| 1961 | } |
| 1962 | else |
| 1963 | { |
| 1964 | error(ERR_NONFATAL, |
| 1965 | "Invalid size type for `%%arg' missing directive"); |
| 1966 | free_tlist(tt); |
| 1967 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 1968 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1969 | } |
| 1970 | free_tlist(tt); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1971 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1972 | /* Now define the macro for the argument */ |
| 1973 | sprintf(directive, "%%define %s (%s+%d)", arg, StackPointer, |
| 1974 | offset); |
| 1975 | do_directive(tokenise(directive)); |
| 1976 | offset += size; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1977 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1978 | /* Move to the next argument in the list */ |
| 1979 | tline = tline->next; |
| 1980 | if (tline && tline->type == TOK_WHITESPACE) |
| 1981 | tline = tline->next; |
| 1982 | } |
| 1983 | while (tline && tline->type == TOK_OTHER |
| 1984 | && tline->text[0] == ','); |
| 1985 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 1986 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1987 | |
| 1988 | case PP_LOCAL: |
| 1989 | /* TASM like LOCAL directive to define local variables for a |
| 1990 | * function, in the following form: |
| 1991 | * |
| 1992 | * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize |
| 1993 | * |
| 1994 | * The '= LocalSize' at the end is ignored by NASM, but is |
| 1995 | * required by TASM to define the local parameter size (and used |
| 1996 | * by the TASM macro package). |
| 1997 | */ |
| 1998 | offset = LocalOffset; |
| 1999 | do |
| 2000 | { |
| 2001 | char *local, directive[256]; |
| 2002 | int size = StackSize; |
| 2003 | |
| 2004 | /* Find the argument name */ |
| 2005 | tline = tline->next; |
| 2006 | if (tline && tline->type == TOK_WHITESPACE) |
| 2007 | tline = tline->next; |
| 2008 | if (!tline || tline->type != TOK_ID) |
| 2009 | { |
| 2010 | error(ERR_NONFATAL, |
| 2011 | "`%%local' missing argument parameter"); |
| 2012 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2013 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2014 | } |
| 2015 | local = tline->text; |
| 2016 | |
| 2017 | /* Find the argument size type */ |
| 2018 | tline = tline->next; |
| 2019 | if (!tline || tline->type != TOK_OTHER |
| 2020 | || tline->text[0] != ':') |
| 2021 | { |
| 2022 | error(ERR_NONFATAL, |
| 2023 | "Syntax error processing `%%local' directive"); |
| 2024 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2025 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2026 | } |
| 2027 | tline = tline->next; |
| 2028 | if (!tline || tline->type != TOK_ID) |
| 2029 | { |
| 2030 | error(ERR_NONFATAL, |
| 2031 | "`%%local' missing size type parameter"); |
| 2032 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2033 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2034 | } |
| 2035 | |
| 2036 | /* Allow macro expansion of type parameter */ |
| 2037 | tt = tokenise(tline->text); |
| 2038 | tt = expand_smacro(tt); |
| 2039 | if (nasm_stricmp(tt->text, "byte") == 0) |
| 2040 | { |
| 2041 | size = MAX(StackSize, 1); |
| 2042 | } |
| 2043 | else if (nasm_stricmp(tt->text, "word") == 0) |
| 2044 | { |
| 2045 | size = MAX(StackSize, 2); |
| 2046 | } |
| 2047 | else if (nasm_stricmp(tt->text, "dword") == 0) |
| 2048 | { |
| 2049 | size = MAX(StackSize, 4); |
| 2050 | } |
| 2051 | else if (nasm_stricmp(tt->text, "qword") == 0) |
| 2052 | { |
| 2053 | size = MAX(StackSize, 8); |
| 2054 | } |
| 2055 | else if (nasm_stricmp(tt->text, "tword") == 0) |
| 2056 | { |
| 2057 | size = MAX(StackSize, 10); |
| 2058 | } |
| 2059 | else |
| 2060 | { |
| 2061 | error(ERR_NONFATAL, |
| 2062 | "Invalid size type for `%%local' missing directive"); |
| 2063 | free_tlist(tt); |
| 2064 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2065 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2066 | } |
| 2067 | free_tlist(tt); |
| 2068 | |
| 2069 | /* Now define the macro for the argument */ |
| 2070 | sprintf(directive, "%%define %s (%s-%d)", local, StackPointer, |
| 2071 | offset); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2072 | do_directive(tokenise(directive)); |
| 2073 | offset += size; |
| 2074 | |
| 2075 | /* Now define the assign to setup the enter_c macro correctly */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2076 | sprintf(directive, "%%assign %%$localsize %%$localsize+%d", |
| 2077 | size); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2078 | do_directive(tokenise(directive)); |
| 2079 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2080 | /* Move to the next argument in the list */ |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2081 | tline = tline->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2082 | if (tline && tline->type == TOK_WHITESPACE) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2083 | tline = tline->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2084 | } |
| 2085 | while (tline && tline->type == TOK_OTHER |
| 2086 | && tline->text[0] == ','); |
| 2087 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2088 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2089 | |
| 2090 | case PP_CLEAR: |
| 2091 | if (tline->next) |
| 2092 | error(ERR_WARNING, |
| 2093 | "trailing garbage after `%%clear' ignored"); |
| 2094 | for (j = 0; j < NHASH; j++) |
| 2095 | { |
| 2096 | while (mmacros[j]) |
| 2097 | { |
| 2098 | MMacro *m = mmacros[j]; |
| 2099 | mmacros[j] = m->next; |
| 2100 | free_mmacro(m); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2101 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2102 | while (smacros[j]) |
| 2103 | { |
| 2104 | SMacro *s = smacros[j]; |
| 2105 | smacros[j] = smacros[j]->next; |
| 2106 | nasm_free(s->name); |
| 2107 | free_tlist(s->expansion); |
| 2108 | nasm_free(s); |
| 2109 | } |
| 2110 | } |
| 2111 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2112 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2113 | |
| 2114 | case PP_INCLUDE: |
| 2115 | tline = tline->next; |
| 2116 | skip_white_(tline); |
| 2117 | if (!tline || (tline->type != TOK_STRING && |
| 2118 | tline->type != TOK_INTERNAL_STRING)) |
| 2119 | { |
| 2120 | error(ERR_NONFATAL, "`%%include' expects a file name"); |
| 2121 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2122 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2123 | } |
| 2124 | if (tline->next) |
| 2125 | error(ERR_WARNING, |
| 2126 | "trailing garbage after `%%include' ignored"); |
| 2127 | if (tline->type != TOK_INTERNAL_STRING) |
| 2128 | { |
| 2129 | p = tline->text + 1; /* point past the quote to the name */ |
| 2130 | p[strlen(p) - 1] = '\0'; /* remove the trailing quote */ |
| 2131 | } |
| 2132 | else |
| 2133 | p = tline->text; /* internal_string is easier */ |
| 2134 | expand_macros_in_string(&p); |
| 2135 | inc = nasm_malloc(sizeof(Include)); |
| 2136 | inc->next = istk; |
| 2137 | inc->conds = NULL; |
| 2138 | inc->fp = inc_fopen(p); |
| 2139 | inc->fname = src_set_fname(p); |
| 2140 | inc->lineno = src_set_linnum(0); |
| 2141 | inc->lineinc = 1; |
| 2142 | inc->expansion = NULL; |
| 2143 | inc->mstk = NULL; |
| 2144 | istk = inc; |
| 2145 | list->uplevel(LIST_INCLUDE); |
| 2146 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2147 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2148 | |
| 2149 | case PP_PUSH: |
| 2150 | tline = tline->next; |
| 2151 | skip_white_(tline); |
| 2152 | tline = expand_id(tline); |
| 2153 | if (!tok_type_(tline, TOK_ID)) |
| 2154 | { |
| 2155 | error(ERR_NONFATAL, "`%%push' expects a context identifier"); |
| 2156 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2157 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2158 | } |
| 2159 | if (tline->next) |
| 2160 | error(ERR_WARNING, "trailing garbage after `%%push' ignored"); |
| 2161 | ctx = nasm_malloc(sizeof(Context)); |
| 2162 | ctx->next = cstk; |
| 2163 | ctx->localmac = NULL; |
| 2164 | ctx->name = nasm_strdup(tline->text); |
| 2165 | ctx->number = unique++; |
| 2166 | cstk = ctx; |
| 2167 | free_tlist(origline); |
| 2168 | break; |
| 2169 | |
| 2170 | case PP_REPL: |
| 2171 | tline = tline->next; |
| 2172 | skip_white_(tline); |
| 2173 | tline = expand_id(tline); |
| 2174 | if (!tok_type_(tline, TOK_ID)) |
| 2175 | { |
| 2176 | error(ERR_NONFATAL, "`%%repl' expects a context identifier"); |
| 2177 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2178 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2179 | } |
| 2180 | if (tline->next) |
| 2181 | error(ERR_WARNING, "trailing garbage after `%%repl' ignored"); |
| 2182 | if (!cstk) |
| 2183 | error(ERR_NONFATAL, "`%%repl': context stack is empty"); |
| 2184 | else |
| 2185 | { |
| 2186 | nasm_free(cstk->name); |
| 2187 | cstk->name = nasm_strdup(tline->text); |
| 2188 | } |
| 2189 | free_tlist(origline); |
| 2190 | break; |
| 2191 | |
| 2192 | case PP_POP: |
| 2193 | if (tline->next) |
| 2194 | error(ERR_WARNING, "trailing garbage after `%%pop' ignored"); |
| 2195 | if (!cstk) |
| 2196 | error(ERR_NONFATAL, |
| 2197 | "`%%pop': context stack is already empty"); |
| 2198 | else |
| 2199 | ctx_pop(); |
| 2200 | free_tlist(origline); |
| 2201 | break; |
| 2202 | |
| 2203 | case PP_ERROR: |
| 2204 | tline->next = expand_smacro(tline->next); |
| 2205 | tline = tline->next; |
| 2206 | skip_white_(tline); |
| 2207 | if (tok_type_(tline, TOK_STRING)) |
| 2208 | { |
| 2209 | p = tline->text + 1; /* point past the quote to the name */ |
| 2210 | p[strlen(p) - 1] = '\0'; /* remove the trailing quote */ |
| 2211 | expand_macros_in_string(&p); |
| 2212 | error(ERR_NONFATAL, "%s", p); |
| 2213 | nasm_free(p); |
| 2214 | } |
| 2215 | else |
| 2216 | { |
| 2217 | p = detoken(tline, FALSE); |
| 2218 | error(ERR_WARNING, "%s", p); |
| 2219 | nasm_free(p); |
| 2220 | } |
| 2221 | free_tlist(origline); |
| 2222 | break; |
| 2223 | |
| 2224 | case PP_IF: |
| 2225 | case PP_IFCTX: |
| 2226 | case PP_IFDEF: |
| 2227 | case PP_IFID: |
| 2228 | case PP_IFIDN: |
| 2229 | case PP_IFIDNI: |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 2230 | case PP_IFMACRO: |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2231 | case PP_IFNCTX: |
| 2232 | case PP_IFNDEF: |
| 2233 | case PP_IFNID: |
| 2234 | case PP_IFNIDN: |
| 2235 | case PP_IFNIDNI: |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 2236 | case PP_IFNMACRO: |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2237 | case PP_IFNNUM: |
| 2238 | case PP_IFNSTR: |
| 2239 | case PP_IFNUM: |
| 2240 | case PP_IFSTR: |
| 2241 | if (istk->conds && !emitting(istk->conds->state)) |
| 2242 | j = COND_NEVER; |
| 2243 | else |
| 2244 | { |
| 2245 | j = if_condition(tline->next, i); |
| 2246 | tline->next = NULL; /* it got freed */ |
| 2247 | free_tlist(origline); |
| 2248 | j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; |
| 2249 | } |
| 2250 | cond = nasm_malloc(sizeof(Cond)); |
| 2251 | cond->next = istk->conds; |
| 2252 | cond->state = j; |
| 2253 | istk->conds = cond; |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2254 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2255 | |
| 2256 | case PP_ELIF: |
| 2257 | case PP_ELIFCTX: |
| 2258 | case PP_ELIFDEF: |
| 2259 | case PP_ELIFID: |
| 2260 | case PP_ELIFIDN: |
| 2261 | case PP_ELIFIDNI: |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 2262 | case PP_ELIFMACRO: |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2263 | case PP_ELIFNCTX: |
| 2264 | case PP_ELIFNDEF: |
| 2265 | case PP_ELIFNID: |
| 2266 | case PP_ELIFNIDN: |
| 2267 | case PP_ELIFNIDNI: |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 2268 | case PP_ELIFNMACRO: |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2269 | case PP_ELIFNNUM: |
| 2270 | case PP_ELIFNSTR: |
| 2271 | case PP_ELIFNUM: |
| 2272 | case PP_ELIFSTR: |
| 2273 | if (!istk->conds) |
| 2274 | error(ERR_FATAL, "`%s': no matching `%%if'", directives[i]); |
| 2275 | if (emitting(istk->conds->state) |
| 2276 | || istk->conds->state == COND_NEVER) |
| 2277 | istk->conds->state = COND_NEVER; |
| 2278 | else |
| 2279 | { |
H. Peter Anvin | 0c60815 | 2002-05-22 22:59:40 +0000 | [diff] [blame] | 2280 | /* |
| 2281 | * IMPORTANT: In the case of %if, we will already have |
| 2282 | * called expand_mmac_params(); however, if we're |
| 2283 | * processing an %elif we must have been in a |
| 2284 | * non-emitting mode, which would have inhibited |
| 2285 | * the normal invocation of expand_mmac_params(). Therefore, |
| 2286 | * we have to do it explicitly here. |
| 2287 | */ |
| 2288 | j = if_condition(expand_mmac_params(tline->next), i); |
| 2289 | tline->next = NULL; /* it got freed */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2290 | free_tlist(origline); |
| 2291 | istk->conds->state = |
| 2292 | j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; |
| 2293 | } |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2294 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2295 | |
| 2296 | case PP_ELSE: |
| 2297 | if (tline->next) |
| 2298 | error(ERR_WARNING, "trailing garbage after `%%else' ignored"); |
| 2299 | if (!istk->conds) |
| 2300 | error(ERR_FATAL, "`%%else': no matching `%%if'"); |
| 2301 | if (emitting(istk->conds->state) |
| 2302 | || istk->conds->state == COND_NEVER) |
| 2303 | istk->conds->state = COND_ELSE_FALSE; |
| 2304 | else |
| 2305 | istk->conds->state = COND_ELSE_TRUE; |
| 2306 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2307 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2308 | |
| 2309 | case PP_ENDIF: |
| 2310 | if (tline->next) |
| 2311 | error(ERR_WARNING, |
| 2312 | "trailing garbage after `%%endif' ignored"); |
| 2313 | if (!istk->conds) |
| 2314 | error(ERR_FATAL, "`%%endif': no matching `%%if'"); |
| 2315 | cond = istk->conds; |
| 2316 | istk->conds = cond->next; |
| 2317 | nasm_free(cond); |
| 2318 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2319 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2320 | |
| 2321 | case PP_MACRO: |
| 2322 | case PP_IMACRO: |
| 2323 | if (defining) |
| 2324 | error(ERR_FATAL, |
| 2325 | "`%%%smacro': already defining a macro", |
| 2326 | (i == PP_IMACRO ? "i" : "")); |
| 2327 | tline = tline->next; |
| 2328 | skip_white_(tline); |
| 2329 | tline = expand_id(tline); |
| 2330 | if (!tok_type_(tline, TOK_ID)) |
| 2331 | { |
| 2332 | error(ERR_NONFATAL, |
| 2333 | "`%%%smacro' expects a macro name", |
| 2334 | (i == PP_IMACRO ? "i" : "")); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2335 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2336 | } |
| 2337 | defining = nasm_malloc(sizeof(MMacro)); |
| 2338 | defining->name = nasm_strdup(tline->text); |
| 2339 | defining->casesense = (i == PP_MACRO); |
| 2340 | defining->plus = FALSE; |
| 2341 | defining->nolist = FALSE; |
| 2342 | defining->in_progress = FALSE; |
| 2343 | defining->rep_nest = NULL; |
| 2344 | tline = expand_smacro(tline->next); |
| 2345 | skip_white_(tline); |
| 2346 | if (!tok_type_(tline, TOK_NUMBER)) |
| 2347 | { |
| 2348 | error(ERR_NONFATAL, |
| 2349 | "`%%%smacro' expects a parameter count", |
| 2350 | (i == PP_IMACRO ? "i" : "")); |
| 2351 | defining->nparam_min = defining->nparam_max = 0; |
| 2352 | } |
| 2353 | else |
| 2354 | { |
| 2355 | defining->nparam_min = defining->nparam_max = |
| 2356 | readnum(tline->text, &j); |
| 2357 | if (j) |
| 2358 | error(ERR_NONFATAL, |
| 2359 | "unable to parse parameter count `%s'", |
| 2360 | tline->text); |
| 2361 | } |
| 2362 | if (tline && tok_is_(tline->next, "-")) |
| 2363 | { |
| 2364 | tline = tline->next->next; |
| 2365 | if (tok_is_(tline, "*")) |
| 2366 | defining->nparam_max = INT_MAX; |
| 2367 | else if (!tok_type_(tline, TOK_NUMBER)) |
| 2368 | error(ERR_NONFATAL, |
| 2369 | "`%%%smacro' expects a parameter count after `-'", |
| 2370 | (i == PP_IMACRO ? "i" : "")); |
| 2371 | else |
| 2372 | { |
| 2373 | defining->nparam_max = readnum(tline->text, &j); |
| 2374 | if (j) |
| 2375 | error(ERR_NONFATAL, |
| 2376 | "unable to parse parameter count `%s'", |
| 2377 | tline->text); |
| 2378 | if (defining->nparam_min > defining->nparam_max) |
| 2379 | error(ERR_NONFATAL, |
| 2380 | "minimum parameter count exceeds maximum"); |
| 2381 | } |
| 2382 | } |
| 2383 | if (tline && tok_is_(tline->next, "+")) |
| 2384 | { |
| 2385 | tline = tline->next; |
| 2386 | defining->plus = TRUE; |
| 2387 | } |
| 2388 | if (tline && tok_type_(tline->next, TOK_ID) && |
| 2389 | !nasm_stricmp(tline->next->text, ".nolist")) |
| 2390 | { |
| 2391 | tline = tline->next; |
| 2392 | defining->nolist = TRUE; |
| 2393 | } |
| 2394 | mmac = mmacros[hash(defining->name)]; |
| 2395 | while (mmac) |
| 2396 | { |
| 2397 | if (!strcmp(mmac->name, defining->name) && |
| 2398 | (mmac->nparam_min <= defining->nparam_max |
| 2399 | || defining->plus) |
| 2400 | && (defining->nparam_min <= mmac->nparam_max |
| 2401 | || mmac->plus)) |
| 2402 | { |
| 2403 | error(ERR_WARNING, |
| 2404 | "redefining multi-line macro `%s'", |
| 2405 | defining->name); |
| 2406 | break; |
| 2407 | } |
| 2408 | mmac = mmac->next; |
| 2409 | } |
| 2410 | /* |
| 2411 | * Handle default parameters. |
| 2412 | */ |
| 2413 | if (tline && tline->next) |
| 2414 | { |
| 2415 | defining->dlist = tline->next; |
| 2416 | tline->next = NULL; |
| 2417 | count_mmac_params(defining->dlist, &defining->ndefs, |
| 2418 | &defining->defaults); |
| 2419 | } |
| 2420 | else |
| 2421 | { |
| 2422 | defining->dlist = NULL; |
| 2423 | defining->defaults = NULL; |
| 2424 | } |
| 2425 | defining->expansion = NULL; |
| 2426 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2427 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2428 | |
| 2429 | case PP_ENDM: |
| 2430 | case PP_ENDMACRO: |
| 2431 | if (!defining) |
| 2432 | { |
| 2433 | error(ERR_NONFATAL, "`%s': not defining a macro", |
| 2434 | tline->text); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2435 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2436 | } |
| 2437 | k = hash(defining->name); |
| 2438 | defining->next = mmacros[k]; |
| 2439 | mmacros[k] = defining; |
| 2440 | defining = NULL; |
| 2441 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2442 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2443 | |
| 2444 | case PP_ROTATE: |
| 2445 | if (tline->next && tline->next->type == TOK_WHITESPACE) |
| 2446 | tline = tline->next; |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2447 | if (tline->next == NULL) |
| 2448 | { |
| 2449 | free_tlist(origline); |
| 2450 | error(ERR_NONFATAL, "`%%rotate' missing rotate count"); |
| 2451 | return DIRECTIVE_FOUND; |
| 2452 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2453 | t = expand_smacro(tline->next); |
| 2454 | tline->next = NULL; |
| 2455 | free_tlist(origline); |
| 2456 | tline = t; |
| 2457 | tptr = &t; |
| 2458 | tokval.t_type = TOKEN_INVALID; |
| 2459 | evalresult = |
| 2460 | evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL); |
| 2461 | free_tlist(tline); |
| 2462 | if (!evalresult) |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2463 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2464 | if (tokval.t_type) |
| 2465 | error(ERR_WARNING, |
| 2466 | "trailing garbage after expression ignored"); |
| 2467 | if (!is_simple(evalresult)) |
| 2468 | { |
| 2469 | error(ERR_NONFATAL, "non-constant value given to `%%rotate'"); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2470 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2471 | } |
| 2472 | mmac = istk->mstk; |
| 2473 | while (mmac && !mmac->name) /* avoid mistaking %reps for macros */ |
| 2474 | mmac = mmac->next_active; |
| 2475 | if (!mmac) |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2476 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2477 | error(ERR_NONFATAL, |
| 2478 | "`%%rotate' invoked outside a macro call"); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2479 | } |
| 2480 | else if (mmac->nparam == 0) |
| 2481 | { |
| 2482 | error(ERR_NONFATAL, |
| 2483 | "`%%rotate' invoked within macro without parameters"); |
| 2484 | } |
| 2485 | else |
| 2486 | { |
| 2487 | mmac->rotate = mmac->rotate + reloc_value(evalresult); |
| 2488 | |
| 2489 | if (mmac->rotate < 0) |
| 2490 | mmac->rotate = |
| 2491 | mmac->nparam - (-mmac->rotate) % mmac->nparam; |
| 2492 | mmac->rotate %= mmac->nparam; |
| 2493 | } |
| 2494 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2495 | |
| 2496 | case PP_REP: |
| 2497 | nolist = FALSE; |
| 2498 | tline = tline->next; |
| 2499 | if (tline->next && tline->next->type == TOK_WHITESPACE) |
| 2500 | tline = tline->next; |
| 2501 | if (tline->next && tline->next->type == TOK_ID && |
| 2502 | !nasm_stricmp(tline->next->text, ".nolist")) |
| 2503 | { |
| 2504 | tline = tline->next; |
| 2505 | nolist = TRUE; |
| 2506 | } |
| 2507 | t = expand_smacro(tline->next); |
| 2508 | tline->next = NULL; |
| 2509 | free_tlist(origline); |
| 2510 | tline = t; |
| 2511 | tptr = &t; |
| 2512 | tokval.t_type = TOKEN_INVALID; |
| 2513 | evalresult = |
| 2514 | evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL); |
| 2515 | free_tlist(tline); |
| 2516 | if (!evalresult) |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2517 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2518 | if (tokval.t_type) |
| 2519 | error(ERR_WARNING, |
| 2520 | "trailing garbage after expression ignored"); |
| 2521 | if (!is_simple(evalresult)) |
| 2522 | { |
| 2523 | error(ERR_NONFATAL, "non-constant value given to `%%rep'"); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2524 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2525 | } |
| 2526 | tmp_defining = defining; |
| 2527 | defining = nasm_malloc(sizeof(MMacro)); |
| 2528 | defining->name = NULL; /* flags this macro as a %rep block */ |
| 2529 | defining->casesense = 0; |
| 2530 | defining->plus = FALSE; |
| 2531 | defining->nolist = nolist; |
| 2532 | defining->in_progress = reloc_value(evalresult) + 1; |
| 2533 | defining->nparam_min = defining->nparam_max = 0; |
| 2534 | defining->defaults = NULL; |
| 2535 | defining->dlist = NULL; |
| 2536 | defining->expansion = NULL; |
| 2537 | defining->next_active = istk->mstk; |
| 2538 | defining->rep_nest = tmp_defining; |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2539 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2540 | |
| 2541 | case PP_ENDREP: |
| 2542 | if (!defining || defining->name) |
| 2543 | { |
| 2544 | error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'"); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2545 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2546 | } |
| 2547 | |
| 2548 | /* |
| 2549 | * Now we have a "macro" defined - although it has no name |
| 2550 | * and we won't be entering it in the hash tables - we must |
| 2551 | * push a macro-end marker for it on to istk->expansion. |
| 2552 | * After that, it will take care of propagating itself (a |
| 2553 | * macro-end marker line for a macro which is really a %rep |
| 2554 | * block will cause the macro to be re-expanded, complete |
| 2555 | * with another macro-end marker to ensure the process |
| 2556 | * continues) until the whole expansion is forcibly removed |
| 2557 | * from istk->expansion by a %exitrep. |
| 2558 | */ |
| 2559 | l = nasm_malloc(sizeof(Line)); |
| 2560 | l->next = istk->expansion; |
| 2561 | l->finishes = defining; |
| 2562 | l->first = NULL; |
| 2563 | istk->expansion = l; |
| 2564 | |
| 2565 | istk->mstk = defining; |
| 2566 | |
| 2567 | list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); |
| 2568 | tmp_defining = defining; |
| 2569 | defining = defining->rep_nest; |
| 2570 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2571 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2572 | |
| 2573 | case PP_EXITREP: |
| 2574 | /* |
| 2575 | * We must search along istk->expansion until we hit a |
| 2576 | * macro-end marker for a macro with no name. Then we set |
| 2577 | * its `in_progress' flag to 0. |
| 2578 | */ |
| 2579 | for (l = istk->expansion; l; l = l->next) |
| 2580 | if (l->finishes && !l->finishes->name) |
| 2581 | break; |
| 2582 | |
| 2583 | if (l) |
| 2584 | l->finishes->in_progress = 0; |
| 2585 | else |
| 2586 | error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block"); |
| 2587 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2588 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2589 | |
| 2590 | case PP_XDEFINE: |
| 2591 | case PP_IXDEFINE: |
| 2592 | case PP_DEFINE: |
| 2593 | case PP_IDEFINE: |
| 2594 | tline = tline->next; |
| 2595 | skip_white_(tline); |
| 2596 | tline = expand_id(tline); |
| 2597 | if (!tline || (tline->type != TOK_ID && |
| 2598 | (tline->type != TOK_PREPROC_ID || |
| 2599 | tline->text[1] != '$'))) |
| 2600 | { |
| 2601 | error(ERR_NONFATAL, |
| 2602 | "`%%%s%sdefine' expects a macro identifier", |
| 2603 | ((i == PP_IDEFINE || i == PP_IXDEFINE) ? "i" : ""), |
| 2604 | ((i == PP_XDEFINE || i == PP_IXDEFINE) ? "x" : "")); |
| 2605 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2606 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2607 | } |
| 2608 | |
| 2609 | ctx = get_ctx(tline->text, FALSE); |
| 2610 | if (!ctx) |
| 2611 | smhead = &smacros[hash(tline->text)]; |
| 2612 | else |
| 2613 | smhead = &ctx->localmac; |
| 2614 | mname = tline->text; |
| 2615 | last = tline; |
| 2616 | param_start = tline = tline->next; |
| 2617 | nparam = 0; |
| 2618 | |
| 2619 | /* Expand the macro definition now for %xdefine and %ixdefine */ |
| 2620 | if ((i == PP_XDEFINE) || (i == PP_IXDEFINE)) |
| 2621 | tline = expand_smacro(tline); |
| 2622 | |
| 2623 | if (tok_is_(tline, "(")) |
| 2624 | { |
| 2625 | /* |
| 2626 | * This macro has parameters. |
| 2627 | */ |
| 2628 | |
| 2629 | tline = tline->next; |
| 2630 | while (1) |
| 2631 | { |
| 2632 | skip_white_(tline); |
| 2633 | if (!tline) |
| 2634 | { |
| 2635 | error(ERR_NONFATAL, "parameter identifier expected"); |
| 2636 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2637 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2638 | } |
| 2639 | if (tline->type != TOK_ID) |
| 2640 | { |
| 2641 | error(ERR_NONFATAL, |
| 2642 | "`%s': parameter identifier expected", |
| 2643 | tline->text); |
| 2644 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2645 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2646 | } |
| 2647 | tline->type = TOK_SMAC_PARAM + nparam++; |
| 2648 | tline = tline->next; |
| 2649 | skip_white_(tline); |
| 2650 | if (tok_is_(tline, ",")) |
| 2651 | { |
| 2652 | tline = tline->next; |
| 2653 | continue; |
| 2654 | } |
| 2655 | if (!tok_is_(tline, ")")) |
| 2656 | { |
| 2657 | error(ERR_NONFATAL, |
| 2658 | "`)' expected to terminate macro template"); |
| 2659 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2660 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2661 | } |
| 2662 | break; |
| 2663 | } |
| 2664 | last = tline; |
| 2665 | tline = tline->next; |
| 2666 | } |
| 2667 | if (tok_type_(tline, TOK_WHITESPACE)) |
| 2668 | last = tline, tline = tline->next; |
| 2669 | macro_start = NULL; |
| 2670 | last->next = NULL; |
| 2671 | t = tline; |
| 2672 | while (t) |
| 2673 | { |
| 2674 | if (t->type == TOK_ID) |
| 2675 | { |
| 2676 | for (tt = param_start; tt; tt = tt->next) |
| 2677 | if (tt->type >= TOK_SMAC_PARAM && |
| 2678 | !strcmp(tt->text, t->text)) |
| 2679 | t->type = tt->type; |
| 2680 | } |
| 2681 | tt = t->next; |
| 2682 | t->next = macro_start; |
| 2683 | macro_start = t; |
| 2684 | t = tt; |
| 2685 | } |
| 2686 | /* |
| 2687 | * Good. We now have a macro name, a parameter count, and a |
| 2688 | * token list (in reverse order) for an expansion. We ought |
| 2689 | * to be OK just to create an SMacro, store it, and let |
| 2690 | * free_tlist have the rest of the line (which we have |
| 2691 | * carefully re-terminated after chopping off the expansion |
| 2692 | * from the end). |
| 2693 | */ |
| 2694 | if (smacro_defined(ctx, mname, nparam, &smac, i == PP_DEFINE)) |
| 2695 | { |
| 2696 | if (!smac) |
| 2697 | { |
| 2698 | error(ERR_WARNING, |
| 2699 | "single-line macro `%s' defined both with and" |
| 2700 | " without parameters", mname); |
| 2701 | free_tlist(origline); |
| 2702 | free_tlist(macro_start); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2703 | return DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2704 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2705 | else |
| 2706 | { |
| 2707 | /* |
| 2708 | * We're redefining, so we have to take over an |
| 2709 | * existing SMacro structure. This means freeing |
| 2710 | * what was already in it. |
| 2711 | */ |
| 2712 | nasm_free(smac->name); |
| 2713 | free_tlist(smac->expansion); |
| 2714 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2715 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2716 | else |
| 2717 | { |
| 2718 | smac = nasm_malloc(sizeof(SMacro)); |
| 2719 | smac->next = *smhead; |
| 2720 | *smhead = smac; |
| 2721 | } |
| 2722 | smac->name = nasm_strdup(mname); |
| 2723 | smac->casesense = ((i == PP_DEFINE) || (i == PP_XDEFINE)); |
| 2724 | smac->nparam = nparam; |
| 2725 | smac->expansion = macro_start; |
| 2726 | smac->in_progress = FALSE; |
| 2727 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2728 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2729 | |
| 2730 | case PP_UNDEF: |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2731 | tline = tline->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2732 | skip_white_(tline); |
| 2733 | tline = expand_id(tline); |
| 2734 | if (!tline || (tline->type != TOK_ID && |
| 2735 | (tline->type != TOK_PREPROC_ID || |
| 2736 | tline->text[1] != '$'))) |
| 2737 | { |
| 2738 | error(ERR_NONFATAL, "`%%undef' expects a macro identifier"); |
| 2739 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2740 | return DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2741 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2742 | if (tline->next) |
| 2743 | { |
| 2744 | error(ERR_WARNING, |
| 2745 | "trailing garbage after macro name ignored"); |
| 2746 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2747 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2748 | /* Find the context that symbol belongs to */ |
| 2749 | ctx = get_ctx(tline->text, FALSE); |
| 2750 | if (!ctx) |
| 2751 | smhead = &smacros[hash(tline->text)]; |
| 2752 | else |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2753 | smhead = &ctx->localmac; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2754 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2755 | mname = tline->text; |
| 2756 | last = tline; |
| 2757 | last->next = NULL; |
| 2758 | |
| 2759 | /* |
| 2760 | * We now have a macro name... go hunt for it. |
| 2761 | */ |
| 2762 | while (smacro_defined(ctx, mname, -1, &smac, 1)) |
| 2763 | { |
| 2764 | /* Defined, so we need to find its predecessor and nuke it */ |
| 2765 | SMacro **s; |
| 2766 | for (s = smhead; *s && *s != smac; s = &(*s)->next); |
| 2767 | if (*s) |
| 2768 | { |
| 2769 | *s = smac->next; |
| 2770 | nasm_free(smac->name); |
| 2771 | free_tlist(smac->expansion); |
| 2772 | nasm_free(smac); |
| 2773 | } |
| 2774 | } |
| 2775 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2776 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2777 | |
| 2778 | case PP_STRLEN: |
| 2779 | tline = tline->next; |
| 2780 | skip_white_(tline); |
| 2781 | tline = expand_id(tline); |
| 2782 | if (!tline || (tline->type != TOK_ID && |
| 2783 | (tline->type != TOK_PREPROC_ID || |
| 2784 | tline->text[1] != '$'))) |
| 2785 | { |
| 2786 | error(ERR_NONFATAL, |
| 2787 | "`%%strlen' expects a macro identifier as first parameter"); |
| 2788 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2789 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2790 | } |
| 2791 | ctx = get_ctx(tline->text, FALSE); |
| 2792 | if (!ctx) |
| 2793 | smhead = &smacros[hash(tline->text)]; |
| 2794 | else |
| 2795 | smhead = &ctx->localmac; |
| 2796 | mname = tline->text; |
| 2797 | last = tline; |
| 2798 | tline = expand_smacro(tline->next); |
| 2799 | last->next = NULL; |
| 2800 | |
| 2801 | t = tline; |
| 2802 | while (tok_type_(t, TOK_WHITESPACE)) |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2803 | t = t->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2804 | /* t should now point to the string */ |
| 2805 | if (t->type != TOK_STRING) |
| 2806 | { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2807 | error(ERR_NONFATAL, |
| 2808 | "`%%strlen` requires string as second parameter"); |
| 2809 | free_tlist(tline); |
| 2810 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2811 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2812 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2813 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2814 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 2815 | macro_start->next = NULL; |
| 2816 | make_tok_num(macro_start, strlen(t->text) - 2); |
| 2817 | macro_start->mac = NULL; |
| 2818 | |
| 2819 | /* |
| 2820 | * We now have a macro name, an implicit parameter count of |
| 2821 | * zero, and a numeric token to use as an expansion. Create |
| 2822 | * and store an SMacro. |
| 2823 | */ |
| 2824 | if (smacro_defined(ctx, mname, 0, &smac, i == PP_STRLEN)) |
| 2825 | { |
| 2826 | if (!smac) |
| 2827 | error(ERR_WARNING, |
| 2828 | "single-line macro `%s' defined both with and" |
| 2829 | " without parameters", mname); |
| 2830 | else |
| 2831 | { |
| 2832 | /* |
| 2833 | * We're redefining, so we have to take over an |
| 2834 | * existing SMacro structure. This means freeing |
| 2835 | * what was already in it. |
| 2836 | */ |
| 2837 | nasm_free(smac->name); |
| 2838 | free_tlist(smac->expansion); |
| 2839 | } |
| 2840 | } |
| 2841 | else |
| 2842 | { |
| 2843 | smac = nasm_malloc(sizeof(SMacro)); |
| 2844 | smac->next = *smhead; |
| 2845 | *smhead = smac; |
| 2846 | } |
| 2847 | smac->name = nasm_strdup(mname); |
| 2848 | smac->casesense = (i == PP_STRLEN); |
| 2849 | smac->nparam = 0; |
| 2850 | smac->expansion = macro_start; |
| 2851 | smac->in_progress = FALSE; |
| 2852 | free_tlist(tline); |
| 2853 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2854 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2855 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2856 | case PP_SUBSTR: |
| 2857 | tline = tline->next; |
| 2858 | skip_white_(tline); |
| 2859 | tline = expand_id(tline); |
| 2860 | if (!tline || (tline->type != TOK_ID && |
| 2861 | (tline->type != TOK_PREPROC_ID || |
| 2862 | tline->text[1] != '$'))) |
| 2863 | { |
| 2864 | error(ERR_NONFATAL, |
| 2865 | "`%%substr' expects a macro identifier as first parameter"); |
| 2866 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2867 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2868 | } |
| 2869 | ctx = get_ctx(tline->text, FALSE); |
| 2870 | if (!ctx) |
| 2871 | smhead = &smacros[hash(tline->text)]; |
| 2872 | else |
| 2873 | smhead = &ctx->localmac; |
| 2874 | mname = tline->text; |
| 2875 | last = tline; |
| 2876 | tline = expand_smacro(tline->next); |
| 2877 | last->next = NULL; |
| 2878 | |
| 2879 | t = tline->next; |
| 2880 | while (tok_type_(t, TOK_WHITESPACE)) |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2881 | t = t->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2882 | |
| 2883 | /* t should now point to the string */ |
| 2884 | if (t->type != TOK_STRING) |
| 2885 | { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2886 | error(ERR_NONFATAL, |
| 2887 | "`%%substr` requires string as second parameter"); |
| 2888 | free_tlist(tline); |
| 2889 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2890 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2891 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2892 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2893 | tt = t->next; |
| 2894 | tptr = &tt; |
| 2895 | tokval.t_type = TOKEN_INVALID; |
| 2896 | evalresult = |
| 2897 | evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL); |
| 2898 | if (!evalresult) |
| 2899 | { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2900 | free_tlist(tline); |
| 2901 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2902 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2903 | } |
| 2904 | if (!is_simple(evalresult)) |
| 2905 | { |
| 2906 | error(ERR_NONFATAL, "non-constant value given to `%%substr`"); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2907 | free_tlist(tline); |
| 2908 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2909 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2910 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2911 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2912 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 2913 | macro_start->next = NULL; |
| 2914 | macro_start->text = nasm_strdup("'''"); |
| 2915 | if (evalresult->value > 0 |
| 2916 | && evalresult->value < strlen(t->text) - 1) |
| 2917 | { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2918 | macro_start->text[1] = t->text[evalresult->value]; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2919 | } |
| 2920 | else |
| 2921 | { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2922 | macro_start->text[2] = '\0'; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2923 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2924 | macro_start->type = TOK_STRING; |
| 2925 | macro_start->mac = NULL; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2926 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2927 | /* |
| 2928 | * We now have a macro name, an implicit parameter count of |
| 2929 | * zero, and a numeric token to use as an expansion. Create |
| 2930 | * and store an SMacro. |
| 2931 | */ |
| 2932 | if (smacro_defined(ctx, mname, 0, &smac, i == PP_SUBSTR)) |
| 2933 | { |
| 2934 | if (!smac) |
| 2935 | error(ERR_WARNING, |
| 2936 | "single-line macro `%s' defined both with and" |
| 2937 | " without parameters", mname); |
| 2938 | else |
| 2939 | { |
| 2940 | /* |
| 2941 | * We're redefining, so we have to take over an |
| 2942 | * existing SMacro structure. This means freeing |
| 2943 | * what was already in it. |
| 2944 | */ |
| 2945 | nasm_free(smac->name); |
| 2946 | free_tlist(smac->expansion); |
| 2947 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2948 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2949 | else |
| 2950 | { |
| 2951 | smac = nasm_malloc(sizeof(SMacro)); |
| 2952 | smac->next = *smhead; |
| 2953 | *smhead = smac; |
| 2954 | } |
| 2955 | smac->name = nasm_strdup(mname); |
| 2956 | smac->casesense = (i == PP_SUBSTR); |
| 2957 | smac->nparam = 0; |
| 2958 | smac->expansion = macro_start; |
| 2959 | smac->in_progress = FALSE; |
| 2960 | free_tlist(tline); |
| 2961 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2962 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2963 | |
| 2964 | |
| 2965 | case PP_ASSIGN: |
| 2966 | case PP_IASSIGN: |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2967 | tline = tline->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2968 | skip_white_(tline); |
| 2969 | tline = expand_id(tline); |
| 2970 | if (!tline || (tline->type != TOK_ID && |
| 2971 | (tline->type != TOK_PREPROC_ID || |
| 2972 | tline->text[1] != '$'))) |
| 2973 | { |
| 2974 | error(ERR_NONFATAL, |
| 2975 | "`%%%sassign' expects a macro identifier", |
| 2976 | (i == PP_IASSIGN ? "i" : "")); |
| 2977 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2978 | return DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2979 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2980 | ctx = get_ctx(tline->text, FALSE); |
| 2981 | if (!ctx) |
| 2982 | smhead = &smacros[hash(tline->text)]; |
| 2983 | else |
| 2984 | smhead = &ctx->localmac; |
| 2985 | mname = tline->text; |
| 2986 | last = tline; |
| 2987 | tline = expand_smacro(tline->next); |
| 2988 | last->next = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2989 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2990 | t = tline; |
| 2991 | tptr = &t; |
| 2992 | tokval.t_type = TOKEN_INVALID; |
| 2993 | evalresult = |
| 2994 | evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL); |
| 2995 | free_tlist(tline); |
| 2996 | if (!evalresult) |
| 2997 | { |
| 2998 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2999 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3000 | } |
| 3001 | |
| 3002 | if (tokval.t_type) |
| 3003 | error(ERR_WARNING, |
| 3004 | "trailing garbage after expression ignored"); |
| 3005 | |
| 3006 | if (!is_simple(evalresult)) |
| 3007 | { |
| 3008 | error(ERR_NONFATAL, |
| 3009 | "non-constant value given to `%%%sassign'", |
| 3010 | (i == PP_IASSIGN ? "i" : "")); |
| 3011 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 3012 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3013 | } |
| 3014 | |
| 3015 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3016 | macro_start->next = NULL; |
| 3017 | make_tok_num(macro_start, reloc_value(evalresult)); |
| 3018 | macro_start->mac = NULL; |
| 3019 | |
| 3020 | /* |
| 3021 | * We now have a macro name, an implicit parameter count of |
| 3022 | * zero, and a numeric token to use as an expansion. Create |
| 3023 | * and store an SMacro. |
| 3024 | */ |
| 3025 | if (smacro_defined(ctx, mname, 0, &smac, i == PP_ASSIGN)) |
| 3026 | { |
| 3027 | if (!smac) |
| 3028 | error(ERR_WARNING, |
| 3029 | "single-line macro `%s' defined both with and" |
| 3030 | " without parameters", mname); |
| 3031 | else |
| 3032 | { |
| 3033 | /* |
| 3034 | * We're redefining, so we have to take over an |
| 3035 | * existing SMacro structure. This means freeing |
| 3036 | * what was already in it. |
| 3037 | */ |
| 3038 | nasm_free(smac->name); |
| 3039 | free_tlist(smac->expansion); |
| 3040 | } |
| 3041 | } |
| 3042 | else |
| 3043 | { |
| 3044 | smac = nasm_malloc(sizeof(SMacro)); |
| 3045 | smac->next = *smhead; |
| 3046 | *smhead = smac; |
| 3047 | } |
| 3048 | smac->name = nasm_strdup(mname); |
| 3049 | smac->casesense = (i == PP_ASSIGN); |
| 3050 | smac->nparam = 0; |
| 3051 | smac->expansion = macro_start; |
| 3052 | smac->in_progress = FALSE; |
| 3053 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 3054 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3055 | |
| 3056 | case PP_LINE: |
| 3057 | /* |
| 3058 | * Syntax is `%line nnn[+mmm] [filename]' |
| 3059 | */ |
| 3060 | tline = tline->next; |
| 3061 | skip_white_(tline); |
| 3062 | if (!tok_type_(tline, TOK_NUMBER)) |
| 3063 | { |
| 3064 | error(ERR_NONFATAL, "`%%line' expects line number"); |
| 3065 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 3066 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3067 | } |
| 3068 | k = readnum(tline->text, &j); |
| 3069 | m = 1; |
| 3070 | tline = tline->next; |
| 3071 | if (tok_is_(tline, "+")) |
| 3072 | { |
| 3073 | tline = tline->next; |
| 3074 | if (!tok_type_(tline, TOK_NUMBER)) |
| 3075 | { |
| 3076 | error(ERR_NONFATAL, "`%%line' expects line increment"); |
| 3077 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 3078 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3079 | } |
| 3080 | m = readnum(tline->text, &j); |
| 3081 | tline = tline->next; |
| 3082 | } |
| 3083 | skip_white_(tline); |
| 3084 | src_set_linnum(k); |
| 3085 | istk->lineinc = m; |
| 3086 | if (tline) |
| 3087 | { |
| 3088 | nasm_free(src_set_fname(detoken(tline, FALSE))); |
| 3089 | } |
| 3090 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 3091 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3092 | |
| 3093 | default: |
| 3094 | error(ERR_FATAL, |
| 3095 | "preprocessor directive `%s' not yet implemented", |
| 3096 | directives[i]); |
| 3097 | break; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3098 | } |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 3099 | return DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3100 | } |
| 3101 | |
| 3102 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3103 | * Ensure that a macro parameter contains a condition code and |
| 3104 | * nothing else. Return the condition code index if so, or -1 |
| 3105 | * otherwise. |
| 3106 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3107 | static int |
| 3108 | find_cc(Token * t) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3109 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3110 | Token *tt; |
| 3111 | int i, j, k, m; |
| 3112 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3113 | skip_white_(t); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3114 | if (t->type != TOK_ID) |
| 3115 | return -1; |
| 3116 | tt = t->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3117 | skip_white_(tt); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3118 | if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ","))) |
| 3119 | return -1; |
| 3120 | |
| 3121 | i = -1; |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 3122 | j = elements(conditions); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3123 | while (j - i > 1) |
| 3124 | { |
| 3125 | k = (j + i) / 2; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3126 | m = nasm_stricmp(t->text, conditions[k]); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3127 | if (m == 0) |
| 3128 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3129 | i = k; |
| 3130 | j = -2; |
| 3131 | break; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3132 | } |
| 3133 | else if (m < 0) |
| 3134 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3135 | j = k; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3136 | } |
| 3137 | else |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3138 | i = k; |
| 3139 | } |
| 3140 | if (j != -2) |
| 3141 | return -1; |
| 3142 | return i; |
| 3143 | } |
| 3144 | |
| 3145 | /* |
| 3146 | * Expand MMacro-local things: parameter references (%0, %n, %+n, |
| 3147 | * %-n) and MMacro-local identifiers (%%foo). |
| 3148 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3149 | static Token * |
| 3150 | expand_mmac_params(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3151 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3152 | Token *t, *tt, **tail, *thead; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3153 | |
| 3154 | tail = &thead; |
| 3155 | thead = NULL; |
| 3156 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3157 | while (tline) |
| 3158 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3159 | if (tline->type == TOK_PREPROC_ID && |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3160 | (((tline->text[1] == '+' || tline->text[1] == '-') |
| 3161 | && tline->text[2]) || tline->text[1] == '%' |
| 3162 | || (tline->text[1] >= '0' && tline->text[1] <= '9'))) |
| 3163 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3164 | char *text = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3165 | int type = 0, cc; /* type = 0 to placate optimisers */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3166 | char tmpbuf[30]; |
| 3167 | int n, i; |
| 3168 | MMacro *mac; |
| 3169 | |
| 3170 | t = tline; |
| 3171 | tline = tline->next; |
| 3172 | |
| 3173 | mac = istk->mstk; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3174 | while (mac && !mac->name) /* avoid mistaking %reps for macros */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3175 | mac = mac->next_active; |
| 3176 | if (!mac) |
| 3177 | error(ERR_NONFATAL, "`%s': not in a macro call", t->text); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3178 | else |
| 3179 | switch (t->text[1]) |
| 3180 | { |
| 3181 | /* |
| 3182 | * We have to make a substitution of one of the |
| 3183 | * forms %1, %-1, %+1, %%foo, %0. |
| 3184 | */ |
| 3185 | case '0': |
| 3186 | type = TOK_NUMBER; |
| 3187 | sprintf(tmpbuf, "%d", mac->nparam); |
| 3188 | text = nasm_strdup(tmpbuf); |
| 3189 | break; |
| 3190 | case '%': |
| 3191 | type = TOK_ID; |
| 3192 | sprintf(tmpbuf, "..@%lu.", mac->unique); |
| 3193 | text = nasm_strcat(tmpbuf, t->text + 2); |
| 3194 | break; |
| 3195 | case '-': |
| 3196 | n = atoi(t->text + 2) - 1; |
| 3197 | if (n >= mac->nparam) |
| 3198 | tt = NULL; |
| 3199 | else |
| 3200 | { |
| 3201 | if (mac->nparam > 1) |
| 3202 | n = (n + mac->rotate) % mac->nparam; |
| 3203 | tt = mac->params[n]; |
| 3204 | } |
| 3205 | cc = find_cc(tt); |
| 3206 | if (cc == -1) |
| 3207 | { |
| 3208 | error(ERR_NONFATAL, |
| 3209 | "macro parameter %d is not a condition code", |
| 3210 | n + 1); |
| 3211 | text = NULL; |
| 3212 | } |
| 3213 | else |
| 3214 | { |
| 3215 | type = TOK_ID; |
| 3216 | if (inverse_ccs[cc] == -1) |
| 3217 | { |
| 3218 | error(ERR_NONFATAL, |
| 3219 | "condition code `%s' is not invertible", |
| 3220 | conditions[cc]); |
| 3221 | text = NULL; |
| 3222 | } |
| 3223 | else |
| 3224 | text = |
| 3225 | nasm_strdup(conditions[inverse_ccs |
| 3226 | [cc]]); |
| 3227 | } |
| 3228 | break; |
| 3229 | case '+': |
| 3230 | n = atoi(t->text + 2) - 1; |
| 3231 | if (n >= mac->nparam) |
| 3232 | tt = NULL; |
| 3233 | else |
| 3234 | { |
| 3235 | if (mac->nparam > 1) |
| 3236 | n = (n + mac->rotate) % mac->nparam; |
| 3237 | tt = mac->params[n]; |
| 3238 | } |
| 3239 | cc = find_cc(tt); |
| 3240 | if (cc == -1) |
| 3241 | { |
| 3242 | error(ERR_NONFATAL, |
| 3243 | "macro parameter %d is not a condition code", |
| 3244 | n + 1); |
| 3245 | text = NULL; |
| 3246 | } |
| 3247 | else |
| 3248 | { |
| 3249 | type = TOK_ID; |
| 3250 | text = nasm_strdup(conditions[cc]); |
| 3251 | } |
| 3252 | break; |
| 3253 | default: |
| 3254 | n = atoi(t->text + 1) - 1; |
| 3255 | if (n >= mac->nparam) |
| 3256 | tt = NULL; |
| 3257 | else |
| 3258 | { |
| 3259 | if (mac->nparam > 1) |
| 3260 | n = (n + mac->rotate) % mac->nparam; |
| 3261 | tt = mac->params[n]; |
| 3262 | } |
| 3263 | if (tt) |
| 3264 | { |
| 3265 | for (i = 0; i < mac->paramlen[n]; i++) |
| 3266 | { |
| 3267 | *tail = |
| 3268 | new_Token(NULL, tt->type, tt->text, |
| 3269 | 0); |
| 3270 | tail = &(*tail)->next; |
| 3271 | tt = tt->next; |
| 3272 | } |
| 3273 | } |
| 3274 | text = NULL; /* we've done it here */ |
| 3275 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3276 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3277 | if (!text) |
| 3278 | { |
| 3279 | delete_Token(t); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3280 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3281 | else |
| 3282 | { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3283 | *tail = t; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3284 | tail = &t->next; |
| 3285 | t->type = type; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3286 | nasm_free(t->text); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3287 | t->text = text; |
| 3288 | t->mac = NULL; |
| 3289 | } |
| 3290 | continue; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3291 | } |
| 3292 | else |
| 3293 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3294 | t = *tail = tline; |
| 3295 | tline = tline->next; |
| 3296 | t->mac = NULL; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3297 | tail = &t->next; |
| 3298 | } |
| 3299 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3300 | *tail = NULL; |
| 3301 | t = thead; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3302 | for (; t && (tt = t->next) != NULL; t = t->next) |
| 3303 | switch (t->type) |
| 3304 | { |
| 3305 | case TOK_WHITESPACE: |
| 3306 | if (tt->type == TOK_WHITESPACE) |
| 3307 | { |
| 3308 | t->next = delete_Token(tt); |
| 3309 | } |
| 3310 | break; |
| 3311 | case TOK_ID: |
| 3312 | if (tt->type == TOK_ID || tt->type == TOK_NUMBER) |
| 3313 | { |
| 3314 | char *tmp = nasm_strcat(t->text, tt->text); |
| 3315 | nasm_free(t->text); |
| 3316 | t->text = tmp; |
| 3317 | t->next = delete_Token(tt); |
| 3318 | } |
| 3319 | break; |
| 3320 | case TOK_NUMBER: |
| 3321 | if (tt->type == TOK_NUMBER) |
| 3322 | { |
| 3323 | char *tmp = nasm_strcat(t->text, tt->text); |
| 3324 | nasm_free(t->text); |
| 3325 | t->text = tmp; |
| 3326 | t->next = delete_Token(tt); |
| 3327 | } |
| 3328 | break; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3329 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3330 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3331 | return thead; |
| 3332 | } |
| 3333 | |
| 3334 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3335 | * Expand all single-line macro calls made in the given line. |
| 3336 | * Return the expanded version of the line. The original is deemed |
| 3337 | * to be destroyed in the process. (In reality we'll just move |
| 3338 | * Tokens from input to output a lot of the time, rather than |
| 3339 | * actually bothering to destroy and replicate.) |
| 3340 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3341 | static Token * |
| 3342 | expand_smacro(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3343 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3344 | Token *t, *tt, *mstart, **tail, *thead; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3345 | SMacro *head = NULL, *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3346 | Token **params; |
| 3347 | int *paramsize; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3348 | int nparam, sparam, brackets, rescan; |
| 3349 | Token *org_tline = tline; |
| 3350 | Context *ctx; |
| 3351 | char *mname; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3352 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3353 | /* |
| 3354 | * Trick: we should avoid changing the start token pointer since it can |
| 3355 | * be contained in "next" field of other token. Because of this |
| 3356 | * we allocate a copy of first token and work with it; at the end of |
| 3357 | * routine we copy it back |
| 3358 | */ |
| 3359 | if (org_tline) |
| 3360 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3361 | tline = |
| 3362 | new_Token(org_tline->next, org_tline->type, org_tline->text, |
| 3363 | 0); |
| 3364 | tline->mac = org_tline->mac; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 3365 | nasm_free(org_tline->text); |
| 3366 | org_tline->text = NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3367 | } |
| 3368 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3369 | again: |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3370 | tail = &thead; |
| 3371 | thead = NULL; |
| 3372 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3373 | while (tline) |
| 3374 | { /* main token loop */ |
| 3375 | if ((mname = tline->text)) |
| 3376 | { |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3377 | /* if this token is a local macro, look in local context */ |
| 3378 | if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3379 | ctx = get_ctx(mname, TRUE); |
| 3380 | else |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3381 | ctx = NULL; |
| 3382 | if (!ctx) |
| 3383 | head = smacros[hash(mname)]; |
| 3384 | else |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3385 | head = ctx->localmac; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3386 | /* |
| 3387 | * We've hit an identifier. As in is_mmacro below, we first |
| 3388 | * check whether the identifier is a single-line macro at |
| 3389 | * all, then think about checking for parameters if |
| 3390 | * necessary. |
| 3391 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3392 | for (m = head; m; m = m->next) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3393 | if (!mstrcmp(m->name, mname, m->casesense)) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3394 | break; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3395 | if (m) |
| 3396 | { |
| 3397 | mstart = tline; |
| 3398 | params = NULL; |
| 3399 | paramsize = NULL; |
| 3400 | if (m->nparam == 0) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3401 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3402 | /* |
| 3403 | * Simple case: the macro is parameterless. Discard the |
| 3404 | * one token that the macro call took, and push the |
| 3405 | * expansion back on the to-do stack. |
| 3406 | */ |
| 3407 | if (!m->expansion) |
| 3408 | { |
| 3409 | if (!strcmp("__FILE__", m->name)) |
| 3410 | { |
| 3411 | long num = 0; |
| 3412 | src_get(&num, &(tline->text)); |
| 3413 | nasm_quote(&(tline->text)); |
| 3414 | tline->type = TOK_STRING; |
| 3415 | continue; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3416 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3417 | if (!strcmp("__LINE__", m->name)) |
| 3418 | { |
| 3419 | nasm_free(tline->text); |
| 3420 | make_tok_num(tline, src_get_linnum()); |
| 3421 | continue; |
| 3422 | } |
| 3423 | tline = delete_Token(tline); |
| 3424 | continue; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3425 | } |
| 3426 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3427 | else |
| 3428 | { |
| 3429 | /* |
| 3430 | * Complicated case: at least one macro with this name |
| 3431 | * exists and takes parameters. We must find the |
| 3432 | * parameters in the call, count them, find the SMacro |
| 3433 | * that corresponds to that form of the macro call, and |
| 3434 | * substitute for the parameters when we expand. What a |
| 3435 | * pain. |
| 3436 | */ |
| 3437 | tline = tline->next; |
| 3438 | skip_white_(tline); |
| 3439 | if (!tok_is_(tline, "(")) |
| 3440 | { |
| 3441 | /* |
| 3442 | * This macro wasn't called with parameters: ignore |
| 3443 | * the call. (Behaviour borrowed from gnu cpp.) |
| 3444 | */ |
| 3445 | tline = mstart; |
| 3446 | m = NULL; |
| 3447 | } |
| 3448 | else |
| 3449 | { |
| 3450 | int paren = 0; |
| 3451 | int white = 0; |
| 3452 | brackets = 0; |
| 3453 | nparam = 0; |
| 3454 | tline = tline->next; |
| 3455 | sparam = PARAM_DELTA; |
| 3456 | params = nasm_malloc(sparam * sizeof(Token *)); |
| 3457 | params[0] = tline; |
| 3458 | paramsize = nasm_malloc(sparam * sizeof(int)); |
| 3459 | paramsize[0] = 0; |
| 3460 | for (;; tline = tline->next) |
| 3461 | { /* parameter loop */ |
| 3462 | if (!tline) |
| 3463 | { |
| 3464 | error(ERR_NONFATAL, |
| 3465 | "macro call expects terminating `)'"); |
| 3466 | break; |
| 3467 | } |
| 3468 | if (tline->type == TOK_WHITESPACE |
| 3469 | && brackets <= 0) |
| 3470 | { |
| 3471 | if (paramsize[nparam]) |
| 3472 | white++; |
| 3473 | else |
| 3474 | params[nparam] = tline->next; |
| 3475 | continue; /* parameter loop */ |
| 3476 | } |
| 3477 | if (tline->type == TOK_OTHER |
| 3478 | && tline->text[1] == 0) |
| 3479 | { |
| 3480 | char ch = tline->text[0]; |
| 3481 | if (ch == ',' && !paren && brackets <= 0) |
| 3482 | { |
| 3483 | if (++nparam >= sparam) |
| 3484 | { |
| 3485 | sparam += PARAM_DELTA; |
| 3486 | params = nasm_realloc(params, |
| 3487 | sparam * sizeof(Token *)); |
| 3488 | paramsize = nasm_realloc(paramsize, |
| 3489 | sparam * sizeof(int)); |
| 3490 | } |
| 3491 | params[nparam] = tline->next; |
| 3492 | paramsize[nparam] = 0; |
| 3493 | white = 0; |
| 3494 | continue; /* parameter loop */ |
| 3495 | } |
| 3496 | if (ch == '{' && |
| 3497 | (brackets > 0 || (brackets == 0 && |
| 3498 | !paramsize[nparam]))) |
| 3499 | { |
| 3500 | if (!(brackets++)) |
| 3501 | { |
| 3502 | params[nparam] = tline->next; |
| 3503 | continue; /* parameter loop */ |
| 3504 | } |
| 3505 | } |
| 3506 | if (ch == '}' && brackets > 0) |
| 3507 | if (--brackets == 0) |
| 3508 | { |
| 3509 | brackets = -1; |
| 3510 | continue; /* parameter loop */ |
| 3511 | } |
| 3512 | if (ch == '(' && !brackets) |
| 3513 | paren++; |
| 3514 | if (ch == ')' && brackets <= 0) |
| 3515 | if (--paren < 0) |
| 3516 | break; |
| 3517 | } |
| 3518 | if (brackets < 0) |
| 3519 | { |
| 3520 | brackets = 0; |
| 3521 | error(ERR_NONFATAL, "braces do not " |
| 3522 | "enclose all of macro parameter"); |
| 3523 | } |
| 3524 | paramsize[nparam] += white + 1; |
| 3525 | white = 0; |
| 3526 | } /* parameter loop */ |
| 3527 | nparam++; |
| 3528 | while (m && (m->nparam != nparam || |
| 3529 | mstrcmp(m->name, mname, |
| 3530 | m->casesense))) |
| 3531 | m = m->next; |
| 3532 | if (!m) |
| 3533 | error(ERR_WARNING | ERR_WARN_MNP, |
| 3534 | "macro `%s' exists, " |
| 3535 | "but not taking %d parameters", |
| 3536 | mstart->text, nparam); |
| 3537 | } |
| 3538 | } |
| 3539 | if (m && m->in_progress) |
| 3540 | m = NULL; |
| 3541 | if (!m) /* in progess or didn't find '(' or wrong nparam */ |
| 3542 | { |
| 3543 | /* |
| 3544 | * Design question: should we handle !tline, which |
| 3545 | * indicates missing ')' here, or expand those |
| 3546 | * macros anyway, which requires the (t) test a few |
| 3547 | * lines down? |
| 3548 | */ |
| 3549 | nasm_free(params); |
| 3550 | nasm_free(paramsize); |
| 3551 | tline = mstart; |
| 3552 | } |
| 3553 | else |
| 3554 | { |
| 3555 | /* |
| 3556 | * Expand the macro: we are placed on the last token of the |
| 3557 | * call, so that we can easily split the call from the |
| 3558 | * following tokens. We also start by pushing an SMAC_END |
| 3559 | * token for the cycle removal. |
| 3560 | */ |
| 3561 | t = tline; |
| 3562 | if (t) |
| 3563 | { |
| 3564 | tline = t->next; |
| 3565 | t->next = NULL; |
| 3566 | } |
| 3567 | tt = new_Token(tline, TOK_SMAC_END, NULL, 0); |
| 3568 | tt->mac = m; |
| 3569 | m->in_progress = TRUE; |
| 3570 | tline = tt; |
| 3571 | for (t = m->expansion; t; t = t->next) |
| 3572 | { |
| 3573 | if (t->type >= TOK_SMAC_PARAM) |
| 3574 | { |
| 3575 | Token *pcopy = tline, **ptail = &pcopy; |
| 3576 | Token *ttt, *pt; |
| 3577 | int i; |
| 3578 | |
| 3579 | ttt = params[t->type - TOK_SMAC_PARAM]; |
| 3580 | for (i = paramsize[t->type - TOK_SMAC_PARAM]; |
| 3581 | --i >= 0;) |
| 3582 | { |
| 3583 | pt = *ptail = |
| 3584 | new_Token(tline, ttt->type, ttt->text, |
| 3585 | 0); |
| 3586 | ptail = &pt->next; |
| 3587 | ttt = ttt->next; |
| 3588 | } |
| 3589 | tline = pcopy; |
| 3590 | } |
| 3591 | else |
| 3592 | { |
| 3593 | tt = new_Token(tline, t->type, t->text, 0); |
| 3594 | tline = tt; |
| 3595 | } |
| 3596 | } |
| 3597 | |
| 3598 | /* |
| 3599 | * Having done that, get rid of the macro call, and clean |
| 3600 | * up the parameters. |
| 3601 | */ |
| 3602 | nasm_free(params); |
| 3603 | nasm_free(paramsize); |
| 3604 | free_tlist(mstart); |
| 3605 | continue; /* main token loop */ |
| 3606 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3607 | } |
| 3608 | } |
| 3609 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3610 | if (tline->type == TOK_SMAC_END) |
| 3611 | { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3612 | tline->mac->in_progress = FALSE; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3613 | tline = delete_Token(tline); |
| 3614 | } |
| 3615 | else |
| 3616 | { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3617 | t = *tail = tline; |
| 3618 | tline = tline->next; |
| 3619 | t->mac = NULL; |
| 3620 | t->next = NULL; |
| 3621 | tail = &t->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3622 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3623 | } |
| 3624 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3625 | /* |
| 3626 | * Now scan the entire line and look for successive TOK_IDs that resulted |
| 3627 | * after expansion (they can't be produced by tokenise()). The successive |
| 3628 | * TOK_IDs should be concatenated. |
| 3629 | * Also we look for %+ tokens and concatenate the tokens before and after |
| 3630 | * them (without white spaces in between). |
| 3631 | */ |
| 3632 | t = thead; |
| 3633 | rescan = 0; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3634 | while (t) |
| 3635 | { |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3636 | while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID) |
| 3637 | t = t->next; |
| 3638 | if (!t || !t->next) |
| 3639 | break; |
| 3640 | if (t->next->type == TOK_ID || |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3641 | t->next->type == TOK_PREPROC_ID || |
| 3642 | t->next->type == TOK_NUMBER) |
| 3643 | { |
| 3644 | char *p = nasm_strcat(t->text, t->next->text); |
| 3645 | nasm_free(t->text); |
| 3646 | t->next = delete_Token(t->next); |
| 3647 | t->text = p; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3648 | rescan = 1; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3649 | } |
| 3650 | else if (t->next->type == TOK_WHITESPACE && t->next->next && |
| 3651 | t->next->next->type == TOK_PREPROC_ID && |
| 3652 | strcmp(t->next->next->text, "%+") == 0) |
| 3653 | { |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3654 | /* free the next whitespace, the %+ token and next whitespace */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3655 | int i; |
| 3656 | for (i = 1; i <= 3; i++) |
| 3657 | { |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3658 | if (!t->next || (i != 2 && t->next->type != TOK_WHITESPACE)) |
| 3659 | break; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3660 | t->next = delete_Token(t->next); |
| 3661 | } /* endfor */ |
| 3662 | } |
| 3663 | else |
| 3664 | t = t->next; |
| 3665 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3666 | /* If we concatenaded something, re-scan the line for macros */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3667 | if (rescan) |
| 3668 | { |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3669 | tline = thead; |
| 3670 | goto again; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3671 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3672 | |
| 3673 | if (org_tline) |
| 3674 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3675 | if (thead) |
| 3676 | { |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3677 | *org_tline = *thead; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3678 | /* since we just gave text to org_line, don't free it */ |
| 3679 | thead->text = NULL; |
| 3680 | delete_Token(thead); |
| 3681 | } |
| 3682 | else |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3683 | { |
| 3684 | /* the expression expanded to empty line; |
| 3685 | we can't return NULL for some reasons |
| 3686 | we just set the line to a single WHITESPACE token. */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3687 | memset(org_tline, 0, sizeof(*org_tline)); |
| 3688 | org_tline->text = NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3689 | org_tline->type = TOK_WHITESPACE; |
| 3690 | } |
| 3691 | thead = org_tline; |
| 3692 | } |
| 3693 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3694 | return thead; |
| 3695 | } |
| 3696 | |
| 3697 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3698 | * Similar to expand_smacro but used exclusively with macro identifiers |
| 3699 | * right before they are fetched in. The reason is that there can be |
| 3700 | * identifiers consisting of several subparts. We consider that if there |
| 3701 | * are more than one element forming the name, user wants a expansion, |
| 3702 | * otherwise it will be left as-is. Example: |
| 3703 | * |
| 3704 | * %define %$abc cde |
| 3705 | * |
| 3706 | * the identifier %$abc will be left as-is so that the handler for %define |
| 3707 | * will suck it and define the corresponding value. Other case: |
| 3708 | * |
| 3709 | * %define _%$abc cde |
| 3710 | * |
| 3711 | * In this case user wants name to be expanded *before* %define starts |
| 3712 | * working, so we'll expand %$abc into something (if it has a value; |
| 3713 | * otherwise it will be left as-is) then concatenate all successive |
| 3714 | * PP_IDs into one. |
| 3715 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3716 | static Token * |
| 3717 | expand_id(Token * tline) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3718 | { |
| 3719 | Token *cur, *oldnext = NULL; |
| 3720 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3721 | if (!tline || !tline->next) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3722 | return tline; |
| 3723 | |
| 3724 | cur = tline; |
| 3725 | while (cur->next && |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3726 | (cur->next->type == TOK_ID || |
| 3727 | cur->next->type == TOK_PREPROC_ID || cur->next->type == TOK_NUMBER)) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3728 | cur = cur->next; |
| 3729 | |
| 3730 | /* If identifier consists of just one token, don't expand */ |
| 3731 | if (cur == tline) |
| 3732 | return tline; |
| 3733 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3734 | if (cur) |
| 3735 | { |
| 3736 | oldnext = cur->next; /* Detach the tail past identifier */ |
| 3737 | cur->next = NULL; /* so that expand_smacro stops here */ |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3738 | } |
| 3739 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3740 | tline = expand_smacro(tline); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3741 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3742 | if (cur) |
| 3743 | { |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3744 | /* expand_smacro possibly changhed tline; re-scan for EOL */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3745 | cur = tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3746 | while (cur && cur->next) |
| 3747 | cur = cur->next; |
| 3748 | if (cur) |
| 3749 | cur->next = oldnext; |
| 3750 | } |
| 3751 | |
| 3752 | return tline; |
| 3753 | } |
| 3754 | |
| 3755 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3756 | * Determine whether the given line constitutes a multi-line macro |
| 3757 | * call, and return the MMacro structure called if so. Doesn't have |
| 3758 | * to check for an initial label - that's taken care of in |
| 3759 | * expand_mmacro - but must check numbers of parameters. Guaranteed |
| 3760 | * to be called with tline->type == TOK_ID, so the putative macro |
| 3761 | * name is easy to find. |
| 3762 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3763 | static MMacro * |
| 3764 | is_mmacro(Token * tline, Token *** params_array) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3765 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3766 | MMacro *head, *m; |
| 3767 | Token **params; |
| 3768 | int nparam; |
| 3769 | |
| 3770 | head = mmacros[hash(tline->text)]; |
| 3771 | |
| 3772 | /* |
| 3773 | * Efficiency: first we see if any macro exists with the given |
| 3774 | * name. If not, we can return NULL immediately. _Then_ we |
| 3775 | * count the parameters, and then we look further along the |
| 3776 | * list if necessary to find the proper MMacro. |
| 3777 | */ |
| 3778 | for (m = head; m; m = m->next) |
| 3779 | if (!mstrcmp(m->name, tline->text, m->casesense)) |
| 3780 | break; |
| 3781 | if (!m) |
| 3782 | return NULL; |
| 3783 | |
| 3784 | /* |
| 3785 | * OK, we have a potential macro. Count and demarcate the |
| 3786 | * parameters. |
| 3787 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3788 | count_mmac_params(tline->next, &nparam, ¶ms); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3789 | |
| 3790 | /* |
| 3791 | * So we know how many parameters we've got. Find the MMacro |
| 3792 | * structure that handles this number. |
| 3793 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3794 | while (m) |
| 3795 | { |
| 3796 | if (m->nparam_min <= nparam && (m->plus || nparam <= m->nparam_max)) |
| 3797 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3798 | /* |
| 3799 | * This one is right. Just check if cycle removal |
| 3800 | * prohibits us using it before we actually celebrate... |
| 3801 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3802 | if (m->in_progress) |
| 3803 | { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3804 | #if 0 |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3805 | error(ERR_NONFATAL, |
| 3806 | "self-reference in multi-line macro `%s'", m->name); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3807 | #endif |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3808 | nasm_free(params); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3809 | return NULL; |
| 3810 | } |
| 3811 | /* |
| 3812 | * It's right, and we can use it. Add its default |
| 3813 | * parameters to the end of our list if necessary. |
| 3814 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3815 | if (m->defaults && nparam < m->nparam_min + m->ndefs) |
| 3816 | { |
| 3817 | params = |
| 3818 | nasm_realloc(params, |
| 3819 | ((m->nparam_min + m->ndefs + 1) * sizeof(*params))); |
| 3820 | while (nparam < m->nparam_min + m->ndefs) |
| 3821 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3822 | params[nparam] = m->defaults[nparam - m->nparam_min]; |
| 3823 | nparam++; |
| 3824 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3825 | } |
| 3826 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3827 | * If we've gone over the maximum parameter count (and |
| 3828 | * we're in Plus mode), ignore parameters beyond |
| 3829 | * nparam_max. |
| 3830 | */ |
| 3831 | if (m->plus && nparam > m->nparam_max) |
| 3832 | nparam = m->nparam_max; |
| 3833 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3834 | * Then terminate the parameter list, and leave. |
| 3835 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3836 | if (!params) |
| 3837 | { /* need this special case */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3838 | params = nasm_malloc(sizeof(*params)); |
| 3839 | nparam = 0; |
| 3840 | } |
| 3841 | params[nparam] = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3842 | *params_array = params; |
| 3843 | return m; |
| 3844 | } |
| 3845 | /* |
| 3846 | * This one wasn't right: look for the next one with the |
| 3847 | * same name. |
| 3848 | */ |
| 3849 | for (m = m->next; m; m = m->next) |
| 3850 | if (!mstrcmp(m->name, tline->text, m->casesense)) |
| 3851 | break; |
| 3852 | } |
| 3853 | |
| 3854 | /* |
| 3855 | * After all that, we didn't find one with the right number of |
| 3856 | * parameters. Issue a warning, and fail to expand the macro. |
| 3857 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3858 | error(ERR_WARNING | ERR_WARN_MNP, |
| 3859 | "macro `%s' exists, but not taking %d parameters", |
| 3860 | tline->text, nparam); |
| 3861 | nasm_free(params); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3862 | return NULL; |
| 3863 | } |
| 3864 | |
| 3865 | /* |
| 3866 | * Expand the multi-line macro call made by the given line, if |
| 3867 | * 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] | 3868 | * istk->expansion and return 1. Otherwise return 0. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3869 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3870 | static int |
| 3871 | expand_mmacro(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3872 | { |
| 3873 | Token *startline = tline; |
| 3874 | Token *label = NULL; |
| 3875 | int dont_prepend = 0; |
| 3876 | Token **params, *t, *tt; |
| 3877 | MMacro *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3878 | Line *l, *ll; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3879 | int i, nparam, *paramlen; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3880 | |
| 3881 | t = tline; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3882 | skip_white_(t); |
H. Peter Anvin | dce1e2f | 2002-04-30 21:06:37 +0000 | [diff] [blame] | 3883 | /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */ |
| 3884 | if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID)) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3885 | return 0; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3886 | m = is_mmacro(t, ¶ms); |
| 3887 | if (!m) |
| 3888 | { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3889 | Token *last; |
| 3890 | /* |
| 3891 | * We have an id which isn't a macro call. We'll assume |
| 3892 | * it might be a label; we'll also check to see if a |
| 3893 | * colon follows it. Then, if there's another id after |
| 3894 | * that lot, we'll check it again for macro-hood. |
| 3895 | */ |
| 3896 | label = last = t; |
| 3897 | t = t->next; |
| 3898 | if (tok_type_(t, TOK_WHITESPACE)) |
| 3899 | last = t, t = t->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3900 | if (tok_is_(t, ":")) |
| 3901 | { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3902 | dont_prepend = 1; |
| 3903 | last = t, t = t->next; |
| 3904 | if (tok_type_(t, TOK_WHITESPACE)) |
| 3905 | last = t, t = t->next; |
| 3906 | } |
| 3907 | if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, ¶ms)) == NULL) |
| 3908 | return 0; |
| 3909 | last->next = NULL; |
| 3910 | tline = t; |
| 3911 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3912 | |
| 3913 | /* |
| 3914 | * Fix up the parameters: this involves stripping leading and |
| 3915 | * trailing whitespace, then stripping braces if they are |
| 3916 | * present. |
| 3917 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3918 | for (nparam = 0; params[nparam]; nparam++) |
| 3919 | ; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3920 | paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3921 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3922 | for (i = 0; params[i]; i++) |
| 3923 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3924 | int brace = FALSE; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3925 | int comma = (!m->plus || i < nparam - 1); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3926 | |
| 3927 | t = params[i]; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3928 | skip_white_(t); |
| 3929 | if (tok_is_(t, "{")) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3930 | t = t->next, brace = TRUE, comma = FALSE; |
| 3931 | params[i] = t; |
| 3932 | paramlen[i] = 0; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3933 | while (t) |
| 3934 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3935 | if (comma && t->type == TOK_OTHER && !strcmp(t->text, ",")) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3936 | break; /* ... because we have hit a comma */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3937 | if (comma && t->type == TOK_WHITESPACE && tok_is_(t->next, ",")) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3938 | break; /* ... or a space then a comma */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3939 | if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}")) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3940 | break; /* ... or a brace */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3941 | t = t->next; |
| 3942 | paramlen[i]++; |
| 3943 | } |
| 3944 | } |
| 3945 | |
| 3946 | /* |
| 3947 | * OK, we have a MMacro structure together with a set of |
| 3948 | * parameters. We must now go through the expansion and push |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3949 | * copies of each Line on to istk->expansion. Substitution of |
| 3950 | * parameter tokens and macro-local tokens doesn't get done |
| 3951 | * until the single-line macro substitution process; this is |
| 3952 | * because delaying them allows us to change the semantics |
| 3953 | * later through %rotate. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3954 | * |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3955 | * First, push an end marker on to istk->expansion, mark this |
| 3956 | * macro as in progress, and set up its invocation-specific |
| 3957 | * variables. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3958 | */ |
| 3959 | ll = nasm_malloc(sizeof(Line)); |
| 3960 | ll->next = istk->expansion; |
| 3961 | ll->finishes = m; |
| 3962 | ll->first = NULL; |
| 3963 | istk->expansion = ll; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3964 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3965 | m->in_progress = TRUE; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3966 | m->params = params; |
| 3967 | m->iline = tline; |
| 3968 | m->nparam = nparam; |
| 3969 | m->rotate = 0; |
| 3970 | m->paramlen = paramlen; |
| 3971 | m->unique = unique++; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3972 | m->lineno = 0; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3973 | |
| 3974 | m->next_active = istk->mstk; |
| 3975 | istk->mstk = m; |
| 3976 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3977 | for (l = m->expansion; l; l = l->next) |
| 3978 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3979 | Token **tail; |
| 3980 | |
| 3981 | ll = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3982 | ll->finishes = NULL; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3983 | ll->next = istk->expansion; |
| 3984 | istk->expansion = ll; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3985 | tail = &ll->first; |
| 3986 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3987 | for (t = l->first; t; t = t->next) |
| 3988 | { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3989 | Token *x = t; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3990 | if (t->type == TOK_PREPROC_ID && |
| 3991 | t->text[1] == '0' && t->text[2] == '0') |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3992 | { |
| 3993 | dont_prepend = -1; |
| 3994 | x = label; |
| 3995 | if (!x) |
| 3996 | continue; |
| 3997 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3998 | tt = *tail = new_Token(NULL, x->type, x->text, 0); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3999 | tail = &tt->next; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4000 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4001 | *tail = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4002 | } |
| 4003 | |
| 4004 | /* |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4005 | * If we had a label, push it on as the first line of |
| 4006 | * the macro expansion. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4007 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4008 | if (label) |
| 4009 | { |
| 4010 | if (dont_prepend < 0) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4011 | free_tlist(startline); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4012 | else |
| 4013 | { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4014 | ll = nasm_malloc(sizeof(Line)); |
| 4015 | ll->finishes = NULL; |
| 4016 | ll->next = istk->expansion; |
| 4017 | istk->expansion = ll; |
| 4018 | ll->first = startline; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4019 | if (!dont_prepend) |
| 4020 | { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4021 | while (label->next) |
| 4022 | label = label->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4023 | label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4024 | } |
H. Peter Anvin | 87bc619 | 2002-04-30 20:53:16 +0000 | [diff] [blame] | 4025 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4026 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4027 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4028 | list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4029 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4030 | return 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4031 | } |
| 4032 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4033 | /* |
| 4034 | * Since preprocessor always operate only on the line that didn't |
| 4035 | * arrived yet, we should always use ERR_OFFBY1. Also since user |
| 4036 | * won't want to see same error twice (preprocessing is done once |
| 4037 | * per pass) we will want to show errors only during pass one. |
| 4038 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4039 | static void |
Ed Beroset | 168c9c0 | 2002-05-17 03:10:13 +0000 | [diff] [blame] | 4040 | error(int severity, const char *fmt, ...) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4041 | { |
| 4042 | va_list arg; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4043 | char buff[1024]; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4044 | |
| 4045 | /* If we're in a dead branch of IF or something like it, ignore the error */ |
H. Peter Anvin | 77ba0c6 | 2002-05-14 03:18:53 +0000 | [diff] [blame] | 4046 | if (istk && istk->conds && !emitting(istk->conds->state)) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4047 | return; |
| 4048 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4049 | va_start(arg, fmt); |
| 4050 | vsprintf(buff, fmt, arg); |
| 4051 | va_end(arg); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4052 | |
H. Peter Anvin | 77ba0c6 | 2002-05-14 03:18:53 +0000 | [diff] [blame] | 4053 | if (istk && istk->mstk && istk->mstk->name) |
H. Peter Anvin | 99941bf | 2002-05-14 17:44:03 +0000 | [diff] [blame] | 4054 | _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name, |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4055 | istk->mstk->lineno, buff); |
Ed Beroset | 168c9c0 | 2002-05-17 03:10:13 +0000 | [diff] [blame] | 4056 | else |
H. Peter Anvin | 99941bf | 2002-05-14 17:44:03 +0000 | [diff] [blame] | 4057 | _error(severity | ERR_PASS1, "%s", buff); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4058 | } |
| 4059 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4060 | static void |
| 4061 | pp_reset(char *file, int apass, efunc errfunc, evalfunc eval, |
| 4062 | ListGen * listgen) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4063 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4064 | int h; |
| 4065 | |
H. Peter Anvin | 99941bf | 2002-05-14 17:44:03 +0000 | [diff] [blame] | 4066 | _error = errfunc; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4067 | cstk = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4068 | istk = nasm_malloc(sizeof(Include)); |
| 4069 | istk->next = NULL; |
| 4070 | istk->conds = NULL; |
| 4071 | istk->expansion = NULL; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4072 | istk->mstk = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4073 | istk->fp = fopen(file, "r"); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4074 | istk->fname = NULL; |
| 4075 | src_set_fname(nasm_strdup(file)); |
| 4076 | src_set_linnum(0); |
| 4077 | istk->lineinc = 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4078 | if (!istk->fp) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4079 | error(ERR_FATAL | ERR_NOFILE, "unable to open input file `%s'", file); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4080 | defining = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4081 | for (h = 0; h < NHASH; h++) |
| 4082 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4083 | mmacros[h] = NULL; |
| 4084 | smacros[h] = NULL; |
| 4085 | } |
| 4086 | unique = 0; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4087 | if (tasm_compatible_mode) { |
| 4088 | stdmacpos = stdmac; |
| 4089 | } else { |
| 4090 | stdmacpos = &stdmac[TASM_MACRO_COUNT]; |
| 4091 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4092 | any_extrastdmac = (extrastdmac != NULL); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4093 | list = listgen; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4094 | evaluate = eval; |
| 4095 | pass = apass; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4096 | } |
| 4097 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4098 | static char * |
| 4099 | pp_getline(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4100 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4101 | char *line; |
| 4102 | Token *tline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4103 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4104 | while (1) |
| 4105 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4106 | /* |
| 4107 | * Fetch a tokenised line, either from the macro-expansion |
| 4108 | * buffer or from the input file. |
| 4109 | */ |
| 4110 | tline = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4111 | while (istk->expansion && istk->expansion->finishes) |
| 4112 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4113 | Line *l = istk->expansion; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4114 | if (!l->finishes->name && l->finishes->in_progress > 1) |
| 4115 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4116 | Line *ll; |
| 4117 | |
| 4118 | /* |
| 4119 | * This is a macro-end marker for a macro with no |
| 4120 | * name, which means it's not really a macro at all |
| 4121 | * but a %rep block, and the `in_progress' field is |
| 4122 | * more than 1, meaning that we still need to |
| 4123 | * repeat. (1 means the natural last repetition; 0 |
| 4124 | * means termination by %exitrep.) We have |
| 4125 | * therefore expanded up to the %endrep, and must |
| 4126 | * push the whole block on to the expansion buffer |
| 4127 | * again. We don't bother to remove the macro-end |
| 4128 | * marker: we'd only have to generate another one |
| 4129 | * if we did. |
| 4130 | */ |
| 4131 | l->finishes->in_progress--; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4132 | for (l = l->finishes->expansion; l; l = l->next) |
| 4133 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4134 | Token *t, *tt, **tail; |
| 4135 | |
| 4136 | ll = nasm_malloc(sizeof(Line)); |
| 4137 | ll->next = istk->expansion; |
| 4138 | ll->finishes = NULL; |
| 4139 | ll->first = NULL; |
| 4140 | tail = &ll->first; |
| 4141 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4142 | for (t = l->first; t; t = t->next) |
| 4143 | { |
| 4144 | if (t->text || t->type == TOK_WHITESPACE) |
| 4145 | { |
| 4146 | tt = *tail = new_Token(NULL, t->type, t->text, 0); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4147 | tail = &tt->next; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4148 | } |
| 4149 | } |
| 4150 | |
| 4151 | istk->expansion = ll; |
| 4152 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4153 | } |
| 4154 | else |
| 4155 | { |
H. Peter Anvin | 87bc619 | 2002-04-30 20:53:16 +0000 | [diff] [blame] | 4156 | /* |
| 4157 | * Check whether a `%rep' was started and not ended |
| 4158 | * within this macro expansion. This can happen and |
| 4159 | * should be detected. It's a fatal error because |
| 4160 | * I'm too confused to work out how to recover |
| 4161 | * sensibly from it. |
| 4162 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4163 | if (defining) |
| 4164 | { |
H. Peter Anvin | 87bc619 | 2002-04-30 20:53:16 +0000 | [diff] [blame] | 4165 | if (defining->name) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4166 | error(ERR_PANIC, "defining with name in expansion"); |
H. Peter Anvin | b64535f | 2002-04-30 20:55:37 +0000 | [diff] [blame] | 4167 | else if (istk->mstk->name) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4168 | error(ERR_FATAL, "`%%rep' without `%%endrep' within" |
| 4169 | " expansion of macro `%s'", istk->mstk->name); |
H. Peter Anvin | 87bc619 | 2002-04-30 20:53:16 +0000 | [diff] [blame] | 4170 | } |
| 4171 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4172 | /* |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4173 | * FIXME: investigate the relationship at this point between |
| 4174 | * istk->mstk and l->finishes |
| 4175 | */ |
| 4176 | { |
| 4177 | MMacro *m = istk->mstk; |
| 4178 | istk->mstk = m->next_active; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4179 | if (m->name) |
| 4180 | { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4181 | /* |
| 4182 | * This was a real macro call, not a %rep, and |
| 4183 | * therefore the parameter information needs to |
| 4184 | * be freed. |
| 4185 | */ |
| 4186 | nasm_free(m->params); |
| 4187 | free_tlist(m->iline); |
| 4188 | nasm_free(m->paramlen); |
| 4189 | l->finishes->in_progress = FALSE; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4190 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4191 | else |
| 4192 | free_mmacro(m); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4193 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4194 | istk->expansion = l->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4195 | nasm_free(l); |
| 4196 | list->downlevel(LIST_MACRO); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4197 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4198 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4199 | while (1) |
| 4200 | { /* until we get a line we can use */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4201 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4202 | if (istk->expansion) |
| 4203 | { /* from a macro expansion */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4204 | char *p; |
| 4205 | Line *l = istk->expansion; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4206 | if (istk->mstk) |
| 4207 | istk->mstk->lineno++; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4208 | tline = l->first; |
| 4209 | istk->expansion = l->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4210 | nasm_free(l); |
| 4211 | p = detoken(tline, FALSE); |
| 4212 | list->line(LIST_MACRO, p); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4213 | nasm_free(p); |
| 4214 | break; |
| 4215 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4216 | line = read_line(); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4217 | if (line) |
| 4218 | { /* from the current input file */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4219 | line = prepreproc(line); |
| 4220 | tline = tokenise(line); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4221 | nasm_free(line); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4222 | break; |
| 4223 | } |
| 4224 | /* |
| 4225 | * The current file has ended; work down the istk |
| 4226 | */ |
| 4227 | { |
| 4228 | Include *i = istk; |
| 4229 | fclose(i->fp); |
| 4230 | if (i->conds) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4231 | error(ERR_FATAL, "expected `%%endif' before end of file"); |
Ed Beroset | 168c9c0 | 2002-05-17 03:10:13 +0000 | [diff] [blame] | 4232 | /* only set line and file name if there's a next node */ |
| 4233 | if (i->next) |
| 4234 | { |
| 4235 | src_set_linnum(i->lineno); |
| 4236 | nasm_free(src_set_fname(i->fname)); |
| 4237 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4238 | istk = i->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4239 | list->downlevel(LIST_INCLUDE); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4240 | nasm_free(i); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4241 | if (!istk) |
| 4242 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4243 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4244 | } |
| 4245 | |
| 4246 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4247 | * We must expand MMacro parameters and MMacro-local labels |
| 4248 | * _before_ we plunge into directive processing, to cope |
| 4249 | * with things like `%define something %1' such as STRUC |
| 4250 | * uses. Unless we're _defining_ a MMacro, in which case |
| 4251 | * those tokens should be left alone to go into the |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4252 | * definition; and unless we're in a non-emitting |
| 4253 | * condition, in which case we don't want to meddle with |
| 4254 | * anything. |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4255 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4256 | if (!defining && !(istk->conds && !emitting(istk->conds->state))) |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4257 | tline = expand_mmac_params(tline); |
| 4258 | |
| 4259 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4260 | * Check the line to see if it's a preprocessor directive. |
| 4261 | */ |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 4262 | if (do_directive(tline) == DIRECTIVE_FOUND) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4263 | { |
| 4264 | continue; |
| 4265 | } |
| 4266 | else if (defining) |
| 4267 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4268 | /* |
| 4269 | * We're defining a multi-line macro. We emit nothing |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4270 | * at all, and just |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4271 | * shove the tokenised line on to the macro definition. |
| 4272 | */ |
| 4273 | Line *l = nasm_malloc(sizeof(Line)); |
| 4274 | l->next = defining->expansion; |
| 4275 | l->first = tline; |
| 4276 | l->finishes = FALSE; |
| 4277 | defining->expansion = l; |
| 4278 | continue; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4279 | } |
| 4280 | else if (istk->conds && !emitting(istk->conds->state)) |
| 4281 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4282 | /* |
| 4283 | * We're in a non-emitting branch of a condition block. |
| 4284 | * Emit nothing at all, not even a blank line: when we |
| 4285 | * emerge from the condition we'll give a line-number |
| 4286 | * directive so we keep our place correctly. |
| 4287 | */ |
| 4288 | free_tlist(tline); |
| 4289 | continue; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4290 | } |
| 4291 | else if (istk->mstk && !istk->mstk->in_progress) |
| 4292 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4293 | /* |
| 4294 | * We're in a %rep block which has been terminated, so |
| 4295 | * we're walking through to the %endrep without |
| 4296 | * emitting anything. Emit nothing at all, not even a |
| 4297 | * blank line: when we emerge from the %rep block we'll |
| 4298 | * give a line-number directive so we keep our place |
| 4299 | * correctly. |
| 4300 | */ |
| 4301 | free_tlist(tline); |
| 4302 | continue; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4303 | } |
| 4304 | else |
| 4305 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4306 | tline = expand_smacro(tline); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4307 | if (!expand_mmacro(tline)) |
| 4308 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4309 | /* |
| 4310 | * De-tokenise the line again, and emit it. |
| 4311 | */ |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4312 | line = detoken(tline, TRUE); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4313 | free_tlist(tline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4314 | break; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4315 | } |
| 4316 | else |
| 4317 | { |
| 4318 | continue; /* expand_mmacro calls free_tlist */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4319 | } |
| 4320 | } |
| 4321 | } |
| 4322 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4323 | return line; |
| 4324 | } |
| 4325 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4326 | static void |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 4327 | pp_cleanup(int pass) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4328 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4329 | int h; |
| 4330 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4331 | if (defining) |
| 4332 | { |
| 4333 | error(ERR_NONFATAL, "end of file while still defining macro `%s'", |
| 4334 | defining->name); |
| 4335 | free_mmacro(defining); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4336 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4337 | while (cstk) |
| 4338 | ctx_pop(); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4339 | for (h = 0; h < NHASH; h++) |
| 4340 | { |
| 4341 | while (mmacros[h]) |
| 4342 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4343 | MMacro *m = mmacros[h]; |
| 4344 | mmacros[h] = mmacros[h]->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4345 | free_mmacro(m); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4346 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4347 | while (smacros[h]) |
| 4348 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4349 | SMacro *s = smacros[h]; |
| 4350 | smacros[h] = smacros[h]->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4351 | nasm_free(s->name); |
| 4352 | free_tlist(s->expansion); |
| 4353 | nasm_free(s); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4354 | } |
| 4355 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4356 | while (istk) |
| 4357 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4358 | Include *i = istk; |
| 4359 | istk = istk->next; |
| 4360 | fclose(i->fp); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4361 | nasm_free(i->fname); |
| 4362 | nasm_free(i); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4363 | } |
| 4364 | while (cstk) |
| 4365 | ctx_pop(); |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 4366 | if (pass == 0) |
| 4367 | { |
| 4368 | free_llist(predef); |
| 4369 | delete_Blocks(); |
| 4370 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4371 | } |
| 4372 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4373 | void |
| 4374 | pp_include_path(char *path) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4375 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4376 | IncPath *i; |
| 4377 | |
| 4378 | i = nasm_malloc(sizeof(IncPath)); |
| 4379 | i->path = nasm_strdup(path); |
| 4380 | i->next = ipath; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4381 | ipath = i; |
| 4382 | } |
| 4383 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4384 | void |
| 4385 | pp_pre_include(char *fname) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4386 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4387 | Token *inc, *space, *name; |
| 4388 | Line *l; |
| 4389 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4390 | name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0); |
| 4391 | space = new_Token(name, TOK_WHITESPACE, NULL, 0); |
| 4392 | inc = new_Token(space, TOK_PREPROC_ID, "%include", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4393 | |
| 4394 | l = nasm_malloc(sizeof(Line)); |
| 4395 | l->next = predef; |
| 4396 | l->first = inc; |
| 4397 | l->finishes = FALSE; |
| 4398 | predef = l; |
| 4399 | } |
| 4400 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4401 | void |
| 4402 | pp_pre_define(char *definition) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4403 | { |
| 4404 | Token *def, *space; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4405 | Line *l; |
| 4406 | char *equals; |
| 4407 | |
| 4408 | equals = strchr(definition, '='); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4409 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 4410 | def = new_Token(space, TOK_PREPROC_ID, "%define", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4411 | if (equals) |
| 4412 | *equals = ' '; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4413 | space->next = tokenise(definition); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4414 | if (equals) |
| 4415 | *equals = '='; |
| 4416 | |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4417 | l = nasm_malloc(sizeof(Line)); |
| 4418 | l->next = predef; |
| 4419 | l->first = def; |
| 4420 | l->finishes = FALSE; |
| 4421 | predef = l; |
| 4422 | } |
| 4423 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4424 | void |
| 4425 | pp_pre_undefine(char *definition) |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 4426 | { |
| 4427 | Token *def, *space; |
| 4428 | Line *l; |
| 4429 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4430 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 4431 | def = new_Token(space, TOK_PREPROC_ID, "%undef", 0); |
Frank Kotler | d352302 | 2003-06-14 12:12:26 +0000 | [diff] [blame^] | 4432 | space->next = tokenise(definition); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 4433 | |
| 4434 | l = nasm_malloc(sizeof(Line)); |
| 4435 | l->next = predef; |
| 4436 | l->first = def; |
| 4437 | l->finishes = FALSE; |
| 4438 | predef = l; |
| 4439 | } |
| 4440 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4441 | void |
H. Peter Anvin | bfebdb0 | 2002-09-12 02:23:54 +0000 | [diff] [blame] | 4442 | pp_extra_stdmac(const char **macros) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4443 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4444 | extrastdmac = macros; |
| 4445 | } |
| 4446 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4447 | static void |
| 4448 | make_tok_num(Token * tok, long val) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4449 | { |
| 4450 | char numbuf[20]; |
| 4451 | sprintf(numbuf, "%ld", val); |
| 4452 | tok->text = nasm_strdup(numbuf); |
| 4453 | tok->type = TOK_NUMBER; |
| 4454 | } |
| 4455 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4456 | Preproc nasmpp = { |
| 4457 | pp_reset, |
| 4458 | pp_getline, |
| 4459 | pp_cleanup |
| 4460 | }; |