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