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