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