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