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