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