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