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++; |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 836 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 837 | if (*p) |
| 838 | { |
| 839 | p++; |
| 840 | } |
| 841 | else |
| 842 | { |
| 843 | error(ERR_WARNING, "unterminated string"); |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 844 | /* Handling unterminated strings by UNV */ |
| 845 | /* type = -1; */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 846 | } |
| 847 | } |
| 848 | else if (isnumstart(*p)) |
| 849 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 850 | /* |
| 851 | * A number token. |
| 852 | */ |
| 853 | type = TOK_NUMBER; |
| 854 | p++; |
| 855 | while (*p && isnumchar(*p)) |
| 856 | p++; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 857 | } |
| 858 | else if (isspace(*p)) |
| 859 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 860 | type = TOK_WHITESPACE; |
| 861 | p++; |
| 862 | while (*p && isspace(*p)) |
| 863 | p++; |
| 864 | /* |
| 865 | * Whitespace just before end-of-line is discarded by |
| 866 | * pretending it's a comment; whitespace just before a |
| 867 | * comment gets lumped into the comment. |
| 868 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 869 | if (!*p || *p == ';') |
| 870 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 871 | type = TOK_COMMENT; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 872 | while (*p) |
| 873 | p++; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 874 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 875 | } |
| 876 | else if (*p == ';') |
| 877 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 878 | type = TOK_COMMENT; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 879 | while (*p) |
| 880 | p++; |
| 881 | } |
| 882 | else |
| 883 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 884 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 885 | * Anything else is an operator of some kind. We check |
| 886 | * for all the double-character operators (>>, <<, //, |
| 887 | * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything |
| 888 | * else is a single-character operator. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 889 | */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 890 | type = TOK_OTHER; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 891 | if ((p[0] == '>' && p[1] == '>') || |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 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] == '>') || |
| 899 | (p[0] == '&' && p[1] == '&') || |
| 900 | (p[0] == '|' && p[1] == '|') || |
| 901 | (p[0] == '^' && p[1] == '^')) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 902 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 903 | p++; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 904 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 905 | p++; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 906 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 907 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 908 | /* Handling unterminated string by UNV */ |
| 909 | /*if (type == -1) |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 910 | { |
| 911 | *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1); |
| 912 | t->text[p-line] = *line; |
| 913 | tail = &t->next; |
| 914 | } |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 915 | else*/ |
| 916 | if (type != TOK_COMMENT) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 917 | { |
| 918 | *tail = t = new_Token(NULL, type, line, p - line); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 919 | tail = &t->next; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 920 | } |
| 921 | line = p; |
| 922 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 923 | return list; |
| 924 | } |
| 925 | |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 926 | /* |
| 927 | * this function allocates a new managed block of memory and |
| 928 | * returns a pointer to the block. The managed blocks are |
| 929 | * deleted only all at once by the delete_Blocks function. |
| 930 | */ |
| 931 | static void * |
| 932 | new_Block(size_t size) |
| 933 | { |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 934 | Blocks *b = &blocks; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 935 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 936 | /* first, get to the end of the linked list */ |
| 937 | while (b->next) |
| 938 | b = b->next; |
| 939 | /* now allocate the requested chunk */ |
| 940 | b->chunk = nasm_malloc(size); |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 941 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 942 | /* now allocate a new block for the next request */ |
| 943 | b->next = nasm_malloc(sizeof(Blocks)); |
| 944 | /* and initialize the contents of the new block */ |
| 945 | b->next->next = NULL; |
| 946 | b->next->chunk = NULL; |
| 947 | return b->chunk; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | /* |
| 951 | * this function deletes all managed blocks of memory |
| 952 | */ |
| 953 | static void |
| 954 | delete_Blocks(void) |
| 955 | { |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 956 | Blocks *a,*b = &blocks; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 957 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 958 | /* |
| 959 | * keep in mind that the first block, pointed to by blocks |
| 960 | * is a static and not dynamically allocated, so we don't |
| 961 | * free it. |
| 962 | */ |
| 963 | while (b) |
| 964 | { |
| 965 | if (b->chunk) |
| 966 | nasm_free(b->chunk); |
| 967 | a = b; |
| 968 | b = b->next; |
| 969 | if (a != &blocks) |
| 970 | nasm_free(a); |
| 971 | } |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 972 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 973 | |
| 974 | /* |
| 975 | * this function creates a new Token and passes a pointer to it |
| 976 | * back to the caller. It sets the type and text elements, and |
| 977 | * also the mac and next elements to NULL. |
| 978 | */ |
| 979 | static Token * |
| 980 | new_Token(Token * next, int type, char *text, int txtlen) |
| 981 | { |
| 982 | Token *t; |
| 983 | int i; |
| 984 | |
| 985 | if (freeTokens == NULL) |
| 986 | { |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 987 | freeTokens = (Token *)new_Block(TOKEN_BLOCKSIZE * sizeof(Token)); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 988 | for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++) |
| 989 | freeTokens[i].next = &freeTokens[i + 1]; |
| 990 | freeTokens[i].next = NULL; |
| 991 | } |
| 992 | t = freeTokens; |
| 993 | freeTokens = t->next; |
| 994 | t->next = next; |
| 995 | t->mac = NULL; |
| 996 | t->type = type; |
| 997 | if (type == TOK_WHITESPACE || text == NULL) |
| 998 | { |
| 999 | t->text = NULL; |
| 1000 | } |
| 1001 | else |
| 1002 | { |
| 1003 | if (txtlen == 0) |
| 1004 | txtlen = strlen(text); |
| 1005 | t->text = nasm_malloc(1 + txtlen); |
| 1006 | strncpy(t->text, text, txtlen); |
| 1007 | t->text[txtlen] = '\0'; |
| 1008 | } |
| 1009 | return t; |
| 1010 | } |
| 1011 | |
| 1012 | static Token * |
| 1013 | delete_Token(Token * t) |
| 1014 | { |
| 1015 | Token *next = t->next; |
| 1016 | nasm_free(t->text); |
H. Peter Anvin | 788e6c1 | 2002-04-30 21:02:01 +0000 | [diff] [blame] | 1017 | t->next = freeTokens; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1018 | freeTokens = t; |
| 1019 | return next; |
| 1020 | } |
| 1021 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1022 | /* |
| 1023 | * Convert a line of tokens back into text. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1024 | * If expand_locals is not zero, identifiers of the form "%$*xxx" |
| 1025 | * will be transformed into ..@ctxnum.xxx |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1026 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1027 | static char * |
| 1028 | detoken(Token * tlist, int expand_locals) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1029 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1030 | Token *t; |
| 1031 | int len; |
| 1032 | char *line, *p; |
| 1033 | |
| 1034 | len = 0; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1035 | for (t = tlist; t; t = t->next) |
| 1036 | { |
| 1037 | if (t->type == TOK_PREPROC_ID && t->text[1] == '!') |
| 1038 | { |
| 1039 | char *p = getenv(t->text + 2); |
| 1040 | nasm_free(t->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1041 | if (p) |
| 1042 | t->text = nasm_strdup(p); |
| 1043 | else |
| 1044 | t->text = NULL; |
| 1045 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1046 | /* Expand local macros here and not during preprocessing */ |
| 1047 | if (expand_locals && |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1048 | t->type == TOK_PREPROC_ID && t->text && |
| 1049 | t->text[0] == '%' && t->text[1] == '$') |
| 1050 | { |
| 1051 | Context *ctx = get_ctx(t->text, FALSE); |
| 1052 | if (ctx) |
| 1053 | { |
| 1054 | char buffer[40]; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1055 | char *p, *q = t->text + 2; |
| 1056 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1057 | q += strspn(q, "$"); |
| 1058 | sprintf(buffer, "..@%lu.", ctx->number); |
| 1059 | p = nasm_strcat(buffer, q); |
| 1060 | nasm_free(t->text); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1061 | t->text = p; |
| 1062 | } |
| 1063 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1064 | if (t->type == TOK_WHITESPACE) |
| 1065 | { |
| 1066 | len++; |
| 1067 | } |
| 1068 | else if (t->text) |
| 1069 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1070 | len += strlen(t->text); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1071 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1072 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1073 | p = line = nasm_malloc(len + 1); |
| 1074 | for (t = tlist; t; t = t->next) |
| 1075 | { |
| 1076 | if (t->type == TOK_WHITESPACE) |
| 1077 | { |
| 1078 | *p = ' '; |
| 1079 | p++; |
| 1080 | *p = '\0'; |
| 1081 | } |
| 1082 | else if (t->text) |
| 1083 | { |
| 1084 | strcpy(p, t->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1085 | p += strlen(p); |
| 1086 | } |
| 1087 | } |
| 1088 | *p = '\0'; |
| 1089 | return line; |
| 1090 | } |
| 1091 | |
| 1092 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1093 | * A scanner, suitable for use by the expression evaluator, which |
| 1094 | * operates on a line of Tokens. Expects a pointer to a pointer to |
| 1095 | * the first token in the line to be passed in as its private_data |
| 1096 | * field. |
| 1097 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1098 | static int |
| 1099 | ppscan(void *private_data, struct tokenval *tokval) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1100 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1101 | Token **tlineptr = private_data; |
| 1102 | Token *tline; |
| 1103 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1104 | do |
| 1105 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1106 | tline = *tlineptr; |
| 1107 | *tlineptr = tline ? tline->next : NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1108 | } |
| 1109 | while (tline && (tline->type == TOK_WHITESPACE || |
| 1110 | tline->type == TOK_COMMENT)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1111 | |
| 1112 | if (!tline) |
| 1113 | return tokval->t_type = TOKEN_EOS; |
| 1114 | |
| 1115 | if (tline->text[0] == '$' && !tline->text[1]) |
| 1116 | return tokval->t_type = TOKEN_HERE; |
H. Peter Anvin | 7cf897e | 2002-05-30 21:30:33 +0000 | [diff] [blame] | 1117 | if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2]) |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1118 | return tokval->t_type = TOKEN_BASE; |
| 1119 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1120 | if (tline->type == TOK_ID) |
| 1121 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1122 | tokval->t_charptr = tline->text; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1123 | if (tline->text[0] == '$') |
| 1124 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1125 | tokval->t_charptr++; |
| 1126 | return tokval->t_type = TOKEN_ID; |
| 1127 | } |
| 1128 | |
| 1129 | /* |
| 1130 | * This is the only special case we actually need to worry |
| 1131 | * about in this restricted context. |
| 1132 | */ |
| 1133 | if (!nasm_stricmp(tline->text, "seg")) |
| 1134 | return tokval->t_type = TOKEN_SEG; |
| 1135 | |
| 1136 | return tokval->t_type = TOKEN_ID; |
| 1137 | } |
| 1138 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1139 | if (tline->type == TOK_NUMBER) |
| 1140 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1141 | int rn_error; |
| 1142 | |
| 1143 | tokval->t_integer = readnum(tline->text, &rn_error); |
| 1144 | if (rn_error) |
| 1145 | return tokval->t_type = TOKEN_ERRNUM; |
| 1146 | tokval->t_charptr = NULL; |
| 1147 | return tokval->t_type = TOKEN_NUM; |
| 1148 | } |
| 1149 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1150 | if (tline->type == TOK_STRING) |
| 1151 | { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1152 | int rn_warn; |
| 1153 | char q, *r; |
| 1154 | int l; |
| 1155 | |
| 1156 | r = tline->text; |
| 1157 | q = *r++; |
| 1158 | l = strlen(r); |
| 1159 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1160 | if (l == 0 || r[l - 1] != q) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1161 | return tokval->t_type = TOKEN_ERRNUM; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1162 | tokval->t_integer = readstrnum(r, l - 1, &rn_warn); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1163 | if (rn_warn) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1164 | error(ERR_WARNING | ERR_PASS1, "character constant too long"); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1165 | tokval->t_charptr = NULL; |
| 1166 | return tokval->t_type = TOKEN_NUM; |
| 1167 | } |
| 1168 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1169 | if (tline->type == TOK_OTHER) |
| 1170 | { |
| 1171 | if (!strcmp(tline->text, "<<")) |
| 1172 | return tokval->t_type = TOKEN_SHL; |
| 1173 | if (!strcmp(tline->text, ">>")) |
| 1174 | return tokval->t_type = TOKEN_SHR; |
| 1175 | if (!strcmp(tline->text, "//")) |
| 1176 | return tokval->t_type = TOKEN_SDIV; |
| 1177 | if (!strcmp(tline->text, "%%")) |
| 1178 | return tokval->t_type = TOKEN_SMOD; |
| 1179 | if (!strcmp(tline->text, "==")) |
| 1180 | return tokval->t_type = TOKEN_EQ; |
| 1181 | if (!strcmp(tline->text, "<>")) |
| 1182 | return tokval->t_type = TOKEN_NE; |
| 1183 | if (!strcmp(tline->text, "!=")) |
| 1184 | return tokval->t_type = TOKEN_NE; |
| 1185 | if (!strcmp(tline->text, "<=")) |
| 1186 | return tokval->t_type = TOKEN_LE; |
| 1187 | if (!strcmp(tline->text, ">=")) |
| 1188 | return tokval->t_type = TOKEN_GE; |
| 1189 | if (!strcmp(tline->text, "&&")) |
| 1190 | return tokval->t_type = TOKEN_DBL_AND; |
| 1191 | if (!strcmp(tline->text, "^^")) |
| 1192 | return tokval->t_type = TOKEN_DBL_XOR; |
| 1193 | if (!strcmp(tline->text, "||")) |
| 1194 | return tokval->t_type = TOKEN_DBL_OR; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1195 | } |
| 1196 | |
| 1197 | /* |
| 1198 | * We have no other options: just return the first character of |
| 1199 | * the token text. |
| 1200 | */ |
| 1201 | return tokval->t_type = tline->text[0]; |
| 1202 | } |
| 1203 | |
| 1204 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1205 | * Compare a string to the name of an existing macro; this is a |
| 1206 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 1207 | * depending on the value of the `casesense' parameter. |
| 1208 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1209 | static int |
| 1210 | mstrcmp(char *p, char *q, int casesense) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1211 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1212 | return casesense ? strcmp(p, q) : nasm_stricmp(p, q); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1216 | * Return the Context structure associated with a %$ token. Return |
| 1217 | * NULL, having _already_ reported an error condition, if the |
| 1218 | * context stack isn't deep enough for the supplied number of $ |
| 1219 | * signs. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1220 | * If all_contexts == TRUE, contexts that enclose current are |
| 1221 | * also scanned for such smacro, until it is found; if not - |
| 1222 | * only the context that directly results from the number of $'s |
| 1223 | * in variable's name. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1224 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1225 | static Context * |
| 1226 | get_ctx(char *name, int all_contexts) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1227 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1228 | Context *ctx; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1229 | SMacro *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1230 | int i; |
| 1231 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1232 | if (!name || name[0] != '%' || name[1] != '$') |
| 1233 | return NULL; |
| 1234 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1235 | if (!cstk) |
| 1236 | { |
| 1237 | error(ERR_NONFATAL, "`%s': context stack is empty", name); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1238 | return NULL; |
| 1239 | } |
| 1240 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1241 | for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--) |
| 1242 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1243 | ctx = ctx->next; |
H. Peter Anvin | dce1e2f | 2002-04-30 21:06:37 +0000 | [diff] [blame] | 1244 | /* i--; Lino - 02/25/02 */ |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1245 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1246 | if (!ctx) |
| 1247 | { |
| 1248 | error(ERR_NONFATAL, "`%s': context stack is only" |
| 1249 | " %d level%s deep", name, i - 1, (i == 2 ? "" : "s")); |
| 1250 | return NULL; |
| 1251 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1252 | if (!all_contexts) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1253 | return ctx; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1254 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1255 | do |
| 1256 | { |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1257 | /* Search for this smacro in found context */ |
| 1258 | m = ctx->localmac; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1259 | while (m) |
| 1260 | { |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1261 | if (!mstrcmp(m->name, name, m->casesense)) |
| 1262 | return ctx; |
| 1263 | m = m->next; |
| 1264 | } |
| 1265 | ctx = ctx->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1266 | } |
| 1267 | while (ctx); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1268 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1272 | * Open an include file. This routine must always return a valid |
| 1273 | * file pointer if it returns - it's responsible for throwing an |
| 1274 | * ERR_FATAL and bombing out completely if not. It should also try |
| 1275 | * the include path one by one until it finds the file or reaches |
| 1276 | * the end of the path. |
| 1277 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1278 | static FILE * |
| 1279 | inc_fopen(char *file) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1280 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1281 | FILE *fp; |
| 1282 | char *prefix = "", *combine; |
| 1283 | IncPath *ip = ipath; |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 1284 | static int namelen = 0; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1285 | int len = strlen(file); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1286 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1287 | while (1) |
| 1288 | { |
Frank Kotler | 7fd4f00 | 2003-08-06 07:10:16 +0000 | [diff] [blame] | 1289 | combine = nasm_malloc(strlen(prefix) + len + 1); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1290 | strcpy(combine, prefix); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1291 | strcat(combine, file); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1292 | fp = fopen(combine, "r"); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 1293 | if (pass == 0 && fp) |
| 1294 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1295 | namelen += strlen(combine) + 1; |
| 1296 | if (namelen > 62) |
| 1297 | { |
| 1298 | printf(" \\\n "); |
| 1299 | namelen = 2; |
| 1300 | } |
| 1301 | printf(" %s", combine); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 1302 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1303 | nasm_free(combine); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1304 | if (fp) |
| 1305 | return fp; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1306 | if (!ip) |
| 1307 | break; |
| 1308 | prefix = ip->path; |
| 1309 | ip = ip->next; |
| 1310 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1311 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1312 | error(ERR_FATAL, "unable to open include file `%s'", file); |
| 1313 | return NULL; /* never reached - placate compilers */ |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1314 | } |
| 1315 | |
| 1316 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1317 | * 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] | 1318 | * 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] | 1319 | * return TRUE if _any_ single-line macro of that name is defined. |
| 1320 | * Otherwise, will return TRUE if a single-line macro with either |
| 1321 | * `nparam' or no parameters is defined. |
| 1322 | * |
| 1323 | * If a macro with precisely the right number of parameters is |
H. Peter Anvin | ef7468f | 2002-04-30 20:57:59 +0000 | [diff] [blame] | 1324 | * defined, or nparam is -1, the address of the definition structure |
| 1325 | * 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] | 1326 | * is NULL, no action will be taken regarding its contents, and no |
| 1327 | * error will occur. |
| 1328 | * |
| 1329 | * Note that this is also called with nparam zero to resolve |
| 1330 | * `ifdef'. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1331 | * |
| 1332 | * If you already know which context macro belongs to, you can pass |
| 1333 | * the context pointer as first parameter; if you won't but name begins |
| 1334 | * with %$ the context will be automatically computed. If all_contexts |
| 1335 | * is true, macro will be searched in outer contexts as well. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1336 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1337 | static int |
| 1338 | smacro_defined(Context * ctx, char *name, int nparam, SMacro ** defn, |
| 1339 | int nocase) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1340 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1341 | SMacro *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1342 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1343 | if (ctx) |
| 1344 | m = ctx->localmac; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1345 | else if (name[0] == '%' && name[1] == '$') |
| 1346 | { |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1347 | if (cstk) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1348 | ctx = get_ctx(name, FALSE); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1349 | if (!ctx) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1350 | return FALSE; /* got to return _something_ */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1351 | m = ctx->localmac; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1352 | } |
| 1353 | else |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1354 | m = smacros[hash(name)]; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1355 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1356 | while (m) |
| 1357 | { |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1358 | if (!mstrcmp(m->name, name, m->casesense && nocase) && |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1359 | (nparam <= 0 || m->nparam == 0 || nparam == m->nparam)) |
| 1360 | { |
| 1361 | if (defn) |
| 1362 | { |
H. Peter Anvin | ef7468f | 2002-04-30 20:57:59 +0000 | [diff] [blame] | 1363 | if (nparam == m->nparam || nparam == -1) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1364 | *defn = m; |
| 1365 | else |
| 1366 | *defn = NULL; |
| 1367 | } |
| 1368 | return TRUE; |
| 1369 | } |
| 1370 | m = m->next; |
| 1371 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1372 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1373 | return FALSE; |
| 1374 | } |
| 1375 | |
| 1376 | /* |
| 1377 | * Count and mark off the parameters in a multi-line macro call. |
| 1378 | * This is called both from within the multi-line macro expansion |
| 1379 | * code, and also to mark off the default parameters when provided |
| 1380 | * in a %macro definition line. |
| 1381 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1382 | static void |
| 1383 | count_mmac_params(Token * t, int *nparam, Token *** params) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1384 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1385 | int paramsize, brace; |
| 1386 | |
| 1387 | *nparam = paramsize = 0; |
| 1388 | *params = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1389 | while (t) |
| 1390 | { |
| 1391 | if (*nparam >= paramsize) |
| 1392 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1393 | paramsize += PARAM_DELTA; |
| 1394 | *params = nasm_realloc(*params, sizeof(**params) * paramsize); |
| 1395 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1396 | skip_white_(t); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1397 | brace = FALSE; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1398 | if (tok_is_(t, "{")) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1399 | brace = TRUE; |
| 1400 | (*params)[(*nparam)++] = t; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1401 | while (tok_isnt_(t, brace ? "}" : ",")) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1402 | t = t->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1403 | if (t) |
| 1404 | { /* got a comma/brace */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1405 | t = t->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1406 | if (brace) |
| 1407 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1408 | /* |
| 1409 | * Now we've found the closing brace, look further |
| 1410 | * for the comma. |
| 1411 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1412 | skip_white_(t); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1413 | if (tok_isnt_(t, ",")) |
| 1414 | { |
| 1415 | error(ERR_NONFATAL, |
| 1416 | "braces do not enclose all of macro parameter"); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1417 | while (tok_isnt_(t, ",")) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1418 | t = t->next; |
| 1419 | } |
| 1420 | if (t) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1421 | t = t->next; /* eat the comma */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1422 | } |
| 1423 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1424 | } |
| 1425 | } |
| 1426 | |
| 1427 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1428 | * Determine whether one of the various `if' conditions is true or |
| 1429 | * not. |
| 1430 | * |
| 1431 | * We must free the tline we get passed. |
| 1432 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1433 | static int |
| 1434 | if_condition(Token * tline, int i) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1435 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1436 | int j, casesense; |
| 1437 | Token *t, *tt, **tptr, *origline; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1438 | struct tokenval tokval; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1439 | expr *evalresult; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1440 | |
| 1441 | origline = tline; |
| 1442 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1443 | switch (i) |
| 1444 | { |
| 1445 | case PP_IFCTX: |
| 1446 | case PP_ELIFCTX: |
| 1447 | case PP_IFNCTX: |
| 1448 | case PP_ELIFNCTX: |
| 1449 | j = FALSE; /* have we matched yet? */ |
| 1450 | while (cstk && tline) |
| 1451 | { |
| 1452 | skip_white_(tline); |
| 1453 | if (!tline || tline->type != TOK_ID) |
| 1454 | { |
| 1455 | error(ERR_NONFATAL, |
| 1456 | "`%s' expects context identifiers", |
| 1457 | directives[i]); |
| 1458 | free_tlist(origline); |
| 1459 | return -1; |
| 1460 | } |
| 1461 | if (!nasm_stricmp(tline->text, cstk->name)) |
| 1462 | j = TRUE; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1463 | tline = tline->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1464 | } |
| 1465 | if (i == PP_IFNCTX || i == PP_ELIFNCTX) |
| 1466 | j = !j; |
| 1467 | free_tlist(origline); |
| 1468 | return j; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1469 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1470 | case PP_IFDEF: |
| 1471 | case PP_ELIFDEF: |
| 1472 | case PP_IFNDEF: |
| 1473 | case PP_ELIFNDEF: |
| 1474 | j = FALSE; /* have we matched yet? */ |
| 1475 | while (tline) |
| 1476 | { |
| 1477 | skip_white_(tline); |
| 1478 | if (!tline || (tline->type != TOK_ID && |
| 1479 | (tline->type != TOK_PREPROC_ID || |
| 1480 | tline->text[1] != '$'))) |
| 1481 | { |
| 1482 | error(ERR_NONFATAL, |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 1483 | "`%s' expects macro identifiers", |
| 1484 | directives[i]); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1485 | free_tlist(origline); |
| 1486 | return -1; |
| 1487 | } |
| 1488 | if (smacro_defined(NULL, tline->text, 0, NULL, 1)) |
| 1489 | j = TRUE; |
| 1490 | tline = tline->next; |
| 1491 | } |
| 1492 | if (i == PP_IFNDEF || i == PP_ELIFNDEF) |
| 1493 | j = !j; |
| 1494 | free_tlist(origline); |
| 1495 | return j; |
| 1496 | |
| 1497 | case PP_IFIDN: |
| 1498 | case PP_ELIFIDN: |
| 1499 | case PP_IFNIDN: |
| 1500 | case PP_ELIFNIDN: |
| 1501 | case PP_IFIDNI: |
| 1502 | case PP_ELIFIDNI: |
| 1503 | case PP_IFNIDNI: |
| 1504 | case PP_ELIFNIDNI: |
| 1505 | tline = expand_smacro(tline); |
| 1506 | t = tt = tline; |
| 1507 | while (tok_isnt_(tt, ",")) |
| 1508 | tt = tt->next; |
| 1509 | if (!tt) |
| 1510 | { |
| 1511 | error(ERR_NONFATAL, |
| 1512 | "`%s' expects two comma-separated arguments", |
| 1513 | directives[i]); |
| 1514 | free_tlist(tline); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1515 | return -1; |
| 1516 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1517 | tt = tt->next; |
| 1518 | casesense = (i == PP_IFIDN || i == PP_ELIFIDN || |
| 1519 | i == PP_IFNIDN || i == PP_ELIFNIDN); |
| 1520 | j = TRUE; /* assume equality unless proved not */ |
| 1521 | while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) |
| 1522 | { |
| 1523 | if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) |
| 1524 | { |
| 1525 | error(ERR_NONFATAL, "`%s': more than one comma on line", |
| 1526 | directives[i]); |
| 1527 | free_tlist(tline); |
| 1528 | return -1; |
| 1529 | } |
| 1530 | if (t->type == TOK_WHITESPACE) |
| 1531 | { |
| 1532 | t = t->next; |
| 1533 | continue; |
| 1534 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1535 | if (tt->type == TOK_WHITESPACE) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1536 | { |
| 1537 | tt = tt->next; |
| 1538 | continue; |
| 1539 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1540 | if (tt->type != t->type) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1541 | { |
| 1542 | j = FALSE; /* found mismatching tokens */ |
| 1543 | break; |
| 1544 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1545 | /* Unify surrounding quotes for strings */ |
| 1546 | if (t->type == TOK_STRING) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1547 | { |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1548 | tt->text[0] = t->text[0]; |
| 1549 | tt->text[strlen(tt->text) - 1] = t->text[0]; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1550 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1551 | if (mstrcmp(tt->text, t->text, casesense) != 0) |
| 1552 | { |
| 1553 | j = FALSE; /* found mismatching tokens */ |
| 1554 | break; |
| 1555 | } |
| 1556 | |
| 1557 | t = t->next; |
| 1558 | tt = tt->next; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1559 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1560 | if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt) |
| 1561 | j = FALSE; /* trailing gunk on one end or other */ |
| 1562 | if (i == PP_IFNIDN || i == PP_ELIFNIDN || |
| 1563 | i == PP_IFNIDNI || i == PP_ELIFNIDNI) |
| 1564 | j = !j; |
| 1565 | free_tlist(tline); |
| 1566 | return j; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1567 | |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 1568 | case PP_IFMACRO: |
| 1569 | case PP_ELIFMACRO: |
| 1570 | case PP_IFNMACRO: |
| 1571 | case PP_ELIFNMACRO: |
| 1572 | { |
| 1573 | int found = 0; |
| 1574 | MMacro searching, *mmac; |
| 1575 | |
| 1576 | tline = tline->next; |
| 1577 | skip_white_(tline); |
| 1578 | tline = expand_id(tline); |
| 1579 | if (!tok_type_(tline, TOK_ID)) |
| 1580 | { |
| 1581 | error(ERR_NONFATAL, |
| 1582 | "`%s' expects a macro name", |
| 1583 | directives[i]); |
| 1584 | return -1; |
| 1585 | } |
| 1586 | searching.name = nasm_strdup(tline->text); |
| 1587 | searching.casesense = (i == PP_MACRO); |
| 1588 | searching.plus = FALSE; |
| 1589 | searching.nolist = FALSE; |
| 1590 | searching.in_progress = FALSE; |
| 1591 | searching.rep_nest = NULL; |
| 1592 | searching.nparam_min = 0; |
| 1593 | searching.nparam_max = INT_MAX; |
| 1594 | tline = expand_smacro(tline->next); |
| 1595 | skip_white_(tline); |
| 1596 | if (!tline) |
| 1597 | { |
| 1598 | } else if (!tok_type_(tline, TOK_NUMBER)) |
| 1599 | { |
| 1600 | error(ERR_NONFATAL, |
| 1601 | "`%s' expects a parameter count or nothing", |
| 1602 | directives[i]); |
| 1603 | } |
| 1604 | else |
| 1605 | { |
| 1606 | searching.nparam_min = searching.nparam_max = |
| 1607 | readnum(tline->text, &j); |
| 1608 | if (j) |
| 1609 | error(ERR_NONFATAL, |
| 1610 | "unable to parse parameter count `%s'", |
| 1611 | tline->text); |
| 1612 | } |
| 1613 | if (tline && tok_is_(tline->next, "-")) |
| 1614 | { |
| 1615 | tline = tline->next->next; |
| 1616 | if (tok_is_(tline, "*")) |
| 1617 | searching.nparam_max = INT_MAX; |
| 1618 | else if (!tok_type_(tline, TOK_NUMBER)) |
| 1619 | error(ERR_NONFATAL, |
| 1620 | "`%s' expects a parameter count after `-'", |
| 1621 | directives[i]); |
| 1622 | else |
| 1623 | { |
| 1624 | searching.nparam_max = readnum(tline->text, &j); |
| 1625 | if (j) |
| 1626 | error(ERR_NONFATAL, |
| 1627 | "unable to parse parameter count `%s'", |
| 1628 | tline->text); |
| 1629 | if (searching.nparam_min > searching.nparam_max) |
| 1630 | error(ERR_NONFATAL, |
| 1631 | "minimum parameter count exceeds maximum"); |
| 1632 | } |
| 1633 | } |
| 1634 | if (tline && tok_is_(tline->next, "+")) |
| 1635 | { |
| 1636 | tline = tline->next; |
| 1637 | searching.plus = TRUE; |
| 1638 | } |
| 1639 | mmac = mmacros[hash(searching.name)]; |
| 1640 | while (mmac) |
| 1641 | { |
| 1642 | if (!strcmp(mmac->name, searching.name) && |
| 1643 | (mmac->nparam_min <= searching.nparam_max |
| 1644 | || searching.plus) |
| 1645 | && (searching.nparam_min <= mmac->nparam_max |
| 1646 | || mmac->plus)) |
| 1647 | { |
| 1648 | found = TRUE; |
| 1649 | break; |
| 1650 | } |
| 1651 | mmac = mmac->next; |
| 1652 | } |
| 1653 | nasm_free(searching.name); |
| 1654 | free_tlist(origline); |
| 1655 | if (i == PP_IFNMACRO || i == PP_ELIFNMACRO) |
| 1656 | found = !found; |
| 1657 | return found; |
| 1658 | } |
| 1659 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1660 | case PP_IFID: |
| 1661 | case PP_ELIFID: |
| 1662 | case PP_IFNID: |
| 1663 | case PP_ELIFNID: |
| 1664 | case PP_IFNUM: |
| 1665 | case PP_ELIFNUM: |
| 1666 | case PP_IFNNUM: |
| 1667 | case PP_ELIFNNUM: |
| 1668 | case PP_IFSTR: |
| 1669 | case PP_ELIFSTR: |
| 1670 | case PP_IFNSTR: |
| 1671 | case PP_ELIFNSTR: |
| 1672 | tline = expand_smacro(tline); |
| 1673 | t = tline; |
| 1674 | while (tok_type_(t, TOK_WHITESPACE)) |
| 1675 | t = t->next; |
| 1676 | j = FALSE; /* placate optimiser */ |
| 1677 | if (t) |
| 1678 | switch (i) |
| 1679 | { |
| 1680 | case PP_IFID: |
| 1681 | case PP_ELIFID: |
| 1682 | case PP_IFNID: |
| 1683 | case PP_ELIFNID: |
| 1684 | j = (t->type == TOK_ID); |
| 1685 | break; |
| 1686 | case PP_IFNUM: |
| 1687 | case PP_ELIFNUM: |
| 1688 | case PP_IFNNUM: |
| 1689 | case PP_ELIFNNUM: |
| 1690 | j = (t->type == TOK_NUMBER); |
| 1691 | break; |
| 1692 | case PP_IFSTR: |
| 1693 | case PP_ELIFSTR: |
| 1694 | case PP_IFNSTR: |
| 1695 | case PP_ELIFNSTR: |
| 1696 | j = (t->type == TOK_STRING); |
| 1697 | break; |
| 1698 | } |
| 1699 | if (i == PP_IFNID || i == PP_ELIFNID || |
| 1700 | i == PP_IFNNUM || i == PP_ELIFNNUM || |
| 1701 | i == PP_IFNSTR || i == PP_ELIFNSTR) |
| 1702 | j = !j; |
| 1703 | free_tlist(tline); |
| 1704 | return j; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1705 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1706 | case PP_IF: |
| 1707 | case PP_ELIF: |
| 1708 | t = tline = expand_smacro(tline); |
| 1709 | tptr = &t; |
| 1710 | tokval.t_type = TOKEN_INVALID; |
| 1711 | evalresult = evaluate(ppscan, tptr, &tokval, |
| 1712 | NULL, pass | CRITICAL, error, NULL); |
| 1713 | free_tlist(tline); |
| 1714 | if (!evalresult) |
| 1715 | return -1; |
| 1716 | if (tokval.t_type) |
| 1717 | error(ERR_WARNING, |
| 1718 | "trailing garbage after expression ignored"); |
| 1719 | if (!is_simple(evalresult)) |
| 1720 | { |
| 1721 | error(ERR_NONFATAL, |
| 1722 | "non-constant value given to `%s'", directives[i]); |
| 1723 | return -1; |
| 1724 | } |
| 1725 | return reloc_value(evalresult) != 0; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1726 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1727 | default: |
| 1728 | error(ERR_FATAL, |
| 1729 | "preprocessor directive `%s' not yet implemented", |
| 1730 | directives[i]); |
| 1731 | free_tlist(origline); |
| 1732 | return -1; /* yeah, right */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1733 | } |
| 1734 | } |
| 1735 | |
| 1736 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1737 | * Expand macros in a string. Used in %error and %include directives. |
| 1738 | * First tokenise the string, apply "expand_smacro" and then de-tokenise back. |
| 1739 | * The returned variable should ALWAYS be freed after usage. |
| 1740 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1741 | void |
| 1742 | expand_macros_in_string(char **p) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1743 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1744 | Token *line = tokenise(*p); |
| 1745 | line = expand_smacro(line); |
| 1746 | *p = detoken(line, FALSE); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1747 | } |
| 1748 | |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 1749 | /** |
| 1750 | * find and process preprocessor directive in passed line |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1751 | * Find out if a line contains a preprocessor directive, and deal |
| 1752 | * with it if so. |
| 1753 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 1754 | * If a directive _is_ found, it is the responsibility of this routine |
| 1755 | * (and not the caller) to free_tlist() the line. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1756 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 1757 | * @param tline a pointer to the current tokeninzed line linked list |
| 1758 | * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1759 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1760 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1761 | static int |
| 1762 | do_directive(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1763 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1764 | int i, j, k, m, nparam, nolist; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1765 | int offset; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1766 | char *p, *mname; |
| 1767 | Include *inc; |
| 1768 | Context *ctx; |
| 1769 | Cond *cond; |
| 1770 | SMacro *smac, **smhead; |
| 1771 | MMacro *mmac; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1772 | Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline; |
| 1773 | Line *l; |
| 1774 | struct tokenval tokval; |
| 1775 | expr *evalresult; |
H. Peter Anvin | b64535f | 2002-04-30 20:55:37 +0000 | [diff] [blame] | 1776 | MMacro *tmp_defining; /* Used when manipulating rep_nest */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1777 | |
| 1778 | origline = tline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1779 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1780 | skip_white_(tline); |
| 1781 | if (!tok_type_(tline, TOK_PREPROC_ID) || |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1782 | (tline->text[1] == '%' || tline->text[1] == '$' |
| 1783 | || tline->text[1] == '!')) |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 1784 | return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1785 | |
| 1786 | i = -1; |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 1787 | j = elements(directives); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1788 | while (j - i > 1) |
| 1789 | { |
| 1790 | k = (j + i) / 2; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1791 | m = nasm_stricmp(tline->text, directives[k]); |
| 1792 | if (m == 0) { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1793 | if (tasm_compatible_mode) { |
| 1794 | i = k; |
| 1795 | j = -2; |
| 1796 | } else if (k != PP_ARG && k != PP_LOCAL && k != PP_STACKSIZE) { |
| 1797 | i = k; |
| 1798 | j = -2; |
| 1799 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1800 | break; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1801 | } |
| 1802 | else if (m < 0) { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1803 | j = k; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1804 | } |
| 1805 | else |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1806 | i = k; |
| 1807 | } |
| 1808 | |
| 1809 | /* |
| 1810 | * 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] | 1811 | * or walking to the end of an already terminated %rep block, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1812 | * we should ignore all directives except for condition |
| 1813 | * directives. |
| 1814 | */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1815 | if (((istk->conds && !emitting(istk->conds->state)) || |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 1816 | (istk->mstk && !istk->mstk->in_progress)) && |
| 1817 | !is_condition(i)) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1818 | { |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 1819 | return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1820 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1821 | |
| 1822 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1823 | * If we're defining a macro or reading a %rep block, we should |
| 1824 | * ignore all directives except for %macro/%imacro (which |
| 1825 | * 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] | 1826 | * %rep block) %endrep. If we're in a %rep block, another %rep |
| 1827 | * causes an error, so should be let through. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1828 | */ |
| 1829 | if (defining && i != PP_MACRO && i != PP_IMACRO && |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1830 | i != PP_ENDMACRO && i != PP_ENDM && |
| 1831 | (defining->name || (i != PP_ENDREP && i != PP_REP))) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1832 | { |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 1833 | return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1834 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1835 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1836 | if (j != -2) |
| 1837 | { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1838 | error(ERR_NONFATAL, "unknown preprocessor directive `%s'", |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1839 | tline->text); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 1840 | return NO_DIRECTIVE_FOUND; /* didn't get it */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1841 | } |
| 1842 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1843 | switch (i) |
| 1844 | { |
| 1845 | case PP_STACKSIZE: |
| 1846 | /* Directive to tell NASM what the default stack size is. The |
| 1847 | * default is for a 16-bit stack, and this can be overriden with |
| 1848 | * %stacksize large. |
| 1849 | * the following form: |
| 1850 | * |
| 1851 | * ARG arg1:WORD, arg2:DWORD, arg4:QWORD |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1852 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1853 | tline = tline->next; |
| 1854 | if (tline && tline->type == TOK_WHITESPACE) |
| 1855 | tline = tline->next; |
| 1856 | if (!tline || tline->type != TOK_ID) |
| 1857 | { |
| 1858 | error(ERR_NONFATAL, "`%%stacksize' missing size parameter"); |
| 1859 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 1860 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1861 | } |
| 1862 | if (nasm_stricmp(tline->text, "flat") == 0) |
| 1863 | { |
| 1864 | /* All subsequent ARG directives are for a 32-bit stack */ |
| 1865 | StackSize = 4; |
| 1866 | StackPointer = "ebp"; |
| 1867 | ArgOffset = 8; |
| 1868 | LocalOffset = 4; |
| 1869 | } |
| 1870 | else if (nasm_stricmp(tline->text, "large") == 0) |
| 1871 | { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1872 | /* All subsequent ARG directives are for a 16-bit stack, |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1873 | * far function call. |
| 1874 | */ |
| 1875 | StackSize = 2; |
| 1876 | StackPointer = "bp"; |
| 1877 | ArgOffset = 4; |
| 1878 | LocalOffset = 2; |
| 1879 | } |
| 1880 | else if (nasm_stricmp(tline->text, "small") == 0) |
| 1881 | { |
| 1882 | /* All subsequent ARG directives are for a 16-bit stack, |
| 1883 | * far function call. We don't support near functions. |
| 1884 | */ |
| 1885 | StackSize = 2; |
| 1886 | StackPointer = "bp"; |
| 1887 | ArgOffset = 6; |
| 1888 | LocalOffset = 2; |
| 1889 | } |
| 1890 | else |
| 1891 | { |
| 1892 | error(ERR_NONFATAL, "`%%stacksize' invalid size type"); |
| 1893 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 1894 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1895 | } |
| 1896 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 1897 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1898 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1899 | case PP_ARG: |
| 1900 | /* TASM like ARG directive to define arguments to functions, in |
| 1901 | * the following form: |
| 1902 | * |
| 1903 | * ARG arg1:WORD, arg2:DWORD, arg4:QWORD |
| 1904 | */ |
| 1905 | offset = ArgOffset; |
| 1906 | do |
| 1907 | { |
| 1908 | char *arg, directive[256]; |
| 1909 | int size = StackSize; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1910 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1911 | /* Find the argument name */ |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1912 | tline = tline->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1913 | if (tline && tline->type == TOK_WHITESPACE) |
| 1914 | tline = tline->next; |
| 1915 | if (!tline || tline->type != TOK_ID) |
| 1916 | { |
| 1917 | error(ERR_NONFATAL, "`%%arg' missing argument parameter"); |
| 1918 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 1919 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1920 | } |
| 1921 | arg = tline->text; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1922 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1923 | /* Find the argument size type */ |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1924 | tline = tline->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1925 | if (!tline || tline->type != TOK_OTHER |
| 1926 | || tline->text[0] != ':') |
| 1927 | { |
| 1928 | error(ERR_NONFATAL, |
| 1929 | "Syntax error processing `%%arg' directive"); |
| 1930 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 1931 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1932 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1933 | tline = tline->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1934 | if (!tline || tline->type != TOK_ID) |
| 1935 | { |
| 1936 | error(ERR_NONFATAL, |
| 1937 | "`%%arg' missing size type parameter"); |
| 1938 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 1939 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1940 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1941 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1942 | /* Allow macro expansion of type parameter */ |
| 1943 | tt = tokenise(tline->text); |
| 1944 | tt = expand_smacro(tt); |
| 1945 | if (nasm_stricmp(tt->text, "byte") == 0) |
| 1946 | { |
| 1947 | size = MAX(StackSize, 1); |
| 1948 | } |
| 1949 | else if (nasm_stricmp(tt->text, "word") == 0) |
| 1950 | { |
| 1951 | size = MAX(StackSize, 2); |
| 1952 | } |
| 1953 | else if (nasm_stricmp(tt->text, "dword") == 0) |
| 1954 | { |
| 1955 | size = MAX(StackSize, 4); |
| 1956 | } |
| 1957 | else if (nasm_stricmp(tt->text, "qword") == 0) |
| 1958 | { |
| 1959 | size = MAX(StackSize, 8); |
| 1960 | } |
| 1961 | else if (nasm_stricmp(tt->text, "tword") == 0) |
| 1962 | { |
| 1963 | size = MAX(StackSize, 10); |
| 1964 | } |
| 1965 | else |
| 1966 | { |
| 1967 | error(ERR_NONFATAL, |
| 1968 | "Invalid size type for `%%arg' missing directive"); |
| 1969 | free_tlist(tt); |
| 1970 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 1971 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1972 | } |
| 1973 | free_tlist(tt); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1974 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1975 | /* Now define the macro for the argument */ |
| 1976 | sprintf(directive, "%%define %s (%s+%d)", arg, StackPointer, |
| 1977 | offset); |
| 1978 | do_directive(tokenise(directive)); |
| 1979 | offset += size; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1980 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1981 | /* Move to the next argument in the list */ |
| 1982 | tline = tline->next; |
| 1983 | if (tline && tline->type == TOK_WHITESPACE) |
| 1984 | tline = tline->next; |
| 1985 | } |
| 1986 | while (tline && tline->type == TOK_OTHER |
| 1987 | && tline->text[0] == ','); |
| 1988 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 1989 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1990 | |
| 1991 | case PP_LOCAL: |
| 1992 | /* TASM like LOCAL directive to define local variables for a |
| 1993 | * function, in the following form: |
| 1994 | * |
| 1995 | * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize |
| 1996 | * |
| 1997 | * The '= LocalSize' at the end is ignored by NASM, but is |
| 1998 | * required by TASM to define the local parameter size (and used |
| 1999 | * by the TASM macro package). |
| 2000 | */ |
| 2001 | offset = LocalOffset; |
| 2002 | do |
| 2003 | { |
| 2004 | char *local, directive[256]; |
| 2005 | int size = StackSize; |
| 2006 | |
| 2007 | /* Find the argument name */ |
| 2008 | tline = tline->next; |
| 2009 | if (tline && tline->type == TOK_WHITESPACE) |
| 2010 | tline = tline->next; |
| 2011 | if (!tline || tline->type != TOK_ID) |
| 2012 | { |
| 2013 | error(ERR_NONFATAL, |
| 2014 | "`%%local' missing argument parameter"); |
| 2015 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2016 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2017 | } |
| 2018 | local = tline->text; |
| 2019 | |
| 2020 | /* Find the argument size type */ |
| 2021 | tline = tline->next; |
| 2022 | if (!tline || tline->type != TOK_OTHER |
| 2023 | || tline->text[0] != ':') |
| 2024 | { |
| 2025 | error(ERR_NONFATAL, |
| 2026 | "Syntax error processing `%%local' directive"); |
| 2027 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2028 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2029 | } |
| 2030 | tline = tline->next; |
| 2031 | if (!tline || tline->type != TOK_ID) |
| 2032 | { |
| 2033 | error(ERR_NONFATAL, |
| 2034 | "`%%local' missing size type parameter"); |
| 2035 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2036 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2037 | } |
| 2038 | |
| 2039 | /* Allow macro expansion of type parameter */ |
| 2040 | tt = tokenise(tline->text); |
| 2041 | tt = expand_smacro(tt); |
| 2042 | if (nasm_stricmp(tt->text, "byte") == 0) |
| 2043 | { |
| 2044 | size = MAX(StackSize, 1); |
| 2045 | } |
| 2046 | else if (nasm_stricmp(tt->text, "word") == 0) |
| 2047 | { |
| 2048 | size = MAX(StackSize, 2); |
| 2049 | } |
| 2050 | else if (nasm_stricmp(tt->text, "dword") == 0) |
| 2051 | { |
| 2052 | size = MAX(StackSize, 4); |
| 2053 | } |
| 2054 | else if (nasm_stricmp(tt->text, "qword") == 0) |
| 2055 | { |
| 2056 | size = MAX(StackSize, 8); |
| 2057 | } |
| 2058 | else if (nasm_stricmp(tt->text, "tword") == 0) |
| 2059 | { |
| 2060 | size = MAX(StackSize, 10); |
| 2061 | } |
| 2062 | else |
| 2063 | { |
| 2064 | error(ERR_NONFATAL, |
| 2065 | "Invalid size type for `%%local' missing directive"); |
| 2066 | free_tlist(tt); |
| 2067 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2068 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2069 | } |
| 2070 | free_tlist(tt); |
| 2071 | |
| 2072 | /* Now define the macro for the argument */ |
| 2073 | sprintf(directive, "%%define %s (%s-%d)", local, StackPointer, |
| 2074 | offset); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2075 | do_directive(tokenise(directive)); |
| 2076 | offset += size; |
| 2077 | |
| 2078 | /* Now define the assign to setup the enter_c macro correctly */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2079 | sprintf(directive, "%%assign %%$localsize %%$localsize+%d", |
| 2080 | size); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2081 | do_directive(tokenise(directive)); |
| 2082 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2083 | /* Move to the next argument in the list */ |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2084 | tline = tline->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2085 | if (tline && tline->type == TOK_WHITESPACE) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2086 | tline = tline->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2087 | } |
| 2088 | while (tline && tline->type == TOK_OTHER |
| 2089 | && tline->text[0] == ','); |
| 2090 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2091 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2092 | |
| 2093 | case PP_CLEAR: |
| 2094 | if (tline->next) |
| 2095 | error(ERR_WARNING, |
| 2096 | "trailing garbage after `%%clear' ignored"); |
| 2097 | for (j = 0; j < NHASH; j++) |
| 2098 | { |
| 2099 | while (mmacros[j]) |
| 2100 | { |
| 2101 | MMacro *m = mmacros[j]; |
| 2102 | mmacros[j] = m->next; |
| 2103 | free_mmacro(m); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2104 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2105 | while (smacros[j]) |
| 2106 | { |
| 2107 | SMacro *s = smacros[j]; |
| 2108 | smacros[j] = smacros[j]->next; |
| 2109 | nasm_free(s->name); |
| 2110 | free_tlist(s->expansion); |
| 2111 | nasm_free(s); |
| 2112 | } |
| 2113 | } |
| 2114 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2115 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2116 | |
| 2117 | case PP_INCLUDE: |
| 2118 | tline = tline->next; |
| 2119 | skip_white_(tline); |
| 2120 | if (!tline || (tline->type != TOK_STRING && |
| 2121 | tline->type != TOK_INTERNAL_STRING)) |
| 2122 | { |
| 2123 | error(ERR_NONFATAL, "`%%include' expects a file name"); |
| 2124 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2125 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2126 | } |
| 2127 | if (tline->next) |
| 2128 | error(ERR_WARNING, |
| 2129 | "trailing garbage after `%%include' ignored"); |
| 2130 | if (tline->type != TOK_INTERNAL_STRING) |
| 2131 | { |
| 2132 | p = tline->text + 1; /* point past the quote to the name */ |
| 2133 | p[strlen(p) - 1] = '\0'; /* remove the trailing quote */ |
| 2134 | } |
| 2135 | else |
| 2136 | p = tline->text; /* internal_string is easier */ |
| 2137 | expand_macros_in_string(&p); |
| 2138 | inc = nasm_malloc(sizeof(Include)); |
| 2139 | inc->next = istk; |
| 2140 | inc->conds = NULL; |
| 2141 | inc->fp = inc_fopen(p); |
| 2142 | inc->fname = src_set_fname(p); |
| 2143 | inc->lineno = src_set_linnum(0); |
| 2144 | inc->lineinc = 1; |
| 2145 | inc->expansion = NULL; |
| 2146 | inc->mstk = NULL; |
| 2147 | istk = inc; |
| 2148 | list->uplevel(LIST_INCLUDE); |
| 2149 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2150 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2151 | |
| 2152 | case PP_PUSH: |
| 2153 | tline = tline->next; |
| 2154 | skip_white_(tline); |
| 2155 | tline = expand_id(tline); |
| 2156 | if (!tok_type_(tline, TOK_ID)) |
| 2157 | { |
| 2158 | error(ERR_NONFATAL, "`%%push' expects a context identifier"); |
| 2159 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2160 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2161 | } |
| 2162 | if (tline->next) |
| 2163 | error(ERR_WARNING, "trailing garbage after `%%push' ignored"); |
| 2164 | ctx = nasm_malloc(sizeof(Context)); |
| 2165 | ctx->next = cstk; |
| 2166 | ctx->localmac = NULL; |
| 2167 | ctx->name = nasm_strdup(tline->text); |
| 2168 | ctx->number = unique++; |
| 2169 | cstk = ctx; |
| 2170 | free_tlist(origline); |
| 2171 | break; |
| 2172 | |
| 2173 | case PP_REPL: |
| 2174 | tline = tline->next; |
| 2175 | skip_white_(tline); |
| 2176 | tline = expand_id(tline); |
| 2177 | if (!tok_type_(tline, TOK_ID)) |
| 2178 | { |
| 2179 | error(ERR_NONFATAL, "`%%repl' expects a context identifier"); |
| 2180 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2181 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2182 | } |
| 2183 | if (tline->next) |
| 2184 | error(ERR_WARNING, "trailing garbage after `%%repl' ignored"); |
| 2185 | if (!cstk) |
| 2186 | error(ERR_NONFATAL, "`%%repl': context stack is empty"); |
| 2187 | else |
| 2188 | { |
| 2189 | nasm_free(cstk->name); |
| 2190 | cstk->name = nasm_strdup(tline->text); |
| 2191 | } |
| 2192 | free_tlist(origline); |
| 2193 | break; |
| 2194 | |
| 2195 | case PP_POP: |
| 2196 | if (tline->next) |
| 2197 | error(ERR_WARNING, "trailing garbage after `%%pop' ignored"); |
| 2198 | if (!cstk) |
| 2199 | error(ERR_NONFATAL, |
| 2200 | "`%%pop': context stack is already empty"); |
| 2201 | else |
| 2202 | ctx_pop(); |
| 2203 | free_tlist(origline); |
| 2204 | break; |
| 2205 | |
| 2206 | case PP_ERROR: |
| 2207 | tline->next = expand_smacro(tline->next); |
| 2208 | tline = tline->next; |
| 2209 | skip_white_(tline); |
| 2210 | if (tok_type_(tline, TOK_STRING)) |
| 2211 | { |
| 2212 | p = tline->text + 1; /* point past the quote to the name */ |
| 2213 | p[strlen(p) - 1] = '\0'; /* remove the trailing quote */ |
| 2214 | expand_macros_in_string(&p); |
| 2215 | error(ERR_NONFATAL, "%s", p); |
| 2216 | nasm_free(p); |
| 2217 | } |
| 2218 | else |
| 2219 | { |
| 2220 | p = detoken(tline, FALSE); |
| 2221 | error(ERR_WARNING, "%s", p); |
| 2222 | nasm_free(p); |
| 2223 | } |
| 2224 | free_tlist(origline); |
| 2225 | break; |
| 2226 | |
| 2227 | case PP_IF: |
| 2228 | case PP_IFCTX: |
| 2229 | case PP_IFDEF: |
| 2230 | case PP_IFID: |
| 2231 | case PP_IFIDN: |
| 2232 | case PP_IFIDNI: |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 2233 | case PP_IFMACRO: |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2234 | case PP_IFNCTX: |
| 2235 | case PP_IFNDEF: |
| 2236 | case PP_IFNID: |
| 2237 | case PP_IFNIDN: |
| 2238 | case PP_IFNIDNI: |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 2239 | case PP_IFNMACRO: |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2240 | case PP_IFNNUM: |
| 2241 | case PP_IFNSTR: |
| 2242 | case PP_IFNUM: |
| 2243 | case PP_IFSTR: |
| 2244 | if (istk->conds && !emitting(istk->conds->state)) |
| 2245 | j = COND_NEVER; |
| 2246 | else |
| 2247 | { |
| 2248 | j = if_condition(tline->next, i); |
| 2249 | tline->next = NULL; /* it got freed */ |
| 2250 | free_tlist(origline); |
| 2251 | j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; |
| 2252 | } |
| 2253 | cond = nasm_malloc(sizeof(Cond)); |
| 2254 | cond->next = istk->conds; |
| 2255 | cond->state = j; |
| 2256 | istk->conds = cond; |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2257 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2258 | |
| 2259 | case PP_ELIF: |
| 2260 | case PP_ELIFCTX: |
| 2261 | case PP_ELIFDEF: |
| 2262 | case PP_ELIFID: |
| 2263 | case PP_ELIFIDN: |
| 2264 | case PP_ELIFIDNI: |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 2265 | case PP_ELIFMACRO: |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2266 | case PP_ELIFNCTX: |
| 2267 | case PP_ELIFNDEF: |
| 2268 | case PP_ELIFNID: |
| 2269 | case PP_ELIFNIDN: |
| 2270 | case PP_ELIFNIDNI: |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 2271 | case PP_ELIFNMACRO: |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2272 | case PP_ELIFNNUM: |
| 2273 | case PP_ELIFNSTR: |
| 2274 | case PP_ELIFNUM: |
| 2275 | case PP_ELIFSTR: |
| 2276 | if (!istk->conds) |
| 2277 | error(ERR_FATAL, "`%s': no matching `%%if'", directives[i]); |
| 2278 | if (emitting(istk->conds->state) |
| 2279 | || istk->conds->state == COND_NEVER) |
| 2280 | istk->conds->state = COND_NEVER; |
| 2281 | else |
| 2282 | { |
H. Peter Anvin | 0c60815 | 2002-05-22 22:59:40 +0000 | [diff] [blame] | 2283 | /* |
| 2284 | * IMPORTANT: In the case of %if, we will already have |
| 2285 | * called expand_mmac_params(); however, if we're |
| 2286 | * processing an %elif we must have been in a |
| 2287 | * non-emitting mode, which would have inhibited |
| 2288 | * the normal invocation of expand_mmac_params(). Therefore, |
| 2289 | * we have to do it explicitly here. |
| 2290 | */ |
| 2291 | j = if_condition(expand_mmac_params(tline->next), i); |
| 2292 | tline->next = NULL; /* it got freed */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2293 | free_tlist(origline); |
| 2294 | istk->conds->state = |
| 2295 | j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; |
| 2296 | } |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2297 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2298 | |
| 2299 | case PP_ELSE: |
| 2300 | if (tline->next) |
| 2301 | error(ERR_WARNING, "trailing garbage after `%%else' ignored"); |
| 2302 | if (!istk->conds) |
| 2303 | error(ERR_FATAL, "`%%else': no matching `%%if'"); |
| 2304 | if (emitting(istk->conds->state) |
| 2305 | || istk->conds->state == COND_NEVER) |
| 2306 | istk->conds->state = COND_ELSE_FALSE; |
| 2307 | else |
| 2308 | istk->conds->state = COND_ELSE_TRUE; |
| 2309 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2310 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2311 | |
| 2312 | case PP_ENDIF: |
| 2313 | if (tline->next) |
| 2314 | error(ERR_WARNING, |
| 2315 | "trailing garbage after `%%endif' ignored"); |
| 2316 | if (!istk->conds) |
| 2317 | error(ERR_FATAL, "`%%endif': no matching `%%if'"); |
| 2318 | cond = istk->conds; |
| 2319 | istk->conds = cond->next; |
| 2320 | nasm_free(cond); |
| 2321 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2322 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2323 | |
| 2324 | case PP_MACRO: |
| 2325 | case PP_IMACRO: |
| 2326 | if (defining) |
| 2327 | error(ERR_FATAL, |
| 2328 | "`%%%smacro': already defining a macro", |
| 2329 | (i == PP_IMACRO ? "i" : "")); |
| 2330 | tline = tline->next; |
| 2331 | skip_white_(tline); |
| 2332 | tline = expand_id(tline); |
| 2333 | if (!tok_type_(tline, TOK_ID)) |
| 2334 | { |
| 2335 | error(ERR_NONFATAL, |
| 2336 | "`%%%smacro' expects a macro name", |
| 2337 | (i == PP_IMACRO ? "i" : "")); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2338 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2339 | } |
| 2340 | defining = nasm_malloc(sizeof(MMacro)); |
| 2341 | defining->name = nasm_strdup(tline->text); |
| 2342 | defining->casesense = (i == PP_MACRO); |
| 2343 | defining->plus = FALSE; |
| 2344 | defining->nolist = FALSE; |
| 2345 | defining->in_progress = FALSE; |
| 2346 | defining->rep_nest = NULL; |
| 2347 | tline = expand_smacro(tline->next); |
| 2348 | skip_white_(tline); |
| 2349 | if (!tok_type_(tline, TOK_NUMBER)) |
| 2350 | { |
| 2351 | error(ERR_NONFATAL, |
| 2352 | "`%%%smacro' expects a parameter count", |
| 2353 | (i == PP_IMACRO ? "i" : "")); |
| 2354 | defining->nparam_min = defining->nparam_max = 0; |
| 2355 | } |
| 2356 | else |
| 2357 | { |
| 2358 | defining->nparam_min = defining->nparam_max = |
| 2359 | readnum(tline->text, &j); |
| 2360 | if (j) |
| 2361 | error(ERR_NONFATAL, |
| 2362 | "unable to parse parameter count `%s'", |
| 2363 | tline->text); |
| 2364 | } |
| 2365 | if (tline && tok_is_(tline->next, "-")) |
| 2366 | { |
| 2367 | tline = tline->next->next; |
| 2368 | if (tok_is_(tline, "*")) |
| 2369 | defining->nparam_max = INT_MAX; |
| 2370 | else if (!tok_type_(tline, TOK_NUMBER)) |
| 2371 | error(ERR_NONFATAL, |
| 2372 | "`%%%smacro' expects a parameter count after `-'", |
| 2373 | (i == PP_IMACRO ? "i" : "")); |
| 2374 | else |
| 2375 | { |
| 2376 | defining->nparam_max = readnum(tline->text, &j); |
| 2377 | if (j) |
| 2378 | error(ERR_NONFATAL, |
| 2379 | "unable to parse parameter count `%s'", |
| 2380 | tline->text); |
| 2381 | if (defining->nparam_min > defining->nparam_max) |
| 2382 | error(ERR_NONFATAL, |
| 2383 | "minimum parameter count exceeds maximum"); |
| 2384 | } |
| 2385 | } |
| 2386 | if (tline && tok_is_(tline->next, "+")) |
| 2387 | { |
| 2388 | tline = tline->next; |
| 2389 | defining->plus = TRUE; |
| 2390 | } |
| 2391 | if (tline && tok_type_(tline->next, TOK_ID) && |
| 2392 | !nasm_stricmp(tline->next->text, ".nolist")) |
| 2393 | { |
| 2394 | tline = tline->next; |
| 2395 | defining->nolist = TRUE; |
| 2396 | } |
| 2397 | mmac = mmacros[hash(defining->name)]; |
| 2398 | while (mmac) |
| 2399 | { |
| 2400 | if (!strcmp(mmac->name, defining->name) && |
| 2401 | (mmac->nparam_min <= defining->nparam_max |
| 2402 | || defining->plus) |
| 2403 | && (defining->nparam_min <= mmac->nparam_max |
| 2404 | || mmac->plus)) |
| 2405 | { |
| 2406 | error(ERR_WARNING, |
| 2407 | "redefining multi-line macro `%s'", |
| 2408 | defining->name); |
| 2409 | break; |
| 2410 | } |
| 2411 | mmac = mmac->next; |
| 2412 | } |
| 2413 | /* |
| 2414 | * Handle default parameters. |
| 2415 | */ |
| 2416 | if (tline && tline->next) |
| 2417 | { |
| 2418 | defining->dlist = tline->next; |
| 2419 | tline->next = NULL; |
| 2420 | count_mmac_params(defining->dlist, &defining->ndefs, |
| 2421 | &defining->defaults); |
| 2422 | } |
| 2423 | else |
| 2424 | { |
| 2425 | defining->dlist = NULL; |
| 2426 | defining->defaults = NULL; |
| 2427 | } |
| 2428 | defining->expansion = NULL; |
| 2429 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2430 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2431 | |
| 2432 | case PP_ENDM: |
| 2433 | case PP_ENDMACRO: |
| 2434 | if (!defining) |
| 2435 | { |
| 2436 | error(ERR_NONFATAL, "`%s': not defining a macro", |
| 2437 | tline->text); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2438 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2439 | } |
| 2440 | k = hash(defining->name); |
| 2441 | defining->next = mmacros[k]; |
| 2442 | mmacros[k] = defining; |
| 2443 | defining = NULL; |
| 2444 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2445 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2446 | |
| 2447 | case PP_ROTATE: |
| 2448 | if (tline->next && tline->next->type == TOK_WHITESPACE) |
| 2449 | tline = tline->next; |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2450 | if (tline->next == NULL) |
| 2451 | { |
| 2452 | free_tlist(origline); |
| 2453 | error(ERR_NONFATAL, "`%%rotate' missing rotate count"); |
| 2454 | return DIRECTIVE_FOUND; |
| 2455 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2456 | t = expand_smacro(tline->next); |
| 2457 | tline->next = NULL; |
| 2458 | free_tlist(origline); |
| 2459 | tline = t; |
| 2460 | tptr = &t; |
| 2461 | tokval.t_type = TOKEN_INVALID; |
| 2462 | evalresult = |
| 2463 | evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL); |
| 2464 | free_tlist(tline); |
| 2465 | if (!evalresult) |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2466 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2467 | if (tokval.t_type) |
| 2468 | error(ERR_WARNING, |
| 2469 | "trailing garbage after expression ignored"); |
| 2470 | if (!is_simple(evalresult)) |
| 2471 | { |
| 2472 | error(ERR_NONFATAL, "non-constant value given to `%%rotate'"); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2473 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2474 | } |
| 2475 | mmac = istk->mstk; |
| 2476 | while (mmac && !mmac->name) /* avoid mistaking %reps for macros */ |
| 2477 | mmac = mmac->next_active; |
| 2478 | if (!mmac) |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2479 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2480 | error(ERR_NONFATAL, |
| 2481 | "`%%rotate' invoked outside a macro call"); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2482 | } |
| 2483 | else if (mmac->nparam == 0) |
| 2484 | { |
| 2485 | error(ERR_NONFATAL, |
| 2486 | "`%%rotate' invoked within macro without parameters"); |
| 2487 | } |
| 2488 | else |
| 2489 | { |
| 2490 | mmac->rotate = mmac->rotate + reloc_value(evalresult); |
| 2491 | |
| 2492 | if (mmac->rotate < 0) |
| 2493 | mmac->rotate = |
| 2494 | mmac->nparam - (-mmac->rotate) % mmac->nparam; |
| 2495 | mmac->rotate %= mmac->nparam; |
| 2496 | } |
| 2497 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2498 | |
| 2499 | case PP_REP: |
| 2500 | nolist = FALSE; |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 2501 | do { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2502 | tline = tline->next; |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 2503 | } while (tok_type_(tline, TOK_WHITESPACE)); |
| 2504 | |
| 2505 | if (tok_type_(tline, TOK_ID) && |
| 2506 | nasm_stricmp(tline->text, ".nolist") == 0) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2507 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2508 | nolist = TRUE; |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 2509 | do { |
| 2510 | tline = tline->next; |
| 2511 | } while (tok_type_(tline, TOK_WHITESPACE)); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2512 | } |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 2513 | |
| 2514 | if (tline) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2515 | { |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 2516 | t = expand_smacro(tline); |
| 2517 | tptr = &t; |
| 2518 | tokval.t_type = TOKEN_INVALID; |
| 2519 | evalresult = |
| 2520 | evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL); |
| 2521 | if (!evalresult) |
| 2522 | { |
| 2523 | free_tlist(origline); |
| 2524 | return DIRECTIVE_FOUND; |
| 2525 | } |
| 2526 | if (tokval.t_type) |
| 2527 | error(ERR_WARNING, |
| 2528 | "trailing garbage after expression ignored"); |
| 2529 | if (!is_simple(evalresult)) |
| 2530 | { |
| 2531 | error(ERR_NONFATAL, "non-constant value given to `%%rep'"); |
| 2532 | return DIRECTIVE_FOUND; |
| 2533 | } |
| 2534 | i = (int)reloc_value(evalresult) + 1; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2535 | } |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 2536 | else |
| 2537 | { |
| 2538 | error(ERR_NONFATAL, "`%%rep' expects a repeat count"); |
| 2539 | i = 0; |
| 2540 | } |
| 2541 | free_tlist(origline); |
| 2542 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2543 | tmp_defining = defining; |
| 2544 | defining = nasm_malloc(sizeof(MMacro)); |
| 2545 | defining->name = NULL; /* flags this macro as a %rep block */ |
| 2546 | defining->casesense = 0; |
| 2547 | defining->plus = FALSE; |
| 2548 | defining->nolist = nolist; |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 2549 | defining->in_progress = i; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2550 | defining->nparam_min = defining->nparam_max = 0; |
| 2551 | defining->defaults = NULL; |
| 2552 | defining->dlist = NULL; |
| 2553 | defining->expansion = NULL; |
| 2554 | defining->next_active = istk->mstk; |
| 2555 | defining->rep_nest = tmp_defining; |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2556 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2557 | |
| 2558 | case PP_ENDREP: |
| 2559 | if (!defining || defining->name) |
| 2560 | { |
| 2561 | error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'"); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2562 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2563 | } |
| 2564 | |
| 2565 | /* |
| 2566 | * Now we have a "macro" defined - although it has no name |
| 2567 | * and we won't be entering it in the hash tables - we must |
| 2568 | * push a macro-end marker for it on to istk->expansion. |
| 2569 | * After that, it will take care of propagating itself (a |
| 2570 | * macro-end marker line for a macro which is really a %rep |
| 2571 | * block will cause the macro to be re-expanded, complete |
| 2572 | * with another macro-end marker to ensure the process |
| 2573 | * continues) until the whole expansion is forcibly removed |
| 2574 | * from istk->expansion by a %exitrep. |
| 2575 | */ |
| 2576 | l = nasm_malloc(sizeof(Line)); |
| 2577 | l->next = istk->expansion; |
| 2578 | l->finishes = defining; |
| 2579 | l->first = NULL; |
| 2580 | istk->expansion = l; |
| 2581 | |
| 2582 | istk->mstk = defining; |
| 2583 | |
| 2584 | list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); |
| 2585 | tmp_defining = defining; |
| 2586 | defining = defining->rep_nest; |
| 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_EXITREP: |
| 2591 | /* |
| 2592 | * We must search along istk->expansion until we hit a |
| 2593 | * macro-end marker for a macro with no name. Then we set |
| 2594 | * its `in_progress' flag to 0. |
| 2595 | */ |
| 2596 | for (l = istk->expansion; l; l = l->next) |
| 2597 | if (l->finishes && !l->finishes->name) |
| 2598 | break; |
| 2599 | |
| 2600 | if (l) |
| 2601 | l->finishes->in_progress = 0; |
| 2602 | else |
| 2603 | error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block"); |
| 2604 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2605 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2606 | |
| 2607 | case PP_XDEFINE: |
| 2608 | case PP_IXDEFINE: |
| 2609 | case PP_DEFINE: |
| 2610 | case PP_IDEFINE: |
| 2611 | tline = tline->next; |
| 2612 | skip_white_(tline); |
| 2613 | tline = expand_id(tline); |
| 2614 | if (!tline || (tline->type != TOK_ID && |
| 2615 | (tline->type != TOK_PREPROC_ID || |
| 2616 | tline->text[1] != '$'))) |
| 2617 | { |
| 2618 | error(ERR_NONFATAL, |
| 2619 | "`%%%s%sdefine' expects a macro identifier", |
| 2620 | ((i == PP_IDEFINE || i == PP_IXDEFINE) ? "i" : ""), |
| 2621 | ((i == PP_XDEFINE || i == PP_IXDEFINE) ? "x" : "")); |
| 2622 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2623 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2624 | } |
| 2625 | |
| 2626 | ctx = get_ctx(tline->text, FALSE); |
| 2627 | if (!ctx) |
| 2628 | smhead = &smacros[hash(tline->text)]; |
| 2629 | else |
| 2630 | smhead = &ctx->localmac; |
| 2631 | mname = tline->text; |
| 2632 | last = tline; |
| 2633 | param_start = tline = tline->next; |
| 2634 | nparam = 0; |
| 2635 | |
| 2636 | /* Expand the macro definition now for %xdefine and %ixdefine */ |
| 2637 | if ((i == PP_XDEFINE) || (i == PP_IXDEFINE)) |
| 2638 | tline = expand_smacro(tline); |
| 2639 | |
| 2640 | if (tok_is_(tline, "(")) |
| 2641 | { |
| 2642 | /* |
| 2643 | * This macro has parameters. |
| 2644 | */ |
| 2645 | |
| 2646 | tline = tline->next; |
| 2647 | while (1) |
| 2648 | { |
| 2649 | skip_white_(tline); |
| 2650 | if (!tline) |
| 2651 | { |
| 2652 | error(ERR_NONFATAL, "parameter identifier expected"); |
| 2653 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2654 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2655 | } |
| 2656 | if (tline->type != TOK_ID) |
| 2657 | { |
| 2658 | error(ERR_NONFATAL, |
| 2659 | "`%s': parameter identifier expected", |
| 2660 | tline->text); |
| 2661 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2662 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2663 | } |
| 2664 | tline->type = TOK_SMAC_PARAM + nparam++; |
| 2665 | tline = tline->next; |
| 2666 | skip_white_(tline); |
| 2667 | if (tok_is_(tline, ",")) |
| 2668 | { |
| 2669 | tline = tline->next; |
| 2670 | continue; |
| 2671 | } |
| 2672 | if (!tok_is_(tline, ")")) |
| 2673 | { |
| 2674 | error(ERR_NONFATAL, |
| 2675 | "`)' expected to terminate macro template"); |
| 2676 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2677 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2678 | } |
| 2679 | break; |
| 2680 | } |
| 2681 | last = tline; |
| 2682 | tline = tline->next; |
| 2683 | } |
| 2684 | if (tok_type_(tline, TOK_WHITESPACE)) |
| 2685 | last = tline, tline = tline->next; |
| 2686 | macro_start = NULL; |
| 2687 | last->next = NULL; |
| 2688 | t = tline; |
| 2689 | while (t) |
| 2690 | { |
| 2691 | if (t->type == TOK_ID) |
| 2692 | { |
| 2693 | for (tt = param_start; tt; tt = tt->next) |
| 2694 | if (tt->type >= TOK_SMAC_PARAM && |
| 2695 | !strcmp(tt->text, t->text)) |
| 2696 | t->type = tt->type; |
| 2697 | } |
| 2698 | tt = t->next; |
| 2699 | t->next = macro_start; |
| 2700 | macro_start = t; |
| 2701 | t = tt; |
| 2702 | } |
| 2703 | /* |
| 2704 | * Good. We now have a macro name, a parameter count, and a |
| 2705 | * token list (in reverse order) for an expansion. We ought |
| 2706 | * to be OK just to create an SMacro, store it, and let |
| 2707 | * free_tlist have the rest of the line (which we have |
| 2708 | * carefully re-terminated after chopping off the expansion |
| 2709 | * from the end). |
| 2710 | */ |
| 2711 | if (smacro_defined(ctx, mname, nparam, &smac, i == PP_DEFINE)) |
| 2712 | { |
| 2713 | if (!smac) |
| 2714 | { |
| 2715 | error(ERR_WARNING, |
| 2716 | "single-line macro `%s' defined both with and" |
| 2717 | " without parameters", mname); |
| 2718 | free_tlist(origline); |
| 2719 | free_tlist(macro_start); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2720 | return DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2721 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2722 | else |
| 2723 | { |
| 2724 | /* |
| 2725 | * We're redefining, so we have to take over an |
| 2726 | * existing SMacro structure. This means freeing |
| 2727 | * what was already in it. |
| 2728 | */ |
| 2729 | nasm_free(smac->name); |
| 2730 | free_tlist(smac->expansion); |
| 2731 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2732 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2733 | else |
| 2734 | { |
| 2735 | smac = nasm_malloc(sizeof(SMacro)); |
| 2736 | smac->next = *smhead; |
| 2737 | *smhead = smac; |
| 2738 | } |
| 2739 | smac->name = nasm_strdup(mname); |
| 2740 | smac->casesense = ((i == PP_DEFINE) || (i == PP_XDEFINE)); |
| 2741 | smac->nparam = nparam; |
| 2742 | smac->expansion = macro_start; |
| 2743 | smac->in_progress = FALSE; |
| 2744 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2745 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2746 | |
| 2747 | case PP_UNDEF: |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2748 | tline = tline->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2749 | skip_white_(tline); |
| 2750 | tline = expand_id(tline); |
| 2751 | if (!tline || (tline->type != TOK_ID && |
| 2752 | (tline->type != TOK_PREPROC_ID || |
| 2753 | tline->text[1] != '$'))) |
| 2754 | { |
| 2755 | error(ERR_NONFATAL, "`%%undef' expects a macro identifier"); |
| 2756 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2757 | return DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2758 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2759 | if (tline->next) |
| 2760 | { |
| 2761 | error(ERR_WARNING, |
| 2762 | "trailing garbage after macro name ignored"); |
| 2763 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2764 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2765 | /* Find the context that symbol belongs to */ |
| 2766 | ctx = get_ctx(tline->text, FALSE); |
| 2767 | if (!ctx) |
| 2768 | smhead = &smacros[hash(tline->text)]; |
| 2769 | else |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2770 | smhead = &ctx->localmac; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2771 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2772 | mname = tline->text; |
| 2773 | last = tline; |
| 2774 | last->next = NULL; |
| 2775 | |
| 2776 | /* |
| 2777 | * We now have a macro name... go hunt for it. |
| 2778 | */ |
| 2779 | while (smacro_defined(ctx, mname, -1, &smac, 1)) |
| 2780 | { |
| 2781 | /* Defined, so we need to find its predecessor and nuke it */ |
| 2782 | SMacro **s; |
| 2783 | for (s = smhead; *s && *s != smac; s = &(*s)->next); |
| 2784 | if (*s) |
| 2785 | { |
| 2786 | *s = smac->next; |
| 2787 | nasm_free(smac->name); |
| 2788 | free_tlist(smac->expansion); |
| 2789 | nasm_free(smac); |
| 2790 | } |
| 2791 | } |
| 2792 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2793 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2794 | |
| 2795 | case PP_STRLEN: |
| 2796 | tline = tline->next; |
| 2797 | skip_white_(tline); |
| 2798 | tline = expand_id(tline); |
| 2799 | if (!tline || (tline->type != TOK_ID && |
| 2800 | (tline->type != TOK_PREPROC_ID || |
| 2801 | tline->text[1] != '$'))) |
| 2802 | { |
| 2803 | error(ERR_NONFATAL, |
| 2804 | "`%%strlen' expects a macro identifier as first parameter"); |
| 2805 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2806 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2807 | } |
| 2808 | ctx = get_ctx(tline->text, FALSE); |
| 2809 | if (!ctx) |
| 2810 | smhead = &smacros[hash(tline->text)]; |
| 2811 | else |
| 2812 | smhead = &ctx->localmac; |
| 2813 | mname = tline->text; |
| 2814 | last = tline; |
| 2815 | tline = expand_smacro(tline->next); |
| 2816 | last->next = NULL; |
| 2817 | |
| 2818 | t = tline; |
| 2819 | while (tok_type_(t, TOK_WHITESPACE)) |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2820 | t = t->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2821 | /* t should now point to the string */ |
| 2822 | if (t->type != TOK_STRING) |
| 2823 | { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2824 | error(ERR_NONFATAL, |
| 2825 | "`%%strlen` requires string as second parameter"); |
| 2826 | free_tlist(tline); |
| 2827 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2828 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2829 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2830 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2831 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 2832 | macro_start->next = NULL; |
| 2833 | make_tok_num(macro_start, strlen(t->text) - 2); |
| 2834 | macro_start->mac = NULL; |
| 2835 | |
| 2836 | /* |
| 2837 | * We now have a macro name, an implicit parameter count of |
| 2838 | * zero, and a numeric token to use as an expansion. Create |
| 2839 | * and store an SMacro. |
| 2840 | */ |
| 2841 | if (smacro_defined(ctx, mname, 0, &smac, i == PP_STRLEN)) |
| 2842 | { |
| 2843 | if (!smac) |
| 2844 | error(ERR_WARNING, |
| 2845 | "single-line macro `%s' defined both with and" |
| 2846 | " without parameters", mname); |
| 2847 | else |
| 2848 | { |
| 2849 | /* |
| 2850 | * We're redefining, so we have to take over an |
| 2851 | * existing SMacro structure. This means freeing |
| 2852 | * what was already in it. |
| 2853 | */ |
| 2854 | nasm_free(smac->name); |
| 2855 | free_tlist(smac->expansion); |
| 2856 | } |
| 2857 | } |
| 2858 | else |
| 2859 | { |
| 2860 | smac = nasm_malloc(sizeof(SMacro)); |
| 2861 | smac->next = *smhead; |
| 2862 | *smhead = smac; |
| 2863 | } |
| 2864 | smac->name = nasm_strdup(mname); |
| 2865 | smac->casesense = (i == PP_STRLEN); |
| 2866 | smac->nparam = 0; |
| 2867 | smac->expansion = macro_start; |
| 2868 | smac->in_progress = FALSE; |
| 2869 | free_tlist(tline); |
| 2870 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2871 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2872 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2873 | case PP_SUBSTR: |
| 2874 | tline = tline->next; |
| 2875 | skip_white_(tline); |
| 2876 | tline = expand_id(tline); |
| 2877 | if (!tline || (tline->type != TOK_ID && |
| 2878 | (tline->type != TOK_PREPROC_ID || |
| 2879 | tline->text[1] != '$'))) |
| 2880 | { |
| 2881 | error(ERR_NONFATAL, |
| 2882 | "`%%substr' expects a macro identifier as first parameter"); |
| 2883 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2884 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2885 | } |
| 2886 | ctx = get_ctx(tline->text, FALSE); |
| 2887 | if (!ctx) |
| 2888 | smhead = &smacros[hash(tline->text)]; |
| 2889 | else |
| 2890 | smhead = &ctx->localmac; |
| 2891 | mname = tline->text; |
| 2892 | last = tline; |
| 2893 | tline = expand_smacro(tline->next); |
| 2894 | last->next = NULL; |
| 2895 | |
| 2896 | t = tline->next; |
| 2897 | while (tok_type_(t, TOK_WHITESPACE)) |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2898 | t = t->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2899 | |
| 2900 | /* t should now point to the string */ |
| 2901 | if (t->type != TOK_STRING) |
| 2902 | { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2903 | error(ERR_NONFATAL, |
| 2904 | "`%%substr` requires string as second parameter"); |
| 2905 | free_tlist(tline); |
| 2906 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2907 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2908 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2909 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2910 | tt = t->next; |
| 2911 | tptr = &tt; |
| 2912 | tokval.t_type = TOKEN_INVALID; |
| 2913 | evalresult = |
| 2914 | evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL); |
| 2915 | if (!evalresult) |
| 2916 | { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2917 | free_tlist(tline); |
| 2918 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2919 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2920 | } |
| 2921 | if (!is_simple(evalresult)) |
| 2922 | { |
| 2923 | error(ERR_NONFATAL, "non-constant value given to `%%substr`"); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2924 | free_tlist(tline); |
| 2925 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2926 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2927 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2928 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2929 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 2930 | macro_start->next = NULL; |
| 2931 | macro_start->text = nasm_strdup("'''"); |
| 2932 | if (evalresult->value > 0 |
| 2933 | && evalresult->value < strlen(t->text) - 1) |
| 2934 | { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2935 | macro_start->text[1] = t->text[evalresult->value]; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2936 | } |
| 2937 | else |
| 2938 | { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2939 | macro_start->text[2] = '\0'; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2940 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2941 | macro_start->type = TOK_STRING; |
| 2942 | macro_start->mac = NULL; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2943 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2944 | /* |
| 2945 | * We now have a macro name, an implicit parameter count of |
| 2946 | * zero, and a numeric token to use as an expansion. Create |
| 2947 | * and store an SMacro. |
| 2948 | */ |
| 2949 | if (smacro_defined(ctx, mname, 0, &smac, i == PP_SUBSTR)) |
| 2950 | { |
| 2951 | if (!smac) |
| 2952 | error(ERR_WARNING, |
| 2953 | "single-line macro `%s' defined both with and" |
| 2954 | " without parameters", mname); |
| 2955 | else |
| 2956 | { |
| 2957 | /* |
| 2958 | * We're redefining, so we have to take over an |
| 2959 | * existing SMacro structure. This means freeing |
| 2960 | * what was already in it. |
| 2961 | */ |
| 2962 | nasm_free(smac->name); |
| 2963 | free_tlist(smac->expansion); |
| 2964 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2965 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2966 | else |
| 2967 | { |
| 2968 | smac = nasm_malloc(sizeof(SMacro)); |
| 2969 | smac->next = *smhead; |
| 2970 | *smhead = smac; |
| 2971 | } |
| 2972 | smac->name = nasm_strdup(mname); |
| 2973 | smac->casesense = (i == PP_SUBSTR); |
| 2974 | smac->nparam = 0; |
| 2975 | smac->expansion = macro_start; |
| 2976 | smac->in_progress = FALSE; |
| 2977 | free_tlist(tline); |
| 2978 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2979 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2980 | |
| 2981 | |
| 2982 | case PP_ASSIGN: |
| 2983 | case PP_IASSIGN: |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2984 | tline = tline->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2985 | skip_white_(tline); |
| 2986 | tline = expand_id(tline); |
| 2987 | if (!tline || (tline->type != TOK_ID && |
| 2988 | (tline->type != TOK_PREPROC_ID || |
| 2989 | tline->text[1] != '$'))) |
| 2990 | { |
| 2991 | error(ERR_NONFATAL, |
| 2992 | "`%%%sassign' expects a macro identifier", |
| 2993 | (i == PP_IASSIGN ? "i" : "")); |
| 2994 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2995 | return DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2996 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2997 | ctx = get_ctx(tline->text, FALSE); |
| 2998 | if (!ctx) |
| 2999 | smhead = &smacros[hash(tline->text)]; |
| 3000 | else |
| 3001 | smhead = &ctx->localmac; |
| 3002 | mname = tline->text; |
| 3003 | last = tline; |
| 3004 | tline = expand_smacro(tline->next); |
| 3005 | last->next = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3006 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3007 | t = tline; |
| 3008 | tptr = &t; |
| 3009 | tokval.t_type = TOKEN_INVALID; |
| 3010 | evalresult = |
| 3011 | evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL); |
| 3012 | free_tlist(tline); |
| 3013 | if (!evalresult) |
| 3014 | { |
| 3015 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 3016 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3017 | } |
| 3018 | |
| 3019 | if (tokval.t_type) |
| 3020 | error(ERR_WARNING, |
| 3021 | "trailing garbage after expression ignored"); |
| 3022 | |
| 3023 | if (!is_simple(evalresult)) |
| 3024 | { |
| 3025 | error(ERR_NONFATAL, |
| 3026 | "non-constant value given to `%%%sassign'", |
| 3027 | (i == PP_IASSIGN ? "i" : "")); |
| 3028 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 3029 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3030 | } |
| 3031 | |
| 3032 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3033 | macro_start->next = NULL; |
| 3034 | make_tok_num(macro_start, reloc_value(evalresult)); |
| 3035 | macro_start->mac = NULL; |
| 3036 | |
| 3037 | /* |
| 3038 | * We now have a macro name, an implicit parameter count of |
| 3039 | * zero, and a numeric token to use as an expansion. Create |
| 3040 | * and store an SMacro. |
| 3041 | */ |
| 3042 | if (smacro_defined(ctx, mname, 0, &smac, i == PP_ASSIGN)) |
| 3043 | { |
| 3044 | if (!smac) |
| 3045 | error(ERR_WARNING, |
| 3046 | "single-line macro `%s' defined both with and" |
| 3047 | " without parameters", mname); |
| 3048 | else |
| 3049 | { |
| 3050 | /* |
| 3051 | * We're redefining, so we have to take over an |
| 3052 | * existing SMacro structure. This means freeing |
| 3053 | * what was already in it. |
| 3054 | */ |
| 3055 | nasm_free(smac->name); |
| 3056 | free_tlist(smac->expansion); |
| 3057 | } |
| 3058 | } |
| 3059 | else |
| 3060 | { |
| 3061 | smac = nasm_malloc(sizeof(SMacro)); |
| 3062 | smac->next = *smhead; |
| 3063 | *smhead = smac; |
| 3064 | } |
| 3065 | smac->name = nasm_strdup(mname); |
| 3066 | smac->casesense = (i == PP_ASSIGN); |
| 3067 | smac->nparam = 0; |
| 3068 | smac->expansion = macro_start; |
| 3069 | smac->in_progress = FALSE; |
| 3070 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 3071 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3072 | |
| 3073 | case PP_LINE: |
| 3074 | /* |
| 3075 | * Syntax is `%line nnn[+mmm] [filename]' |
| 3076 | */ |
| 3077 | tline = tline->next; |
| 3078 | skip_white_(tline); |
| 3079 | if (!tok_type_(tline, TOK_NUMBER)) |
| 3080 | { |
| 3081 | error(ERR_NONFATAL, "`%%line' expects line number"); |
| 3082 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 3083 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3084 | } |
| 3085 | k = readnum(tline->text, &j); |
| 3086 | m = 1; |
| 3087 | tline = tline->next; |
| 3088 | if (tok_is_(tline, "+")) |
| 3089 | { |
| 3090 | tline = tline->next; |
| 3091 | if (!tok_type_(tline, TOK_NUMBER)) |
| 3092 | { |
| 3093 | error(ERR_NONFATAL, "`%%line' expects line increment"); |
| 3094 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 3095 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3096 | } |
| 3097 | m = readnum(tline->text, &j); |
| 3098 | tline = tline->next; |
| 3099 | } |
| 3100 | skip_white_(tline); |
| 3101 | src_set_linnum(k); |
| 3102 | istk->lineinc = m; |
| 3103 | if (tline) |
| 3104 | { |
| 3105 | nasm_free(src_set_fname(detoken(tline, FALSE))); |
| 3106 | } |
| 3107 | free_tlist(origline); |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 3108 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3109 | |
| 3110 | default: |
| 3111 | error(ERR_FATAL, |
| 3112 | "preprocessor directive `%s' not yet implemented", |
| 3113 | directives[i]); |
| 3114 | break; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3115 | } |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 3116 | return DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3117 | } |
| 3118 | |
| 3119 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3120 | * Ensure that a macro parameter contains a condition code and |
| 3121 | * nothing else. Return the condition code index if so, or -1 |
| 3122 | * otherwise. |
| 3123 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3124 | static int |
| 3125 | find_cc(Token * t) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3126 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3127 | Token *tt; |
| 3128 | int i, j, k, m; |
| 3129 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3130 | skip_white_(t); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3131 | if (t->type != TOK_ID) |
| 3132 | return -1; |
| 3133 | tt = t->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3134 | skip_white_(tt); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3135 | if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ","))) |
| 3136 | return -1; |
| 3137 | |
| 3138 | i = -1; |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 3139 | j = elements(conditions); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3140 | while (j - i > 1) |
| 3141 | { |
| 3142 | k = (j + i) / 2; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3143 | m = nasm_stricmp(t->text, conditions[k]); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3144 | if (m == 0) |
| 3145 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3146 | i = k; |
| 3147 | j = -2; |
| 3148 | break; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3149 | } |
| 3150 | else if (m < 0) |
| 3151 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3152 | j = k; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3153 | } |
| 3154 | else |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3155 | i = k; |
| 3156 | } |
| 3157 | if (j != -2) |
| 3158 | return -1; |
| 3159 | return i; |
| 3160 | } |
| 3161 | |
| 3162 | /* |
| 3163 | * Expand MMacro-local things: parameter references (%0, %n, %+n, |
| 3164 | * %-n) and MMacro-local identifiers (%%foo). |
| 3165 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3166 | static Token * |
| 3167 | expand_mmac_params(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3168 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3169 | Token *t, *tt, **tail, *thead; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3170 | |
| 3171 | tail = &thead; |
| 3172 | thead = NULL; |
| 3173 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3174 | while (tline) |
| 3175 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3176 | if (tline->type == TOK_PREPROC_ID && |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3177 | (((tline->text[1] == '+' || tline->text[1] == '-') |
| 3178 | && tline->text[2]) || tline->text[1] == '%' |
| 3179 | || (tline->text[1] >= '0' && tline->text[1] <= '9'))) |
| 3180 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3181 | char *text = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3182 | int type = 0, cc; /* type = 0 to placate optimisers */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3183 | char tmpbuf[30]; |
| 3184 | int n, i; |
| 3185 | MMacro *mac; |
| 3186 | |
| 3187 | t = tline; |
| 3188 | tline = tline->next; |
| 3189 | |
| 3190 | mac = istk->mstk; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3191 | while (mac && !mac->name) /* avoid mistaking %reps for macros */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3192 | mac = mac->next_active; |
| 3193 | if (!mac) |
| 3194 | error(ERR_NONFATAL, "`%s': not in a macro call", t->text); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3195 | else |
| 3196 | switch (t->text[1]) |
| 3197 | { |
| 3198 | /* |
| 3199 | * We have to make a substitution of one of the |
| 3200 | * forms %1, %-1, %+1, %%foo, %0. |
| 3201 | */ |
| 3202 | case '0': |
| 3203 | type = TOK_NUMBER; |
| 3204 | sprintf(tmpbuf, "%d", mac->nparam); |
| 3205 | text = nasm_strdup(tmpbuf); |
| 3206 | break; |
| 3207 | case '%': |
| 3208 | type = TOK_ID; |
| 3209 | sprintf(tmpbuf, "..@%lu.", mac->unique); |
| 3210 | text = nasm_strcat(tmpbuf, t->text + 2); |
| 3211 | break; |
| 3212 | case '-': |
| 3213 | n = atoi(t->text + 2) - 1; |
| 3214 | if (n >= mac->nparam) |
| 3215 | tt = NULL; |
| 3216 | else |
| 3217 | { |
| 3218 | if (mac->nparam > 1) |
| 3219 | n = (n + mac->rotate) % mac->nparam; |
| 3220 | tt = mac->params[n]; |
| 3221 | } |
| 3222 | cc = find_cc(tt); |
| 3223 | if (cc == -1) |
| 3224 | { |
| 3225 | error(ERR_NONFATAL, |
| 3226 | "macro parameter %d is not a condition code", |
| 3227 | n + 1); |
| 3228 | text = NULL; |
| 3229 | } |
| 3230 | else |
| 3231 | { |
| 3232 | type = TOK_ID; |
| 3233 | if (inverse_ccs[cc] == -1) |
| 3234 | { |
| 3235 | error(ERR_NONFATAL, |
| 3236 | "condition code `%s' is not invertible", |
| 3237 | conditions[cc]); |
| 3238 | text = NULL; |
| 3239 | } |
| 3240 | else |
| 3241 | text = |
| 3242 | nasm_strdup(conditions[inverse_ccs |
| 3243 | [cc]]); |
| 3244 | } |
| 3245 | break; |
| 3246 | case '+': |
| 3247 | n = atoi(t->text + 2) - 1; |
| 3248 | if (n >= mac->nparam) |
| 3249 | tt = NULL; |
| 3250 | else |
| 3251 | { |
| 3252 | if (mac->nparam > 1) |
| 3253 | n = (n + mac->rotate) % mac->nparam; |
| 3254 | tt = mac->params[n]; |
| 3255 | } |
| 3256 | cc = find_cc(tt); |
| 3257 | if (cc == -1) |
| 3258 | { |
| 3259 | error(ERR_NONFATAL, |
| 3260 | "macro parameter %d is not a condition code", |
| 3261 | n + 1); |
| 3262 | text = NULL; |
| 3263 | } |
| 3264 | else |
| 3265 | { |
| 3266 | type = TOK_ID; |
| 3267 | text = nasm_strdup(conditions[cc]); |
| 3268 | } |
| 3269 | break; |
| 3270 | default: |
| 3271 | n = atoi(t->text + 1) - 1; |
| 3272 | if (n >= mac->nparam) |
| 3273 | tt = NULL; |
| 3274 | else |
| 3275 | { |
| 3276 | if (mac->nparam > 1) |
| 3277 | n = (n + mac->rotate) % mac->nparam; |
| 3278 | tt = mac->params[n]; |
| 3279 | } |
| 3280 | if (tt) |
| 3281 | { |
| 3282 | for (i = 0; i < mac->paramlen[n]; i++) |
| 3283 | { |
| 3284 | *tail = |
| 3285 | new_Token(NULL, tt->type, tt->text, |
| 3286 | 0); |
| 3287 | tail = &(*tail)->next; |
| 3288 | tt = tt->next; |
| 3289 | } |
| 3290 | } |
| 3291 | text = NULL; /* we've done it here */ |
| 3292 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3293 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3294 | if (!text) |
| 3295 | { |
| 3296 | delete_Token(t); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3297 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3298 | else |
| 3299 | { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3300 | *tail = t; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3301 | tail = &t->next; |
| 3302 | t->type = type; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3303 | nasm_free(t->text); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3304 | t->text = text; |
| 3305 | t->mac = NULL; |
| 3306 | } |
| 3307 | continue; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3308 | } |
| 3309 | else |
| 3310 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3311 | t = *tail = tline; |
| 3312 | tline = tline->next; |
| 3313 | t->mac = NULL; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3314 | tail = &t->next; |
| 3315 | } |
| 3316 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3317 | *tail = NULL; |
| 3318 | t = thead; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3319 | for (; t && (tt = t->next) != NULL; t = t->next) |
| 3320 | switch (t->type) |
| 3321 | { |
| 3322 | case TOK_WHITESPACE: |
| 3323 | if (tt->type == TOK_WHITESPACE) |
| 3324 | { |
| 3325 | t->next = delete_Token(tt); |
| 3326 | } |
| 3327 | break; |
| 3328 | case TOK_ID: |
| 3329 | if (tt->type == TOK_ID || tt->type == TOK_NUMBER) |
| 3330 | { |
| 3331 | char *tmp = nasm_strcat(t->text, tt->text); |
| 3332 | nasm_free(t->text); |
| 3333 | t->text = tmp; |
| 3334 | t->next = delete_Token(tt); |
| 3335 | } |
| 3336 | break; |
| 3337 | case TOK_NUMBER: |
| 3338 | if (tt->type == TOK_NUMBER) |
| 3339 | { |
| 3340 | char *tmp = nasm_strcat(t->text, tt->text); |
| 3341 | nasm_free(t->text); |
| 3342 | t->text = tmp; |
| 3343 | t->next = delete_Token(tt); |
| 3344 | } |
| 3345 | break; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3346 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3347 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3348 | return thead; |
| 3349 | } |
| 3350 | |
| 3351 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3352 | * Expand all single-line macro calls made in the given line. |
| 3353 | * Return the expanded version of the line. The original is deemed |
| 3354 | * to be destroyed in the process. (In reality we'll just move |
| 3355 | * Tokens from input to output a lot of the time, rather than |
| 3356 | * actually bothering to destroy and replicate.) |
| 3357 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3358 | static Token * |
| 3359 | expand_smacro(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3360 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3361 | Token *t, *tt, *mstart, **tail, *thead; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3362 | SMacro *head = NULL, *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3363 | Token **params; |
| 3364 | int *paramsize; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3365 | int nparam, sparam, brackets, rescan; |
| 3366 | Token *org_tline = tline; |
| 3367 | Context *ctx; |
| 3368 | char *mname; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3369 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3370 | /* |
| 3371 | * Trick: we should avoid changing the start token pointer since it can |
| 3372 | * be contained in "next" field of other token. Because of this |
| 3373 | * we allocate a copy of first token and work with it; at the end of |
| 3374 | * routine we copy it back |
| 3375 | */ |
| 3376 | if (org_tline) |
| 3377 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3378 | tline = |
| 3379 | new_Token(org_tline->next, org_tline->type, org_tline->text, |
| 3380 | 0); |
| 3381 | tline->mac = org_tline->mac; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 3382 | nasm_free(org_tline->text); |
| 3383 | org_tline->text = NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3384 | } |
| 3385 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3386 | again: |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3387 | tail = &thead; |
| 3388 | thead = NULL; |
| 3389 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3390 | while (tline) |
| 3391 | { /* main token loop */ |
| 3392 | if ((mname = tline->text)) |
| 3393 | { |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3394 | /* if this token is a local macro, look in local context */ |
| 3395 | if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3396 | ctx = get_ctx(mname, TRUE); |
| 3397 | else |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3398 | ctx = NULL; |
| 3399 | if (!ctx) |
| 3400 | head = smacros[hash(mname)]; |
| 3401 | else |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3402 | head = ctx->localmac; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3403 | /* |
| 3404 | * We've hit an identifier. As in is_mmacro below, we first |
| 3405 | * check whether the identifier is a single-line macro at |
| 3406 | * all, then think about checking for parameters if |
| 3407 | * necessary. |
| 3408 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3409 | for (m = head; m; m = m->next) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3410 | if (!mstrcmp(m->name, mname, m->casesense)) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3411 | break; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3412 | if (m) |
| 3413 | { |
| 3414 | mstart = tline; |
| 3415 | params = NULL; |
| 3416 | paramsize = NULL; |
| 3417 | if (m->nparam == 0) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3418 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3419 | /* |
| 3420 | * Simple case: the macro is parameterless. Discard the |
| 3421 | * one token that the macro call took, and push the |
| 3422 | * expansion back on the to-do stack. |
| 3423 | */ |
| 3424 | if (!m->expansion) |
| 3425 | { |
| 3426 | if (!strcmp("__FILE__", m->name)) |
| 3427 | { |
| 3428 | long num = 0; |
| 3429 | src_get(&num, &(tline->text)); |
| 3430 | nasm_quote(&(tline->text)); |
| 3431 | tline->type = TOK_STRING; |
| 3432 | continue; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3433 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3434 | if (!strcmp("__LINE__", m->name)) |
| 3435 | { |
| 3436 | nasm_free(tline->text); |
| 3437 | make_tok_num(tline, src_get_linnum()); |
| 3438 | continue; |
| 3439 | } |
| 3440 | tline = delete_Token(tline); |
| 3441 | continue; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3442 | } |
| 3443 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3444 | else |
| 3445 | { |
| 3446 | /* |
| 3447 | * Complicated case: at least one macro with this name |
| 3448 | * exists and takes parameters. We must find the |
| 3449 | * parameters in the call, count them, find the SMacro |
| 3450 | * that corresponds to that form of the macro call, and |
| 3451 | * substitute for the parameters when we expand. What a |
| 3452 | * pain. |
| 3453 | */ |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 3454 | /*tline = tline->next; |
| 3455 | skip_white_(tline);*/ |
| 3456 | do { |
| 3457 | t = tline->next; |
| 3458 | while (tok_type_(t, TOK_SMAC_END)) |
| 3459 | { |
| 3460 | t->mac->in_progress = FALSE; |
| 3461 | t->text = NULL; |
| 3462 | t = tline->next = delete_Token(t); |
| 3463 | } |
| 3464 | tline = t; |
| 3465 | } while (tok_type_(tline, TOK_WHITESPACE)); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3466 | if (!tok_is_(tline, "(")) |
| 3467 | { |
| 3468 | /* |
| 3469 | * This macro wasn't called with parameters: ignore |
| 3470 | * the call. (Behaviour borrowed from gnu cpp.) |
| 3471 | */ |
| 3472 | tline = mstart; |
| 3473 | m = NULL; |
| 3474 | } |
| 3475 | else |
| 3476 | { |
| 3477 | int paren = 0; |
| 3478 | int white = 0; |
| 3479 | brackets = 0; |
| 3480 | nparam = 0; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3481 | sparam = PARAM_DELTA; |
| 3482 | params = nasm_malloc(sparam * sizeof(Token *)); |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 3483 | params[0] = tline->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3484 | paramsize = nasm_malloc(sparam * sizeof(int)); |
| 3485 | paramsize[0] = 0; |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 3486 | while (TRUE) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3487 | { /* parameter loop */ |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 3488 | /* |
| 3489 | * For some unusual expansions |
| 3490 | * which concatenates function call |
| 3491 | */ |
| 3492 | t = tline->next; |
| 3493 | while (tok_type_(t, TOK_SMAC_END)) |
| 3494 | { |
| 3495 | t->mac->in_progress = FALSE; |
| 3496 | t->text = NULL; |
| 3497 | t = tline->next = delete_Token(t); |
| 3498 | } |
| 3499 | tline = t; |
| 3500 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3501 | if (!tline) |
| 3502 | { |
| 3503 | error(ERR_NONFATAL, |
| 3504 | "macro call expects terminating `)'"); |
| 3505 | break; |
| 3506 | } |
| 3507 | if (tline->type == TOK_WHITESPACE |
| 3508 | && brackets <= 0) |
| 3509 | { |
| 3510 | if (paramsize[nparam]) |
| 3511 | white++; |
| 3512 | else |
| 3513 | params[nparam] = tline->next; |
| 3514 | continue; /* parameter loop */ |
| 3515 | } |
| 3516 | if (tline->type == TOK_OTHER |
| 3517 | && tline->text[1] == 0) |
| 3518 | { |
| 3519 | char ch = tline->text[0]; |
| 3520 | if (ch == ',' && !paren && brackets <= 0) |
| 3521 | { |
| 3522 | if (++nparam >= sparam) |
| 3523 | { |
| 3524 | sparam += PARAM_DELTA; |
| 3525 | params = nasm_realloc(params, |
| 3526 | sparam * sizeof(Token *)); |
| 3527 | paramsize = nasm_realloc(paramsize, |
| 3528 | sparam * sizeof(int)); |
| 3529 | } |
| 3530 | params[nparam] = tline->next; |
| 3531 | paramsize[nparam] = 0; |
| 3532 | white = 0; |
| 3533 | continue; /* parameter loop */ |
| 3534 | } |
| 3535 | if (ch == '{' && |
| 3536 | (brackets > 0 || (brackets == 0 && |
| 3537 | !paramsize[nparam]))) |
| 3538 | { |
| 3539 | if (!(brackets++)) |
| 3540 | { |
| 3541 | params[nparam] = tline->next; |
| 3542 | continue; /* parameter loop */ |
| 3543 | } |
| 3544 | } |
| 3545 | if (ch == '}' && brackets > 0) |
| 3546 | if (--brackets == 0) |
| 3547 | { |
| 3548 | brackets = -1; |
| 3549 | continue; /* parameter loop */ |
| 3550 | } |
| 3551 | if (ch == '(' && !brackets) |
| 3552 | paren++; |
| 3553 | if (ch == ')' && brackets <= 0) |
| 3554 | if (--paren < 0) |
| 3555 | break; |
| 3556 | } |
| 3557 | if (brackets < 0) |
| 3558 | { |
| 3559 | brackets = 0; |
| 3560 | error(ERR_NONFATAL, "braces do not " |
| 3561 | "enclose all of macro parameter"); |
| 3562 | } |
| 3563 | paramsize[nparam] += white + 1; |
| 3564 | white = 0; |
| 3565 | } /* parameter loop */ |
| 3566 | nparam++; |
| 3567 | while (m && (m->nparam != nparam || |
| 3568 | mstrcmp(m->name, mname, |
| 3569 | m->casesense))) |
| 3570 | m = m->next; |
| 3571 | if (!m) |
| 3572 | error(ERR_WARNING | ERR_WARN_MNP, |
| 3573 | "macro `%s' exists, " |
| 3574 | "but not taking %d parameters", |
| 3575 | mstart->text, nparam); |
| 3576 | } |
| 3577 | } |
| 3578 | if (m && m->in_progress) |
| 3579 | m = NULL; |
| 3580 | if (!m) /* in progess or didn't find '(' or wrong nparam */ |
| 3581 | { |
| 3582 | /* |
| 3583 | * Design question: should we handle !tline, which |
| 3584 | * indicates missing ')' here, or expand those |
| 3585 | * macros anyway, which requires the (t) test a few |
| 3586 | * lines down? |
| 3587 | */ |
| 3588 | nasm_free(params); |
| 3589 | nasm_free(paramsize); |
| 3590 | tline = mstart; |
| 3591 | } |
| 3592 | else |
| 3593 | { |
| 3594 | /* |
| 3595 | * Expand the macro: we are placed on the last token of the |
| 3596 | * call, so that we can easily split the call from the |
| 3597 | * following tokens. We also start by pushing an SMAC_END |
| 3598 | * token for the cycle removal. |
| 3599 | */ |
| 3600 | t = tline; |
| 3601 | if (t) |
| 3602 | { |
| 3603 | tline = t->next; |
| 3604 | t->next = NULL; |
| 3605 | } |
| 3606 | tt = new_Token(tline, TOK_SMAC_END, NULL, 0); |
| 3607 | tt->mac = m; |
| 3608 | m->in_progress = TRUE; |
| 3609 | tline = tt; |
| 3610 | for (t = m->expansion; t; t = t->next) |
| 3611 | { |
| 3612 | if (t->type >= TOK_SMAC_PARAM) |
| 3613 | { |
| 3614 | Token *pcopy = tline, **ptail = &pcopy; |
| 3615 | Token *ttt, *pt; |
| 3616 | int i; |
| 3617 | |
| 3618 | ttt = params[t->type - TOK_SMAC_PARAM]; |
| 3619 | for (i = paramsize[t->type - TOK_SMAC_PARAM]; |
| 3620 | --i >= 0;) |
| 3621 | { |
| 3622 | pt = *ptail = |
| 3623 | new_Token(tline, ttt->type, ttt->text, |
| 3624 | 0); |
| 3625 | ptail = &pt->next; |
| 3626 | ttt = ttt->next; |
| 3627 | } |
| 3628 | tline = pcopy; |
| 3629 | } |
| 3630 | else |
| 3631 | { |
| 3632 | tt = new_Token(tline, t->type, t->text, 0); |
| 3633 | tline = tt; |
| 3634 | } |
| 3635 | } |
| 3636 | |
| 3637 | /* |
| 3638 | * Having done that, get rid of the macro call, and clean |
| 3639 | * up the parameters. |
| 3640 | */ |
| 3641 | nasm_free(params); |
| 3642 | nasm_free(paramsize); |
| 3643 | free_tlist(mstart); |
| 3644 | continue; /* main token loop */ |
| 3645 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3646 | } |
| 3647 | } |
| 3648 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3649 | if (tline->type == TOK_SMAC_END) |
| 3650 | { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3651 | tline->mac->in_progress = FALSE; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3652 | tline = delete_Token(tline); |
| 3653 | } |
| 3654 | else |
| 3655 | { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3656 | t = *tail = tline; |
| 3657 | tline = tline->next; |
| 3658 | t->mac = NULL; |
| 3659 | t->next = NULL; |
| 3660 | tail = &t->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3661 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3662 | } |
| 3663 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3664 | /* |
| 3665 | * Now scan the entire line and look for successive TOK_IDs that resulted |
| 3666 | * after expansion (they can't be produced by tokenise()). The successive |
| 3667 | * TOK_IDs should be concatenated. |
| 3668 | * Also we look for %+ tokens and concatenate the tokens before and after |
| 3669 | * them (without white spaces in between). |
| 3670 | */ |
| 3671 | t = thead; |
| 3672 | rescan = 0; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3673 | while (t) |
| 3674 | { |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3675 | while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID) |
| 3676 | t = t->next; |
| 3677 | if (!t || !t->next) |
| 3678 | break; |
| 3679 | if (t->next->type == TOK_ID || |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3680 | t->next->type == TOK_PREPROC_ID || |
| 3681 | t->next->type == TOK_NUMBER) |
| 3682 | { |
| 3683 | char *p = nasm_strcat(t->text, t->next->text); |
| 3684 | nasm_free(t->text); |
| 3685 | t->next = delete_Token(t->next); |
| 3686 | t->text = p; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3687 | rescan = 1; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3688 | } |
| 3689 | else if (t->next->type == TOK_WHITESPACE && t->next->next && |
| 3690 | t->next->next->type == TOK_PREPROC_ID && |
| 3691 | strcmp(t->next->next->text, "%+") == 0) |
| 3692 | { |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3693 | /* free the next whitespace, the %+ token and next whitespace */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3694 | int i; |
| 3695 | for (i = 1; i <= 3; i++) |
| 3696 | { |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3697 | if (!t->next || (i != 2 && t->next->type != TOK_WHITESPACE)) |
| 3698 | break; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3699 | t->next = delete_Token(t->next); |
| 3700 | } /* endfor */ |
| 3701 | } |
| 3702 | else |
| 3703 | t = t->next; |
| 3704 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3705 | /* If we concatenaded something, re-scan the line for macros */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3706 | if (rescan) |
| 3707 | { |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3708 | tline = thead; |
| 3709 | goto again; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3710 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3711 | |
| 3712 | if (org_tline) |
| 3713 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3714 | if (thead) |
| 3715 | { |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3716 | *org_tline = *thead; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3717 | /* since we just gave text to org_line, don't free it */ |
| 3718 | thead->text = NULL; |
| 3719 | delete_Token(thead); |
| 3720 | } |
| 3721 | else |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3722 | { |
| 3723 | /* the expression expanded to empty line; |
| 3724 | we can't return NULL for some reasons |
| 3725 | we just set the line to a single WHITESPACE token. */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3726 | memset(org_tline, 0, sizeof(*org_tline)); |
| 3727 | org_tline->text = NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3728 | org_tline->type = TOK_WHITESPACE; |
| 3729 | } |
| 3730 | thead = org_tline; |
| 3731 | } |
| 3732 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3733 | return thead; |
| 3734 | } |
| 3735 | |
| 3736 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3737 | * Similar to expand_smacro but used exclusively with macro identifiers |
| 3738 | * right before they are fetched in. The reason is that there can be |
| 3739 | * identifiers consisting of several subparts. We consider that if there |
| 3740 | * are more than one element forming the name, user wants a expansion, |
| 3741 | * otherwise it will be left as-is. Example: |
| 3742 | * |
| 3743 | * %define %$abc cde |
| 3744 | * |
| 3745 | * the identifier %$abc will be left as-is so that the handler for %define |
| 3746 | * will suck it and define the corresponding value. Other case: |
| 3747 | * |
| 3748 | * %define _%$abc cde |
| 3749 | * |
| 3750 | * In this case user wants name to be expanded *before* %define starts |
| 3751 | * working, so we'll expand %$abc into something (if it has a value; |
| 3752 | * otherwise it will be left as-is) then concatenate all successive |
| 3753 | * PP_IDs into one. |
| 3754 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3755 | static Token * |
| 3756 | expand_id(Token * tline) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3757 | { |
| 3758 | Token *cur, *oldnext = NULL; |
| 3759 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3760 | if (!tline || !tline->next) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3761 | return tline; |
| 3762 | |
| 3763 | cur = tline; |
| 3764 | while (cur->next && |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3765 | (cur->next->type == TOK_ID || |
| 3766 | cur->next->type == TOK_PREPROC_ID || cur->next->type == TOK_NUMBER)) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3767 | cur = cur->next; |
| 3768 | |
| 3769 | /* If identifier consists of just one token, don't expand */ |
| 3770 | if (cur == tline) |
| 3771 | return tline; |
| 3772 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3773 | if (cur) |
| 3774 | { |
| 3775 | oldnext = cur->next; /* Detach the tail past identifier */ |
| 3776 | cur->next = NULL; /* so that expand_smacro stops here */ |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3777 | } |
| 3778 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3779 | tline = expand_smacro(tline); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3780 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3781 | if (cur) |
| 3782 | { |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3783 | /* expand_smacro possibly changhed tline; re-scan for EOL */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3784 | cur = tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3785 | while (cur && cur->next) |
| 3786 | cur = cur->next; |
| 3787 | if (cur) |
| 3788 | cur->next = oldnext; |
| 3789 | } |
| 3790 | |
| 3791 | return tline; |
| 3792 | } |
| 3793 | |
| 3794 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3795 | * Determine whether the given line constitutes a multi-line macro |
| 3796 | * call, and return the MMacro structure called if so. Doesn't have |
| 3797 | * to check for an initial label - that's taken care of in |
| 3798 | * expand_mmacro - but must check numbers of parameters. Guaranteed |
| 3799 | * to be called with tline->type == TOK_ID, so the putative macro |
| 3800 | * name is easy to find. |
| 3801 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3802 | static MMacro * |
| 3803 | is_mmacro(Token * tline, Token *** params_array) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3804 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3805 | MMacro *head, *m; |
| 3806 | Token **params; |
| 3807 | int nparam; |
| 3808 | |
| 3809 | head = mmacros[hash(tline->text)]; |
| 3810 | |
| 3811 | /* |
| 3812 | * Efficiency: first we see if any macro exists with the given |
| 3813 | * name. If not, we can return NULL immediately. _Then_ we |
| 3814 | * count the parameters, and then we look further along the |
| 3815 | * list if necessary to find the proper MMacro. |
| 3816 | */ |
| 3817 | for (m = head; m; m = m->next) |
| 3818 | if (!mstrcmp(m->name, tline->text, m->casesense)) |
| 3819 | break; |
| 3820 | if (!m) |
| 3821 | return NULL; |
| 3822 | |
| 3823 | /* |
| 3824 | * OK, we have a potential macro. Count and demarcate the |
| 3825 | * parameters. |
| 3826 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3827 | count_mmac_params(tline->next, &nparam, ¶ms); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3828 | |
| 3829 | /* |
| 3830 | * So we know how many parameters we've got. Find the MMacro |
| 3831 | * structure that handles this number. |
| 3832 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3833 | while (m) |
| 3834 | { |
| 3835 | if (m->nparam_min <= nparam && (m->plus || nparam <= m->nparam_max)) |
| 3836 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3837 | /* |
| 3838 | * This one is right. Just check if cycle removal |
| 3839 | * prohibits us using it before we actually celebrate... |
| 3840 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3841 | if (m->in_progress) |
| 3842 | { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3843 | #if 0 |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3844 | error(ERR_NONFATAL, |
| 3845 | "self-reference in multi-line macro `%s'", m->name); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3846 | #endif |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3847 | nasm_free(params); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3848 | return NULL; |
| 3849 | } |
| 3850 | /* |
| 3851 | * It's right, and we can use it. Add its default |
| 3852 | * parameters to the end of our list if necessary. |
| 3853 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3854 | if (m->defaults && nparam < m->nparam_min + m->ndefs) |
| 3855 | { |
| 3856 | params = |
| 3857 | nasm_realloc(params, |
| 3858 | ((m->nparam_min + m->ndefs + 1) * sizeof(*params))); |
| 3859 | while (nparam < m->nparam_min + m->ndefs) |
| 3860 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3861 | params[nparam] = m->defaults[nparam - m->nparam_min]; |
| 3862 | nparam++; |
| 3863 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3864 | } |
| 3865 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3866 | * If we've gone over the maximum parameter count (and |
| 3867 | * we're in Plus mode), ignore parameters beyond |
| 3868 | * nparam_max. |
| 3869 | */ |
| 3870 | if (m->plus && nparam > m->nparam_max) |
| 3871 | nparam = m->nparam_max; |
| 3872 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3873 | * Then terminate the parameter list, and leave. |
| 3874 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3875 | if (!params) |
| 3876 | { /* need this special case */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3877 | params = nasm_malloc(sizeof(*params)); |
| 3878 | nparam = 0; |
| 3879 | } |
| 3880 | params[nparam] = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3881 | *params_array = params; |
| 3882 | return m; |
| 3883 | } |
| 3884 | /* |
| 3885 | * This one wasn't right: look for the next one with the |
| 3886 | * same name. |
| 3887 | */ |
| 3888 | for (m = m->next; m; m = m->next) |
| 3889 | if (!mstrcmp(m->name, tline->text, m->casesense)) |
| 3890 | break; |
| 3891 | } |
| 3892 | |
| 3893 | /* |
| 3894 | * After all that, we didn't find one with the right number of |
| 3895 | * parameters. Issue a warning, and fail to expand the macro. |
| 3896 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3897 | error(ERR_WARNING | ERR_WARN_MNP, |
| 3898 | "macro `%s' exists, but not taking %d parameters", |
| 3899 | tline->text, nparam); |
| 3900 | nasm_free(params); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3901 | return NULL; |
| 3902 | } |
| 3903 | |
| 3904 | /* |
| 3905 | * Expand the multi-line macro call made by the given line, if |
| 3906 | * 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] | 3907 | * istk->expansion and return 1. Otherwise return 0. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3908 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3909 | static int |
| 3910 | expand_mmacro(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3911 | { |
| 3912 | Token *startline = tline; |
| 3913 | Token *label = NULL; |
| 3914 | int dont_prepend = 0; |
| 3915 | Token **params, *t, *tt; |
| 3916 | MMacro *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3917 | Line *l, *ll; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3918 | int i, nparam, *paramlen; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3919 | |
| 3920 | t = tline; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3921 | skip_white_(t); |
H. Peter Anvin | dce1e2f | 2002-04-30 21:06:37 +0000 | [diff] [blame] | 3922 | /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */ |
| 3923 | 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] | 3924 | return 0; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3925 | m = is_mmacro(t, ¶ms); |
| 3926 | if (!m) |
| 3927 | { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3928 | Token *last; |
| 3929 | /* |
| 3930 | * We have an id which isn't a macro call. We'll assume |
| 3931 | * it might be a label; we'll also check to see if a |
| 3932 | * colon follows it. Then, if there's another id after |
| 3933 | * that lot, we'll check it again for macro-hood. |
| 3934 | */ |
| 3935 | label = last = t; |
| 3936 | t = t->next; |
| 3937 | if (tok_type_(t, TOK_WHITESPACE)) |
| 3938 | last = t, t = t->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3939 | if (tok_is_(t, ":")) |
| 3940 | { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3941 | dont_prepend = 1; |
| 3942 | last = t, t = t->next; |
| 3943 | if (tok_type_(t, TOK_WHITESPACE)) |
| 3944 | last = t, t = t->next; |
| 3945 | } |
| 3946 | if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, ¶ms)) == NULL) |
| 3947 | return 0; |
| 3948 | last->next = NULL; |
| 3949 | tline = t; |
| 3950 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3951 | |
| 3952 | /* |
| 3953 | * Fix up the parameters: this involves stripping leading and |
| 3954 | * trailing whitespace, then stripping braces if they are |
| 3955 | * present. |
| 3956 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3957 | for (nparam = 0; params[nparam]; nparam++) |
| 3958 | ; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3959 | paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3960 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3961 | for (i = 0; params[i]; i++) |
| 3962 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3963 | int brace = FALSE; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3964 | int comma = (!m->plus || i < nparam - 1); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3965 | |
| 3966 | t = params[i]; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3967 | skip_white_(t); |
| 3968 | if (tok_is_(t, "{")) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3969 | t = t->next, brace = TRUE, comma = FALSE; |
| 3970 | params[i] = t; |
| 3971 | paramlen[i] = 0; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3972 | while (t) |
| 3973 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3974 | if (comma && t->type == TOK_OTHER && !strcmp(t->text, ",")) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3975 | break; /* ... because we have hit a comma */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3976 | if (comma && t->type == TOK_WHITESPACE && tok_is_(t->next, ",")) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3977 | break; /* ... or a space then a comma */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3978 | if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}")) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3979 | break; /* ... or a brace */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3980 | t = t->next; |
| 3981 | paramlen[i]++; |
| 3982 | } |
| 3983 | } |
| 3984 | |
| 3985 | /* |
| 3986 | * OK, we have a MMacro structure together with a set of |
| 3987 | * parameters. We must now go through the expansion and push |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3988 | * copies of each Line on to istk->expansion. Substitution of |
| 3989 | * parameter tokens and macro-local tokens doesn't get done |
| 3990 | * until the single-line macro substitution process; this is |
| 3991 | * because delaying them allows us to change the semantics |
| 3992 | * later through %rotate. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3993 | * |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3994 | * First, push an end marker on to istk->expansion, mark this |
| 3995 | * macro as in progress, and set up its invocation-specific |
| 3996 | * variables. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3997 | */ |
| 3998 | ll = nasm_malloc(sizeof(Line)); |
| 3999 | ll->next = istk->expansion; |
| 4000 | ll->finishes = m; |
| 4001 | ll->first = NULL; |
| 4002 | istk->expansion = ll; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4003 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4004 | m->in_progress = TRUE; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4005 | m->params = params; |
| 4006 | m->iline = tline; |
| 4007 | m->nparam = nparam; |
| 4008 | m->rotate = 0; |
| 4009 | m->paramlen = paramlen; |
| 4010 | m->unique = unique++; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4011 | m->lineno = 0; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4012 | |
| 4013 | m->next_active = istk->mstk; |
| 4014 | istk->mstk = m; |
| 4015 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4016 | for (l = m->expansion; l; l = l->next) |
| 4017 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4018 | Token **tail; |
| 4019 | |
| 4020 | ll = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4021 | ll->finishes = NULL; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4022 | ll->next = istk->expansion; |
| 4023 | istk->expansion = ll; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4024 | tail = &ll->first; |
| 4025 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4026 | for (t = l->first; t; t = t->next) |
| 4027 | { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4028 | Token *x = t; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4029 | if (t->type == TOK_PREPROC_ID && |
| 4030 | t->text[1] == '0' && t->text[2] == '0') |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4031 | { |
| 4032 | dont_prepend = -1; |
| 4033 | x = label; |
| 4034 | if (!x) |
| 4035 | continue; |
| 4036 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4037 | tt = *tail = new_Token(NULL, x->type, x->text, 0); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4038 | tail = &tt->next; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4039 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4040 | *tail = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4041 | } |
| 4042 | |
| 4043 | /* |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4044 | * If we had a label, push it on as the first line of |
| 4045 | * the macro expansion. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4046 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4047 | if (label) |
| 4048 | { |
| 4049 | if (dont_prepend < 0) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4050 | free_tlist(startline); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4051 | else |
| 4052 | { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4053 | ll = nasm_malloc(sizeof(Line)); |
| 4054 | ll->finishes = NULL; |
| 4055 | ll->next = istk->expansion; |
| 4056 | istk->expansion = ll; |
| 4057 | ll->first = startline; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4058 | if (!dont_prepend) |
| 4059 | { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4060 | while (label->next) |
| 4061 | label = label->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4062 | label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4063 | } |
H. Peter Anvin | 87bc619 | 2002-04-30 20:53:16 +0000 | [diff] [blame] | 4064 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4065 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4066 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4067 | list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4068 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4069 | return 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4070 | } |
| 4071 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4072 | /* |
| 4073 | * Since preprocessor always operate only on the line that didn't |
| 4074 | * arrived yet, we should always use ERR_OFFBY1. Also since user |
| 4075 | * won't want to see same error twice (preprocessing is done once |
| 4076 | * per pass) we will want to show errors only during pass one. |
| 4077 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4078 | static void |
Ed Beroset | 168c9c0 | 2002-05-17 03:10:13 +0000 | [diff] [blame] | 4079 | error(int severity, const char *fmt, ...) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4080 | { |
| 4081 | va_list arg; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4082 | char buff[1024]; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4083 | |
| 4084 | /* 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] | 4085 | if (istk && istk->conds && !emitting(istk->conds->state)) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4086 | return; |
| 4087 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4088 | va_start(arg, fmt); |
Ed Beroset | ec2e10c | 2004-12-15 16:45:46 +0000 | [diff] [blame^] | 4089 | vsnprintf(buff, 1024, fmt, arg); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4090 | va_end(arg); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4091 | |
H. Peter Anvin | 77ba0c6 | 2002-05-14 03:18:53 +0000 | [diff] [blame] | 4092 | if (istk && istk->mstk && istk->mstk->name) |
H. Peter Anvin | 99941bf | 2002-05-14 17:44:03 +0000 | [diff] [blame] | 4093 | _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name, |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4094 | istk->mstk->lineno, buff); |
Ed Beroset | 168c9c0 | 2002-05-17 03:10:13 +0000 | [diff] [blame] | 4095 | else |
H. Peter Anvin | 99941bf | 2002-05-14 17:44:03 +0000 | [diff] [blame] | 4096 | _error(severity | ERR_PASS1, "%s", buff); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4097 | } |
| 4098 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4099 | static void |
| 4100 | pp_reset(char *file, int apass, efunc errfunc, evalfunc eval, |
| 4101 | ListGen * listgen) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4102 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4103 | int h; |
| 4104 | |
H. Peter Anvin | 99941bf | 2002-05-14 17:44:03 +0000 | [diff] [blame] | 4105 | _error = errfunc; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4106 | cstk = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4107 | istk = nasm_malloc(sizeof(Include)); |
| 4108 | istk->next = NULL; |
| 4109 | istk->conds = NULL; |
| 4110 | istk->expansion = NULL; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4111 | istk->mstk = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4112 | istk->fp = fopen(file, "r"); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4113 | istk->fname = NULL; |
| 4114 | src_set_fname(nasm_strdup(file)); |
| 4115 | src_set_linnum(0); |
| 4116 | istk->lineinc = 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4117 | if (!istk->fp) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4118 | 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] | 4119 | defining = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4120 | for (h = 0; h < NHASH; h++) |
| 4121 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4122 | mmacros[h] = NULL; |
| 4123 | smacros[h] = NULL; |
| 4124 | } |
| 4125 | unique = 0; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4126 | if (tasm_compatible_mode) { |
| 4127 | stdmacpos = stdmac; |
| 4128 | } else { |
| 4129 | stdmacpos = &stdmac[TASM_MACRO_COUNT]; |
| 4130 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4131 | any_extrastdmac = (extrastdmac != NULL); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4132 | list = listgen; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4133 | evaluate = eval; |
| 4134 | pass = apass; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4135 | } |
| 4136 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4137 | static char * |
| 4138 | pp_getline(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4139 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4140 | char *line; |
| 4141 | Token *tline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4142 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4143 | while (1) |
| 4144 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4145 | /* |
| 4146 | * Fetch a tokenised line, either from the macro-expansion |
| 4147 | * buffer or from the input file. |
| 4148 | */ |
| 4149 | tline = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4150 | while (istk->expansion && istk->expansion->finishes) |
| 4151 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4152 | Line *l = istk->expansion; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4153 | if (!l->finishes->name && l->finishes->in_progress > 1) |
| 4154 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4155 | Line *ll; |
| 4156 | |
| 4157 | /* |
| 4158 | * This is a macro-end marker for a macro with no |
| 4159 | * name, which means it's not really a macro at all |
| 4160 | * but a %rep block, and the `in_progress' field is |
| 4161 | * more than 1, meaning that we still need to |
| 4162 | * repeat. (1 means the natural last repetition; 0 |
| 4163 | * means termination by %exitrep.) We have |
| 4164 | * therefore expanded up to the %endrep, and must |
| 4165 | * push the whole block on to the expansion buffer |
| 4166 | * again. We don't bother to remove the macro-end |
| 4167 | * marker: we'd only have to generate another one |
| 4168 | * if we did. |
| 4169 | */ |
| 4170 | l->finishes->in_progress--; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4171 | for (l = l->finishes->expansion; l; l = l->next) |
| 4172 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4173 | Token *t, *tt, **tail; |
| 4174 | |
| 4175 | ll = nasm_malloc(sizeof(Line)); |
| 4176 | ll->next = istk->expansion; |
| 4177 | ll->finishes = NULL; |
| 4178 | ll->first = NULL; |
| 4179 | tail = &ll->first; |
| 4180 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4181 | for (t = l->first; t; t = t->next) |
| 4182 | { |
| 4183 | if (t->text || t->type == TOK_WHITESPACE) |
| 4184 | { |
| 4185 | tt = *tail = new_Token(NULL, t->type, t->text, 0); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4186 | tail = &tt->next; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4187 | } |
| 4188 | } |
| 4189 | |
| 4190 | istk->expansion = ll; |
| 4191 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4192 | } |
| 4193 | else |
| 4194 | { |
H. Peter Anvin | 87bc619 | 2002-04-30 20:53:16 +0000 | [diff] [blame] | 4195 | /* |
| 4196 | * Check whether a `%rep' was started and not ended |
| 4197 | * within this macro expansion. This can happen and |
| 4198 | * should be detected. It's a fatal error because |
| 4199 | * I'm too confused to work out how to recover |
| 4200 | * sensibly from it. |
| 4201 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4202 | if (defining) |
| 4203 | { |
H. Peter Anvin | 87bc619 | 2002-04-30 20:53:16 +0000 | [diff] [blame] | 4204 | if (defining->name) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4205 | error(ERR_PANIC, "defining with name in expansion"); |
H. Peter Anvin | b64535f | 2002-04-30 20:55:37 +0000 | [diff] [blame] | 4206 | else if (istk->mstk->name) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4207 | error(ERR_FATAL, "`%%rep' without `%%endrep' within" |
| 4208 | " expansion of macro `%s'", istk->mstk->name); |
H. Peter Anvin | 87bc619 | 2002-04-30 20:53:16 +0000 | [diff] [blame] | 4209 | } |
| 4210 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4211 | /* |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4212 | * FIXME: investigate the relationship at this point between |
| 4213 | * istk->mstk and l->finishes |
| 4214 | */ |
| 4215 | { |
| 4216 | MMacro *m = istk->mstk; |
| 4217 | istk->mstk = m->next_active; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4218 | if (m->name) |
| 4219 | { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4220 | /* |
| 4221 | * This was a real macro call, not a %rep, and |
| 4222 | * therefore the parameter information needs to |
| 4223 | * be freed. |
| 4224 | */ |
| 4225 | nasm_free(m->params); |
| 4226 | free_tlist(m->iline); |
| 4227 | nasm_free(m->paramlen); |
| 4228 | l->finishes->in_progress = FALSE; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4229 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4230 | else |
| 4231 | free_mmacro(m); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4232 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4233 | istk->expansion = l->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4234 | nasm_free(l); |
| 4235 | list->downlevel(LIST_MACRO); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4236 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4237 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4238 | while (1) |
| 4239 | { /* until we get a line we can use */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4240 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4241 | if (istk->expansion) |
| 4242 | { /* from a macro expansion */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4243 | char *p; |
| 4244 | Line *l = istk->expansion; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4245 | if (istk->mstk) |
| 4246 | istk->mstk->lineno++; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4247 | tline = l->first; |
| 4248 | istk->expansion = l->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4249 | nasm_free(l); |
| 4250 | p = detoken(tline, FALSE); |
| 4251 | list->line(LIST_MACRO, p); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4252 | nasm_free(p); |
| 4253 | break; |
| 4254 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4255 | line = read_line(); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4256 | if (line) |
| 4257 | { /* from the current input file */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4258 | line = prepreproc(line); |
| 4259 | tline = tokenise(line); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4260 | nasm_free(line); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4261 | break; |
| 4262 | } |
| 4263 | /* |
| 4264 | * The current file has ended; work down the istk |
| 4265 | */ |
| 4266 | { |
| 4267 | Include *i = istk; |
| 4268 | fclose(i->fp); |
| 4269 | if (i->conds) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4270 | error(ERR_FATAL, "expected `%%endif' before end of file"); |
Ed Beroset | 168c9c0 | 2002-05-17 03:10:13 +0000 | [diff] [blame] | 4271 | /* only set line and file name if there's a next node */ |
| 4272 | if (i->next) |
| 4273 | { |
| 4274 | src_set_linnum(i->lineno); |
| 4275 | nasm_free(src_set_fname(i->fname)); |
| 4276 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4277 | istk = i->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4278 | list->downlevel(LIST_INCLUDE); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4279 | nasm_free(i); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4280 | if (!istk) |
| 4281 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4282 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4283 | } |
| 4284 | |
| 4285 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4286 | * We must expand MMacro parameters and MMacro-local labels |
| 4287 | * _before_ we plunge into directive processing, to cope |
| 4288 | * with things like `%define something %1' such as STRUC |
| 4289 | * uses. Unless we're _defining_ a MMacro, in which case |
| 4290 | * those tokens should be left alone to go into the |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4291 | * definition; and unless we're in a non-emitting |
| 4292 | * condition, in which case we don't want to meddle with |
| 4293 | * anything. |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4294 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4295 | if (!defining && !(istk->conds && !emitting(istk->conds->state))) |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4296 | tline = expand_mmac_params(tline); |
| 4297 | |
| 4298 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4299 | * Check the line to see if it's a preprocessor directive. |
| 4300 | */ |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 4301 | if (do_directive(tline) == DIRECTIVE_FOUND) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4302 | { |
| 4303 | continue; |
| 4304 | } |
| 4305 | else if (defining) |
| 4306 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4307 | /* |
| 4308 | * We're defining a multi-line macro. We emit nothing |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4309 | * at all, and just |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4310 | * shove the tokenised line on to the macro definition. |
| 4311 | */ |
| 4312 | Line *l = nasm_malloc(sizeof(Line)); |
| 4313 | l->next = defining->expansion; |
| 4314 | l->first = tline; |
| 4315 | l->finishes = FALSE; |
| 4316 | defining->expansion = l; |
| 4317 | continue; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4318 | } |
| 4319 | else if (istk->conds && !emitting(istk->conds->state)) |
| 4320 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4321 | /* |
| 4322 | * We're in a non-emitting branch of a condition block. |
| 4323 | * Emit nothing at all, not even a blank line: when we |
| 4324 | * emerge from the condition we'll give a line-number |
| 4325 | * directive so we keep our place correctly. |
| 4326 | */ |
| 4327 | free_tlist(tline); |
| 4328 | continue; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4329 | } |
| 4330 | else if (istk->mstk && !istk->mstk->in_progress) |
| 4331 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4332 | /* |
| 4333 | * We're in a %rep block which has been terminated, so |
| 4334 | * we're walking through to the %endrep without |
| 4335 | * emitting anything. Emit nothing at all, not even a |
| 4336 | * blank line: when we emerge from the %rep block we'll |
| 4337 | * give a line-number directive so we keep our place |
| 4338 | * correctly. |
| 4339 | */ |
| 4340 | free_tlist(tline); |
| 4341 | continue; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4342 | } |
| 4343 | else |
| 4344 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4345 | tline = expand_smacro(tline); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4346 | if (!expand_mmacro(tline)) |
| 4347 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4348 | /* |
| 4349 | * De-tokenise the line again, and emit it. |
| 4350 | */ |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4351 | line = detoken(tline, TRUE); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4352 | free_tlist(tline); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4353 | break; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4354 | } |
| 4355 | else |
| 4356 | { |
| 4357 | continue; /* expand_mmacro calls free_tlist */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4358 | } |
| 4359 | } |
| 4360 | } |
| 4361 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4362 | return line; |
| 4363 | } |
| 4364 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4365 | static void |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 4366 | pp_cleanup(int pass) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4367 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4368 | int h; |
| 4369 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4370 | if (defining) |
| 4371 | { |
| 4372 | error(ERR_NONFATAL, "end of file while still defining macro `%s'", |
| 4373 | defining->name); |
| 4374 | free_mmacro(defining); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4375 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4376 | while (cstk) |
| 4377 | ctx_pop(); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4378 | for (h = 0; h < NHASH; h++) |
| 4379 | { |
| 4380 | while (mmacros[h]) |
| 4381 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4382 | MMacro *m = mmacros[h]; |
| 4383 | mmacros[h] = mmacros[h]->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4384 | free_mmacro(m); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4385 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4386 | while (smacros[h]) |
| 4387 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4388 | SMacro *s = smacros[h]; |
| 4389 | smacros[h] = smacros[h]->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4390 | nasm_free(s->name); |
| 4391 | free_tlist(s->expansion); |
| 4392 | nasm_free(s); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4393 | } |
| 4394 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4395 | while (istk) |
| 4396 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4397 | Include *i = istk; |
| 4398 | istk = istk->next; |
| 4399 | fclose(i->fp); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4400 | nasm_free(i->fname); |
| 4401 | nasm_free(i); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4402 | } |
| 4403 | while (cstk) |
| 4404 | ctx_pop(); |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 4405 | if (pass == 0) |
| 4406 | { |
| 4407 | free_llist(predef); |
| 4408 | delete_Blocks(); |
| 4409 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4410 | } |
| 4411 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4412 | void |
| 4413 | pp_include_path(char *path) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4414 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4415 | IncPath *i; |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 4416 | /* by alexfru: order of path inclusion fixed (was reverse order) */ |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4417 | i = nasm_malloc(sizeof(IncPath)); |
| 4418 | i->path = nasm_strdup(path); |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 4419 | i->next = NULL; |
| 4420 | |
| 4421 | if (ipath != NULL) |
| 4422 | { |
| 4423 | IncPath *j = ipath; |
| 4424 | while (j->next != NULL) |
| 4425 | j = j->next; |
| 4426 | j->next = i; |
| 4427 | } |
| 4428 | else |
| 4429 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4430 | ipath = i; |
| 4431 | } |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 4432 | } |
| 4433 | |
| 4434 | /* |
| 4435 | * added by alexfru: |
| 4436 | * |
| 4437 | * This function is used to "export" the include paths, e.g. |
| 4438 | * the paths specified in the '-I' command switch. |
| 4439 | * The need for such exporting is due to the 'incbin' directive, |
| 4440 | * which includes raw binary files (unlike '%include', which |
| 4441 | * includes text source files). It would be real nice to be |
| 4442 | * able to specify paths to search for incbin'ned files also. |
| 4443 | * So, this is a simple workaround. |
| 4444 | * |
| 4445 | * The function use is simple: |
| 4446 | * |
| 4447 | * The 1st call (with NULL argument) returns a pointer to the 1st path |
| 4448 | * (char** type) or NULL if none include paths available. |
| 4449 | * |
| 4450 | * All subsequent calls take as argument the value returned by this |
| 4451 | * function last. The return value is either the next path |
| 4452 | * (char** type) or NULL if the end of the paths list is reached. |
| 4453 | * |
| 4454 | * It is maybe not the best way to do things, but I didn't want |
| 4455 | * to export too much, just one or two functions and no types or |
| 4456 | * variables exported. |
| 4457 | * |
| 4458 | * Can't say I like the current situation with e.g. this path list either, |
| 4459 | * it seems to be never deallocated after creation... |
| 4460 | */ |
| 4461 | char** |
| 4462 | pp_get_include_path_ptr (char **pPrevPath) |
| 4463 | { |
| 4464 | /* This macro returns offset of a member of a structure */ |
| 4465 | #define GetMemberOffset(StructType,MemberName)\ |
| 4466 | ((size_t)&((StructType*)0)->MemberName) |
| 4467 | IncPath *i; |
| 4468 | |
| 4469 | if (pPrevPath == NULL) |
| 4470 | { |
| 4471 | if(ipath != NULL) |
| 4472 | return &ipath->path; |
| 4473 | else |
| 4474 | return NULL; |
| 4475 | } |
| 4476 | i = (IncPath*) ((char*)pPrevPath - GetMemberOffset(IncPath,path)); |
| 4477 | i = i->next; |
| 4478 | if (i != NULL) |
| 4479 | return &i->path; |
| 4480 | else |
| 4481 | return NULL; |
| 4482 | #undef GetMemberOffset |
| 4483 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4484 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4485 | void |
| 4486 | pp_pre_include(char *fname) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4487 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4488 | Token *inc, *space, *name; |
| 4489 | Line *l; |
| 4490 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4491 | name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0); |
| 4492 | space = new_Token(name, TOK_WHITESPACE, NULL, 0); |
| 4493 | inc = new_Token(space, TOK_PREPROC_ID, "%include", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4494 | |
| 4495 | l = nasm_malloc(sizeof(Line)); |
| 4496 | l->next = predef; |
| 4497 | l->first = inc; |
| 4498 | l->finishes = FALSE; |
| 4499 | predef = l; |
| 4500 | } |
| 4501 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4502 | void |
| 4503 | pp_pre_define(char *definition) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4504 | { |
| 4505 | Token *def, *space; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4506 | Line *l; |
| 4507 | char *equals; |
| 4508 | |
| 4509 | equals = strchr(definition, '='); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4510 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 4511 | def = new_Token(space, TOK_PREPROC_ID, "%define", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4512 | if (equals) |
| 4513 | *equals = ' '; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4514 | space->next = tokenise(definition); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4515 | if (equals) |
| 4516 | *equals = '='; |
| 4517 | |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4518 | l = nasm_malloc(sizeof(Line)); |
| 4519 | l->next = predef; |
| 4520 | l->first = def; |
| 4521 | l->finishes = FALSE; |
| 4522 | predef = l; |
| 4523 | } |
| 4524 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4525 | void |
| 4526 | pp_pre_undefine(char *definition) |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 4527 | { |
| 4528 | Token *def, *space; |
| 4529 | Line *l; |
| 4530 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4531 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 4532 | def = new_Token(space, TOK_PREPROC_ID, "%undef", 0); |
Frank Kotler | d352302 | 2003-06-14 12:12:26 +0000 | [diff] [blame] | 4533 | space->next = tokenise(definition); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 4534 | |
| 4535 | l = nasm_malloc(sizeof(Line)); |
| 4536 | l->next = predef; |
| 4537 | l->first = def; |
| 4538 | l->finishes = FALSE; |
| 4539 | predef = l; |
| 4540 | } |
| 4541 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4542 | void |
H. Peter Anvin | bfebdb0 | 2002-09-12 02:23:54 +0000 | [diff] [blame] | 4543 | pp_extra_stdmac(const char **macros) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4544 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4545 | extrastdmac = macros; |
| 4546 | } |
| 4547 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4548 | static void |
| 4549 | make_tok_num(Token * tok, long val) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4550 | { |
| 4551 | char numbuf[20]; |
| 4552 | sprintf(numbuf, "%ld", val); |
| 4553 | tok->text = nasm_strdup(numbuf); |
| 4554 | tok->type = TOK_NUMBER; |
| 4555 | } |
| 4556 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4557 | Preproc nasmpp = { |
| 4558 | pp_reset, |
| 4559 | pp_getline, |
| 4560 | pp_cleanup |
| 4561 | }; |