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