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