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