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