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 | * |
H. Peter Anvin | 0599034 | 2018-06-11 13:32:42 -0700 | [diff] [blame] | 3 | * Copyright 1996-2018 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> |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 72 | |
| 73 | #include "nasm.h" |
| 74 | #include "nasmlib.h" |
H. Peter Anvin | b20bc73 | 2017-03-07 19:23:03 -0800 | [diff] [blame] | 75 | #include "error.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 | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 83 | #include "listing.h" |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 84 | |
| 85 | typedef struct SMacro SMacro; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 86 | typedef struct MMacro MMacro; |
| 87 | typedef struct MMacroInvocation MMacroInvocation; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 88 | typedef struct Context Context; |
| 89 | typedef struct Token Token; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 90 | typedef struct Blocks Blocks; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 91 | typedef struct Line Line; |
| 92 | typedef struct Include Include; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 93 | typedef struct Cond Cond; |
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 | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 108 | SMacro *next; |
| 109 | char *name; |
| 110 | bool casesense; |
| 111 | bool in_progress; |
| 112 | unsigned int nparam; |
| 113 | Token *expansion; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [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. |
| 132 | */ |
| 133 | struct MMacro { |
| 134 | MMacro *next; |
| 135 | MMacroInvocation *prev; /* previous invocation */ |
| 136 | char *name; |
| 137 | int nparam_min, nparam_max; |
| 138 | bool casesense; |
| 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 */ |
| 143 | Token *dlist; /* All defaults as one list */ |
| 144 | Token **defaults; /* Parameter default pointers */ |
| 145 | int ndefs; /* number of default parameters */ |
| 146 | Line *expansion; |
| 147 | |
| 148 | MMacro *next_active; |
| 149 | MMacro *rep_nest; /* used for nesting %rep */ |
| 150 | Token **params; /* actual parameters */ |
| 151 | Token *iline; /* invocation line */ |
| 152 | unsigned int nparam, rotate; |
| 153 | int *paramlen; |
| 154 | uint64_t unique; |
| 155 | int lineno; /* Current line number on expansion */ |
| 156 | uint64_t condcnt; /* number of if blocks... */ |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 157 | |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 158 | const char *fname; /* File where defined */ |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 159 | int32_t xline; /* First line in macro */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | |
| 163 | /* Store the definition of a multi-line macro, as defined in a |
| 164 | * previous recursive macro expansion. |
| 165 | */ |
| 166 | struct MMacroInvocation { |
| 167 | MMacroInvocation *prev; /* previous invocation */ |
| 168 | Token **params; /* actual parameters */ |
| 169 | Token *iline; /* invocation line */ |
| 170 | unsigned int nparam, rotate; |
| 171 | int *paramlen; |
| 172 | uint64_t unique; |
| 173 | uint64_t condcnt; |
| 174 | }; |
| 175 | |
| 176 | |
| 177 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 178 | * The context stack is composed of a linked list of these. |
| 179 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 180 | struct Context { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 181 | Context *next; |
| 182 | char *name; |
| 183 | struct hash_table localmac; |
| 184 | uint32_t number; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | /* |
| 188 | * This is the internal form which we break input lines up into. |
| 189 | * Typically stored in linked lists. |
| 190 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 191 | * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not |
| 192 | * necessarily used as-is, but is intended to denote the number of |
| 193 | * the substituted parameter. So in the definition |
| 194 | * |
| 195 | * %define a(x,y) ( (x) & ~(y) ) |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 196 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 197 | * the token representing `x' will have its type changed to |
| 198 | * TOK_SMAC_PARAM, but the one representing `y' will be |
| 199 | * TOK_SMAC_PARAM+1. |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 200 | * |
| 201 | * TOK_INTERNAL_STRING is a dirty hack: it's a single string token |
| 202 | * which doesn't need quotes around it. Used in the pre-include |
| 203 | * mechanism as an alternative to trying to find a sensible type of |
| 204 | * quote to use on the filename we were passed. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 205 | */ |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 206 | enum pp_token_type { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 207 | TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID, |
| 208 | TOK_PREPROC_ID, TOK_STRING, |
| 209 | TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER, |
H. Peter Anvin | 6c81f0a | 2008-05-25 21:46:17 -0700 | [diff] [blame] | 210 | TOK_INTERNAL_STRING, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 211 | TOK_PREPROC_Q, TOK_PREPROC_QQ, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 212 | TOK_PASTE, /* %+ */ |
| 213 | TOK_INDIRECT, /* %[...] */ |
| 214 | TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */ |
| 215 | TOK_MAX = INT_MAX /* Keep compiler from reducing the range */ |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 216 | }; |
| 217 | |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 218 | #define PP_CONCAT_MASK(x) (1 << (x)) |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 219 | #define PP_CONCAT_MATCH(t, mask) (PP_CONCAT_MASK((t)->type) & mask) |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 220 | |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 221 | struct tokseq_match { |
| 222 | int mask_head; |
| 223 | int mask_tail; |
| 224 | }; |
| 225 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 226 | struct Token { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 227 | Token *next; |
| 228 | char *text; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 229 | union { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 230 | SMacro *mac; /* associated macro for TOK_SMAC_END */ |
| 231 | size_t len; /* scratch length field */ |
| 232 | } a; /* Auxiliary data */ |
| 233 | enum pp_token_type type; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 234 | }; |
| 235 | |
| 236 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 237 | * Multi-line macro definitions are stored as a linked list of |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 238 | * these, which is essentially a container to allow several linked |
| 239 | * lists of Tokens. |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 240 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 241 | * Note that in this module, linked lists are treated as stacks |
| 242 | * wherever possible. For this reason, Lines are _pushed_ on to the |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 243 | * `expansion' field in MMacro structures, so that the linked list, |
| 244 | * if walked, would give the macro lines in reverse order; this |
| 245 | * means that we can walk the list when expanding a macro, and thus |
| 246 | * push the lines on to the `expansion' field in _istk_ in reverse |
| 247 | * order (so that when popped back off they are in the right |
| 248 | * order). It may seem cockeyed, and it relies on my design having |
| 249 | * an even number of steps in, but it works... |
| 250 | * |
| 251 | * Some of these structures, rather than being actual lines, are |
| 252 | * markers delimiting the end of the expansion of a given macro. |
| 253 | * This is for use in the cycle-tracking and %rep-handling code. |
| 254 | * Such structures have `finishes' non-NULL, and `first' NULL. All |
| 255 | * others have `finishes' NULL, but `first' may still be NULL if |
| 256 | * the line is blank. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 257 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 258 | struct Line { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 259 | Line *next; |
| 260 | MMacro *finishes; |
| 261 | Token *first; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 262 | }; |
| 263 | |
| 264 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 265 | * To handle an arbitrary level of file inclusion, we maintain a |
| 266 | * stack (ie linked list) of these things. |
| 267 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 268 | struct Include { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 269 | Include *next; |
| 270 | FILE *fp; |
| 271 | Cond *conds; |
| 272 | Line *expansion; |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 273 | const char *fname; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 274 | int lineno, lineinc; |
| 275 | MMacro *mstk; /* stack of active macros/reps */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 276 | }; |
| 277 | |
| 278 | /* |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 279 | * File real name hash, so we don't have to re-search the include |
| 280 | * path for every pass (and potentially more than that if a file |
| 281 | * is used more than once.) |
| 282 | */ |
| 283 | struct hash_table FileHash; |
| 284 | |
| 285 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 286 | * Conditional assembly: we maintain a separate stack of these for |
| 287 | * each level of file inclusion. (The only reason we keep the |
| 288 | * stacks separate is to ensure that a stray `%endif' in a file |
| 289 | * included from within the true branch of a `%if' won't terminate |
| 290 | * it and cause confusion: instead, rightly, it'll cause an error.) |
| 291 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 292 | struct Cond { |
| 293 | Cond *next; |
| 294 | int state; |
| 295 | }; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 296 | enum { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 297 | /* |
| 298 | * These states are for use just after %if or %elif: IF_TRUE |
| 299 | * means the condition has evaluated to truth so we are |
| 300 | * currently emitting, whereas IF_FALSE means we are not |
| 301 | * currently emitting but will start doing so if a %else comes |
| 302 | * up. In these states, all directives are admissible: %elif, |
| 303 | * %else and %endif. (And of course %if.) |
| 304 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 305 | COND_IF_TRUE, COND_IF_FALSE, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 306 | /* |
| 307 | * These states come up after a %else: ELSE_TRUE means we're |
| 308 | * emitting, and ELSE_FALSE means we're not. In ELSE_* states, |
| 309 | * any %elif or %else will cause an error. |
| 310 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 311 | COND_ELSE_TRUE, COND_ELSE_FALSE, |
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 | * These states mean that we're not emitting now, and also that |
| 314 | * nothing until %endif will be emitted at all. COND_DONE is |
| 315 | * used when we've had our moment of emission |
| 316 | * and have now started seeing %elifs. COND_NEVER is used when |
| 317 | * the condition construct in question is contained within a |
| 318 | * non-emitting branch of a larger condition construct, |
| 319 | * or if there is an error. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 320 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 321 | COND_DONE, COND_NEVER |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 322 | }; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 323 | #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE ) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 324 | |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 325 | /* |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 326 | * These defines are used as the possible return values for do_directive |
| 327 | */ |
| 328 | #define NO_DIRECTIVE_FOUND 0 |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 329 | #define DIRECTIVE_FOUND 1 |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 330 | |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 331 | /* max reps */ |
| 332 | #define REP_LIMIT ((INT64_C(1) << 62)) |
| 333 | |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 334 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 335 | * Condition codes. Note that we use c_ prefix not C_ because C_ is |
| 336 | * used in nasm.h for the "real" condition codes. At _this_ level, |
| 337 | * we treat CXZ and ECXZ as condition codes, albeit non-invertible |
| 338 | * ones, so we need a different enum... |
| 339 | */ |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 340 | static const char * const conditions[] = { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 341 | "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le", |
| 342 | "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no", |
H. Peter Anvin | ce9be34 | 2007-09-12 00:22:29 +0000 | [diff] [blame] | 343 | "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z" |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 344 | }; |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 345 | enum pp_conds { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 346 | c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE, |
| 347 | 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] | 348 | c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z, |
| 349 | c_none = -1 |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 350 | }; |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 351 | static const enum pp_conds inverse_ccs[] = { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 352 | c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE, |
| 353 | 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] | 354 | 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] | 355 | }; |
| 356 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 357 | /* |
| 358 | * Directive names. |
| 359 | */ |
| 360 | /* If this is a an IF, ELIF, ELSE or ENDIF keyword */ |
| 361 | static int is_condition(enum preproc_token arg) |
| 362 | { |
| 363 | return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF); |
| 364 | } |
| 365 | |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 366 | /* For TASM compatibility we need to be able to recognise TASM compatible |
| 367 | * conditional compilation directives. Using the NASM pre-processor does |
| 368 | * not work, so we look for them specifically from the following list and |
| 369 | * then jam in the equivalent NASM directive into the input stream. |
| 370 | */ |
| 371 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 372 | enum { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 373 | TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI, |
| 374 | TM_IFNDEF, TM_INCLUDE, TM_LOCAL |
| 375 | }; |
| 376 | |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 377 | static const char * const tasm_directives[] = { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 378 | "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi", |
| 379 | "ifndef", "include", "local" |
| 380 | }; |
| 381 | |
| 382 | static int StackSize = 4; |
H. Peter Anvin | 6c8b2be | 2016-05-24 23:46:50 -0700 | [diff] [blame] | 383 | static const char *StackPointer = "ebp"; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 384 | static int ArgOffset = 8; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 385 | static int LocalOffset = 0; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 386 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 387 | static Context *cstk; |
| 388 | static Include *istk; |
Cyrill Gorcunov | 8c0666b | 2018-11-24 14:33:48 +0300 | [diff] [blame] | 389 | static const struct strlist *ipath_list; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 390 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 391 | static int pass; /* HACK: pass 0 = generate dependencies only */ |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 392 | static struct strlist *deplist; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 393 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 394 | static uint64_t unique; /* unique identifier numbers */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 395 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 396 | static Line *predef = NULL; |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 397 | static bool do_predef; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 398 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 399 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 400 | * The current set of multi-line macros we have defined. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 401 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [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 | 36206cd | 2012-03-03 16:14:51 -0800 | [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 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 413 | static MMacro *defining; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 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 | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 424 | * The standard macro set: defined in macros.c in a set of arrays. |
| 425 | * This gives our position in any macro set, while we are processing it. |
| 426 | * The stdmacset is an array of such macro sets. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 427 | */ |
H. Peter Anvin | a70547f | 2008-07-19 21:44:26 -0700 | [diff] [blame] | 428 | static macros_t *stdmacpos; |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 429 | static macros_t **stdmacnext; |
| 430 | static macros_t *stdmacros[8]; |
Cyrill Gorcunov | 15ce78f | 2017-01-06 20:21:28 +0300 | [diff] [blame] | 431 | static macros_t *extrastdmac; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 432 | |
| 433 | /* |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 434 | * Tokens are allocated in blocks to improve speed |
| 435 | */ |
| 436 | #define TOKEN_BLOCKSIZE 4096 |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 437 | static Token *freeTokens = NULL; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 438 | struct Blocks { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 439 | Blocks *next; |
| 440 | void *chunk; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 441 | }; |
| 442 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 443 | static Blocks blocks = { NULL, NULL }; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 444 | |
| 445 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 446 | * Forward declarations. |
| 447 | */ |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 448 | static void pp_add_stdmac(macros_t *macros); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 449 | static Token *expand_mmac_params(Token * tline); |
| 450 | static Token *expand_smacro(Token * tline); |
| 451 | static Token *expand_id(Token * tline); |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 452 | static Context *get_ctx(const char *name, const char **namep); |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 453 | static void make_tok_num(Token * tok, int64_t val); |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 454 | static void pp_verror(int severity, const char *fmt, va_list ap); |
| 455 | static vefunc real_verror; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 456 | static void *new_Block(size_t size); |
| 457 | static void delete_Blocks(void); |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 458 | static Token *new_Token(Token * next, enum pp_token_type type, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 459 | const char *text, int txtlen); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 460 | static Token *delete_Token(Token * t); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 461 | |
| 462 | /* |
| 463 | * Macros for safe checking of token pointers, avoid *(NULL) |
| 464 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 465 | #define tok_type_(x,t) ((x) && (x)->type == (t)) |
| 466 | #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next |
| 467 | #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v))) |
| 468 | #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v)))) |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 469 | |
Cyrill Gorcunov | 194ba89 | 2011-06-30 01:16:35 +0400 | [diff] [blame] | 470 | /* |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 471 | * nasm_unquote with error if the string contains NUL characters. |
| 472 | * If the string contains NUL characters, issue an error and return |
| 473 | * the C len, i.e. truncate at the NUL. |
| 474 | */ |
| 475 | static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive) |
| 476 | { |
| 477 | size_t len = nasm_unquote(qstr, NULL); |
| 478 | size_t clen = strlen(qstr); |
| 479 | |
| 480 | if (len != clen) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 481 | nasm_nonfatal("NUL character in `%s' directive", |
| 482 | pp_directives[directive]); |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 483 | return clen; |
| 484 | } |
| 485 | |
| 486 | /* |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 487 | * In-place reverse a list of tokens. |
| 488 | */ |
| 489 | static Token *reverse_tokens(Token *t) |
| 490 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 491 | Token *prev = NULL; |
| 492 | Token *next; |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 493 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 494 | while (t) { |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 495 | next = t->next; |
| 496 | t->next = prev; |
| 497 | prev = t; |
| 498 | t = next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 499 | } |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 500 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 501 | return prev; |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | /* |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 505 | * Handle TASM specific directives, which do not contain a % in |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 506 | * front of them. We do it here because I could not find any other |
| 507 | * place to do it for the moment, and it is a hack (ideally it would |
| 508 | * be nice to be able to use the NASM pre-processor to do it). |
| 509 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 510 | static char *check_tasm_directive(char *line) |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 511 | { |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 512 | int32_t i, j, k, m, len; |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 513 | char *p, *q, *oldline, oldchar; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 514 | |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 515 | p = nasm_skip_spaces(line); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 516 | |
| 517 | /* Binary search for the directive name */ |
| 518 | i = -1; |
Cyrill Gorcunov | a731924 | 2010-06-03 22:04:36 +0400 | [diff] [blame] | 519 | j = ARRAY_SIZE(tasm_directives); |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 520 | q = nasm_skip_word(p); |
| 521 | len = q - p; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 522 | if (len) { |
| 523 | oldchar = p[len]; |
| 524 | p[len] = 0; |
| 525 | while (j - i > 1) { |
| 526 | k = (j + i) / 2; |
| 527 | m = nasm_stricmp(p, tasm_directives[k]); |
| 528 | if (m == 0) { |
| 529 | /* We have found a directive, so jam a % in front of it |
| 530 | * so that NASM will then recognise it as one if it's own. |
| 531 | */ |
| 532 | p[len] = oldchar; |
| 533 | len = strlen(p); |
| 534 | oldline = line; |
| 535 | line = nasm_malloc(len + 2); |
| 536 | line[0] = '%'; |
| 537 | if (k == TM_IFDIFI) { |
H. Peter Anvin | 18f4879 | 2009-06-27 15:56:27 -0700 | [diff] [blame] | 538 | /* |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 539 | * NASM does not recognise IFDIFI, so we convert |
| 540 | * it to %if 0. This is not used in NASM |
| 541 | * compatible code, but does need to parse for the |
| 542 | * TASM macro package. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 543 | */ |
H. Peter Anvin | 18f4879 | 2009-06-27 15:56:27 -0700 | [diff] [blame] | 544 | strcpy(line + 1, "if 0"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 545 | } else { |
| 546 | memcpy(line + 1, p, len + 1); |
| 547 | } |
| 548 | nasm_free(oldline); |
| 549 | return line; |
| 550 | } else if (m < 0) { |
| 551 | j = k; |
| 552 | } else |
| 553 | i = k; |
| 554 | } |
| 555 | p[len] = oldchar; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 556 | } |
| 557 | return line; |
| 558 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 559 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 560 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 561 | * The pre-preprocessing stage... This function translates line |
| 562 | * number indications as they emerge from GNU cpp (`# lineno "file" |
| 563 | * flags') into NASM preprocessor line number indications (`%line |
| 564 | * lineno file'). |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 565 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 566 | static char *prepreproc(char *line) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 567 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 568 | int lineno, fnlen; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 569 | char *fname, *oldline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 570 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 571 | if (line[0] == '#' && line[1] == ' ') { |
| 572 | oldline = line; |
| 573 | fname = oldline + 2; |
| 574 | lineno = atoi(fname); |
| 575 | fname += strspn(fname, "0123456789 "); |
| 576 | if (*fname == '"') |
| 577 | fname++; |
| 578 | fnlen = strcspn(fname, "\""); |
| 579 | line = nasm_malloc(20 + fnlen); |
| 580 | snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname); |
| 581 | nasm_free(oldline); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 582 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 583 | if (tasm_compatible_mode) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 584 | return check_tasm_directive(line); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 585 | return line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 589 | * Free a linked list of tokens. |
| 590 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 591 | static void free_tlist(Token * list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 592 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 593 | while (list) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 594 | list = delete_Token(list); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | /* |
| 598 | * Free a linked list of lines. |
| 599 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 600 | static void free_llist(Line * list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 601 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 602 | Line *l, *tmp; |
| 603 | list_for_each_safe(l, tmp, list) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 604 | free_tlist(l->first); |
| 605 | nasm_free(l); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 606 | } |
| 607 | } |
| 608 | |
| 609 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 610 | * Free an MMacro |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 611 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 612 | static void free_mmacro(MMacro * m) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 613 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 614 | nasm_free(m->name); |
| 615 | free_tlist(m->dlist); |
| 616 | nasm_free(m->defaults); |
| 617 | free_llist(m->expansion); |
| 618 | nasm_free(m); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | /* |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 622 | * Free all currently defined macros, and free the hash tables |
| 623 | */ |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 624 | static void free_smacro_table(struct hash_table *smt) |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 625 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 626 | SMacro *s, *tmp; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 627 | const char *key; |
| 628 | struct hash_tbl_node *it = NULL; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 629 | |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 630 | while ((s = hash_iterate(smt, &it, &key)) != NULL) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 631 | nasm_free((void *)key); |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 632 | list_for_each_safe(s, tmp, s) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 633 | nasm_free(s->name); |
| 634 | free_tlist(s->expansion); |
| 635 | nasm_free(s); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 636 | } |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 637 | } |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 638 | hash_free(smt); |
| 639 | } |
| 640 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 641 | static void free_mmacro_table(struct hash_table *mmt) |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 642 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 643 | MMacro *m, *tmp; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 644 | const char *key; |
| 645 | struct hash_tbl_node *it = NULL; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 646 | |
| 647 | it = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 648 | while ((m = hash_iterate(mmt, &it, &key)) != NULL) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 649 | nasm_free((void *)key); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 650 | list_for_each_safe(m ,tmp, m) |
| 651 | free_mmacro(m); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 652 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 653 | hash_free(mmt); |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | static void free_macros(void) |
| 657 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 658 | free_smacro_table(&smacros); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 659 | free_mmacro_table(&mmacros); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | /* |
| 663 | * Initialize the hash tables |
| 664 | */ |
| 665 | static void init_macros(void) |
| 666 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 667 | hash_init(&smacros, HASH_LARGE); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 668 | hash_init(&mmacros, HASH_LARGE); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 672 | * Pop the context stack. |
| 673 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 674 | static void ctx_pop(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 675 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 676 | Context *c = cstk; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 677 | |
| 678 | cstk = cstk->next; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 679 | free_smacro_table(&c->localmac); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 680 | nasm_free(c->name); |
| 681 | nasm_free(c); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 682 | } |
| 683 | |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 684 | /* |
| 685 | * Search for a key in the hash index; adding it if necessary |
| 686 | * (in which case we initialize the data pointer to NULL.) |
| 687 | */ |
| 688 | static void ** |
| 689 | hash_findi_add(struct hash_table *hash, const char *str) |
| 690 | { |
| 691 | struct hash_insert hi; |
| 692 | void **r; |
| 693 | char *strx; |
| 694 | |
| 695 | r = hash_findi(hash, str, &hi); |
| 696 | if (r) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 697 | return r; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 698 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 699 | strx = nasm_strdup(str); /* Use a more efficient allocator here? */ |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 700 | return hash_add(&hi, strx, NULL); |
| 701 | } |
| 702 | |
| 703 | /* |
| 704 | * Like hash_findi, but returns the data element rather than a pointer |
| 705 | * to it. Used only when not adding a new element, hence no third |
| 706 | * argument. |
| 707 | */ |
| 708 | static void * |
| 709 | hash_findix(struct hash_table *hash, const char *str) |
| 710 | { |
| 711 | void **p; |
| 712 | |
| 713 | p = hash_findi(hash, str, NULL); |
| 714 | return p ? *p : NULL; |
| 715 | } |
| 716 | |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 717 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 718 | * read line from standart macros set, |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 719 | * if there no more left -- return NULL |
| 720 | */ |
| 721 | static char *line_from_stdmac(void) |
| 722 | { |
| 723 | unsigned char c; |
| 724 | const unsigned char *p = stdmacpos; |
| 725 | char *line, *q; |
| 726 | size_t len = 0; |
| 727 | |
| 728 | if (!stdmacpos) |
| 729 | return NULL; |
| 730 | |
| 731 | while ((c = *p++)) { |
| 732 | if (c >= 0x80) |
| 733 | len += pp_directives_len[c - 0x80] + 1; |
| 734 | else |
| 735 | len++; |
| 736 | } |
| 737 | |
| 738 | line = nasm_malloc(len + 1); |
| 739 | q = line; |
| 740 | while ((c = *stdmacpos++)) { |
| 741 | if (c >= 0x80) { |
| 742 | memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]); |
| 743 | q += pp_directives_len[c - 0x80]; |
| 744 | *q++ = ' '; |
| 745 | } else { |
| 746 | *q++ = c; |
| 747 | } |
| 748 | } |
| 749 | stdmacpos = p; |
| 750 | *q = '\0'; |
| 751 | |
| 752 | if (!*stdmacpos) { |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 753 | /* This was the last of this particular macro set */ |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 754 | stdmacpos = NULL; |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 755 | if (*stdmacnext) { |
| 756 | stdmacpos = *stdmacnext++; |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 757 | } else if (do_predef) { |
| 758 | Line *pd, *l; |
| 759 | Token *head, **tail, *t; |
| 760 | |
| 761 | /* |
| 762 | * Nasty hack: here we push the contents of |
| 763 | * `predef' on to the top-level expansion stack, |
| 764 | * since this is the most convenient way to |
| 765 | * implement the pre-include and pre-define |
| 766 | * features. |
| 767 | */ |
| 768 | list_for_each(pd, predef) { |
| 769 | head = NULL; |
| 770 | tail = &head; |
| 771 | list_for_each(t, pd->first) { |
| 772 | *tail = new_Token(NULL, t->type, t->text, 0); |
| 773 | tail = &(*tail)->next; |
| 774 | } |
| 775 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 776 | l = nasm_malloc(sizeof(Line)); |
| 777 | l->next = istk->expansion; |
| 778 | l->first = head; |
| 779 | l->finishes = NULL; |
| 780 | |
| 781 | istk->expansion = l; |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 782 | } |
| 783 | do_predef = false; |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | return line; |
| 788 | } |
| 789 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 790 | static char *read_line(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 791 | { |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 792 | unsigned int size, c, next; |
| 793 | const unsigned int delta = 512; |
| 794 | const unsigned int pad = 8; |
| 795 | unsigned int nr_cont = 0; |
| 796 | bool cont = false; |
| 797 | char *buffer, *p; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 798 | |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 799 | /* Standart macros set (predefined) goes first */ |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 800 | p = line_from_stdmac(); |
| 801 | if (p) |
| 802 | return p; |
H. Peter Anvin | 72edbb8 | 2008-06-19 16:00:04 -0700 | [diff] [blame] | 803 | |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 804 | size = delta; |
| 805 | p = buffer = nasm_malloc(size); |
| 806 | |
| 807 | for (;;) { |
| 808 | c = fgetc(istk->fp); |
| 809 | if ((int)(c) == EOF) { |
| 810 | p[0] = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 811 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 812 | } |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 813 | |
| 814 | switch (c) { |
| 815 | case '\r': |
| 816 | next = fgetc(istk->fp); |
| 817 | if (next != '\n') |
| 818 | ungetc(next, istk->fp); |
| 819 | if (cont) { |
| 820 | cont = false; |
| 821 | continue; |
| 822 | } |
| 823 | break; |
| 824 | |
| 825 | case '\n': |
| 826 | if (cont) { |
| 827 | cont = false; |
| 828 | continue; |
| 829 | } |
| 830 | break; |
| 831 | |
| 832 | case '\\': |
| 833 | next = fgetc(istk->fp); |
| 834 | ungetc(next, istk->fp); |
Cyrill Gorcunov | 490f85e | 2012-12-27 20:02:17 +0400 | [diff] [blame] | 835 | if (next == '\r' || next == '\n') { |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 836 | cont = true; |
| 837 | nr_cont++; |
| 838 | continue; |
| 839 | } |
| 840 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 841 | } |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 842 | |
| 843 | if (c == '\r' || c == '\n') { |
| 844 | *p++ = 0; |
| 845 | break; |
| 846 | } |
| 847 | |
| 848 | if (p >= (buffer + size - pad)) { |
| 849 | buffer = nasm_realloc(buffer, size + delta); |
| 850 | p = buffer + size - pad; |
| 851 | size += delta; |
| 852 | } |
| 853 | |
| 854 | *p++ = (unsigned char)c; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 855 | } |
| 856 | |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 857 | if (p == buffer) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 858 | nasm_free(buffer); |
| 859 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 860 | } |
| 861 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 862 | src_set_linnum(src_get_linnum() + istk->lineinc + |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 863 | (nr_cont * istk->lineinc)); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 864 | |
| 865 | /* |
| 866 | * Handle spurious ^Z, which may be inserted into source files |
| 867 | * by some file transfer utilities. |
| 868 | */ |
| 869 | buffer[strcspn(buffer, "\032")] = '\0'; |
| 870 | |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 871 | lfmt->line(LIST_READ, buffer); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 872 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 873 | return buffer; |
| 874 | } |
| 875 | |
| 876 | /* |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 877 | * 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] | 878 | * don't need to parse the value out of e.g. numeric tokens: we |
| 879 | * simply split one string into many. |
| 880 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 881 | static Token *tokenize(char *line) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 882 | { |
H. Peter Anvin | ca544db | 2008-10-19 19:30:11 -0700 | [diff] [blame] | 883 | char c, *p = line; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 884 | enum pp_token_type type; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 885 | Token *list = NULL; |
| 886 | Token *t, **tail = &list; |
| 887 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 888 | while (*line) { |
| 889 | p = line; |
| 890 | if (*p == '%') { |
| 891 | p++; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 892 | if (*p == '+' && !nasm_isdigit(p[1])) { |
| 893 | p++; |
| 894 | type = TOK_PASTE; |
| 895 | } else if (nasm_isdigit(*p) || |
| 896 | ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 897 | do { |
| 898 | p++; |
| 899 | } |
H. Peter Anvin | bda7a6e | 2008-06-21 10:23:17 -0700 | [diff] [blame] | 900 | while (nasm_isdigit(*p)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 901 | type = TOK_PREPROC_ID; |
| 902 | } else if (*p == '{') { |
| 903 | p++; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 904 | while (*p) { |
| 905 | if (*p == '}') |
| 906 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 907 | p[-1] = *p; |
| 908 | p++; |
| 909 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 910 | if (*p != '}') |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 911 | nasm_warnf(ERR_PASS1, "unterminated %%{ construct"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 912 | p[-1] = '\0'; |
| 913 | if (*p) |
| 914 | p++; |
| 915 | type = TOK_PREPROC_ID; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 916 | } else if (*p == '[') { |
| 917 | int lvl = 1; |
| 918 | line += 2; /* Skip the leading %[ */ |
| 919 | p++; |
| 920 | while (lvl && (c = *p++)) { |
| 921 | switch (c) { |
| 922 | case ']': |
| 923 | lvl--; |
| 924 | break; |
| 925 | case '%': |
| 926 | if (*p == '[') |
| 927 | lvl++; |
| 928 | break; |
| 929 | case '\'': |
| 930 | case '\"': |
| 931 | case '`': |
Cyrill Gorcunov | 3144e84 | 2017-10-22 10:50:55 +0300 | [diff] [blame] | 932 | p = nasm_skip_string(p - 1); |
| 933 | if (*p) |
| 934 | p++; |
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'; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 943 | if (lvl) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 944 | nasm_nonfatalf(ERR_PASS1, "unterminated %%[ construct"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 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 | } |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 953 | } else if (*p == '!') { |
| 954 | type = TOK_PREPROC_ID; |
| 955 | p++; |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 956 | if (nasm_isidchar(*p)) { |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 957 | do { |
| 958 | p++; |
| 959 | } |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 960 | while (nasm_isidchar(*p)); |
H. Peter Anvin | 53e2e4c | 2018-11-28 15:01:40 -0800 | [diff] [blame] | 961 | } else if (nasm_isquote(*p)) { |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 962 | p = nasm_skip_string(p); |
| 963 | if (*p) |
| 964 | p++; |
| 965 | else |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 966 | nasm_nonfatalf(ERR_PASS1, "unterminated %%! string"); |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 967 | } else { |
| 968 | /* %! without string or identifier */ |
| 969 | type = TOK_OTHER; /* Legacy behavior... */ |
| 970 | } |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 971 | } else if (nasm_isidchar(*p) || |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 972 | ((*p == '!' || *p == '%' || *p == '$') && |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 973 | nasm_isidchar(p[1]))) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 974 | do { |
| 975 | p++; |
| 976 | } |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 977 | while (nasm_isidchar(*p)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 978 | type = TOK_PREPROC_ID; |
| 979 | } else { |
| 980 | type = TOK_OTHER; |
| 981 | if (*p == '%') |
| 982 | p++; |
| 983 | } |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 984 | } else if (nasm_isidstart(*p) || (*p == '$' && nasm_isidstart(p[1]))) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 985 | type = TOK_ID; |
| 986 | p++; |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 987 | while (*p && nasm_isidchar(*p)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 988 | p++; |
H. Peter Anvin | 53e2e4c | 2018-11-28 15:01:40 -0800 | [diff] [blame] | 989 | } else if (nasm_isquote(*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++; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 998 | } else { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 999 | nasm_warnf(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 | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 1006 | } else if (nasm_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++; |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 1040 | } else if (nasm_isnumchar(c)) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 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 */ |
Cyrill Gorcunov | c31767c | 2014-06-28 02:22:17 +0400 | [diff] [blame] | 1153 | b->next = nasm_zalloc(sizeof(Blocks)); |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1154 | return b->chunk; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | /* |
| 1158 | * this function deletes all managed blocks of memory |
| 1159 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1160 | static void delete_Blocks(void) |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1161 | { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1162 | Blocks *a, *b = &blocks; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1163 | |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1164 | /* |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1165 | * keep in mind that the first block, pointed to by blocks |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1166 | * is a static and not dynamically allocated, so we don't |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1167 | * free it. |
| 1168 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1169 | while (b) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1170 | if (b->chunk) |
| 1171 | nasm_free(b->chunk); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1172 | a = b; |
| 1173 | b = b->next; |
| 1174 | if (a != &blocks) |
| 1175 | nasm_free(a); |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1176 | } |
Cyrill Gorcunov | dae24d7 | 2014-06-28 10:17:39 +0400 | [diff] [blame] | 1177 | memset(&blocks, 0, sizeof(blocks)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1178 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1179 | |
| 1180 | /* |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1181 | * 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] | 1182 | * 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] | 1183 | * also the a.mac and next elements to NULL. |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1184 | */ |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1185 | static Token *new_Token(Token * next, enum pp_token_type type, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1186 | const char *text, int txtlen) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1187 | { |
| 1188 | Token *t; |
| 1189 | int i; |
| 1190 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1191 | if (!freeTokens) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1192 | freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token)); |
| 1193 | for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++) |
| 1194 | freeTokens[i].next = &freeTokens[i + 1]; |
| 1195 | freeTokens[i].next = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1196 | } |
| 1197 | t = freeTokens; |
| 1198 | freeTokens = t->next; |
| 1199 | t->next = next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 1200 | t->a.mac = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1201 | t->type = type; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1202 | if (type == TOK_WHITESPACE || !text) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1203 | t->text = NULL; |
| 1204 | } else { |
| 1205 | if (txtlen == 0) |
| 1206 | txtlen = strlen(text); |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1207 | t->text = nasm_malloc(txtlen+1); |
| 1208 | memcpy(t->text, text, txtlen); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1209 | t->text[txtlen] = '\0'; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1210 | } |
| 1211 | return t; |
| 1212 | } |
| 1213 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1214 | static Token *delete_Token(Token * t) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1215 | { |
| 1216 | Token *next = t->next; |
| 1217 | nasm_free(t->text); |
H. Peter Anvin | 788e6c1 | 2002-04-30 21:02:01 +0000 | [diff] [blame] | 1218 | t->next = freeTokens; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1219 | freeTokens = t; |
| 1220 | return next; |
| 1221 | } |
| 1222 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1223 | /* |
| 1224 | * Convert a line of tokens back into text. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1225 | * If expand_locals is not zero, identifiers of the form "%$*xxx" |
| 1226 | * will be transformed into ..@ctxnum.xxx |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1227 | */ |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 1228 | static char *detoken(Token * tlist, bool expand_locals) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1229 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1230 | Token *t; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1231 | char *line, *p; |
H. Peter Anvin | b4daadc | 2008-01-21 16:31:57 -0800 | [diff] [blame] | 1232 | const char *q; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1233 | int len = 0; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1234 | |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1235 | list_for_each(t, tlist) { |
Cyrill Gorcunov | 9b7ee09 | 2017-10-22 21:42:59 +0300 | [diff] [blame] | 1236 | if (t->type == TOK_PREPROC_ID && t->text && |
| 1237 | t->text[0] && t->text[1] == '!') { |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1238 | char *v; |
| 1239 | char *q = t->text; |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1240 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1241 | v = t->text + 2; |
H. Peter Anvin | 53e2e4c | 2018-11-28 15:01:40 -0800 | [diff] [blame] | 1242 | if (nasm_isquote(*v)) { |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1243 | size_t len = nasm_unquote(v, NULL); |
| 1244 | size_t clen = strlen(v); |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1245 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1246 | if (len != clen) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1247 | nasm_nonfatalf(ERR_PASS1, "NUL character in %%! string"); |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1248 | v = NULL; |
| 1249 | } |
| 1250 | } |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1251 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1252 | if (v) { |
| 1253 | char *p = getenv(v); |
| 1254 | if (!p) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1255 | nasm_nonfatalf(ERR_PASS1, "nonexistent environment variable `%s'", v); |
Cyrill Gorcunov | bbb7a1a | 2016-06-19 12:15:24 +0300 | [diff] [blame] | 1256 | /* |
| 1257 | * FIXME We better should investigate if accessing |
| 1258 | * ->text[1] without ->text[0] is safe enough. |
| 1259 | */ |
| 1260 | t->text = nasm_zalloc(2); |
| 1261 | } else |
| 1262 | t->text = nasm_strdup(p); |
Cyrill Gorcunov | 7500487 | 2017-07-26 01:21:16 +0300 | [diff] [blame] | 1263 | nasm_free(q); |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1264 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1265 | } |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1266 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1267 | /* Expand local macros here and not during preprocessing */ |
| 1268 | if (expand_locals && |
| 1269 | t->type == TOK_PREPROC_ID && t->text && |
| 1270 | t->text[0] == '%' && t->text[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1271 | const char *q; |
| 1272 | char *p; |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1273 | Context *ctx = get_ctx(t->text, &q); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1274 | if (ctx) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1275 | char buffer[40]; |
Keith Kanios | 93f2e9a | 2007-04-14 00:10:59 +0000 | [diff] [blame] | 1276 | snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1277 | p = nasm_strcat(buffer, q); |
| 1278 | nasm_free(t->text); |
| 1279 | t->text = p; |
| 1280 | } |
| 1281 | } |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1282 | if (t->type == TOK_WHITESPACE) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1283 | len++; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1284 | else if (t->text) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1285 | len += strlen(t->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1286 | } |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1287 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1288 | p = line = nasm_malloc(len + 1); |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1289 | |
| 1290 | list_for_each(t, tlist) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1291 | if (t->type == TOK_WHITESPACE) { |
H. Peter Anvin | b4daadc | 2008-01-21 16:31:57 -0800 | [diff] [blame] | 1292 | *p++ = ' '; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1293 | } else if (t->text) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1294 | q = t->text; |
| 1295 | while (*q) |
| 1296 | *p++ = *q++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1297 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1298 | } |
| 1299 | *p = '\0'; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1300 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1301 | return line; |
| 1302 | } |
| 1303 | |
| 1304 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1305 | * A scanner, suitable for use by the expression evaluator, which |
| 1306 | * operates on a line of Tokens. Expects a pointer to a pointer to |
| 1307 | * the first token in the line to be passed in as its private_data |
| 1308 | * field. |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1309 | * |
| 1310 | * FIX: This really needs to be unified with stdscan. |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1311 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1312 | static int ppscan(void *private_data, struct tokenval *tokval) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1313 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1314 | Token **tlineptr = private_data; |
| 1315 | Token *tline; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1316 | char ourcopy[MAX_KEYWORD+1], *p, *r, *s; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1317 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1318 | do { |
| 1319 | tline = *tlineptr; |
| 1320 | *tlineptr = tline ? tline->next : NULL; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 1321 | } while (tline && (tline->type == TOK_WHITESPACE || |
| 1322 | tline->type == TOK_COMMENT)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1323 | |
| 1324 | if (!tline) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1325 | return tokval->t_type = TOKEN_EOS; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1326 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1327 | tokval->t_charptr = tline->text; |
| 1328 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1329 | if (tline->text[0] == '$' && !tline->text[1]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1330 | return tokval->t_type = TOKEN_HERE; |
H. Peter Anvin | 7cf897e | 2002-05-30 21:30:33 +0000 | [diff] [blame] | 1331 | if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1332 | return tokval->t_type = TOKEN_BASE; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1333 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1334 | if (tline->type == TOK_ID) { |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1335 | p = tokval->t_charptr = tline->text; |
| 1336 | if (p[0] == '$') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1337 | tokval->t_charptr++; |
| 1338 | return tokval->t_type = TOKEN_ID; |
| 1339 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1340 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1341 | for (r = p, s = ourcopy; *r; r++) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1342 | if (r >= p+MAX_KEYWORD) |
| 1343 | return tokval->t_type = TOKEN_ID; /* Not a keyword */ |
H. Peter Anvin | ac8f8fc | 2008-06-11 15:49:41 -0700 | [diff] [blame] | 1344 | *s++ = nasm_tolower(*r); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1345 | } |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1346 | *s = '\0'; |
| 1347 | /* right, so we have an identifier sitting in temp storage. now, |
| 1348 | * is it actually a register or instruction name, or what? */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1349 | return nasm_token_hash(ourcopy, tokval); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1352 | if (tline->type == TOK_NUMBER) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1353 | bool rn_error; |
| 1354 | tokval->t_integer = readnum(tline->text, &rn_error); |
| 1355 | tokval->t_charptr = tline->text; |
| 1356 | if (rn_error) |
| 1357 | return tokval->t_type = TOKEN_ERRNUM; |
| 1358 | else |
| 1359 | return tokval->t_type = TOKEN_NUM; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1360 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1361 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1362 | if (tline->type == TOK_FLOAT) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1363 | return tokval->t_type = TOKEN_FLOAT; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1364 | } |
| 1365 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1366 | if (tline->type == TOK_STRING) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1367 | char bq, *ep; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1368 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1369 | bq = tline->text[0]; |
H. Peter Anvin | 1162704 | 2008-06-09 20:45:19 -0700 | [diff] [blame] | 1370 | tokval->t_charptr = tline->text; |
| 1371 | tokval->t_inttwo = nasm_unquote(tline->text, &ep); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 1372 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1373 | if (ep[0] != bq || ep[1] != '\0') |
| 1374 | return tokval->t_type = TOKEN_ERRSTR; |
| 1375 | else |
| 1376 | return tokval->t_type = TOKEN_STR; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1379 | if (tline->type == TOK_OTHER) { |
| 1380 | if (!strcmp(tline->text, "<<")) |
| 1381 | return tokval->t_type = TOKEN_SHL; |
| 1382 | if (!strcmp(tline->text, ">>")) |
| 1383 | return tokval->t_type = TOKEN_SHR; |
| 1384 | if (!strcmp(tline->text, "//")) |
| 1385 | return tokval->t_type = TOKEN_SDIV; |
| 1386 | if (!strcmp(tline->text, "%%")) |
| 1387 | return tokval->t_type = TOKEN_SMOD; |
| 1388 | if (!strcmp(tline->text, "==")) |
| 1389 | return tokval->t_type = TOKEN_EQ; |
| 1390 | if (!strcmp(tline->text, "<>")) |
| 1391 | return tokval->t_type = TOKEN_NE; |
| 1392 | if (!strcmp(tline->text, "!=")) |
| 1393 | return tokval->t_type = TOKEN_NE; |
| 1394 | if (!strcmp(tline->text, "<=")) |
| 1395 | return tokval->t_type = TOKEN_LE; |
| 1396 | if (!strcmp(tline->text, ">=")) |
| 1397 | return tokval->t_type = TOKEN_GE; |
| 1398 | if (!strcmp(tline->text, "&&")) |
| 1399 | return tokval->t_type = TOKEN_DBL_AND; |
| 1400 | if (!strcmp(tline->text, "^^")) |
| 1401 | return tokval->t_type = TOKEN_DBL_XOR; |
| 1402 | if (!strcmp(tline->text, "||")) |
| 1403 | return tokval->t_type = TOKEN_DBL_OR; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | /* |
| 1407 | * We have no other options: just return the first character of |
| 1408 | * the token text. |
| 1409 | */ |
| 1410 | return tokval->t_type = tline->text[0]; |
| 1411 | } |
| 1412 | |
| 1413 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1414 | * Compare a string to the name of an existing macro; this is a |
| 1415 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 1416 | * depending on the value of the `casesense' parameter. |
| 1417 | */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 1418 | static int mstrcmp(const char *p, const char *q, bool casesense) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1419 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1420 | return casesense ? strcmp(p, q) : nasm_stricmp(p, q); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1421 | } |
| 1422 | |
| 1423 | /* |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1424 | * Compare a string to the name of an existing macro; this is a |
| 1425 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 1426 | * depending on the value of the `casesense' parameter. |
| 1427 | */ |
| 1428 | static int mmemcmp(const char *p, const char *q, size_t l, bool casesense) |
| 1429 | { |
| 1430 | return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l); |
| 1431 | } |
| 1432 | |
| 1433 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1434 | * Return the Context structure associated with a %$ token. Return |
| 1435 | * NULL, having _already_ reported an error condition, if the |
| 1436 | * context stack isn't deep enough for the supplied number of $ |
| 1437 | * signs. |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1438 | * |
| 1439 | * If "namep" is non-NULL, set it to the pointer to the macro name |
| 1440 | * tail, i.e. the part beyond %$... |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1441 | */ |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1442 | static Context *get_ctx(const char *name, const char **namep) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1443 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1444 | Context *ctx; |
| 1445 | int i; |
| 1446 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1447 | if (namep) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1448 | *namep = name; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1449 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1450 | if (!name || name[0] != '%' || name[1] != '$') |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1451 | return NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1452 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1453 | if (!cstk) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1454 | nasm_nonfatal("`%s': context stack is empty", name); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1455 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1458 | name += 2; |
| 1459 | ctx = cstk; |
| 1460 | i = 0; |
| 1461 | while (ctx && *name == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1462 | name++; |
| 1463 | i++; |
| 1464 | ctx = ctx->next; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1465 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1466 | if (!ctx) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1467 | nasm_nonfatal("`%s': context stack is only" |
| 1468 | " %d level%s deep", name, i, (i == 1 ? "" : "s")); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1469 | return NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1470 | } |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1471 | |
| 1472 | if (namep) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1473 | *namep = name; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1474 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1475 | return ctx; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1479 | * Open an include file. This routine must always return a valid |
| 1480 | * file pointer if it returns - it's responsible for throwing an |
| 1481 | * ERR_FATAL and bombing out completely if not. It should also try |
| 1482 | * the include path one by one until it finds the file or reaches |
| 1483 | * the end of the path. |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1484 | * |
| 1485 | * Note: for INC_PROBE the function returns NULL at all times; |
| 1486 | * instead look for the |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1487 | */ |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1488 | enum incopen_mode { |
| 1489 | INC_NEEDED, /* File must exist */ |
| 1490 | INC_OPTIONAL, /* Missing is OK */ |
| 1491 | INC_PROBE /* Only an existence probe */ |
| 1492 | }; |
| 1493 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1494 | /* This is conducts a full pathname search */ |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1495 | static FILE *inc_fopen_search(const char *file, char **slpath, |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1496 | enum incopen_mode omode, enum file_flags fmode) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1497 | { |
Cyrill Gorcunov | 8c0666b | 2018-11-24 14:33:48 +0300 | [diff] [blame] | 1498 | const struct strlist_entry *ip = NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1499 | FILE *fp; |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1500 | const char *prefix = ""; |
night199uk | fdb1a1b | 2018-10-18 23:19:47 +0200 | [diff] [blame] | 1501 | char *sp; |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1502 | bool found; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1503 | |
Cyrill Gorcunov | 8c0666b | 2018-11-24 14:33:48 +0300 | [diff] [blame] | 1504 | if (ipath_list) |
| 1505 | ip = ipath_list->head; |
| 1506 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1507 | while (1) { |
night199uk | fdb1a1b | 2018-10-18 23:19:47 +0200 | [diff] [blame] | 1508 | sp = nasm_catfile(prefix, file); |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1509 | if (omode == INC_PROBE) { |
| 1510 | fp = NULL; |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1511 | found = nasm_file_exists(sp); |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1512 | } else { |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1513 | fp = nasm_open_read(sp, fmode); |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1514 | found = (fp != NULL); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1515 | } |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1516 | if (found) { |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1517 | *slpath = sp; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1518 | return fp; |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1519 | } |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 1520 | |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1521 | nasm_free(sp); |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 1522 | |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1523 | if (!ip) { |
| 1524 | *slpath = NULL; |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1525 | return NULL; |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1526 | } |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1527 | |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1528 | prefix = ip->str; |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1529 | ip = ip->next; |
| 1530 | } |
| 1531 | } |
| 1532 | |
| 1533 | /* |
| 1534 | * Open a file, or test for the presence of one (depending on omode), |
| 1535 | * considering the include path. |
| 1536 | */ |
| 1537 | static FILE *inc_fopen(const char *file, |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 1538 | struct strlist *dhead, |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 1539 | const char **found_path, |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1540 | enum incopen_mode omode, |
| 1541 | enum file_flags fmode) |
| 1542 | { |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1543 | struct hash_insert hi; |
| 1544 | void **hp; |
| 1545 | char *path; |
| 1546 | FILE *fp = NULL; |
| 1547 | |
| 1548 | hp = hash_find(&FileHash, file, &hi); |
| 1549 | if (hp) { |
| 1550 | path = *hp; |
Martin Storsjö | f283c8f | 2017-08-13 17:28:46 +0300 | [diff] [blame] | 1551 | if (path || omode != INC_NEEDED) { |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 1552 | strlist_add(dhead, path ? path : file); |
Martin Storsjö | f283c8f | 2017-08-13 17:28:46 +0300 | [diff] [blame] | 1553 | } |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1554 | } else { |
| 1555 | /* Need to do the actual path search */ |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1556 | fp = inc_fopen_search(file, &path, omode, fmode); |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1557 | |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1558 | /* Positive or negative result */ |
| 1559 | hash_add(&hi, nasm_strdup(file), path); |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1560 | |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 1561 | /* |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1562 | * Add file to dependency path. |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 1563 | */ |
| 1564 | if (path || omode != INC_NEEDED) |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 1565 | strlist_add(dhead, file); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1566 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1567 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1568 | if (!path) { |
| 1569 | if (omode == INC_NEEDED) |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 1570 | nasm_fatal("unable to open include file `%s'", file); |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1571 | } else { |
| 1572 | if (!fp && omode != INC_PROBE) |
| 1573 | fp = nasm_open_read(path, fmode); |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1574 | } |
| 1575 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1576 | if (found_path) |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 1577 | *found_path = path; |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1578 | |
| 1579 | return fp; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1580 | } |
| 1581 | |
| 1582 | /* |
Fabian Giesen | 0bbc38d | 2016-04-28 13:48:14 -0700 | [diff] [blame] | 1583 | * Opens an include or input file. Public version, for use by modules |
| 1584 | * that get a file:lineno pair and need to look at the file again |
| 1585 | * (e.g. the CodeView debug backend). Returns NULL on failure. |
| 1586 | */ |
H. Peter Anvin | 3e83cec | 2016-05-25 04:28:46 -0700 | [diff] [blame] | 1587 | FILE *pp_input_fopen(const char *filename, enum file_flags mode) |
Fabian Giesen | 0bbc38d | 2016-04-28 13:48:14 -0700 | [diff] [blame] | 1588 | { |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 1589 | return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode); |
Fabian Giesen | 0bbc38d | 2016-04-28 13:48:14 -0700 | [diff] [blame] | 1590 | } |
| 1591 | |
| 1592 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1593 | * 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] | 1594 | * 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] | 1595 | * return true if _any_ single-line macro of that name is defined. |
| 1596 | * Otherwise, will return true if a single-line macro with either |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1597 | * `nparam' or no parameters is defined. |
| 1598 | * |
| 1599 | * If a macro with precisely the right number of parameters is |
H. Peter Anvin | ef7468f | 2002-04-30 20:57:59 +0000 | [diff] [blame] | 1600 | * defined, or nparam is -1, the address of the definition structure |
| 1601 | * 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] | 1602 | * is NULL, no action will be taken regarding its contents, and no |
| 1603 | * error will occur. |
| 1604 | * |
| 1605 | * Note that this is also called with nparam zero to resolve |
| 1606 | * `ifdef'. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1607 | * |
| 1608 | * If you already know which context macro belongs to, you can pass |
| 1609 | * the context pointer as first parameter; if you won't but name begins |
| 1610 | * with %$ the context will be automatically computed. If all_contexts |
| 1611 | * is true, macro will be searched in outer contexts as well. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1612 | */ |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1613 | static bool |
H. Peter Anvin | b2a5fda | 2008-06-19 21:42:42 -0700 | [diff] [blame] | 1614 | smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn, |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1615 | bool nocase) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1616 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1617 | struct hash_table *smtbl; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1618 | SMacro *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1619 | |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1620 | if (ctx) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1621 | smtbl = &ctx->localmac; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1622 | } else if (name[0] == '%' && name[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1623 | if (cstk) |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1624 | ctx = get_ctx(name, &name); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1625 | if (!ctx) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1626 | return false; /* got to return _something_ */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1627 | smtbl = &ctx->localmac; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1628 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1629 | smtbl = &smacros; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1630 | } |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1631 | m = (SMacro *) hash_findix(smtbl, name); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1632 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1633 | while (m) { |
| 1634 | if (!mstrcmp(m->name, name, m->casesense && nocase) && |
Charles Crayne | 192d5b5 | 2007-10-18 19:02:42 -0700 | [diff] [blame] | 1635 | (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1636 | if (defn) { |
Charles Crayne | 192d5b5 | 2007-10-18 19:02:42 -0700 | [diff] [blame] | 1637 | if (nparam == (int) m->nparam || nparam == -1) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1638 | *defn = m; |
| 1639 | else |
| 1640 | *defn = NULL; |
| 1641 | } |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1642 | return true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1643 | } |
| 1644 | m = m->next; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1645 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1646 | |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1647 | return false; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1648 | } |
| 1649 | |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 1650 | /* param should be a natural number [0; INT_MAX] */ |
| 1651 | static int read_param_count(const char *str) |
| 1652 | { |
| 1653 | int result; |
| 1654 | bool err; |
| 1655 | |
| 1656 | result = readnum(str, &err); |
| 1657 | if (result < 0 || result > INT_MAX) { |
| 1658 | result = 0; |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1659 | nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]", |
| 1660 | str, 0, INT_MAX); |
| 1661 | } else if (err) |
| 1662 | nasm_nonfatal("unable to parse parameter count `%s'", str); |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 1663 | return result; |
| 1664 | } |
| 1665 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1666 | /* |
| 1667 | * Count and mark off the parameters in a multi-line macro call. |
| 1668 | * This is called both from within the multi-line macro expansion |
| 1669 | * code, and also to mark off the default parameters when provided |
| 1670 | * in a %macro definition line. |
| 1671 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1672 | static void count_mmac_params(Token * t, int *nparam, Token *** params) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1673 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1674 | int paramsize, brace; |
| 1675 | |
| 1676 | *nparam = paramsize = 0; |
| 1677 | *params = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1678 | while (t) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1679 | /* +1: we need space for the final NULL */ |
H. Peter Anvin | 91fb6f1 | 2008-09-01 10:56:33 -0700 | [diff] [blame] | 1680 | if (*nparam+1 >= paramsize) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1681 | paramsize += PARAM_DELTA; |
| 1682 | *params = nasm_realloc(*params, sizeof(**params) * paramsize); |
| 1683 | } |
| 1684 | skip_white_(t); |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1685 | brace = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1686 | if (tok_is_(t, "{")) |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1687 | brace++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1688 | (*params)[(*nparam)++] = t; |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1689 | if (brace) { |
| 1690 | while (brace && (t = t->next) != NULL) { |
| 1691 | if (tok_is_(t, "{")) |
| 1692 | brace++; |
| 1693 | else if (tok_is_(t, "}")) |
| 1694 | brace--; |
| 1695 | } |
| 1696 | |
| 1697 | if (t) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1698 | /* |
| 1699 | * Now we've found the closing brace, look further |
| 1700 | * for the comma. |
| 1701 | */ |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1702 | t = t->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1703 | skip_white_(t); |
| 1704 | if (tok_isnt_(t, ",")) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1705 | nasm_nonfatal("braces do not enclose all of macro parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1706 | while (tok_isnt_(t, ",")) |
| 1707 | t = t->next; |
| 1708 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1709 | } |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1710 | } else { |
| 1711 | while (tok_isnt_(t, ",")) |
| 1712 | t = t->next; |
| 1713 | } |
| 1714 | if (t) { /* got a comma/brace */ |
| 1715 | t = t->next; /* eat the comma */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1716 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1717 | } |
| 1718 | } |
| 1719 | |
| 1720 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1721 | * Determine whether one of the various `if' conditions is true or |
| 1722 | * not. |
| 1723 | * |
| 1724 | * We must free the tline we get passed. |
| 1725 | */ |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1726 | static bool if_condition(Token * tline, enum preproc_token ct) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1727 | { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1728 | enum pp_conditional i = PP_COND(ct); |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1729 | bool j; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1730 | Token *t, *tt, **tptr, *origline; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1731 | struct tokenval tokval; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1732 | expr *evalresult; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1733 | enum pp_token_type needtype; |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1734 | char *p; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1735 | |
| 1736 | origline = tline; |
| 1737 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1738 | switch (i) { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1739 | case PPC_IFCTX: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1740 | j = false; /* have we matched yet? */ |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1741 | while (true) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1742 | skip_white_(tline); |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1743 | if (!tline) |
| 1744 | break; |
| 1745 | if (tline->type != TOK_ID) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1746 | nasm_nonfatal("`%s' expects context identifiers", |
| 1747 | pp_directives[ct]); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1748 | free_tlist(origline); |
| 1749 | return -1; |
| 1750 | } |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1751 | if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1752 | j = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1753 | tline = tline->next; |
| 1754 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1755 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1756 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1757 | case PPC_IFDEF: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1758 | j = false; /* have we matched yet? */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1759 | while (tline) { |
| 1760 | skip_white_(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1761 | if (!tline || (tline->type != TOK_ID && |
| 1762 | (tline->type != TOK_PREPROC_ID || |
| 1763 | tline->text[1] != '$'))) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1764 | nasm_nonfatal("`%s' expects macro identifiers", |
| 1765 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1766 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1767 | } |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1768 | if (smacro_defined(NULL, tline->text, 0, NULL, true)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1769 | j = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1770 | tline = tline->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1771 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1772 | break; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1773 | |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1774 | case PPC_IFENV: |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1775 | tline = expand_smacro(tline); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1776 | j = false; /* have we matched yet? */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1777 | while (tline) { |
| 1778 | skip_white_(tline); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1779 | if (!tline || (tline->type != TOK_ID && |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1780 | tline->type != TOK_STRING && |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1781 | (tline->type != TOK_PREPROC_ID || |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1782 | tline->text[1] != '!'))) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1783 | nasm_nonfatal("`%s' expects environment variable names", |
| 1784 | pp_directives[ct]); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1785 | goto fail; |
| 1786 | } |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1787 | p = tline->text; |
| 1788 | if (tline->type == TOK_PREPROC_ID) |
| 1789 | p += 2; /* Skip leading %! */ |
H. Peter Anvin | 53e2e4c | 2018-11-28 15:01:40 -0800 | [diff] [blame] | 1790 | if (nasm_isquote(*p)) |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1791 | nasm_unquote_cstr(p, ct); |
| 1792 | if (getenv(p)) |
| 1793 | j = true; |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1794 | tline = tline->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1795 | } |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1796 | break; |
| 1797 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1798 | case PPC_IFIDN: |
| 1799 | case PPC_IFIDNI: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1800 | tline = expand_smacro(tline); |
| 1801 | t = tt = tline; |
| 1802 | while (tok_isnt_(tt, ",")) |
| 1803 | tt = tt->next; |
| 1804 | if (!tt) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1805 | nasm_nonfatal("`%s' expects two comma-separated arguments", |
| 1806 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1807 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1808 | } |
| 1809 | tt = tt->next; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1810 | j = true; /* assume equality unless proved not */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1811 | while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) { |
| 1812 | if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1813 | nasm_nonfatal("`%s': more than one comma on line", |
| 1814 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1815 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1816 | } |
| 1817 | if (t->type == TOK_WHITESPACE) { |
| 1818 | t = t->next; |
| 1819 | continue; |
| 1820 | } |
| 1821 | if (tt->type == TOK_WHITESPACE) { |
| 1822 | tt = tt->next; |
| 1823 | continue; |
| 1824 | } |
| 1825 | if (tt->type != t->type) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1826 | j = false; /* found mismatching tokens */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1827 | break; |
| 1828 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1829 | /* When comparing strings, need to unquote them first */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1830 | if (t->type == TOK_STRING) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1831 | size_t l1 = nasm_unquote(t->text, NULL); |
| 1832 | size_t l2 = nasm_unquote(tt->text, NULL); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 1833 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1834 | if (l1 != l2) { |
| 1835 | j = false; |
| 1836 | break; |
| 1837 | } |
| 1838 | if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) { |
| 1839 | j = false; |
| 1840 | break; |
| 1841 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1842 | } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1843 | j = false; /* found mismatching tokens */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1844 | break; |
| 1845 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1846 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1847 | t = t->next; |
| 1848 | tt = tt->next; |
| 1849 | } |
| 1850 | if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1851 | j = false; /* trailing gunk on one end or other */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1852 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1853 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1854 | case PPC_IFMACRO: |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1855 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1856 | bool found = false; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1857 | MMacro searching, *mmac; |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 1858 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1859 | skip_white_(tline); |
| 1860 | tline = expand_id(tline); |
| 1861 | if (!tok_type_(tline, TOK_ID)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1862 | nasm_nonfatal("`%s' expects a macro name", pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1863 | goto fail; |
| 1864 | } |
| 1865 | searching.name = nasm_strdup(tline->text); |
| 1866 | searching.casesense = true; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1867 | searching.plus = false; |
| 1868 | searching.nolist = false; |
| 1869 | searching.in_progress = 0; |
| 1870 | searching.max_depth = 0; |
| 1871 | searching.rep_nest = NULL; |
| 1872 | searching.nparam_min = 0; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1873 | searching.nparam_max = INT_MAX; |
| 1874 | tline = expand_smacro(tline->next); |
| 1875 | skip_white_(tline); |
| 1876 | if (!tline) { |
| 1877 | } else if (!tok_type_(tline, TOK_NUMBER)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1878 | nasm_nonfatal("`%s' expects a parameter count or nothing", |
| 1879 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1880 | } else { |
| 1881 | searching.nparam_min = searching.nparam_max = |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 1882 | read_param_count(tline->text); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1883 | } |
| 1884 | if (tline && tok_is_(tline->next, "-")) { |
| 1885 | tline = tline->next->next; |
| 1886 | if (tok_is_(tline, "*")) |
| 1887 | searching.nparam_max = INT_MAX; |
| 1888 | else if (!tok_type_(tline, TOK_NUMBER)) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1889 | nasm_nonfatal("`%s' expects a parameter count after `-'", |
| 1890 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1891 | else { |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 1892 | searching.nparam_max = read_param_count(tline->text); |
Cyrill Gorcunov | c9244ea | 2017-10-22 15:25:48 +0300 | [diff] [blame] | 1893 | if (searching.nparam_min > searching.nparam_max) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1894 | nasm_nonfatal("minimum parameter count exceeds maximum"); |
Cyrill Gorcunov | c9244ea | 2017-10-22 15:25:48 +0300 | [diff] [blame] | 1895 | searching.nparam_max = searching.nparam_min; |
| 1896 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1897 | } |
| 1898 | } |
| 1899 | if (tline && tok_is_(tline->next, "+")) { |
| 1900 | tline = tline->next; |
| 1901 | searching.plus = true; |
| 1902 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1903 | mmac = (MMacro *) hash_findix(&mmacros, searching.name); |
| 1904 | while (mmac) { |
| 1905 | if (!strcmp(mmac->name, searching.name) && |
| 1906 | (mmac->nparam_min <= searching.nparam_max |
| 1907 | || searching.plus) |
| 1908 | && (searching.nparam_min <= mmac->nparam_max |
| 1909 | || mmac->plus)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1910 | found = true; |
| 1911 | break; |
| 1912 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1913 | mmac = mmac->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1914 | } |
| 1915 | if (tline && tline->next) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1916 | nasm_warnf(ERR_PASS1, "trailing garbage after %%ifmacro ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1917 | nasm_free(searching.name); |
| 1918 | j = found; |
| 1919 | break; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1920 | } |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 1921 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1922 | case PPC_IFID: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1923 | needtype = TOK_ID; |
| 1924 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1925 | case PPC_IFNUM: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1926 | needtype = TOK_NUMBER; |
| 1927 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1928 | case PPC_IFSTR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1929 | needtype = TOK_STRING; |
| 1930 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1931 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1932 | iftype: |
| 1933 | t = tline = expand_smacro(tline); |
H. Peter Anvin | d85d250 | 2008-05-04 17:53:31 -0700 | [diff] [blame] | 1934 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1935 | while (tok_type_(t, TOK_WHITESPACE) || |
| 1936 | (needtype == TOK_NUMBER && |
| 1937 | tok_type_(t, TOK_OTHER) && |
| 1938 | (t->text[0] == '-' || t->text[0] == '+') && |
| 1939 | !t->text[1])) |
| 1940 | t = t->next; |
H. Peter Anvin | d85d250 | 2008-05-04 17:53:31 -0700 | [diff] [blame] | 1941 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1942 | j = tok_type_(t, needtype); |
| 1943 | break; |
H. Peter Anvin | cbf768d | 2008-02-16 16:41:25 -0800 | [diff] [blame] | 1944 | |
| 1945 | case PPC_IFTOKEN: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1946 | t = tline = expand_smacro(tline); |
| 1947 | while (tok_type_(t, TOK_WHITESPACE)) |
| 1948 | t = t->next; |
H. Peter Anvin | cbf768d | 2008-02-16 16:41:25 -0800 | [diff] [blame] | 1949 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1950 | j = false; |
| 1951 | if (t) { |
| 1952 | t = t->next; /* Skip the actual token */ |
| 1953 | while (tok_type_(t, TOK_WHITESPACE)) |
| 1954 | t = t->next; |
| 1955 | j = !t; /* Should be nothing left */ |
| 1956 | } |
| 1957 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1958 | |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 1959 | case PPC_IFEMPTY: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1960 | t = tline = expand_smacro(tline); |
| 1961 | while (tok_type_(t, TOK_WHITESPACE)) |
| 1962 | t = t->next; |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 1963 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1964 | j = !t; /* Should be empty */ |
| 1965 | break; |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 1966 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1967 | case PPC_IF: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1968 | t = tline = expand_smacro(tline); |
| 1969 | tptr = &t; |
| 1970 | tokval.t_type = TOKEN_INVALID; |
| 1971 | evalresult = evaluate(ppscan, tptr, &tokval, |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1972 | NULL, pass | CRITICAL, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1973 | if (!evalresult) |
| 1974 | return -1; |
| 1975 | if (tokval.t_type) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1976 | nasm_warnf(ERR_PASS1, "trailing garbage after expression ignored"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1977 | if (!is_simple(evalresult)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1978 | nasm_nonfatal("non-constant value given to `%s'", |
| 1979 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1980 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1981 | } |
Chuck Crayne | 60ae75d | 2007-05-02 01:59:16 +0000 | [diff] [blame] | 1982 | j = reloc_value(evalresult) != 0; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1983 | break; |
H. Peter Anvin | 95e2882 | 2007-09-12 04:20:08 +0000 | [diff] [blame] | 1984 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1985 | default: |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1986 | nasm_fatal("preprocessor directive `%s' not yet implemented", |
| 1987 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1988 | goto fail; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1989 | } |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1990 | |
| 1991 | free_tlist(origline); |
| 1992 | return j ^ PP_NEGATIVE(ct); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1993 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1994 | fail: |
| 1995 | free_tlist(origline); |
| 1996 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1997 | } |
| 1998 | |
| 1999 | /* |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2000 | * Common code for defining an smacro |
| 2001 | */ |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 2002 | static bool define_smacro(Context *ctx, const char *mname, bool casesense, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2003 | int nparam, Token *expansion) |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2004 | { |
| 2005 | SMacro *smac, **smhead; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2006 | struct hash_table *smtbl; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2007 | |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2008 | if (smacro_defined(ctx, mname, nparam, &smac, casesense)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2009 | if (!smac) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2010 | nasm_warnf(ERR_PASS1, "single-line macro `%s' defined both with and" |
| 2011 | " without parameters", mname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2012 | /* |
| 2013 | * Some instances of the old code considered this a failure, |
| 2014 | * some others didn't. What is the right thing to do here? |
| 2015 | */ |
| 2016 | free_tlist(expansion); |
| 2017 | return false; /* Failure */ |
| 2018 | } else { |
| 2019 | /* |
| 2020 | * We're redefining, so we have to take over an |
| 2021 | * existing SMacro structure. This means freeing |
| 2022 | * what was already in it. |
| 2023 | */ |
| 2024 | nasm_free(smac->name); |
| 2025 | free_tlist(smac->expansion); |
| 2026 | } |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2027 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2028 | smtbl = ctx ? &ctx->localmac : &smacros; |
| 2029 | smhead = (SMacro **) hash_findi_add(smtbl, mname); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2030 | smac = nasm_malloc(sizeof(SMacro)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2031 | smac->next = *smhead; |
| 2032 | *smhead = smac; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2033 | } |
| 2034 | smac->name = nasm_strdup(mname); |
| 2035 | smac->casesense = casesense; |
| 2036 | smac->nparam = nparam; |
| 2037 | smac->expansion = expansion; |
| 2038 | smac->in_progress = false; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2039 | return true; /* Success */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2040 | } |
| 2041 | |
| 2042 | /* |
| 2043 | * Undefine an smacro |
| 2044 | */ |
| 2045 | static void undef_smacro(Context *ctx, const char *mname) |
| 2046 | { |
| 2047 | SMacro **smhead, *s, **sp; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2048 | struct hash_table *smtbl; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2049 | |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2050 | smtbl = ctx ? &ctx->localmac : &smacros; |
| 2051 | smhead = (SMacro **)hash_findi(smtbl, mname, NULL); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2052 | |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2053 | if (smhead) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2054 | /* |
| 2055 | * We now have a macro name... go hunt for it. |
| 2056 | */ |
| 2057 | sp = smhead; |
| 2058 | while ((s = *sp) != NULL) { |
| 2059 | if (!mstrcmp(s->name, mname, s->casesense)) { |
| 2060 | *sp = s->next; |
| 2061 | nasm_free(s->name); |
| 2062 | free_tlist(s->expansion); |
| 2063 | nasm_free(s); |
| 2064 | } else { |
| 2065 | sp = &s->next; |
| 2066 | } |
| 2067 | } |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2068 | } |
| 2069 | } |
| 2070 | |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2071 | /* |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2072 | * Parse a mmacro specification. |
| 2073 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2074 | static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive) |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2075 | { |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2076 | tline = tline->next; |
| 2077 | skip_white_(tline); |
| 2078 | tline = expand_id(tline); |
| 2079 | if (!tok_type_(tline, TOK_ID)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2080 | nasm_nonfatal("`%s' expects a macro name", directive); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2081 | return false; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2082 | } |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2083 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2084 | def->prev = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2085 | def->name = nasm_strdup(tline->text); |
| 2086 | def->plus = false; |
| 2087 | def->nolist = false; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2088 | def->in_progress = 0; |
| 2089 | def->rep_nest = NULL; |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2090 | def->nparam_min = 0; |
| 2091 | def->nparam_max = 0; |
| 2092 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2093 | tline = expand_smacro(tline->next); |
| 2094 | skip_white_(tline); |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2095 | if (!tok_type_(tline, TOK_NUMBER)) |
| 2096 | nasm_nonfatal("`%s' expects a parameter count", directive); |
| 2097 | else |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 2098 | def->nparam_min = def->nparam_max = read_param_count(tline->text); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2099 | if (tline && tok_is_(tline->next, "-")) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2100 | tline = tline->next->next; |
| 2101 | if (tok_is_(tline, "*")) { |
| 2102 | def->nparam_max = INT_MAX; |
| 2103 | } else if (!tok_type_(tline, TOK_NUMBER)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2104 | nasm_nonfatal("`%s' expects a parameter count after `-'", directive); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2105 | } else { |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 2106 | def->nparam_max = read_param_count(tline->text); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2107 | if (def->nparam_min > def->nparam_max) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2108 | nasm_nonfatal("minimum parameter count exceeds maximum"); |
Cyrill Gorcunov | c9244ea | 2017-10-22 15:25:48 +0300 | [diff] [blame] | 2109 | def->nparam_max = def->nparam_min; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2110 | } |
| 2111 | } |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2112 | } |
| 2113 | if (tline && tok_is_(tline->next, "+")) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2114 | tline = tline->next; |
| 2115 | def->plus = true; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2116 | } |
| 2117 | if (tline && tok_type_(tline->next, TOK_ID) && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2118 | !nasm_stricmp(tline->next->text, ".nolist")) { |
| 2119 | tline = tline->next; |
| 2120 | def->nolist = true; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2121 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2122 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2123 | /* |
| 2124 | * Handle default parameters. |
| 2125 | */ |
| 2126 | if (tline && tline->next) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2127 | def->dlist = tline->next; |
| 2128 | tline->next = NULL; |
| 2129 | count_mmac_params(def->dlist, &def->ndefs, &def->defaults); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2130 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2131 | def->dlist = NULL; |
| 2132 | def->defaults = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2133 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2134 | def->expansion = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2135 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2136 | if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2137 | !def->plus) |
H. Peter Anvin | 1722fcf | 2018-11-28 13:05:42 -0800 | [diff] [blame] | 2138 | nasm_warnf(ERR_PASS1|ERR_WARN_MDP, "too many default macro parameters in macro `%s'", def->name); |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2139 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2140 | return true; |
| 2141 | } |
| 2142 | |
| 2143 | |
| 2144 | /* |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2145 | * Decode a size directive |
| 2146 | */ |
| 2147 | static int parse_size(const char *str) { |
| 2148 | static const char *size_names[] = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2149 | { "byte", "dword", "oword", "qword", "tword", "word", "yword" }; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2150 | static const int sizes[] = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2151 | { 0, 1, 4, 16, 8, 10, 2, 32 }; |
Cyrill Gorcunov | c713b5f | 2018-09-29 14:30:14 +0300 | [diff] [blame] | 2152 | return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2153 | } |
| 2154 | |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2155 | /* |
| 2156 | * Process a preprocessor %pragma directive. Currently there are none. |
| 2157 | * Gets passed the token list starting with the "preproc" token from |
| 2158 | * "%pragma preproc". |
| 2159 | */ |
| 2160 | static void do_pragma_preproc(Token *tline) |
| 2161 | { |
| 2162 | /* Skip to the real stuff */ |
| 2163 | tline = tline->next; |
| 2164 | skip_white_(tline); |
| 2165 | if (!tline) |
| 2166 | return; |
| 2167 | |
| 2168 | (void)tline; /* Nothing else to do at present */ |
| 2169 | } |
| 2170 | |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2171 | /** |
| 2172 | * find and process preprocessor directive in passed line |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2173 | * Find out if a line contains a preprocessor directive, and deal |
| 2174 | * with it if so. |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2175 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2176 | * If a directive _is_ found, it is the responsibility of this routine |
| 2177 | * (and not the caller) to free_tlist() the line. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2178 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2179 | * @param tline a pointer to the current tokeninzed line linked list |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2180 | * @param output if this directive generated output |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2181 | * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2182 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2183 | */ |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2184 | static int do_directive(Token *tline, char **output) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2185 | { |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2186 | enum preproc_token i; |
| 2187 | int j; |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 2188 | bool err; |
| 2189 | int nparam; |
| 2190 | bool nolist; |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 2191 | bool casesense; |
H. Peter Anvin | 8cfdb9d | 2007-09-14 18:36:01 -0700 | [diff] [blame] | 2192 | int k, m; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2193 | int offset; |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 2194 | char *p, *pp; |
| 2195 | const char *found_path; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 2196 | const char *mname; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2197 | Include *inc; |
| 2198 | Context *ctx; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2199 | Cond *cond; |
| 2200 | MMacro *mmac, **mmhead; |
Jin Kyu Song | c9486b9 | 2013-10-28 17:07:57 -0700 | [diff] [blame] | 2201 | Token *t = NULL, *tt, *param_start, *macro_start, *last, **tptr, *origline; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2202 | Line *l; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2203 | struct tokenval tokval; |
| 2204 | expr *evalresult; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2205 | MMacro *tmp_defining; /* Used when manipulating rep_nest */ |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 2206 | int64_t count; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 2207 | size_t len; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2208 | int severity; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2209 | |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2210 | *output = NULL; /* No output generated */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2211 | origline = tline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2212 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2213 | skip_white_(tline); |
H. Peter Anvin | f2936d7 | 2008-06-04 15:11:23 -0700 | [diff] [blame] | 2214 | if (!tline || !tok_type_(tline, TOK_PREPROC_ID) || |
Cyrill Gorcunov | 4b5b737 | 2018-10-29 22:54:08 +0300 | [diff] [blame] | 2215 | (tline->text[0] && (tline->text[1] == '%' || |
| 2216 | tline->text[1] == '$' || |
| 2217 | tline->text[1] == '!'))) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2218 | return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2219 | |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2220 | i = pp_token_hash(tline->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2221 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2222 | /* |
| 2223 | * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO |
| 2224 | * since they are known to be buggy at moment, we need to fix them |
| 2225 | * in future release (2.09-2.10) |
| 2226 | */ |
Philipp Kloke | b432f57 | 2013-03-31 12:01:23 +0200 | [diff] [blame] | 2227 | if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2228 | nasm_nonfatal("unknown preprocessor directive `%s'", tline->text); |
| 2229 | return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2230 | } |
| 2231 | |
| 2232 | /* |
| 2233 | * If we're in a non-emitting branch of a condition construct, |
| 2234 | * or walking to the end of an already terminated %rep block, |
| 2235 | * we should ignore all directives except for condition |
| 2236 | * directives. |
| 2237 | */ |
| 2238 | if (((istk->conds && !emitting(istk->conds->state)) || |
| 2239 | (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) { |
| 2240 | return NO_DIRECTIVE_FOUND; |
| 2241 | } |
| 2242 | |
| 2243 | /* |
| 2244 | * If we're defining a macro or reading a %rep block, we should |
| 2245 | * ignore all directives except for %macro/%imacro (which nest), |
| 2246 | * %endm/%endmacro, and (only if we're in a %rep block) %endrep. |
| 2247 | * If we're in a %rep block, another %rep nests, so should be let through. |
| 2248 | */ |
| 2249 | if (defining && i != PP_MACRO && i != PP_IMACRO && |
| 2250 | i != PP_RMACRO && i != PP_IRMACRO && |
| 2251 | i != PP_ENDMACRO && i != PP_ENDM && |
| 2252 | (defining->name || (i != PP_ENDREP && i != PP_REP))) { |
| 2253 | return NO_DIRECTIVE_FOUND; |
| 2254 | } |
| 2255 | |
| 2256 | if (defining) { |
| 2257 | if (i == PP_MACRO || i == PP_IMACRO || |
| 2258 | i == PP_RMACRO || i == PP_IRMACRO) { |
| 2259 | nested_mac_count++; |
| 2260 | return NO_DIRECTIVE_FOUND; |
| 2261 | } else if (nested_mac_count > 0) { |
| 2262 | if (i == PP_ENDMACRO) { |
| 2263 | nested_mac_count--; |
| 2264 | return NO_DIRECTIVE_FOUND; |
| 2265 | } |
| 2266 | } |
| 2267 | if (!defining->name) { |
| 2268 | if (i == PP_REP) { |
| 2269 | nested_rep_count++; |
| 2270 | return NO_DIRECTIVE_FOUND; |
| 2271 | } else if (nested_rep_count > 0) { |
| 2272 | if (i == PP_ENDREP) { |
| 2273 | nested_rep_count--; |
| 2274 | return NO_DIRECTIVE_FOUND; |
| 2275 | } |
| 2276 | } |
| 2277 | } |
| 2278 | } |
| 2279 | |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2280 | switch (i) { |
| 2281 | case PP_INVALID: |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2282 | nasm_nonfatal("unknown preprocessor directive `%s'", tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2283 | return NO_DIRECTIVE_FOUND; /* didn't get it */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2284 | |
H. Peter Anvin | 3f87a2a | 2016-10-04 14:07:19 -0700 | [diff] [blame] | 2285 | case PP_PRAGMA: |
| 2286 | /* |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2287 | * %pragma namespace options... |
| 2288 | * |
| 2289 | * The namespace "preproc" is reserved for the preprocessor; |
| 2290 | * all other namespaces generate a [pragma] assembly directive. |
| 2291 | * |
| 2292 | * Invalid %pragmas are ignored and may have different |
| 2293 | * meaning in future versions of NASM. |
H. Peter Anvin | 3f87a2a | 2016-10-04 14:07:19 -0700 | [diff] [blame] | 2294 | */ |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2295 | tline = tline->next; |
| 2296 | skip_white_(tline); |
| 2297 | tline = expand_smacro(tline); |
| 2298 | if (tok_type_(tline, TOK_ID)) { |
| 2299 | if (!nasm_stricmp(tline->text, "preproc")) { |
| 2300 | /* Preprocessor pragma */ |
| 2301 | do_pragma_preproc(tline); |
| 2302 | } else { |
| 2303 | /* Build the assembler directive */ |
| 2304 | t = new_Token(NULL, TOK_OTHER, "[", 1); |
| 2305 | t->next = new_Token(NULL, TOK_ID, "pragma", 6); |
| 2306 | t->next->next = new_Token(tline, TOK_WHITESPACE, NULL, 0); |
| 2307 | tline = t; |
| 2308 | for (t = tline; t->next; t = t->next) |
| 2309 | ; |
| 2310 | t->next = new_Token(NULL, TOK_OTHER, "]", 1); |
| 2311 | /* true here can be revisited in the future */ |
| 2312 | *output = detoken(tline, true); |
| 2313 | } |
| 2314 | } |
H. Peter Anvin | 3f87a2a | 2016-10-04 14:07:19 -0700 | [diff] [blame] | 2315 | free_tlist(origline); |
| 2316 | return DIRECTIVE_FOUND; |
| 2317 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2318 | case PP_STACKSIZE: |
| 2319 | /* Directive to tell NASM what the default stack size is. The |
| 2320 | * default is for a 16-bit stack, and this can be overriden with |
| 2321 | * %stacksize large. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2322 | */ |
| 2323 | tline = tline->next; |
| 2324 | if (tline && tline->type == TOK_WHITESPACE) |
| 2325 | tline = tline->next; |
| 2326 | if (!tline || tline->type != TOK_ID) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2327 | nasm_nonfatal("`%%stacksize' missing size parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2328 | free_tlist(origline); |
| 2329 | return DIRECTIVE_FOUND; |
| 2330 | } |
| 2331 | if (nasm_stricmp(tline->text, "flat") == 0) { |
| 2332 | /* All subsequent ARG directives are for a 32-bit stack */ |
| 2333 | StackSize = 4; |
| 2334 | StackPointer = "ebp"; |
| 2335 | ArgOffset = 8; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2336 | LocalOffset = 0; |
Charles Crayne | 7eaf919 | 2007-11-08 22:11:14 -0800 | [diff] [blame] | 2337 | } else if (nasm_stricmp(tline->text, "flat64") == 0) { |
| 2338 | /* All subsequent ARG directives are for a 64-bit stack */ |
| 2339 | StackSize = 8; |
| 2340 | StackPointer = "rbp"; |
Per Jessen | 53252e0 | 2010-02-11 00:16:59 +0300 | [diff] [blame] | 2341 | ArgOffset = 16; |
Charles Crayne | 7eaf919 | 2007-11-08 22:11:14 -0800 | [diff] [blame] | 2342 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2343 | } else if (nasm_stricmp(tline->text, "large") == 0) { |
| 2344 | /* All subsequent ARG directives are for a 16-bit stack, |
| 2345 | * far function call. |
| 2346 | */ |
| 2347 | StackSize = 2; |
| 2348 | StackPointer = "bp"; |
| 2349 | ArgOffset = 4; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2350 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2351 | } else if (nasm_stricmp(tline->text, "small") == 0) { |
| 2352 | /* All subsequent ARG directives are for a 16-bit stack, |
| 2353 | * far function call. We don't support near functions. |
| 2354 | */ |
| 2355 | StackSize = 2; |
| 2356 | StackPointer = "bp"; |
| 2357 | ArgOffset = 6; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2358 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2359 | } else { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2360 | nasm_nonfatal("`%%stacksize' invalid size type"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2361 | free_tlist(origline); |
| 2362 | return DIRECTIVE_FOUND; |
| 2363 | } |
| 2364 | free_tlist(origline); |
| 2365 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2366 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2367 | case PP_ARG: |
| 2368 | /* TASM like ARG directive to define arguments to functions, in |
| 2369 | * the following form: |
| 2370 | * |
| 2371 | * ARG arg1:WORD, arg2:DWORD, arg4:QWORD |
| 2372 | */ |
| 2373 | offset = ArgOffset; |
| 2374 | do { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 2375 | char *arg, directive[256]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2376 | int size = StackSize; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2377 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2378 | /* Find the argument name */ |
| 2379 | tline = tline->next; |
| 2380 | if (tline && tline->type == TOK_WHITESPACE) |
| 2381 | tline = tline->next; |
| 2382 | if (!tline || tline->type != TOK_ID) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2383 | nasm_nonfatal("`%%arg' missing argument parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2384 | free_tlist(origline); |
| 2385 | return DIRECTIVE_FOUND; |
| 2386 | } |
| 2387 | arg = tline->text; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2388 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2389 | /* Find the argument size type */ |
| 2390 | tline = tline->next; |
| 2391 | if (!tline || tline->type != TOK_OTHER |
| 2392 | || tline->text[0] != ':') { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2393 | nasm_nonfatal("Syntax error processing `%%arg' directive"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2394 | free_tlist(origline); |
| 2395 | return DIRECTIVE_FOUND; |
| 2396 | } |
| 2397 | tline = tline->next; |
| 2398 | if (!tline || tline->type != TOK_ID) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2399 | nasm_nonfatal("`%%arg' missing size type parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2400 | free_tlist(origline); |
| 2401 | return DIRECTIVE_FOUND; |
| 2402 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2403 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2404 | /* Allow macro expansion of type parameter */ |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2405 | tt = tokenize(tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2406 | tt = expand_smacro(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2407 | size = parse_size(tt->text); |
| 2408 | if (!size) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2409 | nasm_nonfatal("Invalid size type for `%%arg' missing directive"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2410 | free_tlist(tt); |
| 2411 | free_tlist(origline); |
| 2412 | return DIRECTIVE_FOUND; |
| 2413 | } |
| 2414 | free_tlist(tt); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2415 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2416 | /* Round up to even stack slots */ |
| 2417 | size = ALIGN(size, StackSize); |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2418 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2419 | /* Now define the macro for the argument */ |
| 2420 | snprintf(directive, sizeof(directive), "%%define %s (%s+%d)", |
| 2421 | arg, StackPointer, offset); |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2422 | do_directive(tokenize(directive), output); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2423 | offset += size; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2424 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2425 | /* Move to the next argument in the list */ |
| 2426 | tline = tline->next; |
| 2427 | if (tline && tline->type == TOK_WHITESPACE) |
| 2428 | tline = tline->next; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2429 | } while (tline && tline->type == TOK_OTHER && tline->text[0] == ','); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2430 | ArgOffset = offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2431 | free_tlist(origline); |
| 2432 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2433 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2434 | case PP_LOCAL: |
| 2435 | /* TASM like LOCAL directive to define local variables for a |
| 2436 | * function, in the following form: |
| 2437 | * |
| 2438 | * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize |
| 2439 | * |
| 2440 | * The '= LocalSize' at the end is ignored by NASM, but is |
| 2441 | * required by TASM to define the local parameter size (and used |
| 2442 | * by the TASM macro package). |
| 2443 | */ |
| 2444 | offset = LocalOffset; |
| 2445 | do { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 2446 | char *local, directive[256]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2447 | int size = StackSize; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2448 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2449 | /* Find the argument name */ |
| 2450 | tline = tline->next; |
| 2451 | if (tline && tline->type == TOK_WHITESPACE) |
| 2452 | tline = tline->next; |
| 2453 | if (!tline || tline->type != TOK_ID) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2454 | nasm_nonfatal("`%%local' missing argument parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2455 | free_tlist(origline); |
| 2456 | return DIRECTIVE_FOUND; |
| 2457 | } |
| 2458 | local = tline->text; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2459 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2460 | /* Find the argument size type */ |
| 2461 | tline = tline->next; |
| 2462 | if (!tline || tline->type != TOK_OTHER |
| 2463 | || tline->text[0] != ':') { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2464 | nasm_nonfatal("Syntax error processing `%%local' directive"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2465 | free_tlist(origline); |
| 2466 | return DIRECTIVE_FOUND; |
| 2467 | } |
| 2468 | tline = tline->next; |
| 2469 | if (!tline || tline->type != TOK_ID) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2470 | nasm_nonfatal("`%%local' missing size type parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2471 | free_tlist(origline); |
| 2472 | return DIRECTIVE_FOUND; |
| 2473 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2474 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2475 | /* Allow macro expansion of type parameter */ |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2476 | tt = tokenize(tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2477 | tt = expand_smacro(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2478 | size = parse_size(tt->text); |
| 2479 | if (!size) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2480 | nasm_nonfatal("Invalid size type for `%%local' missing directive"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2481 | free_tlist(tt); |
| 2482 | free_tlist(origline); |
| 2483 | return DIRECTIVE_FOUND; |
| 2484 | } |
| 2485 | free_tlist(tt); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2486 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2487 | /* Round up to even stack slots */ |
| 2488 | size = ALIGN(size, StackSize); |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2489 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2490 | offset += size; /* Negative offset, increment before */ |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2491 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2492 | /* Now define the macro for the argument */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2493 | snprintf(directive, sizeof(directive), "%%define %s (%s-%d)", |
| 2494 | local, StackPointer, offset); |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2495 | do_directive(tokenize(directive), output); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2496 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2497 | /* Now define the assign to setup the enter_c macro correctly */ |
| 2498 | snprintf(directive, sizeof(directive), |
| 2499 | "%%assign %%$localsize %%$localsize+%d", size); |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2500 | do_directive(tokenize(directive), output); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2501 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2502 | /* Move to the next argument in the list */ |
| 2503 | tline = tline->next; |
| 2504 | if (tline && tline->type == TOK_WHITESPACE) |
| 2505 | tline = tline->next; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2506 | } while (tline && tline->type == TOK_OTHER && tline->text[0] == ','); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2507 | LocalOffset = offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2508 | free_tlist(origline); |
| 2509 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2510 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2511 | case PP_CLEAR: |
| 2512 | if (tline->next) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2513 | nasm_warnf(ERR_PASS1, "trailing garbage after `%%clear' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2514 | free_macros(); |
| 2515 | init_macros(); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2516 | free_tlist(origline); |
| 2517 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2518 | |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2519 | case PP_DEPEND: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2520 | t = tline->next = expand_smacro(tline->next); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2521 | skip_white_(t); |
| 2522 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2523 | t->type != TOK_INTERNAL_STRING)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2524 | nasm_nonfatal("`%%depend' expects a file name"); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2525 | free_tlist(origline); |
| 2526 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2527 | } |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2528 | if (t->next) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2529 | nasm_warnf(ERR_PASS1, "trailing garbage after `%%depend' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2530 | p = t->text; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2531 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2532 | nasm_unquote_cstr(p, i); |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 2533 | strlist_add(deplist, p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2534 | free_tlist(origline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2535 | return DIRECTIVE_FOUND; |
| 2536 | |
| 2537 | case PP_INCLUDE: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2538 | t = tline->next = expand_smacro(tline->next); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2539 | skip_white_(t); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2540 | |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2541 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2542 | t->type != TOK_INTERNAL_STRING)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2543 | nasm_nonfatal("`%%include' expects a file name"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2544 | free_tlist(origline); |
| 2545 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2546 | } |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2547 | if (t->next) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2548 | nasm_warnf(ERR_PASS1, "trailing garbage after `%%include' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2549 | p = t->text; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2550 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2551 | nasm_unquote_cstr(p, i); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2552 | inc = nasm_malloc(sizeof(Include)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2553 | inc->next = istk; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2554 | inc->conds = NULL; |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 2555 | found_path = NULL; |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 2556 | inc->fp = inc_fopen(p, deplist, &found_path, |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 2557 | pass == 0 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2558 | if (!inc->fp) { |
| 2559 | /* -MG given but file not found */ |
| 2560 | nasm_free(inc); |
| 2561 | } else { |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 2562 | inc->fname = src_set_fname(found_path ? found_path : p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2563 | inc->lineno = src_set_linnum(0); |
| 2564 | inc->lineinc = 1; |
| 2565 | inc->expansion = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2566 | inc->mstk = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2567 | istk = inc; |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 2568 | lfmt->uplevel(LIST_INCLUDE); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2569 | } |
| 2570 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2571 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2572 | |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2573 | case PP_USE: |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2574 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2575 | static macros_t *use_pkg; |
| 2576 | const char *pkg_macro = NULL; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2577 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2578 | tline = tline->next; |
| 2579 | skip_white_(tline); |
| 2580 | tline = expand_id(tline); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2581 | |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2582 | if (!tline || (tline->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2583 | tline->type != TOK_INTERNAL_STRING && |
| 2584 | tline->type != TOK_ID)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2585 | nasm_nonfatal("`%%use' expects a package name"); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2586 | free_tlist(origline); |
| 2587 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2588 | } |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2589 | if (tline->next) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2590 | nasm_warnf(ERR_PASS1, "trailing garbage after `%%use' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2591 | if (tline->type == TOK_STRING) |
| 2592 | nasm_unquote_cstr(tline->text, i); |
| 2593 | use_pkg = nasm_stdmac_find_package(tline->text); |
| 2594 | if (!use_pkg) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2595 | nasm_nonfatal("unknown `%%use' package: %s", tline->text); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2596 | else |
| 2597 | 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] | 2598 | if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2599 | /* Not already included, go ahead and include it */ |
| 2600 | stdmacpos = use_pkg; |
| 2601 | } |
| 2602 | free_tlist(origline); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2603 | return DIRECTIVE_FOUND; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2604 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2605 | case PP_PUSH: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2606 | case PP_REPL: |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2607 | case PP_POP: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2608 | tline = tline->next; |
| 2609 | skip_white_(tline); |
| 2610 | tline = expand_id(tline); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2611 | if (tline) { |
| 2612 | if (!tok_type_(tline, TOK_ID)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2613 | nasm_nonfatal("`%s' expects a context identifier", |
| 2614 | pp_directives[i]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2615 | free_tlist(origline); |
| 2616 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2617 | } |
| 2618 | if (tline->next) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2619 | nasm_warnf(ERR_PASS1, "trailing garbage after `%s' ignored", |
| 2620 | pp_directives[i]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2621 | p = nasm_strdup(tline->text); |
| 2622 | } else { |
| 2623 | p = NULL; /* Anonymous */ |
| 2624 | } |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2625 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2626 | if (i == PP_PUSH) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2627 | ctx = nasm_malloc(sizeof(Context)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2628 | ctx->next = cstk; |
| 2629 | hash_init(&ctx->localmac, HASH_SMALL); |
| 2630 | ctx->name = p; |
| 2631 | ctx->number = unique++; |
| 2632 | cstk = ctx; |
| 2633 | } else { |
| 2634 | /* %pop or %repl */ |
| 2635 | if (!cstk) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2636 | nasm_nonfatal("`%s': context stack is empty", |
| 2637 | pp_directives[i]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2638 | } else if (i == PP_POP) { |
| 2639 | if (p && (!cstk->name || nasm_stricmp(p, cstk->name))) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2640 | nasm_nonfatal("`%%pop' in wrong context: %s, expected %s", |
| 2641 | cstk->name ? cstk->name : "anonymous", p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2642 | else |
| 2643 | ctx_pop(); |
| 2644 | } else { |
| 2645 | /* i == PP_REPL */ |
| 2646 | nasm_free(cstk->name); |
| 2647 | cstk->name = p; |
| 2648 | p = NULL; |
| 2649 | } |
| 2650 | nasm_free(p); |
| 2651 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2652 | free_tlist(origline); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2653 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2654 | case PP_FATAL: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2655 | severity = ERR_FATAL; |
| 2656 | goto issue_error; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2657 | case PP_ERROR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2658 | severity = ERR_NONFATAL; |
| 2659 | goto issue_error; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2660 | case PP_WARNING: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2661 | severity = ERR_WARNING|ERR_WARN_USER; |
| 2662 | goto issue_error; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2663 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2664 | issue_error: |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2665 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2666 | /* Only error out if this is the final pass */ |
| 2667 | if (pass != 2 && i != PP_FATAL) |
| 2668 | return DIRECTIVE_FOUND; |
| 2669 | |
| 2670 | tline->next = expand_smacro(tline->next); |
| 2671 | tline = tline->next; |
| 2672 | skip_white_(tline); |
| 2673 | t = tline ? tline->next : NULL; |
| 2674 | skip_white_(t); |
| 2675 | if (tok_type_(tline, TOK_STRING) && !t) { |
| 2676 | /* The line contains only a quoted string */ |
| 2677 | p = tline->text; |
| 2678 | nasm_unquote(p, NULL); /* Ignore NUL character truncation */ |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2679 | nasm_error(severity, "%s", p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2680 | } else { |
| 2681 | /* Not a quoted string, or more than a quoted string */ |
| 2682 | p = detoken(tline, false); |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2683 | nasm_error(severity, "%s", p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2684 | nasm_free(p); |
| 2685 | } |
| 2686 | free_tlist(origline); |
| 2687 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2688 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2689 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2690 | CASE_PP_IF: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2691 | if (istk->conds && !emitting(istk->conds->state)) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2692 | j = COND_NEVER; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2693 | else { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2694 | j = if_condition(tline->next, i); |
| 2695 | tline->next = NULL; /* it got freed */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2696 | j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2697 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2698 | cond = nasm_malloc(sizeof(Cond)); |
| 2699 | cond->next = istk->conds; |
| 2700 | cond->state = j; |
| 2701 | istk->conds = cond; |
| 2702 | if(istk->mstk) |
| 2703 | istk->mstk->condcnt ++; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2704 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2705 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2706 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2707 | CASE_PP_ELIF: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2708 | if (!istk->conds) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2709 | nasm_fatal("`%s': no matching `%%if'", pp_directives[i]); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2710 | switch(istk->conds->state) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2711 | case COND_IF_TRUE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2712 | istk->conds->state = COND_DONE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2713 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2714 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2715 | case COND_DONE: |
| 2716 | case COND_NEVER: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2717 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2718 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2719 | case COND_ELSE_TRUE: |
| 2720 | case COND_ELSE_FALSE: |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2721 | nasm_warnf(ERR_PASS1|ERR_PP_PRECOND, |
| 2722 | "`%%elif' after `%%else' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2723 | istk->conds->state = COND_NEVER; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2724 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2725 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2726 | case COND_IF_FALSE: |
| 2727 | /* |
| 2728 | * IMPORTANT: In the case of %if, we will already have |
| 2729 | * called expand_mmac_params(); however, if we're |
| 2730 | * processing an %elif we must have been in a |
| 2731 | * non-emitting mode, which would have inhibited |
| 2732 | * the normal invocation of expand_mmac_params(). |
| 2733 | * Therefore, we have to do it explicitly here. |
| 2734 | */ |
| 2735 | j = if_condition(expand_mmac_params(tline->next), i); |
| 2736 | tline->next = NULL; /* it got freed */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2737 | istk->conds->state = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2738 | j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; |
| 2739 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2740 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2741 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2742 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2743 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2744 | case PP_ELSE: |
| 2745 | if (tline->next) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2746 | nasm_warnf(ERR_PASS1|ERR_PP_PRECOND, |
| 2747 | "trailing garbage after `%%else' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2748 | if (!istk->conds) |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 2749 | nasm_fatal("`%%else: no matching `%%if'"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2750 | switch(istk->conds->state) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2751 | case COND_IF_TRUE: |
| 2752 | case COND_DONE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2753 | istk->conds->state = COND_ELSE_FALSE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2754 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2755 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2756 | case COND_NEVER: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2757 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2758 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2759 | case COND_IF_FALSE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2760 | istk->conds->state = COND_ELSE_TRUE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2761 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2762 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2763 | case COND_ELSE_TRUE: |
| 2764 | case COND_ELSE_FALSE: |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2765 | nasm_warnf(ERR_PASS1|ERR_PP_PRECOND, |
| 2766 | "`%%else' after `%%else' ignored."); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2767 | istk->conds->state = COND_NEVER; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2768 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2769 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2770 | free_tlist(origline); |
| 2771 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2772 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2773 | case PP_ENDIF: |
| 2774 | if (tline->next) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2775 | nasm_warnf(ERR_PASS1|ERR_PP_PRECOND, |
| 2776 | "trailing garbage after `%%endif' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2777 | if (!istk->conds) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2778 | nasm_fatal("`%%endif': no matching `%%if'"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2779 | cond = istk->conds; |
| 2780 | istk->conds = cond->next; |
| 2781 | nasm_free(cond); |
| 2782 | if(istk->mstk) |
| 2783 | istk->mstk->condcnt --; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2784 | free_tlist(origline); |
| 2785 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2786 | |
H. Peter Anvin | db8f96e | 2009-07-15 09:07:29 -0400 | [diff] [blame] | 2787 | case PP_RMACRO: |
| 2788 | case PP_IRMACRO: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2789 | case PP_MACRO: |
| 2790 | case PP_IMACRO: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2791 | if (defining) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2792 | nasm_fatal("`%s': already defining a macro", |
| 2793 | pp_directives[i]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2794 | return DIRECTIVE_FOUND; |
| 2795 | } |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 2796 | defining = nasm_zalloc(sizeof(MMacro)); |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 2797 | defining->max_depth = ((i == PP_RMACRO) || (i == PP_IRMACRO)) |
| 2798 | ? nasm_limit[LIMIT_MACROS] : 0; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2799 | defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO); |
| 2800 | if (!parse_mmacro_spec(tline, defining, pp_directives[i])) { |
| 2801 | nasm_free(defining); |
| 2802 | defining = NULL; |
| 2803 | return DIRECTIVE_FOUND; |
| 2804 | } |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2805 | |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 2806 | src_get(&defining->xline, &defining->fname); |
| 2807 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2808 | mmac = (MMacro *) hash_findix(&mmacros, defining->name); |
| 2809 | while (mmac) { |
| 2810 | if (!strcmp(mmac->name, defining->name) && |
| 2811 | (mmac->nparam_min <= defining->nparam_max |
| 2812 | || defining->plus) |
| 2813 | && (defining->nparam_min <= mmac->nparam_max |
| 2814 | || mmac->plus)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2815 | nasm_warnf(ERR_PASS1, "redefining multi-line macro `%s'", |
| 2816 | defining->name); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2817 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2818 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2819 | mmac = mmac->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2820 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2821 | free_tlist(origline); |
| 2822 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2823 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2824 | case PP_ENDM: |
| 2825 | case PP_ENDMACRO: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2826 | if (! (defining && defining->name)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2827 | nasm_nonfatal("`%s': not defining a macro", tline->text); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2828 | return DIRECTIVE_FOUND; |
| 2829 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2830 | mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name); |
| 2831 | defining->next = *mmhead; |
| 2832 | *mmhead = defining; |
| 2833 | defining = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2834 | free_tlist(origline); |
| 2835 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2836 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2837 | case PP_EXITMACRO: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2838 | /* |
| 2839 | * We must search along istk->expansion until we hit a |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2840 | * macro-end marker for a macro with a name. Then we |
| 2841 | * bypass all lines between exitmacro and endmacro. |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2842 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2843 | list_for_each(l, istk->expansion) |
| 2844 | if (l->finishes && l->finishes->name) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2845 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2846 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2847 | if (l) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2848 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2849 | * Remove all conditional entries relative to this |
| 2850 | * macro invocation. (safe to do in this context) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2851 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2852 | for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) { |
| 2853 | cond = istk->conds; |
| 2854 | istk->conds = cond->next; |
| 2855 | nasm_free(cond); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2856 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2857 | istk->expansion = l; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2858 | } else { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2859 | nasm_nonfatal("`%%exitmacro' not within `%%macro' block"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2860 | } |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 2861 | free_tlist(origline); |
| 2862 | return DIRECTIVE_FOUND; |
| 2863 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2864 | case PP_UNMACRO: |
| 2865 | case PP_UNIMACRO: |
| 2866 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2867 | MMacro **mmac_p; |
| 2868 | MMacro spec; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2869 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2870 | spec.casesense = (i == PP_UNMACRO); |
| 2871 | if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) { |
| 2872 | return DIRECTIVE_FOUND; |
| 2873 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2874 | mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL); |
| 2875 | while (mmac_p && *mmac_p) { |
| 2876 | mmac = *mmac_p; |
| 2877 | if (mmac->casesense == spec.casesense && |
| 2878 | !mstrcmp(mmac->name, spec.name, spec.casesense) && |
| 2879 | mmac->nparam_min == spec.nparam_min && |
| 2880 | mmac->nparam_max == spec.nparam_max && |
| 2881 | mmac->plus == spec.plus) { |
| 2882 | *mmac_p = mmac->next; |
| 2883 | free_mmacro(mmac); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2884 | } else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2885 | mmac_p = &mmac->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2886 | } |
| 2887 | } |
| 2888 | free_tlist(origline); |
| 2889 | free_tlist(spec.dlist); |
| 2890 | return DIRECTIVE_FOUND; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2891 | } |
| 2892 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2893 | case PP_ROTATE: |
| 2894 | if (tline->next && tline->next->type == TOK_WHITESPACE) |
| 2895 | tline = tline->next; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2896 | if (!tline->next) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2897 | free_tlist(origline); |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2898 | nasm_nonfatal("`%%rotate' missing rotate count"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2899 | return DIRECTIVE_FOUND; |
| 2900 | } |
| 2901 | t = expand_smacro(tline->next); |
| 2902 | tline->next = NULL; |
| 2903 | free_tlist(origline); |
| 2904 | tline = t; |
| 2905 | tptr = &t; |
| 2906 | tokval.t_type = TOKEN_INVALID; |
| 2907 | evalresult = |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2908 | evaluate(ppscan, tptr, &tokval, NULL, pass, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2909 | free_tlist(tline); |
| 2910 | if (!evalresult) |
| 2911 | return DIRECTIVE_FOUND; |
| 2912 | if (tokval.t_type) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2913 | nasm_warnf(ERR_PASS1, "trailing garbage after expression ignored"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2914 | if (!is_simple(evalresult)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2915 | nasm_nonfatal("non-constant value given to `%%rotate'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2916 | return DIRECTIVE_FOUND; |
| 2917 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2918 | mmac = istk->mstk; |
| 2919 | while (mmac && !mmac->name) /* avoid mistaking %reps for macros */ |
| 2920 | mmac = mmac->next_active; |
| 2921 | if (!mmac) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2922 | nasm_nonfatal("`%%rotate' invoked outside a macro call"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2923 | } else if (mmac->nparam == 0) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2924 | nasm_nonfatal("`%%rotate' invoked within macro without parameters"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2925 | } else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2926 | int rotate = mmac->rotate + reloc_value(evalresult); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2927 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2928 | rotate %= (int)mmac->nparam; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2929 | if (rotate < 0) |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2930 | rotate += mmac->nparam; |
| 2931 | |
| 2932 | mmac->rotate = rotate; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2933 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2934 | return DIRECTIVE_FOUND; |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 2935 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2936 | case PP_REP: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 2937 | nolist = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2938 | do { |
| 2939 | tline = tline->next; |
| 2940 | } while (tok_type_(tline, TOK_WHITESPACE)); |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 2941 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2942 | if (tok_type_(tline, TOK_ID) && |
| 2943 | nasm_stricmp(tline->text, ".nolist") == 0) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 2944 | nolist = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2945 | do { |
| 2946 | tline = tline->next; |
| 2947 | } while (tok_type_(tline, TOK_WHITESPACE)); |
| 2948 | } |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 2949 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2950 | if (tline) { |
| 2951 | t = expand_smacro(tline); |
| 2952 | tptr = &t; |
| 2953 | tokval.t_type = TOKEN_INVALID; |
| 2954 | evalresult = |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2955 | evaluate(ppscan, tptr, &tokval, NULL, pass, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2956 | if (!evalresult) { |
| 2957 | free_tlist(origline); |
| 2958 | return DIRECTIVE_FOUND; |
| 2959 | } |
| 2960 | if (tokval.t_type) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2961 | nasm_warnf(ERR_PASS1, "trailing garbage after expression ignored"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2962 | if (!is_simple(evalresult)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2963 | nasm_nonfatal("non-constant value given to `%%rep'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2964 | return DIRECTIVE_FOUND; |
| 2965 | } |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 2966 | count = reloc_value(evalresult); |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 2967 | if (count > nasm_limit[LIMIT_REP]) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2968 | nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")", |
| 2969 | count, nasm_limit[LIMIT_REP]); |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 2970 | count = 0; |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 2971 | } else if (count < 0) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2972 | nasm_warnf(ERR_PASS2|ERR_WARN_NEG_REP, |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 2973 | "negative `%%rep' count: %"PRId64, count); |
| 2974 | count = 0; |
| 2975 | } else { |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 2976 | count++; |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 2977 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2978 | } else { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2979 | nasm_nonfatal("`%%rep' expects a repeat count"); |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 2980 | count = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2981 | } |
| 2982 | free_tlist(origline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2983 | |
| 2984 | tmp_defining = defining; |
| 2985 | defining = nasm_malloc(sizeof(MMacro)); |
| 2986 | defining->prev = NULL; |
| 2987 | defining->name = NULL; /* flags this macro as a %rep block */ |
| 2988 | defining->casesense = false; |
| 2989 | defining->plus = false; |
| 2990 | defining->nolist = nolist; |
| 2991 | defining->in_progress = count; |
| 2992 | defining->max_depth = 0; |
| 2993 | defining->nparam_min = defining->nparam_max = 0; |
| 2994 | defining->defaults = NULL; |
| 2995 | defining->dlist = NULL; |
| 2996 | defining->expansion = NULL; |
| 2997 | defining->next_active = istk->mstk; |
| 2998 | defining->rep_nest = tmp_defining; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2999 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3000 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3001 | case PP_ENDREP: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3002 | if (!defining || defining->name) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3003 | nasm_nonfatal("`%%endrep': no matching `%%rep'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3004 | return DIRECTIVE_FOUND; |
| 3005 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3006 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3007 | /* |
| 3008 | * Now we have a "macro" defined - although it has no name |
| 3009 | * and we won't be entering it in the hash tables - we must |
| 3010 | * push a macro-end marker for it on to istk->expansion. |
| 3011 | * After that, it will take care of propagating itself (a |
| 3012 | * macro-end marker line for a macro which is really a %rep |
| 3013 | * block will cause the macro to be re-expanded, complete |
| 3014 | * with another macro-end marker to ensure the process |
| 3015 | * continues) until the whole expansion is forcibly removed |
| 3016 | * from istk->expansion by a %exitrep. |
| 3017 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3018 | l = nasm_malloc(sizeof(Line)); |
| 3019 | l->next = istk->expansion; |
| 3020 | l->finishes = defining; |
| 3021 | l->first = NULL; |
| 3022 | istk->expansion = l; |
| 3023 | |
| 3024 | istk->mstk = defining; |
| 3025 | |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 3026 | lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3027 | tmp_defining = defining; |
| 3028 | defining = defining->rep_nest; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3029 | free_tlist(origline); |
| 3030 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3031 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3032 | case PP_EXITREP: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3033 | /* |
| 3034 | * We must search along istk->expansion until we hit a |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3035 | * macro-end marker for a macro with no name. Then we set |
| 3036 | * its `in_progress' flag to 0. |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3037 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3038 | list_for_each(l, istk->expansion) |
| 3039 | if (l->finishes && !l->finishes->name) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3040 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3041 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3042 | if (l) |
| 3043 | l->finishes->in_progress = 1; |
| 3044 | else |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3045 | nasm_nonfatal("`%%exitrep' not within `%%rep' block"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3046 | free_tlist(origline); |
| 3047 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3048 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3049 | case PP_XDEFINE: |
| 3050 | case PP_IXDEFINE: |
| 3051 | case PP_DEFINE: |
| 3052 | case PP_IDEFINE: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3053 | casesense = (i == PP_DEFINE || i == PP_XDEFINE); |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3054 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3055 | tline = tline->next; |
| 3056 | skip_white_(tline); |
| 3057 | tline = expand_id(tline); |
| 3058 | if (!tline || (tline->type != TOK_ID && |
| 3059 | (tline->type != TOK_PREPROC_ID || |
| 3060 | tline->text[1] != '$'))) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3061 | nasm_nonfatal("`%s' expects a macro identifier", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3062 | pp_directives[i]); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3063 | free_tlist(origline); |
| 3064 | return DIRECTIVE_FOUND; |
| 3065 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3066 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3067 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3068 | last = tline; |
| 3069 | param_start = tline = tline->next; |
| 3070 | nparam = 0; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3071 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3072 | /* Expand the macro definition now for %xdefine and %ixdefine */ |
| 3073 | if ((i == PP_XDEFINE) || (i == PP_IXDEFINE)) |
| 3074 | tline = expand_smacro(tline); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3075 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3076 | if (tok_is_(tline, "(")) { |
| 3077 | /* |
| 3078 | * This macro has parameters. |
| 3079 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3080 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3081 | tline = tline->next; |
| 3082 | while (1) { |
| 3083 | skip_white_(tline); |
| 3084 | if (!tline) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3085 | nasm_nonfatal("parameter identifier expected"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3086 | free_tlist(origline); |
| 3087 | return DIRECTIVE_FOUND; |
| 3088 | } |
| 3089 | if (tline->type != TOK_ID) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3090 | nasm_nonfatal("`%s': parameter identifier expected", |
| 3091 | tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3092 | free_tlist(origline); |
| 3093 | return DIRECTIVE_FOUND; |
| 3094 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3095 | tline->type = TOK_SMAC_PARAM + nparam++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3096 | tline = tline->next; |
| 3097 | skip_white_(tline); |
| 3098 | if (tok_is_(tline, ",")) { |
| 3099 | tline = tline->next; |
H. Peter Anvin | ca348b6 | 2008-07-23 10:49:26 -0400 | [diff] [blame] | 3100 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3101 | if (!tok_is_(tline, ")")) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3102 | nasm_nonfatal("`)' expected to terminate macro template"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3103 | free_tlist(origline); |
| 3104 | return DIRECTIVE_FOUND; |
| 3105 | } |
| 3106 | break; |
| 3107 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3108 | } |
| 3109 | last = tline; |
| 3110 | tline = tline->next; |
| 3111 | } |
| 3112 | if (tok_type_(tline, TOK_WHITESPACE)) |
| 3113 | last = tline, tline = tline->next; |
| 3114 | macro_start = NULL; |
| 3115 | last->next = NULL; |
| 3116 | t = tline; |
| 3117 | while (t) { |
| 3118 | if (t->type == TOK_ID) { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3119 | list_for_each(tt, param_start) |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3120 | if (tt->type >= TOK_SMAC_PARAM && |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3121 | !strcmp(tt->text, t->text)) |
| 3122 | t->type = tt->type; |
| 3123 | } |
| 3124 | tt = t->next; |
| 3125 | t->next = macro_start; |
| 3126 | macro_start = t; |
| 3127 | t = tt; |
| 3128 | } |
| 3129 | /* |
| 3130 | * Good. We now have a macro name, a parameter count, and a |
| 3131 | * token list (in reverse order) for an expansion. We ought |
| 3132 | * to be OK just to create an SMacro, store it, and let |
| 3133 | * free_tlist have the rest of the line (which we have |
| 3134 | * carefully re-terminated after chopping off the expansion |
| 3135 | * from the end). |
| 3136 | */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 3137 | define_smacro(ctx, mname, casesense, nparam, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3138 | free_tlist(origline); |
| 3139 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3140 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3141 | case PP_UNDEF: |
| 3142 | tline = tline->next; |
| 3143 | skip_white_(tline); |
| 3144 | tline = expand_id(tline); |
| 3145 | if (!tline || (tline->type != TOK_ID && |
| 3146 | (tline->type != TOK_PREPROC_ID || |
| 3147 | tline->text[1] != '$'))) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3148 | nasm_nonfatal("`%%undef' expects a macro identifier"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3149 | free_tlist(origline); |
| 3150 | return DIRECTIVE_FOUND; |
| 3151 | } |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3152 | if (tline->next) |
| 3153 | nasm_warnf(ERR_PASS1, "trailing garbage after macro name ignored"); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3154 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3155 | /* Find the context that symbol belongs to */ |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3156 | ctx = get_ctx(tline->text, &mname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3157 | undef_smacro(ctx, mname); |
| 3158 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3159 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3160 | |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3161 | case PP_DEFSTR: |
| 3162 | case PP_IDEFSTR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3163 | casesense = (i == PP_DEFSTR); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3164 | |
| 3165 | tline = tline->next; |
| 3166 | skip_white_(tline); |
| 3167 | tline = expand_id(tline); |
| 3168 | if (!tline || (tline->type != TOK_ID && |
| 3169 | (tline->type != TOK_PREPROC_ID || |
| 3170 | tline->text[1] != '$'))) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3171 | nasm_nonfatal("`%s' expects a macro identifier", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3172 | pp_directives[i]); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3173 | free_tlist(origline); |
| 3174 | return DIRECTIVE_FOUND; |
| 3175 | } |
| 3176 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3177 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3178 | last = tline; |
| 3179 | tline = expand_smacro(tline->next); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3180 | last->next = NULL; |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3181 | |
| 3182 | while (tok_type_(tline, TOK_WHITESPACE)) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3183 | tline = delete_Token(tline); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3184 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3185 | p = detoken(tline, false); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3186 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3187 | macro_start->next = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3188 | macro_start->text = nasm_quote(p, strlen(p)); |
| 3189 | macro_start->type = TOK_STRING; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3190 | macro_start->a.mac = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3191 | nasm_free(p); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3192 | |
| 3193 | /* |
| 3194 | * We now have a macro name, an implicit parameter count of |
| 3195 | * zero, and a string token to use as an expansion. Create |
| 3196 | * and store an SMacro. |
| 3197 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3198 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3199 | free_tlist(origline); |
| 3200 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3201 | |
H. Peter Anvin | 2f55bda | 2009-07-14 15:04:04 -0400 | [diff] [blame] | 3202 | case PP_DEFTOK: |
| 3203 | case PP_IDEFTOK: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3204 | casesense = (i == PP_DEFTOK); |
| 3205 | |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3206 | tline = tline->next; |
| 3207 | skip_white_(tline); |
| 3208 | tline = expand_id(tline); |
| 3209 | if (!tline || (tline->type != TOK_ID && |
| 3210 | (tline->type != TOK_PREPROC_ID || |
| 3211 | tline->text[1] != '$'))) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3212 | nasm_nonfatal("`%s' expects a macro identifier as first parameter", |
| 3213 | pp_directives[i]); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3214 | free_tlist(origline); |
| 3215 | return DIRECTIVE_FOUND; |
| 3216 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3217 | ctx = get_ctx(tline->text, &mname); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3218 | last = tline; |
| 3219 | tline = expand_smacro(tline->next); |
| 3220 | last->next = NULL; |
| 3221 | |
| 3222 | t = tline; |
| 3223 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3224 | t = t->next; |
| 3225 | /* t should now point to the string */ |
Cyrill Gorcunov | 6908e58 | 2010-09-06 19:36:15 +0400 | [diff] [blame] | 3226 | if (!tok_type_(t, TOK_STRING)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3227 | nasm_nonfatal("`%s` requires string as second parameter", |
| 3228 | pp_directives[i]); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3229 | free_tlist(tline); |
| 3230 | free_tlist(origline); |
| 3231 | return DIRECTIVE_FOUND; |
| 3232 | } |
| 3233 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 3234 | /* |
| 3235 | * Convert the string to a token stream. Note that smacros |
| 3236 | * are stored with the token stream reversed, so we have to |
| 3237 | * reverse the output of tokenize(). |
| 3238 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3239 | nasm_unquote_cstr(t->text, i); |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 3240 | macro_start = reverse_tokens(tokenize(t->text)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3241 | |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3242 | /* |
| 3243 | * We now have a macro name, an implicit parameter count of |
| 3244 | * zero, and a numeric token to use as an expansion. Create |
| 3245 | * and store an SMacro. |
| 3246 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3247 | define_smacro(ctx, mname, casesense, 0, macro_start); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3248 | free_tlist(tline); |
| 3249 | free_tlist(origline); |
| 3250 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3251 | |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3252 | case PP_PATHSEARCH: |
| 3253 | { |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 3254 | const char *found_path; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3255 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3256 | casesense = true; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3257 | |
| 3258 | tline = tline->next; |
| 3259 | skip_white_(tline); |
| 3260 | tline = expand_id(tline); |
| 3261 | if (!tline || (tline->type != TOK_ID && |
| 3262 | (tline->type != TOK_PREPROC_ID || |
| 3263 | tline->text[1] != '$'))) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3264 | nasm_nonfatal("`%%pathsearch' expects a macro identifier as first parameter"); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3265 | free_tlist(origline); |
| 3266 | return DIRECTIVE_FOUND; |
| 3267 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3268 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3269 | last = tline; |
| 3270 | tline = expand_smacro(tline->next); |
| 3271 | last->next = NULL; |
| 3272 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3273 | t = tline; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3274 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3275 | t = t->next; |
| 3276 | |
| 3277 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3278 | t->type != TOK_INTERNAL_STRING)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3279 | nasm_nonfatal("`%%pathsearch' expects a file name"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3280 | free_tlist(tline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3281 | free_tlist(origline); |
| 3282 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 3283 | } |
| 3284 | if (t->next) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3285 | nasm_warnf(ERR_PASS1, "trailing garbage after `%%pathsearch' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3286 | p = t->text; |
H. Peter Anvin | 427cc91 | 2008-06-01 21:43:03 -0700 | [diff] [blame] | 3287 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3288 | nasm_unquote(p, NULL); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3289 | |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 3290 | inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY); |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 3291 | if (!found_path) |
| 3292 | found_path = p; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3293 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3294 | macro_start->next = NULL; |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 3295 | macro_start->text = nasm_quote(found_path, strlen(found_path)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3296 | macro_start->type = TOK_STRING; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3297 | macro_start->a.mac = NULL; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3298 | |
| 3299 | /* |
| 3300 | * We now have a macro name, an implicit parameter count of |
| 3301 | * zero, and a string token to use as an expansion. Create |
| 3302 | * and store an SMacro. |
| 3303 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3304 | define_smacro(ctx, mname, casesense, 0, macro_start); |
| 3305 | free_tlist(tline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3306 | free_tlist(origline); |
| 3307 | return DIRECTIVE_FOUND; |
| 3308 | } |
| 3309 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3310 | case PP_STRLEN: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3311 | casesense = true; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 3312 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3313 | tline = tline->next; |
| 3314 | skip_white_(tline); |
| 3315 | tline = expand_id(tline); |
| 3316 | if (!tline || (tline->type != TOK_ID && |
| 3317 | (tline->type != TOK_PREPROC_ID || |
| 3318 | tline->text[1] != '$'))) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3319 | nasm_nonfatal("`%%strlen' expects a macro identifier as first parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3320 | free_tlist(origline); |
| 3321 | return DIRECTIVE_FOUND; |
| 3322 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3323 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3324 | last = tline; |
| 3325 | tline = expand_smacro(tline->next); |
| 3326 | last->next = NULL; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3327 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3328 | t = tline; |
| 3329 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3330 | t = t->next; |
| 3331 | /* t should now point to the string */ |
Cyrill Gorcunov | 4e1d5ab | 2010-07-23 18:51:51 +0400 | [diff] [blame] | 3332 | if (!tok_type_(t, TOK_STRING)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3333 | nasm_nonfatal("`%%strlen` requires string as second parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3334 | free_tlist(tline); |
| 3335 | free_tlist(origline); |
| 3336 | return DIRECTIVE_FOUND; |
| 3337 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3338 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3339 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3340 | macro_start->next = NULL; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 3341 | make_tok_num(macro_start, nasm_unquote(t->text, NULL)); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3342 | macro_start->a.mac = NULL; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3343 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3344 | /* |
| 3345 | * We now have a macro name, an implicit parameter count of |
| 3346 | * zero, and a numeric token to use as an expansion. Create |
| 3347 | * and store an SMacro. |
| 3348 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3349 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3350 | free_tlist(tline); |
| 3351 | free_tlist(origline); |
| 3352 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3353 | |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3354 | case PP_STRCAT: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3355 | casesense = true; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3356 | |
| 3357 | tline = tline->next; |
| 3358 | skip_white_(tline); |
| 3359 | tline = expand_id(tline); |
| 3360 | if (!tline || (tline->type != TOK_ID && |
| 3361 | (tline->type != TOK_PREPROC_ID || |
| 3362 | tline->text[1] != '$'))) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3363 | nasm_nonfatal("`%%strcat' expects a macro identifier as first parameter"); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3364 | free_tlist(origline); |
| 3365 | return DIRECTIVE_FOUND; |
| 3366 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3367 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3368 | last = tline; |
| 3369 | tline = expand_smacro(tline->next); |
| 3370 | last->next = NULL; |
| 3371 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3372 | len = 0; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3373 | list_for_each(t, tline) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3374 | switch (t->type) { |
| 3375 | case TOK_WHITESPACE: |
| 3376 | break; |
| 3377 | case TOK_STRING: |
| 3378 | len += t->a.len = nasm_unquote(t->text, NULL); |
| 3379 | break; |
| 3380 | case TOK_OTHER: |
| 3381 | if (!strcmp(t->text, ",")) /* permit comma separators */ |
| 3382 | break; |
| 3383 | /* else fall through */ |
| 3384 | default: |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3385 | nasm_nonfatal("non-string passed to `%%strcat' (%d)", t->type); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3386 | free_tlist(tline); |
| 3387 | free_tlist(origline); |
| 3388 | return DIRECTIVE_FOUND; |
| 3389 | } |
| 3390 | } |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3391 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3392 | p = pp = nasm_malloc(len); |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3393 | list_for_each(t, tline) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3394 | if (t->type == TOK_STRING) { |
| 3395 | memcpy(p, t->text, t->a.len); |
| 3396 | p += t->a.len; |
| 3397 | } |
| 3398 | } |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3399 | |
| 3400 | /* |
| 3401 | * We now have a macro name, an implicit parameter count of |
| 3402 | * zero, and a numeric token to use as an expansion. Create |
| 3403 | * and store an SMacro. |
| 3404 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3405 | macro_start = new_Token(NULL, TOK_STRING, NULL, 0); |
| 3406 | macro_start->text = nasm_quote(pp, len); |
| 3407 | nasm_free(pp); |
| 3408 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3409 | free_tlist(tline); |
| 3410 | free_tlist(origline); |
| 3411 | return DIRECTIVE_FOUND; |
| 3412 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3413 | case PP_SUBSTR: |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3414 | { |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3415 | int64_t start, count; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3416 | size_t len; |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 3417 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3418 | casesense = true; |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3419 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3420 | tline = tline->next; |
| 3421 | skip_white_(tline); |
| 3422 | tline = expand_id(tline); |
| 3423 | if (!tline || (tline->type != TOK_ID && |
| 3424 | (tline->type != TOK_PREPROC_ID || |
| 3425 | tline->text[1] != '$'))) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3426 | nasm_nonfatal("`%%substr' expects a macro identifier as first parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3427 | free_tlist(origline); |
| 3428 | return DIRECTIVE_FOUND; |
| 3429 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3430 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3431 | last = tline; |
| 3432 | tline = expand_smacro(tline->next); |
| 3433 | last->next = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3434 | |
Cyrill Gorcunov | 35519d6 | 2010-09-06 23:49:52 +0400 | [diff] [blame] | 3435 | if (tline) /* skip expanded id */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3436 | t = tline->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3437 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3438 | t = t->next; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3439 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3440 | /* t should now point to the string */ |
Cyrill Gorcunov | 35519d6 | 2010-09-06 23:49:52 +0400 | [diff] [blame] | 3441 | if (!tok_type_(t, TOK_STRING)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3442 | nasm_nonfatal("`%%substr` requires string as second parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3443 | free_tlist(tline); |
| 3444 | free_tlist(origline); |
| 3445 | return DIRECTIVE_FOUND; |
| 3446 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3447 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3448 | tt = t->next; |
| 3449 | tptr = &tt; |
| 3450 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3451 | evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3452 | if (!evalresult) { |
| 3453 | free_tlist(tline); |
| 3454 | free_tlist(origline); |
| 3455 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3456 | } else if (!is_simple(evalresult)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3457 | nasm_nonfatal("non-constant value given to `%%substr`"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3458 | free_tlist(tline); |
| 3459 | free_tlist(origline); |
| 3460 | return DIRECTIVE_FOUND; |
| 3461 | } |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3462 | start = evalresult->value - 1; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3463 | |
| 3464 | while (tok_type_(tt, TOK_WHITESPACE)) |
| 3465 | tt = tt->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3466 | if (!tt) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3467 | count = 1; /* Backwards compatibility: one character */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3468 | } else { |
| 3469 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3470 | evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3471 | if (!evalresult) { |
| 3472 | free_tlist(tline); |
| 3473 | free_tlist(origline); |
| 3474 | return DIRECTIVE_FOUND; |
| 3475 | } else if (!is_simple(evalresult)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3476 | nasm_nonfatal("non-constant value given to `%%substr`"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3477 | free_tlist(tline); |
| 3478 | free_tlist(origline); |
| 3479 | return DIRECTIVE_FOUND; |
| 3480 | } |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3481 | count = evalresult->value; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3482 | } |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3483 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3484 | len = nasm_unquote(t->text, NULL); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3485 | |
Cyrill Gorcunov | cff031e | 2010-09-07 20:31:11 +0400 | [diff] [blame] | 3486 | /* make start and count being in range */ |
| 3487 | if (start < 0) |
| 3488 | start = 0; |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3489 | if (count < 0) |
| 3490 | count = len + count + 1 - start; |
| 3491 | if (start + count > (int64_t)len) |
Cyrill Gorcunov | cff031e | 2010-09-07 20:31:11 +0400 | [diff] [blame] | 3492 | count = len - start; |
| 3493 | if (!len || count < 0 || start >=(int64_t)len) |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3494 | start = -1, count = 0; /* empty string */ |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3495 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3496 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3497 | macro_start->next = NULL; |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3498 | macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3499 | macro_start->type = TOK_STRING; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3500 | macro_start->a.mac = NULL; |
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 | /* |
| 3503 | * We now have a macro name, an implicit parameter count of |
| 3504 | * zero, and a numeric token to use as an expansion. Create |
| 3505 | * and store an SMacro. |
| 3506 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3507 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3508 | free_tlist(tline); |
| 3509 | free_tlist(origline); |
| 3510 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3511 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3512 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3513 | case PP_ASSIGN: |
| 3514 | case PP_IASSIGN: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3515 | casesense = (i == PP_ASSIGN); |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3516 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3517 | tline = tline->next; |
| 3518 | skip_white_(tline); |
| 3519 | tline = expand_id(tline); |
| 3520 | if (!tline || (tline->type != TOK_ID && |
| 3521 | (tline->type != TOK_PREPROC_ID || |
| 3522 | tline->text[1] != '$'))) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3523 | nasm_nonfatal("`%%%sassign' expects a macro identifier", |
| 3524 | (i == PP_IASSIGN ? "i" : "")); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3525 | free_tlist(origline); |
| 3526 | return DIRECTIVE_FOUND; |
| 3527 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3528 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3529 | last = tline; |
| 3530 | tline = expand_smacro(tline->next); |
| 3531 | last->next = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3532 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3533 | t = tline; |
| 3534 | tptr = &t; |
| 3535 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3536 | evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3537 | free_tlist(tline); |
| 3538 | if (!evalresult) { |
| 3539 | free_tlist(origline); |
| 3540 | return DIRECTIVE_FOUND; |
| 3541 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3542 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3543 | if (tokval.t_type) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3544 | nasm_warnf(ERR_PASS1, "trailing garbage after expression ignored"); |
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 | if (!is_simple(evalresult)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3547 | nasm_nonfatal("non-constant value given to `%%%sassign'", |
| 3548 | (i == PP_IASSIGN ? "i" : "")); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3549 | free_tlist(origline); |
| 3550 | return DIRECTIVE_FOUND; |
| 3551 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3552 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3553 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3554 | macro_start->next = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3555 | make_tok_num(macro_start, reloc_value(evalresult)); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3556 | macro_start->a.mac = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3557 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3558 | /* |
| 3559 | * We now have a macro name, an implicit parameter count of |
| 3560 | * zero, and a numeric token to use as an expansion. Create |
| 3561 | * and store an SMacro. |
| 3562 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3563 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3564 | free_tlist(origline); |
| 3565 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3566 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3567 | case PP_LINE: |
| 3568 | /* |
| 3569 | * Syntax is `%line nnn[+mmm] [filename]' |
| 3570 | */ |
| 3571 | tline = tline->next; |
| 3572 | skip_white_(tline); |
| 3573 | if (!tok_type_(tline, TOK_NUMBER)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3574 | nasm_nonfatal("`%%line' expects line number"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3575 | free_tlist(origline); |
| 3576 | return DIRECTIVE_FOUND; |
| 3577 | } |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 3578 | k = readnum(tline->text, &err); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3579 | m = 1; |
| 3580 | tline = tline->next; |
| 3581 | if (tok_is_(tline, "+")) { |
| 3582 | tline = tline->next; |
| 3583 | if (!tok_type_(tline, TOK_NUMBER)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3584 | nasm_nonfatal("`%%line' expects line increment"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3585 | free_tlist(origline); |
| 3586 | return DIRECTIVE_FOUND; |
| 3587 | } |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 3588 | m = readnum(tline->text, &err); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3589 | tline = tline->next; |
| 3590 | } |
| 3591 | skip_white_(tline); |
| 3592 | src_set_linnum(k); |
| 3593 | istk->lineinc = m; |
| 3594 | if (tline) { |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 3595 | char *fname = detoken(tline, false); |
| 3596 | src_set_fname(fname); |
| 3597 | nasm_free(fname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3598 | } |
| 3599 | free_tlist(origline); |
| 3600 | return DIRECTIVE_FOUND; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3601 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3602 | default: |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3603 | nasm_fatal("preprocessor directive `%s' not yet implemented", |
| 3604 | pp_directives[i]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3605 | return DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3606 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3607 | } |
| 3608 | |
| 3609 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3610 | * Ensure that a macro parameter contains a condition code and |
| 3611 | * nothing else. Return the condition code index if so, or -1 |
| 3612 | * otherwise. |
| 3613 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3614 | static int find_cc(Token * t) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3615 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3616 | Token *tt; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3617 | |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3618 | if (!t) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3619 | return -1; /* Probably a %+ without a space */ |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3620 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3621 | skip_white_(t); |
Cyrill Gorcunov | 7524cfd | 2017-10-22 19:01:16 +0300 | [diff] [blame] | 3622 | if (!t) |
| 3623 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3624 | if (t->type != TOK_ID) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3625 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3626 | tt = t->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3627 | skip_white_(tt); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3628 | if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ","))) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3629 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3630 | |
Cyrill Gorcunov | 1945639 | 2012-05-02 00:18:56 +0400 | [diff] [blame] | 3631 | return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3632 | } |
| 3633 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3634 | /* |
| 3635 | * This routines walks over tokens strem and hadnles tokens |
| 3636 | * pasting, if @handle_explicit passed then explicit pasting |
| 3637 | * term is handled, otherwise -- implicit pastings only. |
| 3638 | */ |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 3639 | static bool paste_tokens(Token **head, const struct tokseq_match *m, |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3640 | size_t mnum, bool handle_explicit) |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3641 | { |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3642 | Token *tok, *next, **prev_next, **prev_nonspace; |
| 3643 | bool pasted = false; |
| 3644 | char *buf, *p; |
| 3645 | size_t len, i; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3646 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3647 | /* |
| 3648 | * The last token before pasting. We need it |
| 3649 | * to be able to connect new handled tokens. |
| 3650 | * In other words if there were a tokens stream |
| 3651 | * |
| 3652 | * A -> B -> C -> D |
| 3653 | * |
| 3654 | * and we've joined tokens B and C, the resulting |
| 3655 | * stream should be |
| 3656 | * |
| 3657 | * A -> BC -> D |
| 3658 | */ |
| 3659 | tok = *head; |
| 3660 | prev_next = NULL; |
| 3661 | |
| 3662 | if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE)) |
| 3663 | prev_nonspace = head; |
| 3664 | else |
| 3665 | prev_nonspace = NULL; |
| 3666 | |
| 3667 | while (tok && (next = tok->next)) { |
| 3668 | |
| 3669 | switch (tok->type) { |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3670 | case TOK_WHITESPACE: |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3671 | /* Zap redundant whitespaces */ |
| 3672 | while (tok_type_(next, TOK_WHITESPACE)) |
| 3673 | next = delete_Token(next); |
| 3674 | tok->next = next; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3675 | break; |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3676 | |
| 3677 | case TOK_PASTE: |
| 3678 | /* Explicit pasting */ |
| 3679 | if (!handle_explicit) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3680 | break; |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3681 | next = delete_Token(tok); |
| 3682 | |
| 3683 | while (tok_type_(next, TOK_WHITESPACE)) |
| 3684 | next = delete_Token(next); |
| 3685 | |
| 3686 | if (!pasted) |
| 3687 | pasted = true; |
| 3688 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3689 | /* Left pasting token is start of line */ |
| 3690 | if (!prev_nonspace) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3691 | nasm_fatal("No lvalue found on pasting"); |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3692 | |
Cyrill Gorcunov | 8b5c9fb | 2013-02-04 01:24:54 +0400 | [diff] [blame] | 3693 | /* |
| 3694 | * No ending token, this might happen in two |
| 3695 | * cases |
| 3696 | * |
| 3697 | * 1) There indeed no right token at all |
| 3698 | * 2) There is a bare "%define ID" statement, |
| 3699 | * and @ID does expand to whitespace. |
| 3700 | * |
| 3701 | * So technically we need to do a grammar analysis |
| 3702 | * in another stage of parsing, but for now lets don't |
| 3703 | * change the behaviour people used to. Simply allow |
| 3704 | * whitespace after paste token. |
| 3705 | */ |
| 3706 | if (!next) { |
| 3707 | /* |
| 3708 | * Zap ending space tokens and that's all. |
| 3709 | */ |
| 3710 | tok = (*prev_nonspace)->next; |
| 3711 | while (tok_type_(tok, TOK_WHITESPACE)) |
| 3712 | tok = delete_Token(tok); |
| 3713 | tok = *prev_nonspace; |
| 3714 | tok->next = NULL; |
| 3715 | break; |
| 3716 | } |
| 3717 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3718 | tok = *prev_nonspace; |
| 3719 | while (tok_type_(tok, TOK_WHITESPACE)) |
| 3720 | tok = delete_Token(tok); |
| 3721 | len = strlen(tok->text); |
| 3722 | len += strlen(next->text); |
| 3723 | |
| 3724 | p = buf = nasm_malloc(len + 1); |
| 3725 | strcpy(p, tok->text); |
| 3726 | p = strchr(p, '\0'); |
| 3727 | strcpy(p, next->text); |
| 3728 | |
| 3729 | delete_Token(tok); |
| 3730 | |
| 3731 | tok = tokenize(buf); |
| 3732 | nasm_free(buf); |
| 3733 | |
| 3734 | *prev_nonspace = tok; |
| 3735 | while (tok && tok->next) |
| 3736 | tok = tok->next; |
| 3737 | |
| 3738 | tok->next = delete_Token(next); |
| 3739 | |
| 3740 | /* Restart from pasted tokens head */ |
| 3741 | tok = *prev_nonspace; |
| 3742 | break; |
| 3743 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3744 | default: |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3745 | /* implicit pasting */ |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 3746 | for (i = 0; i < mnum; i++) { |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3747 | if (!(PP_CONCAT_MATCH(tok, m[i].mask_head))) |
| 3748 | continue; |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 3749 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3750 | len = 0; |
| 3751 | while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) { |
| 3752 | len += strlen(next->text); |
| 3753 | next = next->next; |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 3754 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3755 | |
Cyrill Gorcunov | 6f8109e | 2017-10-22 21:26:36 +0300 | [diff] [blame] | 3756 | /* No match or no text to process */ |
| 3757 | if (tok == next || len == 0) |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3758 | break; |
| 3759 | |
| 3760 | len += strlen(tok->text); |
| 3761 | p = buf = nasm_malloc(len + 1); |
| 3762 | |
Adam Majer | 1a06943 | 2017-07-25 11:12:35 +0200 | [diff] [blame] | 3763 | strcpy(p, tok->text); |
| 3764 | p = strchr(p, '\0'); |
| 3765 | tok = delete_Token(tok); |
| 3766 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3767 | while (tok != next) { |
Adam Majer | 1a06943 | 2017-07-25 11:12:35 +0200 | [diff] [blame] | 3768 | if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) { |
| 3769 | strcpy(p, tok->text); |
| 3770 | p = strchr(p, '\0'); |
| 3771 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3772 | tok = delete_Token(tok); |
| 3773 | } |
| 3774 | |
| 3775 | tok = tokenize(buf); |
| 3776 | nasm_free(buf); |
| 3777 | |
| 3778 | if (prev_next) |
| 3779 | *prev_next = tok; |
| 3780 | else |
| 3781 | *head = tok; |
| 3782 | |
| 3783 | /* |
| 3784 | * Connect pasted into original stream, |
| 3785 | * ie A -> new-tokens -> B |
| 3786 | */ |
| 3787 | while (tok && tok->next) |
| 3788 | tok = tok->next; |
| 3789 | tok->next = next; |
| 3790 | |
| 3791 | if (!pasted) |
| 3792 | pasted = true; |
| 3793 | |
| 3794 | /* Restart from pasted tokens head */ |
| 3795 | tok = prev_next ? *prev_next : *head; |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 3796 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3797 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3798 | break; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3799 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3800 | |
| 3801 | prev_next = &tok->next; |
| 3802 | |
| 3803 | if (tok->next && |
| 3804 | !tok_type_(tok->next, TOK_WHITESPACE) && |
| 3805 | !tok_type_(tok->next, TOK_PASTE)) |
| 3806 | prev_nonspace = prev_next; |
| 3807 | |
| 3808 | tok = tok->next; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3809 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3810 | |
| 3811 | return pasted; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3812 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3813 | |
| 3814 | /* |
| 3815 | * expands to a list of tokens from %{x:y} |
| 3816 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3817 | static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3818 | { |
| 3819 | Token *t = tline, **tt, *tm, *head; |
| 3820 | char *pos; |
| 3821 | int fst, lst, j, i; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3822 | |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3823 | pos = strchr(tline->text, ':'); |
| 3824 | nasm_assert(pos); |
| 3825 | |
| 3826 | lst = atoi(pos + 1); |
| 3827 | fst = atoi(tline->text + 1); |
| 3828 | |
| 3829 | /* |
| 3830 | * only macros params are accounted so |
| 3831 | * if someone passes %0 -- we reject such |
| 3832 | * value(s) |
| 3833 | */ |
| 3834 | if (lst == 0 || fst == 0) |
| 3835 | goto err; |
| 3836 | |
| 3837 | /* the values should be sane */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3838 | if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) || |
| 3839 | (lst > (int)mac->nparam || lst < (-(int)mac->nparam))) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3840 | goto err; |
| 3841 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3842 | fst = fst < 0 ? fst + (int)mac->nparam + 1: fst; |
| 3843 | lst = lst < 0 ? lst + (int)mac->nparam + 1: lst; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3844 | |
| 3845 | /* counted from zero */ |
| 3846 | fst--, lst--; |
| 3847 | |
| 3848 | /* |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3849 | * It will be at least one token. Note we |
| 3850 | * need to scan params until separator, otherwise |
| 3851 | * only first token will be passed. |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3852 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3853 | tm = mac->params[(fst + mac->rotate) % mac->nparam]; |
Cyrill Gorcunov | 67f2ca2 | 2018-10-13 19:41:01 +0300 | [diff] [blame] | 3854 | if (!tm) |
| 3855 | goto err; |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3856 | head = new_Token(NULL, tm->type, tm->text, 0); |
| 3857 | tt = &head->next, tm = tm->next; |
| 3858 | while (tok_isnt_(tm, ",")) { |
| 3859 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 3860 | *tt = t, tt = &t->next, tm = tm->next; |
| 3861 | } |
| 3862 | |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3863 | if (fst < lst) { |
| 3864 | for (i = fst + 1; i <= lst; i++) { |
| 3865 | t = new_Token(NULL, TOK_OTHER, ",", 0); |
| 3866 | *tt = t, tt = &t->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3867 | j = (i + mac->rotate) % mac->nparam; |
| 3868 | tm = mac->params[j]; |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3869 | while (tok_isnt_(tm, ",")) { |
| 3870 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 3871 | *tt = t, tt = &t->next, tm = tm->next; |
| 3872 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3873 | } |
| 3874 | } else { |
| 3875 | for (i = fst - 1; i >= lst; i--) { |
| 3876 | t = new_Token(NULL, TOK_OTHER, ",", 0); |
| 3877 | *tt = t, tt = &t->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3878 | j = (i + mac->rotate) % mac->nparam; |
| 3879 | tm = mac->params[j]; |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3880 | while (tok_isnt_(tm, ",")) { |
| 3881 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 3882 | *tt = t, tt = &t->next, tm = tm->next; |
| 3883 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3884 | } |
| 3885 | } |
| 3886 | |
| 3887 | *last = tt; |
| 3888 | return head; |
| 3889 | |
| 3890 | err: |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3891 | nasm_nonfatal("`%%{%s}': macro parameters out of range", |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3892 | &tline->text[1]); |
| 3893 | return tline; |
| 3894 | } |
| 3895 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3896 | /* |
| 3897 | * Expand MMacro-local things: parameter references (%0, %n, %+n, |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 3898 | * %-n) and MMacro-local identifiers (%%foo) as well as |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3899 | * macro indirection (%[...]) and range (%{..:..}). |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3900 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3901 | static Token *expand_mmac_params(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3902 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3903 | Token *t, *tt, **tail, *thead; |
H. Peter Anvin | 6125b62 | 2009-04-08 14:02:25 -0700 | [diff] [blame] | 3904 | bool changed = false; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3905 | char *pos; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3906 | |
| 3907 | tail = &thead; |
| 3908 | thead = NULL; |
| 3909 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3910 | while (tline) { |
Cyrill Gorcunov | 661f723 | 2018-10-28 20:39:34 +0300 | [diff] [blame] | 3911 | if (tline->type == TOK_PREPROC_ID && tline->text && tline->text[0] && |
Cyrill Gorcunov | ca61119 | 2010-06-04 09:22:12 +0400 | [diff] [blame] | 3912 | (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) || |
| 3913 | (tline->text[1] >= '0' && tline->text[1] <= '9') || |
| 3914 | tline->text[1] == '%')) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 3915 | char *text = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3916 | int type = 0, cc; /* type = 0 to placate optimisers */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 3917 | char tmpbuf[30]; |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3918 | unsigned int n; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3919 | int i; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3920 | MMacro *mac; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3921 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3922 | t = tline; |
| 3923 | tline = tline->next; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3924 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3925 | mac = istk->mstk; |
| 3926 | while (mac && !mac->name) /* avoid mistaking %reps for macros */ |
| 3927 | mac = mac->next_active; |
| 3928 | if (!mac) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3929 | nasm_nonfatal("`%s': not in a macro call", t->text); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3930 | } else { |
| 3931 | pos = strchr(t->text, ':'); |
| 3932 | if (!pos) { |
| 3933 | switch (t->text[1]) { |
| 3934 | /* |
| 3935 | * We have to make a substitution of one of the |
| 3936 | * forms %1, %-1, %+1, %%foo, %0. |
| 3937 | */ |
| 3938 | case '0': |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3939 | type = TOK_NUMBER; |
| 3940 | snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam); |
| 3941 | text = nasm_strdup(tmpbuf); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3942 | break; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3943 | case '%': |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3944 | type = TOK_ID; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3945 | snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".", |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3946 | mac->unique); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3947 | text = nasm_strcat(tmpbuf, t->text + 2); |
| 3948 | break; |
| 3949 | case '-': |
| 3950 | n = atoi(t->text + 2) - 1; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3951 | if (n >= mac->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3952 | tt = NULL; |
| 3953 | else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3954 | if (mac->nparam > 1) |
| 3955 | n = (n + mac->rotate) % mac->nparam; |
| 3956 | tt = mac->params[n]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3957 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3958 | cc = find_cc(tt); |
| 3959 | if (cc == -1) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3960 | nasm_nonfatal("macro parameter %d is not a condition code", |
| 3961 | n + 1); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3962 | text = NULL; |
| 3963 | } else { |
| 3964 | type = TOK_ID; |
| 3965 | if (inverse_ccs[cc] == -1) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3966 | nasm_nonfatal("condition code `%s' is not invertible", |
| 3967 | conditions[cc]); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3968 | text = NULL; |
| 3969 | } else |
| 3970 | text = nasm_strdup(conditions[inverse_ccs[cc]]); |
| 3971 | } |
| 3972 | break; |
| 3973 | case '+': |
| 3974 | n = atoi(t->text + 2) - 1; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3975 | if (n >= mac->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3976 | tt = NULL; |
| 3977 | else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3978 | if (mac->nparam > 1) |
| 3979 | n = (n + mac->rotate) % mac->nparam; |
| 3980 | tt = mac->params[n]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3981 | } |
| 3982 | cc = find_cc(tt); |
| 3983 | if (cc == -1) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3984 | nasm_nonfatal("macro parameter %d is not a condition code", |
| 3985 | n + 1); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3986 | text = NULL; |
| 3987 | } else { |
| 3988 | type = TOK_ID; |
| 3989 | text = nasm_strdup(conditions[cc]); |
| 3990 | } |
| 3991 | break; |
| 3992 | default: |
| 3993 | n = atoi(t->text + 1) - 1; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3994 | if (n >= mac->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3995 | tt = NULL; |
| 3996 | else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3997 | if (mac->nparam > 1) |
| 3998 | n = (n + mac->rotate) % mac->nparam; |
| 3999 | tt = mac->params[n]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4000 | } |
| 4001 | if (tt) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4002 | for (i = 0; i < mac->paramlen[n]; i++) { |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4003 | *tail = new_Token(NULL, tt->type, tt->text, 0); |
| 4004 | tail = &(*tail)->next; |
| 4005 | tt = tt->next; |
| 4006 | } |
| 4007 | } |
| 4008 | text = NULL; /* we've done it here */ |
| 4009 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4010 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4011 | } else { |
| 4012 | /* |
| 4013 | * seems we have a parameters range here |
| 4014 | */ |
| 4015 | Token *head, **last; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4016 | head = expand_mmac_params_range(mac, t, &last); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4017 | if (head != t) { |
| 4018 | *tail = head; |
| 4019 | *last = tline; |
| 4020 | tline = head; |
| 4021 | text = NULL; |
| 4022 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4023 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4024 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4025 | if (!text) { |
| 4026 | delete_Token(t); |
| 4027 | } else { |
| 4028 | *tail = t; |
| 4029 | tail = &t->next; |
| 4030 | t->type = type; |
| 4031 | nasm_free(t->text); |
| 4032 | t->text = text; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4033 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4034 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4035 | changed = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4036 | continue; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4037 | } else if (tline->type == TOK_INDIRECT) { |
| 4038 | t = tline; |
| 4039 | tline = tline->next; |
| 4040 | tt = tokenize(t->text); |
| 4041 | tt = expand_mmac_params(tt); |
| 4042 | tt = expand_smacro(tt); |
| 4043 | *tail = tt; |
| 4044 | while (tt) { |
| 4045 | tt->a.mac = NULL; /* Necessary? */ |
| 4046 | tail = &tt->next; |
| 4047 | tt = tt->next; |
| 4048 | } |
| 4049 | delete_Token(t); |
| 4050 | changed = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4051 | } else { |
| 4052 | t = *tail = tline; |
| 4053 | tline = tline->next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4054 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4055 | tail = &t->next; |
| 4056 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4057 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4058 | *tail = NULL; |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 4059 | |
Cyrill Gorcunov | c6a742c | 2011-06-27 01:23:09 +0400 | [diff] [blame] | 4060 | if (changed) { |
| 4061 | const struct tokseq_match t[] = { |
| 4062 | { |
| 4063 | PP_CONCAT_MASK(TOK_ID) | |
| 4064 | PP_CONCAT_MASK(TOK_FLOAT), /* head */ |
| 4065 | PP_CONCAT_MASK(TOK_ID) | |
| 4066 | PP_CONCAT_MASK(TOK_NUMBER) | |
| 4067 | PP_CONCAT_MASK(TOK_FLOAT) | |
| 4068 | PP_CONCAT_MASK(TOK_OTHER) /* tail */ |
| 4069 | }, |
| 4070 | { |
| 4071 | PP_CONCAT_MASK(TOK_NUMBER), /* head */ |
| 4072 | PP_CONCAT_MASK(TOK_NUMBER) /* tail */ |
| 4073 | } |
| 4074 | }; |
| 4075 | paste_tokens(&thead, t, ARRAY_SIZE(t), false); |
| 4076 | } |
H. Peter Anvin | 6125b62 | 2009-04-08 14:02:25 -0700 | [diff] [blame] | 4077 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4078 | return thead; |
| 4079 | } |
| 4080 | |
| 4081 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4082 | * Expand all single-line macro calls made in the given line. |
| 4083 | * Return the expanded version of the line. The original is deemed |
| 4084 | * to be destroyed in the process. (In reality we'll just move |
| 4085 | * Tokens from input to output a lot of the time, rather than |
| 4086 | * actually bothering to destroy and replicate.) |
| 4087 | */ |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4088 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4089 | static Token *expand_smacro(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4090 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4091 | Token *t, *tt, *mstart, **tail, *thead; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4092 | SMacro *head = NULL, *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4093 | Token **params; |
| 4094 | int *paramsize; |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 4095 | unsigned int nparam, sparam; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 4096 | int brackets; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4097 | Token *org_tline = tline; |
| 4098 | Context *ctx; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 4099 | const char *mname; |
H. Peter Anvin | a3d96d0 | 2018-06-15 17:51:39 -0700 | [diff] [blame] | 4100 | int64_t deadman = nasm_limit[LIMIT_MACROS]; |
H. Peter Anvin | 8287daf | 2009-07-07 16:00:58 -0700 | [diff] [blame] | 4101 | bool expanded; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4102 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4103 | /* |
| 4104 | * Trick: we should avoid changing the start token pointer since it can |
| 4105 | * be contained in "next" field of other token. Because of this |
| 4106 | * we allocate a copy of first token and work with it; at the end of |
| 4107 | * routine we copy it back |
| 4108 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4109 | if (org_tline) { |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4110 | tline = new_Token(org_tline->next, org_tline->type, |
| 4111 | org_tline->text, 0); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4112 | tline->a.mac = org_tline->a.mac; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4113 | nasm_free(org_tline->text); |
| 4114 | org_tline->text = NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4115 | } |
| 4116 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4117 | expanded = true; /* Always expand %+ at least once */ |
H. Peter Anvin | 8287daf | 2009-07-07 16:00:58 -0700 | [diff] [blame] | 4118 | |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4119 | again: |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4120 | thead = NULL; |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4121 | tail = &thead; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4122 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4123 | while (tline) { /* main token loop */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4124 | if (!--deadman) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4125 | nasm_nonfatal("interminable macro recursion"); |
Cyrill Gorcunov | bd38c8f | 2009-11-21 11:11:23 +0300 | [diff] [blame] | 4126 | goto err; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4127 | } |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4128 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4129 | if ((mname = tline->text)) { |
| 4130 | /* if this token is a local macro, look in local context */ |
Cyrill Gorcunov | c56d9ad | 2010-02-11 15:12:19 +0300 | [diff] [blame] | 4131 | if (tline->type == TOK_ID) { |
| 4132 | head = (SMacro *)hash_findix(&smacros, mname); |
| 4133 | } else if (tline->type == TOK_PREPROC_ID) { |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 4134 | ctx = get_ctx(mname, &mname); |
Cyrill Gorcunov | c56d9ad | 2010-02-11 15:12:19 +0300 | [diff] [blame] | 4135 | head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL; |
| 4136 | } else |
| 4137 | head = NULL; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 4138 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4139 | /* |
| 4140 | * We've hit an identifier. As in is_mmacro below, we first |
| 4141 | * check whether the identifier is a single-line macro at |
| 4142 | * all, then think about checking for parameters if |
| 4143 | * necessary. |
| 4144 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4145 | list_for_each(m, head) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4146 | if (!mstrcmp(m->name, mname, m->casesense)) |
| 4147 | break; |
| 4148 | if (m) { |
| 4149 | mstart = tline; |
| 4150 | params = NULL; |
| 4151 | paramsize = NULL; |
| 4152 | if (m->nparam == 0) { |
| 4153 | /* |
| 4154 | * Simple case: the macro is parameterless. Discard the |
| 4155 | * one token that the macro call took, and push the |
| 4156 | * expansion back on the to-do stack. |
| 4157 | */ |
| 4158 | if (!m->expansion) { |
| 4159 | if (!strcmp("__FILE__", m->name)) { |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 4160 | const char *file = src_get_fname(); |
| 4161 | /* nasm_free(tline->text); here? */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4162 | tline->text = nasm_quote(file, strlen(file)); |
| 4163 | tline->type = TOK_STRING; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4164 | continue; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4165 | } |
| 4166 | if (!strcmp("__LINE__", m->name)) { |
| 4167 | nasm_free(tline->text); |
| 4168 | make_tok_num(tline, src_get_linnum()); |
| 4169 | continue; |
| 4170 | } |
| 4171 | if (!strcmp("__BITS__", m->name)) { |
| 4172 | nasm_free(tline->text); |
| 4173 | make_tok_num(tline, globalbits); |
| 4174 | continue; |
| 4175 | } |
| 4176 | tline = delete_Token(tline); |
| 4177 | continue; |
| 4178 | } |
| 4179 | } else { |
| 4180 | /* |
| 4181 | * Complicated case: at least one macro with this name |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4182 | * exists and takes parameters. We must find the |
| 4183 | * parameters in the call, count them, find the SMacro |
| 4184 | * that corresponds to that form of the macro call, and |
| 4185 | * substitute for the parameters when we expand. What a |
| 4186 | * pain. |
| 4187 | */ |
| 4188 | /*tline = tline->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4189 | skip_white_(tline); */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4190 | do { |
| 4191 | t = tline->next; |
| 4192 | while (tok_type_(t, TOK_SMAC_END)) { |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4193 | t->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4194 | t->text = NULL; |
| 4195 | t = tline->next = delete_Token(t); |
| 4196 | } |
| 4197 | tline = t; |
| 4198 | } while (tok_type_(tline, TOK_WHITESPACE)); |
| 4199 | if (!tok_is_(tline, "(")) { |
| 4200 | /* |
| 4201 | * This macro wasn't called with parameters: ignore |
| 4202 | * the call. (Behaviour borrowed from gnu cpp.) |
| 4203 | */ |
| 4204 | tline = mstart; |
| 4205 | m = NULL; |
| 4206 | } else { |
| 4207 | int paren = 0; |
| 4208 | int white = 0; |
| 4209 | brackets = 0; |
| 4210 | nparam = 0; |
| 4211 | sparam = PARAM_DELTA; |
| 4212 | params = nasm_malloc(sparam * sizeof(Token *)); |
| 4213 | params[0] = tline->next; |
| 4214 | paramsize = nasm_malloc(sparam * sizeof(int)); |
| 4215 | paramsize[0] = 0; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4216 | while (true) { /* parameter loop */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4217 | /* |
| 4218 | * For some unusual expansions |
| 4219 | * which concatenates function call |
| 4220 | */ |
| 4221 | t = tline->next; |
| 4222 | while (tok_type_(t, TOK_SMAC_END)) { |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4223 | t->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4224 | t->text = NULL; |
| 4225 | t = tline->next = delete_Token(t); |
| 4226 | } |
| 4227 | tline = t; |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 4228 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4229 | if (!tline) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4230 | nasm_nonfatal("macro call expects terminating `)'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4231 | break; |
| 4232 | } |
| 4233 | if (tline->type == TOK_WHITESPACE |
| 4234 | && brackets <= 0) { |
| 4235 | if (paramsize[nparam]) |
| 4236 | white++; |
| 4237 | else |
| 4238 | params[nparam] = tline->next; |
| 4239 | continue; /* parameter loop */ |
| 4240 | } |
| 4241 | if (tline->type == TOK_OTHER |
| 4242 | && tline->text[1] == 0) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4243 | char ch = tline->text[0]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4244 | if (ch == ',' && !paren && brackets <= 0) { |
| 4245 | if (++nparam >= sparam) { |
| 4246 | sparam += PARAM_DELTA; |
| 4247 | params = nasm_realloc(params, |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4248 | sparam * sizeof(Token *)); |
| 4249 | paramsize = nasm_realloc(paramsize, |
| 4250 | sparam * sizeof(int)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4251 | } |
| 4252 | params[nparam] = tline->next; |
| 4253 | paramsize[nparam] = 0; |
| 4254 | white = 0; |
| 4255 | continue; /* parameter loop */ |
| 4256 | } |
| 4257 | if (ch == '{' && |
| 4258 | (brackets > 0 || (brackets == 0 && |
| 4259 | !paramsize[nparam]))) |
| 4260 | { |
| 4261 | if (!(brackets++)) { |
| 4262 | params[nparam] = tline->next; |
| 4263 | continue; /* parameter loop */ |
| 4264 | } |
| 4265 | } |
| 4266 | if (ch == '}' && brackets > 0) |
| 4267 | if (--brackets == 0) { |
| 4268 | brackets = -1; |
| 4269 | continue; /* parameter loop */ |
| 4270 | } |
| 4271 | if (ch == '(' && !brackets) |
| 4272 | paren++; |
| 4273 | if (ch == ')' && brackets <= 0) |
| 4274 | if (--paren < 0) |
| 4275 | break; |
| 4276 | } |
| 4277 | if (brackets < 0) { |
| 4278 | brackets = 0; |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4279 | nasm_nonfatal("braces do not " |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4280 | "enclose all of macro parameter"); |
| 4281 | } |
| 4282 | paramsize[nparam] += white + 1; |
| 4283 | white = 0; |
| 4284 | } /* parameter loop */ |
| 4285 | nparam++; |
| 4286 | while (m && (m->nparam != nparam || |
| 4287 | mstrcmp(m->name, mname, |
| 4288 | m->casesense))) |
| 4289 | m = m->next; |
| 4290 | if (!m) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4291 | nasm_warnf(ERR_PASS1|ERR_WARN_MNP, |
| 4292 | "macro `%s' exists, " |
| 4293 | "but not taking %d parameters", |
| 4294 | mstart->text, nparam); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4295 | } |
| 4296 | } |
| 4297 | if (m && m->in_progress) |
| 4298 | m = NULL; |
| 4299 | if (!m) { /* in progess or didn't find '(' or wrong nparam */ |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 4300 | /* |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4301 | * Design question: should we handle !tline, which |
| 4302 | * indicates missing ')' here, or expand those |
| 4303 | * macros anyway, which requires the (t) test a few |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 4304 | * lines down? |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4305 | */ |
| 4306 | nasm_free(params); |
| 4307 | nasm_free(paramsize); |
| 4308 | tline = mstart; |
| 4309 | } else { |
| 4310 | /* |
| 4311 | * Expand the macro: we are placed on the last token of the |
| 4312 | * call, so that we can easily split the call from the |
| 4313 | * following tokens. We also start by pushing an SMAC_END |
| 4314 | * token for the cycle removal. |
| 4315 | */ |
| 4316 | t = tline; |
| 4317 | if (t) { |
| 4318 | tline = t->next; |
| 4319 | t->next = NULL; |
| 4320 | } |
| 4321 | tt = new_Token(tline, TOK_SMAC_END, NULL, 0); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4322 | tt->a.mac = m; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4323 | m->in_progress = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4324 | tline = tt; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 4325 | list_for_each(t, m->expansion) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4326 | if (t->type >= TOK_SMAC_PARAM) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4327 | Token *pcopy = tline, **ptail = &pcopy; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4328 | Token *ttt, *pt; |
| 4329 | int i; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4330 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4331 | ttt = params[t->type - TOK_SMAC_PARAM]; |
| 4332 | i = paramsize[t->type - TOK_SMAC_PARAM]; |
| 4333 | while (--i >= 0) { |
| 4334 | pt = *ptail = new_Token(tline, ttt->type, |
| 4335 | ttt->text, 0); |
| 4336 | ptail = &pt->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4337 | ttt = ttt->next; |
Cyrill Gorcunov | 59ce1c6 | 2017-10-22 18:42:07 +0300 | [diff] [blame] | 4338 | if (!ttt && i > 0) { |
| 4339 | /* |
| 4340 | * FIXME: Need to handle more gracefully, |
| 4341 | * exiting early on agruments analysis. |
| 4342 | */ |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4343 | nasm_fatal("macro `%s' expects %d args", |
Cyrill Gorcunov | 59ce1c6 | 2017-10-22 18:42:07 +0300 | [diff] [blame] | 4344 | mstart->text, |
| 4345 | (int)paramsize[t->type - TOK_SMAC_PARAM]); |
| 4346 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4347 | } |
| 4348 | tline = pcopy; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4349 | } else if (t->type == TOK_PREPROC_Q) { |
| 4350 | tt = new_Token(tline, TOK_ID, mname, 0); |
| 4351 | tline = tt; |
| 4352 | } else if (t->type == TOK_PREPROC_QQ) { |
| 4353 | tt = new_Token(tline, TOK_ID, m->name, 0); |
| 4354 | tline = tt; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4355 | } else { |
| 4356 | tt = new_Token(tline, t->type, t->text, 0); |
| 4357 | tline = tt; |
| 4358 | } |
| 4359 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4360 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4361 | /* |
| 4362 | * Having done that, get rid of the macro call, and clean |
| 4363 | * up the parameters. |
| 4364 | */ |
| 4365 | nasm_free(params); |
| 4366 | nasm_free(paramsize); |
| 4367 | free_tlist(mstart); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4368 | expanded = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4369 | continue; /* main token loop */ |
| 4370 | } |
| 4371 | } |
| 4372 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4373 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4374 | if (tline->type == TOK_SMAC_END) { |
Cyrill Gorcunov | 980dd65 | 2018-10-14 19:25:32 +0300 | [diff] [blame] | 4375 | /* On error path it might already be dropped */ |
| 4376 | if (tline->a.mac) |
| 4377 | tline->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4378 | tline = delete_Token(tline); |
| 4379 | } else { |
| 4380 | t = *tail = tline; |
| 4381 | tline = tline->next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4382 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4383 | t->next = NULL; |
| 4384 | tail = &t->next; |
| 4385 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4386 | } |
| 4387 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4388 | /* |
| 4389 | * 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] | 4390 | * after expansion (they can't be produced by tokenize()). The successive |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4391 | * TOK_IDs should be concatenated. |
| 4392 | * Also we look for %+ tokens and concatenate the tokens before and after |
| 4393 | * them (without white spaces in between). |
| 4394 | */ |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4395 | if (expanded) { |
Cyrill Gorcunov | c6a742c | 2011-06-27 01:23:09 +0400 | [diff] [blame] | 4396 | const struct tokseq_match t[] = { |
| 4397 | { |
| 4398 | PP_CONCAT_MASK(TOK_ID) | |
| 4399 | PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */ |
| 4400 | PP_CONCAT_MASK(TOK_ID) | |
| 4401 | PP_CONCAT_MASK(TOK_PREPROC_ID) | |
| 4402 | PP_CONCAT_MASK(TOK_NUMBER) /* tail */ |
| 4403 | } |
| 4404 | }; |
| 4405 | if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) { |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4406 | /* |
| 4407 | * If we concatenated something, *and* we had previously expanded |
| 4408 | * an actual macro, scan the lines again for macros... |
| 4409 | */ |
| 4410 | tline = thead; |
| 4411 | expanded = false; |
| 4412 | goto again; |
| 4413 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4414 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4415 | |
Cyrill Gorcunov | bd38c8f | 2009-11-21 11:11:23 +0300 | [diff] [blame] | 4416 | err: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4417 | if (org_tline) { |
| 4418 | if (thead) { |
| 4419 | *org_tline = *thead; |
| 4420 | /* since we just gave text to org_line, don't free it */ |
| 4421 | thead->text = NULL; |
| 4422 | delete_Token(thead); |
| 4423 | } else { |
| 4424 | /* the expression expanded to empty line; |
| 4425 | we can't return NULL for some reasons |
| 4426 | we just set the line to a single WHITESPACE token. */ |
| 4427 | memset(org_tline, 0, sizeof(*org_tline)); |
| 4428 | org_tline->text = NULL; |
| 4429 | org_tline->type = TOK_WHITESPACE; |
| 4430 | } |
| 4431 | thead = org_tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4432 | } |
| 4433 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4434 | return thead; |
| 4435 | } |
| 4436 | |
| 4437 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4438 | * Similar to expand_smacro but used exclusively with macro identifiers |
| 4439 | * right before they are fetched in. The reason is that there can be |
| 4440 | * identifiers consisting of several subparts. We consider that if there |
| 4441 | * are more than one element forming the name, user wants a expansion, |
| 4442 | * otherwise it will be left as-is. Example: |
| 4443 | * |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4444 | * %define %$abc cde |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4445 | * |
| 4446 | * the identifier %$abc will be left as-is so that the handler for %define |
| 4447 | * will suck it and define the corresponding value. Other case: |
| 4448 | * |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4449 | * %define _%$abc cde |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4450 | * |
| 4451 | * In this case user wants name to be expanded *before* %define starts |
| 4452 | * working, so we'll expand %$abc into something (if it has a value; |
| 4453 | * otherwise it will be left as-is) then concatenate all successive |
| 4454 | * PP_IDs into one. |
| 4455 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4456 | static Token *expand_id(Token * tline) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4457 | { |
| 4458 | Token *cur, *oldnext = NULL; |
| 4459 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4460 | if (!tline || !tline->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4461 | return tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4462 | |
| 4463 | cur = tline; |
| 4464 | while (cur->next && |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4465 | (cur->next->type == TOK_ID || |
| 4466 | cur->next->type == TOK_PREPROC_ID |
| 4467 | || cur->next->type == TOK_NUMBER)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4468 | cur = cur->next; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4469 | |
| 4470 | /* If identifier consists of just one token, don't expand */ |
| 4471 | if (cur == tline) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4472 | return tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4473 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4474 | if (cur) { |
| 4475 | oldnext = cur->next; /* Detach the tail past identifier */ |
| 4476 | cur->next = NULL; /* so that expand_smacro stops here */ |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4477 | } |
| 4478 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4479 | tline = expand_smacro(tline); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4480 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4481 | if (cur) { |
| 4482 | /* expand_smacro possibly changhed tline; re-scan for EOL */ |
| 4483 | cur = tline; |
| 4484 | while (cur && cur->next) |
| 4485 | cur = cur->next; |
| 4486 | if (cur) |
| 4487 | cur->next = oldnext; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4488 | } |
| 4489 | |
| 4490 | return tline; |
| 4491 | } |
| 4492 | |
| 4493 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4494 | * Determine whether the given line constitutes a multi-line macro |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4495 | * call, and return the MMacro structure called if so. Doesn't have |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4496 | * to check for an initial label - that's taken care of in |
| 4497 | * expand_mmacro - but must check numbers of parameters. Guaranteed |
| 4498 | * to be called with tline->type == TOK_ID, so the putative macro |
| 4499 | * name is easy to find. |
| 4500 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4501 | static MMacro *is_mmacro(Token * tline, Token *** params_array) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4502 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4503 | MMacro *head, *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4504 | Token **params; |
| 4505 | int nparam; |
| 4506 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4507 | head = (MMacro *) hash_findix(&mmacros, tline->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4508 | |
| 4509 | /* |
| 4510 | * Efficiency: first we see if any macro exists with the given |
| 4511 | * name. If not, we can return NULL immediately. _Then_ we |
| 4512 | * count the parameters, and then we look further along the |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4513 | * list if necessary to find the proper MMacro. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4514 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4515 | list_for_each(m, head) |
| 4516 | if (!mstrcmp(m->name, tline->text, m->casesense)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4517 | break; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4518 | if (!m) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4519 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4520 | |
| 4521 | /* |
| 4522 | * OK, we have a potential macro. Count and demarcate the |
| 4523 | * parameters. |
| 4524 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4525 | count_mmac_params(tline->next, &nparam, ¶ms); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4526 | |
| 4527 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4528 | * So we know how many parameters we've got. Find the MMacro |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4529 | * structure that handles this number. |
| 4530 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4531 | while (m) { |
| 4532 | if (m->nparam_min <= nparam |
| 4533 | && (m->plus || nparam <= m->nparam_max)) { |
| 4534 | /* |
| 4535 | * This one is right. Just check if cycle removal |
| 4536 | * prohibits us using it before we actually celebrate... |
| 4537 | */ |
| 4538 | if (m->in_progress > m->max_depth) { |
| 4539 | if (m->max_depth > 0) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4540 | nasm_warn("reached maximum recursion depth of %i", |
| 4541 | m->max_depth); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4542 | } |
| 4543 | nasm_free(params); |
| 4544 | return NULL; |
| 4545 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4546 | /* |
| 4547 | * It's right, and we can use it. Add its default |
| 4548 | * parameters to the end of our list if necessary. |
| 4549 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4550 | if (m->defaults && nparam < m->nparam_min + m->ndefs) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4551 | params = |
| 4552 | nasm_realloc(params, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4553 | ((m->nparam_min + m->ndefs + |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4554 | 1) * sizeof(*params))); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4555 | while (nparam < m->nparam_min + m->ndefs) { |
| 4556 | params[nparam] = m->defaults[nparam - m->nparam_min]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4557 | nparam++; |
| 4558 | } |
| 4559 | } |
| 4560 | /* |
| 4561 | * If we've gone over the maximum parameter count (and |
| 4562 | * we're in Plus mode), ignore parameters beyond |
| 4563 | * nparam_max. |
| 4564 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4565 | if (m->plus && nparam > m->nparam_max) |
| 4566 | nparam = m->nparam_max; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4567 | /* |
| 4568 | * Then terminate the parameter list, and leave. |
| 4569 | */ |
| 4570 | if (!params) { /* need this special case */ |
| 4571 | params = nasm_malloc(sizeof(*params)); |
| 4572 | nparam = 0; |
| 4573 | } |
| 4574 | params[nparam] = NULL; |
| 4575 | *params_array = params; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4576 | return m; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4577 | } |
| 4578 | /* |
| 4579 | * This one wasn't right: look for the next one with the |
| 4580 | * same name. |
| 4581 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4582 | list_for_each(m, m->next) |
| 4583 | if (!mstrcmp(m->name, tline->text, m->casesense)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4584 | break; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4585 | } |
| 4586 | |
| 4587 | /* |
| 4588 | * After all that, we didn't find one with the right number of |
| 4589 | * parameters. Issue a warning, and fail to expand the macro. |
| 4590 | */ |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4591 | nasm_warnf(ERR_PASS1|ERR_WARN_MNP, |
| 4592 | "macro `%s' exists, but not taking %d parameters", |
| 4593 | tline->text, nparam); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4594 | nasm_free(params); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4595 | return NULL; |
| 4596 | } |
| 4597 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4598 | |
| 4599 | /* |
| 4600 | * Save MMacro invocation specific fields in |
| 4601 | * preparation for a recursive macro expansion |
| 4602 | */ |
| 4603 | static void push_mmacro(MMacro *m) |
| 4604 | { |
| 4605 | MMacroInvocation *i; |
| 4606 | |
| 4607 | i = nasm_malloc(sizeof(MMacroInvocation)); |
| 4608 | i->prev = m->prev; |
| 4609 | i->params = m->params; |
| 4610 | i->iline = m->iline; |
| 4611 | i->nparam = m->nparam; |
| 4612 | i->rotate = m->rotate; |
| 4613 | i->paramlen = m->paramlen; |
| 4614 | i->unique = m->unique; |
| 4615 | i->condcnt = m->condcnt; |
| 4616 | m->prev = i; |
| 4617 | } |
| 4618 | |
| 4619 | |
| 4620 | /* |
| 4621 | * Restore MMacro invocation specific fields that were |
| 4622 | * saved during a previous recursive macro expansion |
| 4623 | */ |
| 4624 | static void pop_mmacro(MMacro *m) |
| 4625 | { |
| 4626 | MMacroInvocation *i; |
| 4627 | |
| 4628 | if (m->prev) { |
| 4629 | i = m->prev; |
| 4630 | m->prev = i->prev; |
| 4631 | m->params = i->params; |
| 4632 | m->iline = i->iline; |
| 4633 | m->nparam = i->nparam; |
| 4634 | m->rotate = i->rotate; |
| 4635 | m->paramlen = i->paramlen; |
| 4636 | m->unique = i->unique; |
| 4637 | m->condcnt = i->condcnt; |
| 4638 | nasm_free(i); |
| 4639 | } |
| 4640 | } |
| 4641 | |
| 4642 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4643 | /* |
| 4644 | * Expand the multi-line macro call made by the given line, if |
| 4645 | * there is one to be expanded. If there is, push the expansion on |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4646 | * istk->expansion and return 1. Otherwise return 0. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4647 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4648 | static int expand_mmacro(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4649 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4650 | Token *startline = tline; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4651 | Token *label = NULL; |
| 4652 | int dont_prepend = 0; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4653 | Token **params, *t, *tt; |
| 4654 | MMacro *m; |
| 4655 | Line *l, *ll; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4656 | int i, nparam, *paramlen; |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 4657 | const char *mname; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4658 | |
| 4659 | t = tline; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4660 | skip_white_(t); |
H. Peter Anvin | ce2233b | 2008-05-25 21:57:00 -0700 | [diff] [blame] | 4661 | /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */ |
H. Peter Anvin | dce1e2f | 2002-04-30 21:06:37 +0000 | [diff] [blame] | 4662 | if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID)) |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4663 | return 0; |
| 4664 | m = is_mmacro(t, ¶ms); |
| 4665 | if (m) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4666 | mname = t->text; |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 4667 | } else { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4668 | Token *last; |
| 4669 | /* |
| 4670 | * We have an id which isn't a macro call. We'll assume |
| 4671 | * it might be a label; we'll also check to see if a |
| 4672 | * colon follows it. Then, if there's another id after |
| 4673 | * that lot, we'll check it again for macro-hood. |
| 4674 | */ |
| 4675 | label = last = t; |
| 4676 | t = t->next; |
| 4677 | if (tok_type_(t, TOK_WHITESPACE)) |
| 4678 | last = t, t = t->next; |
| 4679 | if (tok_is_(t, ":")) { |
| 4680 | dont_prepend = 1; |
| 4681 | last = t, t = t->next; |
| 4682 | if (tok_type_(t, TOK_WHITESPACE)) |
| 4683 | last = t, t = t->next; |
| 4684 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4685 | if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, ¶ms))) |
| 4686 | return 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4687 | last->next = NULL; |
Keith Kanios | 891775e | 2009-07-11 06:08:54 -0500 | [diff] [blame] | 4688 | mname = t->text; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4689 | tline = t; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4690 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4691 | |
| 4692 | /* |
| 4693 | * Fix up the parameters: this involves stripping leading and |
| 4694 | * trailing whitespace, then stripping braces if they are |
| 4695 | * present. |
| 4696 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4697 | for (nparam = 0; params[nparam]; nparam++) ; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4698 | paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4699 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4700 | for (i = 0; params[i]; i++) { |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4701 | int brace = 0; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4702 | int comma = (!m->plus || i < nparam - 1); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4703 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4704 | t = params[i]; |
| 4705 | skip_white_(t); |
| 4706 | if (tok_is_(t, "{")) |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4707 | t = t->next, brace++, comma = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4708 | params[i] = t; |
| 4709 | paramlen[i] = 0; |
| 4710 | while (t) { |
| 4711 | if (comma && t->type == TOK_OTHER && !strcmp(t->text, ",")) |
| 4712 | break; /* ... because we have hit a comma */ |
| 4713 | if (comma && t->type == TOK_WHITESPACE |
| 4714 | && tok_is_(t->next, ",")) |
| 4715 | break; /* ... or a space then a comma */ |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4716 | if (brace && t->type == TOK_OTHER) { |
| 4717 | if (t->text[0] == '{') |
| 4718 | brace++; /* ... or a nested opening brace */ |
| 4719 | else if (t->text[0] == '}') |
| 4720 | if (!--brace) |
| 4721 | break; /* ... or a brace */ |
| 4722 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4723 | t = t->next; |
| 4724 | paramlen[i]++; |
| 4725 | } |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4726 | if (brace) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4727 | nasm_nonfatal("macro params should be enclosed in braces"); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4728 | } |
| 4729 | |
| 4730 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4731 | * OK, we have a MMacro structure together with a set of |
| 4732 | * parameters. We must now go through the expansion and push |
| 4733 | * copies of each Line on to istk->expansion. Substitution of |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4734 | * parameter tokens and macro-local tokens doesn't get done |
| 4735 | * until the single-line macro substitution process; this is |
| 4736 | * because delaying them allows us to change the semantics |
| 4737 | * later through %rotate. |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4738 | * |
| 4739 | * First, push an end marker on to istk->expansion, mark this |
| 4740 | * macro as in progress, and set up its invocation-specific |
| 4741 | * variables. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4742 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4743 | ll = nasm_malloc(sizeof(Line)); |
| 4744 | ll->next = istk->expansion; |
| 4745 | ll->finishes = m; |
| 4746 | ll->first = NULL; |
| 4747 | istk->expansion = ll; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4748 | |
| 4749 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4750 | * Save the previous MMacro expansion in the case of |
| 4751 | * macro recursion |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4752 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4753 | if (m->max_depth && m->in_progress) |
| 4754 | push_mmacro(m); |
| 4755 | |
| 4756 | m->in_progress ++; |
| 4757 | m->params = params; |
| 4758 | m->iline = tline; |
| 4759 | m->nparam = nparam; |
| 4760 | m->rotate = 0; |
| 4761 | m->paramlen = paramlen; |
| 4762 | m->unique = unique++; |
| 4763 | m->lineno = 0; |
| 4764 | m->condcnt = 0; |
| 4765 | |
| 4766 | m->next_active = istk->mstk; |
| 4767 | istk->mstk = m; |
| 4768 | |
| 4769 | list_for_each(l, m->expansion) { |
| 4770 | Token **tail; |
| 4771 | |
| 4772 | ll = nasm_malloc(sizeof(Line)); |
| 4773 | ll->finishes = NULL; |
| 4774 | ll->next = istk->expansion; |
| 4775 | istk->expansion = ll; |
| 4776 | tail = &ll->first; |
| 4777 | |
| 4778 | list_for_each(t, l->first) { |
| 4779 | Token *x = t; |
| 4780 | switch (t->type) { |
| 4781 | case TOK_PREPROC_Q: |
| 4782 | tt = *tail = new_Token(NULL, TOK_ID, mname, 0); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4783 | break; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4784 | case TOK_PREPROC_QQ: |
| 4785 | tt = *tail = new_Token(NULL, TOK_ID, m->name, 0); |
| 4786 | break; |
| 4787 | case TOK_PREPROC_ID: |
| 4788 | if (t->text[1] == '0' && t->text[2] == '0') { |
| 4789 | dont_prepend = -1; |
| 4790 | x = label; |
| 4791 | if (!x) |
| 4792 | continue; |
| 4793 | } |
| 4794 | /* fall through */ |
| 4795 | default: |
| 4796 | tt = *tail = new_Token(NULL, x->type, x->text, 0); |
| 4797 | break; |
| 4798 | } |
| 4799 | tail = &tt->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4800 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4801 | *tail = NULL; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4802 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4803 | |
| 4804 | /* |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4805 | * If we had a label, push it on as the first line of |
| 4806 | * the macro expansion. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4807 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4808 | if (label) { |
| 4809 | if (dont_prepend < 0) |
| 4810 | free_tlist(startline); |
| 4811 | else { |
| 4812 | ll = nasm_malloc(sizeof(Line)); |
| 4813 | ll->finishes = NULL; |
| 4814 | ll->next = istk->expansion; |
| 4815 | istk->expansion = ll; |
| 4816 | ll->first = startline; |
| 4817 | if (!dont_prepend) { |
| 4818 | while (label->next) |
| 4819 | label = label->next; |
| 4820 | label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4821 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4822 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4823 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4824 | |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 4825 | lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4826 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4827 | return 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4828 | } |
| 4829 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4830 | /* |
| 4831 | * This function adds macro names to error messages, and suppresses |
| 4832 | * them if necessary. |
| 4833 | */ |
| 4834 | static void pp_verror(int severity, const char *fmt, va_list arg) |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4835 | { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4836 | char buff[BUFSIZ]; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4837 | MMacro *mmac = NULL; |
| 4838 | int delta = 0; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4839 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4840 | /* |
| 4841 | * If we're in a dead branch of IF or something like it, ignore the error. |
| 4842 | * However, because %else etc are evaluated in the state context |
| 4843 | * of the previous branch, errors might get lost: |
| 4844 | * %if 0 ... %else trailing garbage ... %endif |
| 4845 | * So %else etc should set the ERR_PP_PRECOND flag. |
| 4846 | */ |
| 4847 | if ((severity & ERR_MASK) < ERR_FATAL && |
| 4848 | istk && istk->conds && |
| 4849 | ((severity & ERR_PP_PRECOND) ? |
| 4850 | istk->conds->state == COND_NEVER : |
H. Peter Anvin | eb6653f | 2016-04-05 13:03:10 -0700 | [diff] [blame] | 4851 | !emitting(istk->conds->state))) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4852 | return; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4853 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4854 | /* get %macro name */ |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4855 | if (!(severity & ERR_NOFILE) && istk && istk->mstk) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4856 | mmac = istk->mstk; |
| 4857 | /* but %rep blocks should be skipped */ |
| 4858 | while (mmac && !mmac->name) |
| 4859 | mmac = mmac->next_active, delta++; |
Cyrill Gorcunov | 9900c6b | 2011-10-09 18:58:46 +0400 | [diff] [blame] | 4860 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4861 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4862 | if (mmac) { |
| 4863 | vsnprintf(buff, sizeof(buff), fmt, arg); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4864 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4865 | nasm_set_verror(real_verror); |
| 4866 | nasm_error(severity, "(%s:%d) %s", |
| 4867 | mmac->name, mmac->lineno - delta, buff); |
| 4868 | nasm_set_verror(pp_verror); |
| 4869 | } else { |
| 4870 | real_verror(severity, fmt, arg); |
| 4871 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4872 | } |
| 4873 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4874 | static void |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 4875 | pp_reset(const char *file, int apass, struct strlist *dep_list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4876 | { |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 4877 | Token *t; |
| 4878 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4879 | cstk = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4880 | istk = nasm_malloc(sizeof(Include)); |
| 4881 | istk->next = NULL; |
| 4882 | istk->conds = NULL; |
| 4883 | istk->expansion = NULL; |
| 4884 | istk->mstk = NULL; |
H. Peter Anvin | 3e83cec | 2016-05-25 04:28:46 -0700 | [diff] [blame] | 4885 | istk->fp = nasm_open_read(file, NF_TEXT); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4886 | istk->fname = NULL; |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 4887 | src_set(0, file); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4888 | istk->lineinc = 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4889 | if (!istk->fp) |
Cyrill Gorcunov | c3527dd | 2018-11-24 22:17:47 +0300 | [diff] [blame] | 4890 | nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4891 | defining = NULL; |
Charles Crayne | d4200be | 2008-07-12 16:42:33 -0700 | [diff] [blame] | 4892 | nested_mac_count = 0; |
| 4893 | nested_rep_count = 0; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 4894 | init_macros(); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4895 | unique = 0; |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 4896 | deplist = dep_list; |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 4897 | |
| 4898 | if (tasm_compatible_mode) |
| 4899 | pp_add_stdmac(nasm_stdmac_tasm); |
| 4900 | |
| 4901 | pp_add_stdmac(nasm_stdmac_nasm); |
| 4902 | pp_add_stdmac(nasm_stdmac_version); |
| 4903 | |
Cyrill Gorcunov | 15ce78f | 2017-01-06 20:21:28 +0300 | [diff] [blame] | 4904 | if (extrastdmac) |
| 4905 | pp_add_stdmac(extrastdmac); |
| 4906 | |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 4907 | stdmacpos = stdmacros[0]; |
| 4908 | stdmacnext = &stdmacros[1]; |
| 4909 | |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 4910 | do_predef = true; |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 4911 | |
| 4912 | /* |
| 4913 | * 0 for dependencies, 1 for preparatory passes, 2 for final pass. |
| 4914 | * The caller, however, will also pass in 3 for preprocess-only so |
| 4915 | * we can set __PASS__ accordingly. |
| 4916 | */ |
| 4917 | pass = apass > 2 ? 2 : apass; |
| 4918 | |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 4919 | strlist_add(deplist, file); |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 4920 | |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 4921 | /* |
| 4922 | * Define the __PASS__ macro. This is defined here unlike |
| 4923 | * all the other builtins, because it is special -- it varies between |
| 4924 | * passes. |
| 4925 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4926 | t = nasm_malloc(sizeof(*t)); |
| 4927 | t->next = NULL; |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 4928 | make_tok_num(t, apass); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4929 | t->a.mac = NULL; |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 4930 | define_smacro(NULL, "__PASS__", true, 0, t); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4931 | } |
| 4932 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 4933 | static void pp_init(void) |
| 4934 | { |
| 4935 | hash_init(&FileHash, HASH_MEDIUM); |
| 4936 | } |
| 4937 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4938 | static char *pp_getline(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4939 | { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4940 | char *line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4941 | Token *tline; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4942 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4943 | real_verror = nasm_set_verror(pp_verror); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4944 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4945 | while (1) { |
| 4946 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4947 | * Fetch a tokenized line, either from the macro-expansion |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4948 | * buffer or from the input file. |
| 4949 | */ |
| 4950 | tline = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4951 | while (istk->expansion && istk->expansion->finishes) { |
| 4952 | Line *l = istk->expansion; |
| 4953 | if (!l->finishes->name && l->finishes->in_progress > 1) { |
| 4954 | Line *ll; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4955 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4956 | /* |
| 4957 | * This is a macro-end marker for a macro with no |
| 4958 | * name, which means it's not really a macro at all |
| 4959 | * but a %rep block, and the `in_progress' field is |
| 4960 | * more than 1, meaning that we still need to |
| 4961 | * repeat. (1 means the natural last repetition; 0 |
| 4962 | * means termination by %exitrep.) We have |
| 4963 | * therefore expanded up to the %endrep, and must |
| 4964 | * push the whole block on to the expansion buffer |
| 4965 | * again. We don't bother to remove the macro-end |
| 4966 | * marker: we'd only have to generate another one |
| 4967 | * if we did. |
| 4968 | */ |
| 4969 | l->finishes->in_progress--; |
| 4970 | list_for_each(l, l->finishes->expansion) { |
| 4971 | Token *t, *tt, **tail; |
| 4972 | |
| 4973 | ll = nasm_malloc(sizeof(Line)); |
| 4974 | ll->next = istk->expansion; |
| 4975 | ll->finishes = NULL; |
| 4976 | ll->first = NULL; |
| 4977 | tail = &ll->first; |
| 4978 | |
| 4979 | list_for_each(t, l->first) { |
| 4980 | if (t->text || t->type == TOK_WHITESPACE) { |
| 4981 | tt = *tail = new_Token(NULL, t->type, t->text, 0); |
| 4982 | tail = &tt->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4983 | } |
| 4984 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4985 | |
| 4986 | istk->expansion = ll; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4987 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4988 | } else { |
| 4989 | /* |
| 4990 | * Check whether a `%rep' was started and not ended |
| 4991 | * within this macro expansion. This can happen and |
| 4992 | * should be detected. It's a fatal error because |
| 4993 | * I'm too confused to work out how to recover |
| 4994 | * sensibly from it. |
| 4995 | */ |
| 4996 | if (defining) { |
| 4997 | if (defining->name) |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 4998 | nasm_panic("defining with name in expansion"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4999 | else if (istk->mstk->name) |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 5000 | nasm_fatal("`%%rep' without `%%endrep' within" |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5001 | " expansion of macro `%s'", |
| 5002 | istk->mstk->name); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5003 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5004 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5005 | /* |
| 5006 | * FIXME: investigate the relationship at this point between |
| 5007 | * istk->mstk and l->finishes |
| 5008 | */ |
| 5009 | { |
| 5010 | MMacro *m = istk->mstk; |
| 5011 | istk->mstk = m->next_active; |
| 5012 | if (m->name) { |
| 5013 | /* |
| 5014 | * This was a real macro call, not a %rep, and |
| 5015 | * therefore the parameter information needs to |
| 5016 | * be freed. |
| 5017 | */ |
| 5018 | if (m->prev) { |
| 5019 | pop_mmacro(m); |
| 5020 | l->finishes->in_progress --; |
| 5021 | } else { |
| 5022 | nasm_free(m->params); |
| 5023 | free_tlist(m->iline); |
| 5024 | nasm_free(m->paramlen); |
| 5025 | l->finishes->in_progress = 0; |
| 5026 | } |
Adam Majer | 91e7240 | 2017-07-25 10:42:01 +0200 | [diff] [blame] | 5027 | } |
| 5028 | |
| 5029 | /* |
| 5030 | * FIXME It is incorrect to always free_mmacro here. |
| 5031 | * It leads to usage-after-free. |
| 5032 | * |
| 5033 | * https://bugzilla.nasm.us/show_bug.cgi?id=3392414 |
| 5034 | */ |
| 5035 | #if 0 |
| 5036 | else |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5037 | free_mmacro(m); |
Adam Majer | 91e7240 | 2017-07-25 10:42:01 +0200 | [diff] [blame] | 5038 | #endif |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5039 | } |
| 5040 | istk->expansion = l->next; |
| 5041 | nasm_free(l); |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 5042 | lfmt->downlevel(LIST_MACRO); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5043 | } |
| 5044 | } |
| 5045 | while (1) { /* until we get a line we can use */ |
| 5046 | |
| 5047 | if (istk->expansion) { /* from a macro expansion */ |
| 5048 | char *p; |
| 5049 | Line *l = istk->expansion; |
| 5050 | if (istk->mstk) |
| 5051 | istk->mstk->lineno++; |
| 5052 | tline = l->first; |
| 5053 | istk->expansion = l->next; |
| 5054 | nasm_free(l); |
| 5055 | p = detoken(tline, false); |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 5056 | lfmt->line(LIST_MACRO, p); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5057 | nasm_free(p); |
| 5058 | break; |
| 5059 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5060 | line = read_line(); |
| 5061 | if (line) { /* from the current input file */ |
| 5062 | line = prepreproc(line); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5063 | tline = tokenize(line); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5064 | nasm_free(line); |
| 5065 | break; |
| 5066 | } |
| 5067 | /* |
| 5068 | * The current file has ended; work down the istk |
| 5069 | */ |
| 5070 | { |
| 5071 | Include *i = istk; |
| 5072 | fclose(i->fp); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5073 | if (i->conds) { |
| 5074 | /* nasm_error can't be conditionally suppressed */ |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 5075 | nasm_fatal("expected `%%endif' before end of file"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5076 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5077 | /* only set line and file name if there's a next node */ |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 5078 | if (i->next) |
| 5079 | src_set(i->lineno, i->fname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5080 | istk = i->next; |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 5081 | lfmt->downlevel(LIST_INCLUDE); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5082 | nasm_free(i); |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5083 | if (!istk) { |
| 5084 | line = NULL; |
| 5085 | goto done; |
| 5086 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5087 | if (istk->expansion && istk->expansion->finishes) |
| 5088 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5089 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5090 | } |
| 5091 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5092 | /* |
| 5093 | * We must expand MMacro parameters and MMacro-local labels |
| 5094 | * _before_ we plunge into directive processing, to cope |
| 5095 | * with things like `%define something %1' such as STRUC |
| 5096 | * uses. Unless we're _defining_ a MMacro, in which case |
| 5097 | * those tokens should be left alone to go into the |
| 5098 | * definition; and unless we're in a non-emitting |
| 5099 | * condition, in which case we don't want to meddle with |
| 5100 | * anything. |
| 5101 | */ |
| 5102 | if (!defining && !(istk->conds && !emitting(istk->conds->state)) |
| 5103 | && !(istk->mstk && !istk->mstk->in_progress)) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5104 | tline = expand_mmac_params(tline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5105 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5106 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5107 | /* |
| 5108 | * Check the line to see if it's a preprocessor directive. |
| 5109 | */ |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 5110 | if (do_directive(tline, &line) == DIRECTIVE_FOUND) { |
| 5111 | if (line) |
| 5112 | break; /* Directive generated output */ |
| 5113 | else |
| 5114 | continue; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5115 | } else if (defining) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5116 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5117 | * We're defining a multi-line macro. We emit nothing |
| 5118 | * at all, and just |
| 5119 | * shove the tokenized line on to the macro definition. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5120 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5121 | Line *l = nasm_malloc(sizeof(Line)); |
| 5122 | l->next = defining->expansion; |
| 5123 | l->first = tline; |
| 5124 | l->finishes = NULL; |
| 5125 | defining->expansion = l; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5126 | continue; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5127 | } else if (istk->conds && !emitting(istk->conds->state)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5128 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5129 | * We're in a non-emitting branch of a condition block. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5130 | * Emit nothing at all, not even a blank line: when we |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5131 | * emerge from the condition we'll give a line-number |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5132 | * directive so we keep our place correctly. |
| 5133 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5134 | free_tlist(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5135 | continue; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5136 | } else if (istk->mstk && !istk->mstk->in_progress) { |
| 5137 | /* |
| 5138 | * We're in a %rep block which has been terminated, so |
| 5139 | * we're walking through to the %endrep without |
| 5140 | * emitting anything. Emit nothing at all, not even a |
| 5141 | * blank line: when we emerge from the %rep block we'll |
| 5142 | * give a line-number directive so we keep our place |
| 5143 | * correctly. |
| 5144 | */ |
| 5145 | free_tlist(tline); |
| 5146 | continue; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5147 | } else { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5148 | tline = expand_smacro(tline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5149 | if (!expand_mmacro(tline)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5150 | /* |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5151 | * De-tokenize the line again, and emit it. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5152 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5153 | line = detoken(tline, true); |
| 5154 | free_tlist(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5155 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5156 | } else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5157 | continue; /* expand_mmacro calls free_tlist */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5158 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5159 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5160 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5161 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5162 | done: |
| 5163 | nasm_set_verror(real_verror); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5164 | return line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5165 | } |
| 5166 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5167 | static void pp_cleanup(int pass) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5168 | { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5169 | real_verror = nasm_set_verror(pp_verror); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5170 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5171 | if (defining) { |
| 5172 | if (defining->name) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 5173 | nasm_nonfatal("end of file while still defining macro `%s'", |
| 5174 | defining->name); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5175 | } else { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 5176 | nasm_nonfatal("end of file while still in %%rep"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5177 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5178 | |
| 5179 | free_mmacro(defining); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5180 | defining = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5181 | } |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5182 | |
| 5183 | nasm_set_verror(real_verror); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5184 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5185 | while (cstk) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5186 | ctx_pop(); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 5187 | free_macros(); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5188 | while (istk) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5189 | Include *i = istk; |
| 5190 | istk = istk->next; |
| 5191 | fclose(i->fp); |
Cyrill Gorcunov | 8dcfd88 | 2011-03-03 09:18:56 +0300 | [diff] [blame] | 5192 | nasm_free(i); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5193 | } |
| 5194 | while (cstk) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5195 | ctx_pop(); |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 5196 | src_set_fname(NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5197 | if (pass == 0) { |
| 5198 | free_llist(predef); |
Cyrill Gorcunov | dae24d7 | 2014-06-28 10:17:39 +0400 | [diff] [blame] | 5199 | predef = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5200 | delete_Blocks(); |
Cyrill Gorcunov | dae24d7 | 2014-06-28 10:17:39 +0400 | [diff] [blame] | 5201 | freeTokens = NULL; |
Cyrill Gorcunov | 8c0666b | 2018-11-24 14:33:48 +0300 | [diff] [blame] | 5202 | ipath_list = NULL; |
H. Peter Anvin | 11dfa1a | 2008-07-02 18:11:04 -0700 | [diff] [blame] | 5203 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5204 | } |
| 5205 | |
Cyrill Gorcunov | 8c0666b | 2018-11-24 14:33:48 +0300 | [diff] [blame] | 5206 | static void pp_include_path(struct strlist *list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5207 | { |
Cyrill Gorcunov | 8c0666b | 2018-11-24 14:33:48 +0300 | [diff] [blame] | 5208 | ipath_list = list; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5209 | } |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5210 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5211 | static void pp_pre_include(char *fname) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5212 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5213 | Token *inc, *space, *name; |
| 5214 | Line *l; |
| 5215 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5216 | name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0); |
| 5217 | space = new_Token(name, TOK_WHITESPACE, NULL, 0); |
| 5218 | inc = new_Token(space, TOK_PREPROC_ID, "%include", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5219 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5220 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5221 | l->next = predef; |
| 5222 | l->first = inc; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5223 | l->finishes = NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5224 | predef = l; |
| 5225 | } |
| 5226 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5227 | static void pp_pre_define(char *definition) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5228 | { |
| 5229 | Token *def, *space; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5230 | Line *l; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5231 | char *equals; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5232 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5233 | real_verror = nasm_set_verror(pp_verror); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5234 | |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5235 | equals = strchr(definition, '='); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5236 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 5237 | def = new_Token(space, TOK_PREPROC_ID, "%define", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5238 | if (equals) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5239 | *equals = ' '; |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5240 | space->next = tokenize(definition); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5241 | if (equals) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5242 | *equals = '='; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5243 | |
Cyrill Gorcunov | 6d42e9b | 2015-02-08 11:07:17 +0300 | [diff] [blame] | 5244 | if (space->next->type != TOK_PREPROC_ID && |
| 5245 | space->next->type != TOK_ID) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 5246 | nasm_warn("pre-defining non ID `%s\'\n", definition); |
Cyrill Gorcunov | 6d42e9b | 2015-02-08 11:07:17 +0300 | [diff] [blame] | 5247 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5248 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5249 | l->next = predef; |
| 5250 | l->first = def; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5251 | l->finishes = NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5252 | predef = l; |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5253 | |
| 5254 | nasm_set_verror(real_verror); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5255 | } |
| 5256 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5257 | static void pp_pre_undefine(char *definition) |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5258 | { |
| 5259 | Token *def, *space; |
| 5260 | Line *l; |
| 5261 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5262 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 5263 | def = new_Token(space, TOK_PREPROC_ID, "%undef", 0); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5264 | space->next = tokenize(definition); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5265 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5266 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5267 | l->next = predef; |
| 5268 | l->first = def; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5269 | l->finishes = NULL; |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5270 | predef = l; |
| 5271 | } |
| 5272 | |
H. Peter Anvin | 0599034 | 2018-06-11 13:32:42 -0700 | [diff] [blame] | 5273 | /* Insert an early preprocessor command that doesn't need special handling */ |
| 5274 | static void pp_pre_command(const char *what, char *string) |
| 5275 | { |
| 5276 | char *cmd; |
| 5277 | Token *def, *space; |
| 5278 | Line *l; |
| 5279 | |
| 5280 | def = tokenize(string); |
| 5281 | if (what) { |
| 5282 | cmd = nasm_strcat(what[0] == '%' ? "" : "%", what); |
| 5283 | space = new_Token(def, TOK_WHITESPACE, NULL, 0); |
| 5284 | def = new_Token(space, TOK_PREPROC_ID, cmd, 0); |
| 5285 | } |
| 5286 | |
| 5287 | l = nasm_malloc(sizeof(Line)); |
| 5288 | l->next = predef; |
| 5289 | l->first = def; |
| 5290 | l->finishes = NULL; |
| 5291 | predef = l; |
| 5292 | } |
| 5293 | |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 5294 | static void pp_add_stdmac(macros_t *macros) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5295 | { |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 5296 | macros_t **mp; |
| 5297 | |
| 5298 | /* Find the end of the list and avoid duplicates */ |
| 5299 | for (mp = stdmacros; *mp; mp++) { |
| 5300 | if (*mp == macros) |
| 5301 | return; /* Nothing to do */ |
| 5302 | } |
| 5303 | |
| 5304 | nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]); |
| 5305 | |
| 5306 | *mp = macros; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 5307 | } |
| 5308 | |
Cyrill Gorcunov | 15ce78f | 2017-01-06 20:21:28 +0300 | [diff] [blame] | 5309 | static void pp_extra_stdmac(macros_t *macros) |
| 5310 | { |
| 5311 | extrastdmac = macros; |
| 5312 | } |
| 5313 | |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 5314 | static void make_tok_num(Token * tok, int64_t val) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5315 | { |
Cyrill Gorcunov | ce65274 | 2013-05-06 23:43:43 +0400 | [diff] [blame] | 5316 | char numbuf[32]; |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 5317 | snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5318 | tok->text = nasm_strdup(numbuf); |
| 5319 | tok->type = TOK_NUMBER; |
| 5320 | } |
| 5321 | |
H. Peter Anvin | 3736895 | 2016-05-09 14:10:32 -0700 | [diff] [blame] | 5322 | static void pp_list_one_macro(MMacro *m, int severity) |
| 5323 | { |
| 5324 | if (!m) |
| 5325 | return; |
| 5326 | |
| 5327 | /* We need to print the next_active list in reverse order */ |
| 5328 | pp_list_one_macro(m->next_active, severity); |
| 5329 | |
| 5330 | if (m->name && !m->nolist) { |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 5331 | src_set(m->xline + m->lineno, m->fname); |
H. Peter Anvin | ddb2906 | 2018-12-11 00:06:29 -0800 | [diff] [blame^] | 5332 | nasm_error(severity, "... from macro `%s' defined", m->name); |
H. Peter Anvin | 3736895 | 2016-05-09 14:10:32 -0700 | [diff] [blame] | 5333 | } |
| 5334 | } |
| 5335 | |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5336 | static void pp_error_list_macros(int severity) |
| 5337 | { |
H. Peter Anvin | ddb2906 | 2018-12-11 00:06:29 -0800 | [diff] [blame^] | 5338 | struct src_location saved; |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5339 | |
H. Peter Anvin | ddb2906 | 2018-12-11 00:06:29 -0800 | [diff] [blame^] | 5340 | severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE; |
| 5341 | saved = src_where(); |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5342 | |
Cyrill Gorcunov | 771d04e | 2016-05-10 23:27:03 +0300 | [diff] [blame] | 5343 | if (istk) |
| 5344 | pp_list_one_macro(istk->mstk, severity); |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5345 | |
H. Peter Anvin | ddb2906 | 2018-12-11 00:06:29 -0800 | [diff] [blame^] | 5346 | src_update(saved); |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5347 | } |
| 5348 | |
H. Peter Anvin | e746971 | 2016-02-18 02:20:59 -0800 | [diff] [blame] | 5349 | const struct preproc_ops nasmpp = { |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 5350 | pp_init, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5351 | pp_reset, |
| 5352 | pp_getline, |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5353 | pp_cleanup, |
Cyrill Gorcunov | 15ce78f | 2017-01-06 20:21:28 +0300 | [diff] [blame] | 5354 | pp_extra_stdmac, |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5355 | pp_pre_define, |
| 5356 | pp_pre_undefine, |
| 5357 | pp_pre_include, |
H. Peter Anvin | 0599034 | 2018-06-11 13:32:42 -0700 | [diff] [blame] | 5358 | pp_pre_command, |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5359 | pp_include_path, |
| 5360 | pp_error_list_macros, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5361 | }; |