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