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