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