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> |
H. Peter Anvin | c2f3f26 | 2018-12-27 12:37:25 -0800 | [diff] [blame^] | 69 | #include "nctype.h" |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 70 | #include <limits.h> |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 71 | |
| 72 | #include "nasm.h" |
| 73 | #include "nasmlib.h" |
H. Peter Anvin | b20bc73 | 2017-03-07 19:23:03 -0800 | [diff] [blame] | 74 | #include "error.h" |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 75 | #include "preproc.h" |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 76 | #include "hashtbl.h" |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 77 | #include "quote.h" |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 78 | #include "stdscan.h" |
H. Peter Anvin | dbb640b | 2009-07-18 18:57:16 -0700 | [diff] [blame] | 79 | #include "eval.h" |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 80 | #include "tokens.h" |
H. Peter Anvin | a4835d4 | 2008-05-20 14:21:29 -0700 | [diff] [blame] | 81 | #include "tables.h" |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 82 | #include "listing.h" |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 83 | |
| 84 | typedef struct SMacro SMacro; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 85 | typedef struct MMacro MMacro; |
| 86 | typedef struct MMacroInvocation MMacroInvocation; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 87 | typedef struct Context Context; |
| 88 | typedef struct Token Token; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 89 | typedef struct Blocks Blocks; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 90 | typedef struct Line Line; |
| 91 | typedef struct Include Include; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 92 | typedef struct Cond Cond; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 93 | |
| 94 | /* |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 95 | * Note on the storage of both SMacro and MMacros: the hash table |
| 96 | * indexes them case-insensitively, and we then have to go through a |
| 97 | * linked list of potential case aliases (and, for MMacros, parameter |
| 98 | * ranges); this is to preserve the matching semantics of the earlier |
| 99 | * code. If the number of case aliases for a specific macro is a |
| 100 | * performance issue, you may want to reconsider your coding style. |
| 101 | */ |
| 102 | |
| 103 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 104 | * Store the definition of a single-line macro. |
| 105 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 106 | struct SMacro { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 107 | SMacro *next; |
| 108 | char *name; |
| 109 | bool casesense; |
| 110 | bool in_progress; |
| 111 | unsigned int nparam; |
| 112 | Token *expansion; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 116 | * Store the definition of a multi-line macro. This is also used to |
| 117 | * store the interiors of `%rep...%endrep' blocks, which are |
| 118 | * effectively self-re-invoking multi-line macros which simply |
| 119 | * don't have a name or bother to appear in the hash tables. %rep |
| 120 | * blocks are signified by having a NULL `name' field. |
| 121 | * |
| 122 | * In a MMacro describing a `%rep' block, the `in_progress' field |
| 123 | * isn't merely boolean, but gives the number of repeats left to |
| 124 | * run. |
| 125 | * |
| 126 | * The `next' field is used for storing MMacros in hash tables; the |
| 127 | * `next_active' field is for stacking them on istk entries. |
| 128 | * |
| 129 | * When a MMacro is being expanded, `params', `iline', `nparam', |
| 130 | * `paramlen', `rotate' and `unique' are local to the invocation. |
| 131 | */ |
| 132 | struct MMacro { |
| 133 | MMacro *next; |
| 134 | MMacroInvocation *prev; /* previous invocation */ |
| 135 | char *name; |
| 136 | int nparam_min, nparam_max; |
| 137 | bool casesense; |
| 138 | bool plus; /* is the last parameter greedy? */ |
| 139 | bool nolist; /* is this macro listing-inhibited? */ |
| 140 | int64_t in_progress; /* is this macro currently being expanded? */ |
| 141 | int32_t max_depth; /* maximum number of recursive expansions allowed */ |
| 142 | Token *dlist; /* All defaults as one list */ |
| 143 | Token **defaults; /* Parameter default pointers */ |
| 144 | int ndefs; /* number of default parameters */ |
| 145 | Line *expansion; |
| 146 | |
| 147 | MMacro *next_active; |
| 148 | MMacro *rep_nest; /* used for nesting %rep */ |
| 149 | Token **params; /* actual parameters */ |
| 150 | Token *iline; /* invocation line */ |
| 151 | unsigned int nparam, rotate; |
| 152 | int *paramlen; |
| 153 | uint64_t unique; |
| 154 | int lineno; /* Current line number on expansion */ |
| 155 | uint64_t condcnt; /* number of if blocks... */ |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 156 | |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 157 | const char *fname; /* File where defined */ |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 158 | int32_t xline; /* First line in macro */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | |
| 162 | /* Store the definition of a multi-line macro, as defined in a |
| 163 | * previous recursive macro expansion. |
| 164 | */ |
| 165 | struct MMacroInvocation { |
| 166 | MMacroInvocation *prev; /* previous invocation */ |
| 167 | Token **params; /* actual parameters */ |
| 168 | Token *iline; /* invocation line */ |
| 169 | unsigned int nparam, rotate; |
| 170 | int *paramlen; |
| 171 | uint64_t unique; |
| 172 | uint64_t condcnt; |
| 173 | }; |
| 174 | |
| 175 | |
| 176 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 177 | * The context stack is composed of a linked list of these. |
| 178 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 179 | struct Context { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 180 | Context *next; |
| 181 | char *name; |
| 182 | struct hash_table localmac; |
| 183 | uint32_t number; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 184 | }; |
| 185 | |
| 186 | /* |
| 187 | * This is the internal form which we break input lines up into. |
| 188 | * Typically stored in linked lists. |
| 189 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 190 | * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not |
| 191 | * necessarily used as-is, but is intended to denote the number of |
| 192 | * the substituted parameter. So in the definition |
| 193 | * |
| 194 | * %define a(x,y) ( (x) & ~(y) ) |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 195 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 196 | * the token representing `x' will have its type changed to |
| 197 | * TOK_SMAC_PARAM, but the one representing `y' will be |
| 198 | * TOK_SMAC_PARAM+1. |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 199 | * |
| 200 | * TOK_INTERNAL_STRING is a dirty hack: it's a single string token |
| 201 | * which doesn't need quotes around it. Used in the pre-include |
| 202 | * mechanism as an alternative to trying to find a sensible type of |
| 203 | * quote to use on the filename we were passed. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 204 | */ |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 205 | enum pp_token_type { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 206 | TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID, |
| 207 | TOK_PREPROC_ID, TOK_STRING, |
| 208 | TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER, |
H. Peter Anvin | 6c81f0a | 2008-05-25 21:46:17 -0700 | [diff] [blame] | 209 | TOK_INTERNAL_STRING, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 210 | TOK_PREPROC_Q, TOK_PREPROC_QQ, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 211 | TOK_PASTE, /* %+ */ |
| 212 | TOK_INDIRECT, /* %[...] */ |
| 213 | TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */ |
| 214 | TOK_MAX = INT_MAX /* Keep compiler from reducing the range */ |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 215 | }; |
| 216 | |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 217 | #define PP_CONCAT_MASK(x) (1 << (x)) |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 218 | #define PP_CONCAT_MATCH(t, mask) (PP_CONCAT_MASK((t)->type) & mask) |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 219 | |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 220 | struct tokseq_match { |
| 221 | int mask_head; |
| 222 | int mask_tail; |
| 223 | }; |
| 224 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 225 | struct Token { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 226 | Token *next; |
| 227 | char *text; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 228 | union { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 229 | SMacro *mac; /* associated macro for TOK_SMAC_END */ |
| 230 | size_t len; /* scratch length field */ |
| 231 | } a; /* Auxiliary data */ |
| 232 | enum pp_token_type type; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 233 | }; |
| 234 | |
| 235 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 236 | * Multi-line macro definitions are stored as a linked list of |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 237 | * these, which is essentially a container to allow several linked |
| 238 | * lists of Tokens. |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 239 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 240 | * Note that in this module, linked lists are treated as stacks |
| 241 | * wherever possible. For this reason, Lines are _pushed_ on to the |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 242 | * `expansion' field in MMacro structures, so that the linked list, |
| 243 | * if walked, would give the macro lines in reverse order; this |
| 244 | * means that we can walk the list when expanding a macro, and thus |
| 245 | * push the lines on to the `expansion' field in _istk_ in reverse |
| 246 | * order (so that when popped back off they are in the right |
| 247 | * order). It may seem cockeyed, and it relies on my design having |
| 248 | * an even number of steps in, but it works... |
| 249 | * |
| 250 | * Some of these structures, rather than being actual lines, are |
| 251 | * markers delimiting the end of the expansion of a given macro. |
| 252 | * This is for use in the cycle-tracking and %rep-handling code. |
| 253 | * Such structures have `finishes' non-NULL, and `first' NULL. All |
| 254 | * others have `finishes' NULL, but `first' may still be NULL if |
| 255 | * the line is blank. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 256 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 257 | struct Line { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 258 | Line *next; |
| 259 | MMacro *finishes; |
| 260 | Token *first; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 261 | }; |
| 262 | |
| 263 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 264 | * To handle an arbitrary level of file inclusion, we maintain a |
| 265 | * stack (ie linked list) of these things. |
| 266 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 267 | struct Include { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 268 | Include *next; |
| 269 | FILE *fp; |
| 270 | Cond *conds; |
| 271 | Line *expansion; |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 272 | const char *fname; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 273 | int lineno, lineinc; |
| 274 | MMacro *mstk; /* stack of active macros/reps */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 275 | }; |
| 276 | |
| 277 | /* |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 278 | * File real name hash, so we don't have to re-search the include |
| 279 | * path for every pass (and potentially more than that if a file |
| 280 | * is used more than once.) |
| 281 | */ |
| 282 | struct hash_table FileHash; |
| 283 | |
| 284 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 285 | * Conditional assembly: we maintain a separate stack of these for |
| 286 | * each level of file inclusion. (The only reason we keep the |
| 287 | * stacks separate is to ensure that a stray `%endif' in a file |
| 288 | * included from within the true branch of a `%if' won't terminate |
| 289 | * it and cause confusion: instead, rightly, it'll cause an error.) |
| 290 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 291 | struct Cond { |
| 292 | Cond *next; |
| 293 | int state; |
| 294 | }; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 295 | enum { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 296 | /* |
| 297 | * These states are for use just after %if or %elif: IF_TRUE |
| 298 | * means the condition has evaluated to truth so we are |
| 299 | * currently emitting, whereas IF_FALSE means we are not |
| 300 | * currently emitting but will start doing so if a %else comes |
| 301 | * up. In these states, all directives are admissible: %elif, |
| 302 | * %else and %endif. (And of course %if.) |
| 303 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 304 | COND_IF_TRUE, COND_IF_FALSE, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 305 | /* |
| 306 | * These states come up after a %else: ELSE_TRUE means we're |
| 307 | * emitting, and ELSE_FALSE means we're not. In ELSE_* states, |
| 308 | * any %elif or %else will cause an error. |
| 309 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 310 | COND_ELSE_TRUE, COND_ELSE_FALSE, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 311 | /* |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 312 | * These states mean that we're not emitting now, and also that |
| 313 | * nothing until %endif will be emitted at all. COND_DONE is |
| 314 | * used when we've had our moment of emission |
| 315 | * and have now started seeing %elifs. COND_NEVER is used when |
| 316 | * the condition construct in question is contained within a |
| 317 | * non-emitting branch of a larger condition construct, |
| 318 | * or if there is an error. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 319 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 320 | COND_DONE, COND_NEVER |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 321 | }; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 322 | #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE ) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 323 | |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 324 | /* |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 325 | * These defines are used as the possible return values for do_directive |
| 326 | */ |
| 327 | #define NO_DIRECTIVE_FOUND 0 |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 328 | #define DIRECTIVE_FOUND 1 |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 329 | |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 330 | /* max reps */ |
| 331 | #define REP_LIMIT ((INT64_C(1) << 62)) |
| 332 | |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 333 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 334 | * Condition codes. Note that we use c_ prefix not C_ because C_ is |
| 335 | * used in nasm.h for the "real" condition codes. At _this_ level, |
| 336 | * we treat CXZ and ECXZ as condition codes, albeit non-invertible |
| 337 | * ones, so we need a different enum... |
| 338 | */ |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 339 | static const char * const conditions[] = { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 340 | "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le", |
| 341 | "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no", |
H. Peter Anvin | ce9be34 | 2007-09-12 00:22:29 +0000 | [diff] [blame] | 342 | "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z" |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 343 | }; |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 344 | enum pp_conds { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 345 | c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE, |
| 346 | 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] | 347 | c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z, |
| 348 | c_none = -1 |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 349 | }; |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 350 | static const enum pp_conds inverse_ccs[] = { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 351 | c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE, |
| 352 | 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] | 353 | 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] | 354 | }; |
| 355 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 356 | /* |
| 357 | * Directive names. |
| 358 | */ |
| 359 | /* If this is a an IF, ELIF, ELSE or ENDIF keyword */ |
| 360 | static int is_condition(enum preproc_token arg) |
| 361 | { |
| 362 | return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF); |
| 363 | } |
| 364 | |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 365 | /* For TASM compatibility we need to be able to recognise TASM compatible |
| 366 | * conditional compilation directives. Using the NASM pre-processor does |
| 367 | * not work, so we look for them specifically from the following list and |
| 368 | * then jam in the equivalent NASM directive into the input stream. |
| 369 | */ |
| 370 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 371 | enum { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 372 | TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI, |
| 373 | TM_IFNDEF, TM_INCLUDE, TM_LOCAL |
| 374 | }; |
| 375 | |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 376 | static const char * const tasm_directives[] = { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 377 | "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi", |
| 378 | "ifndef", "include", "local" |
| 379 | }; |
| 380 | |
| 381 | static int StackSize = 4; |
H. Peter Anvin | 6c8b2be | 2016-05-24 23:46:50 -0700 | [diff] [blame] | 382 | static const char *StackPointer = "ebp"; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 383 | static int ArgOffset = 8; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 384 | static int LocalOffset = 0; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 385 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 386 | static Context *cstk; |
| 387 | static Include *istk; |
Cyrill Gorcunov | 8c0666b | 2018-11-24 14:33:48 +0300 | [diff] [blame] | 388 | static const struct strlist *ipath_list; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 389 | |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 390 | static struct strlist *deplist; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 391 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 392 | static uint64_t unique; /* unique identifier numbers */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 393 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 394 | static Line *predef = NULL; |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 395 | static bool do_predef; |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 396 | static enum preproc_mode pp_mode; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 397 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 398 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 399 | * The current set of multi-line macros we have defined. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 400 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 401 | static struct hash_table mmacros; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 402 | |
| 403 | /* |
| 404 | * The current set of single-line macros we have defined. |
| 405 | */ |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 406 | static struct hash_table smacros; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 407 | |
| 408 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 409 | * The multi-line macro we are currently defining, or the %rep |
| 410 | * block we are currently reading, if any. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 411 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 412 | static MMacro *defining; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 413 | |
Charles Crayne | d4200be | 2008-07-12 16:42:33 -0700 | [diff] [blame] | 414 | static uint64_t nested_mac_count; |
| 415 | static uint64_t nested_rep_count; |
| 416 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 417 | /* |
| 418 | * The number of macro parameters to allocate space for at a time. |
| 419 | */ |
| 420 | #define PARAM_DELTA 16 |
| 421 | |
| 422 | /* |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 423 | * The standard macro set: defined in macros.c in a set of arrays. |
| 424 | * This gives our position in any macro set, while we are processing it. |
| 425 | * The stdmacset is an array of such macro sets. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 426 | */ |
H. Peter Anvin | a70547f | 2008-07-19 21:44:26 -0700 | [diff] [blame] | 427 | static macros_t *stdmacpos; |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 428 | static macros_t **stdmacnext; |
| 429 | static macros_t *stdmacros[8]; |
Cyrill Gorcunov | 15ce78f | 2017-01-06 20:21:28 +0300 | [diff] [blame] | 430 | static macros_t *extrastdmac; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 431 | |
| 432 | /* |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 433 | * Tokens are allocated in blocks to improve speed |
| 434 | */ |
| 435 | #define TOKEN_BLOCKSIZE 4096 |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 436 | static Token *freeTokens = NULL; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 437 | struct Blocks { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 438 | Blocks *next; |
| 439 | void *chunk; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 440 | }; |
| 441 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 442 | static Blocks blocks = { NULL, NULL }; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 443 | |
| 444 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 445 | * Forward declarations. |
| 446 | */ |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 447 | static void pp_add_stdmac(macros_t *macros); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 448 | static Token *expand_mmac_params(Token * tline); |
| 449 | static Token *expand_smacro(Token * tline); |
| 450 | static Token *expand_id(Token * tline); |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 451 | static Context *get_ctx(const char *name, const char **namep); |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 452 | static void make_tok_num(Token * tok, int64_t val); |
H. Peter Anvin (Intel) | 6bde2ed | 2018-12-13 19:39:41 -0800 | [diff] [blame] | 453 | static void pp_verror(errflags severity, const char *fmt, va_list ap); |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 454 | static vefunc real_verror; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 455 | static void *new_Block(size_t size); |
| 456 | static void delete_Blocks(void); |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 457 | static Token *new_Token(Token * next, enum pp_token_type type, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 458 | const char *text, int txtlen); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 459 | static Token *delete_Token(Token * t); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 460 | |
| 461 | /* |
| 462 | * Macros for safe checking of token pointers, avoid *(NULL) |
| 463 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 464 | #define tok_type_(x,t) ((x) && (x)->type == (t)) |
| 465 | #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next |
| 466 | #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v))) |
| 467 | #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] | 468 | |
Cyrill Gorcunov | 194ba89 | 2011-06-30 01:16:35 +0400 | [diff] [blame] | 469 | /* |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 470 | * nasm_unquote with error if the string contains NUL characters. |
| 471 | * If the string contains NUL characters, issue an error and return |
| 472 | * the C len, i.e. truncate at the NUL. |
| 473 | */ |
| 474 | static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive) |
| 475 | { |
| 476 | size_t len = nasm_unquote(qstr, NULL); |
| 477 | size_t clen = strlen(qstr); |
| 478 | |
| 479 | if (len != clen) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 480 | nasm_nonfatal("NUL character in `%s' directive", |
| 481 | pp_directives[directive]); |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 482 | return clen; |
| 483 | } |
| 484 | |
| 485 | /* |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 486 | * In-place reverse a list of tokens. |
| 487 | */ |
| 488 | static Token *reverse_tokens(Token *t) |
| 489 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 490 | Token *prev = NULL; |
| 491 | Token *next; |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 492 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 493 | while (t) { |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 494 | next = t->next; |
| 495 | t->next = prev; |
| 496 | prev = t; |
| 497 | t = next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 498 | } |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 499 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 500 | return prev; |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | /* |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 504 | * Handle TASM specific directives, which do not contain a % in |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 505 | * front of them. We do it here because I could not find any other |
| 506 | * place to do it for the moment, and it is a hack (ideally it would |
| 507 | * be nice to be able to use the NASM pre-processor to do it). |
| 508 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 509 | static char *check_tasm_directive(char *line) |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 510 | { |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 511 | int32_t i, j, k, m, len; |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 512 | char *p, *q, *oldline, oldchar; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 513 | |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 514 | p = nasm_skip_spaces(line); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 515 | |
| 516 | /* Binary search for the directive name */ |
| 517 | i = -1; |
Cyrill Gorcunov | a731924 | 2010-06-03 22:04:36 +0400 | [diff] [blame] | 518 | j = ARRAY_SIZE(tasm_directives); |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 519 | q = nasm_skip_word(p); |
| 520 | len = q - p; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 521 | if (len) { |
| 522 | oldchar = p[len]; |
| 523 | p[len] = 0; |
| 524 | while (j - i > 1) { |
| 525 | k = (j + i) / 2; |
| 526 | m = nasm_stricmp(p, tasm_directives[k]); |
| 527 | if (m == 0) { |
| 528 | /* We have found a directive, so jam a % in front of it |
| 529 | * so that NASM will then recognise it as one if it's own. |
| 530 | */ |
| 531 | p[len] = oldchar; |
| 532 | len = strlen(p); |
| 533 | oldline = line; |
| 534 | line = nasm_malloc(len + 2); |
| 535 | line[0] = '%'; |
| 536 | if (k == TM_IFDIFI) { |
H. Peter Anvin | 18f4879 | 2009-06-27 15:56:27 -0700 | [diff] [blame] | 537 | /* |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 538 | * NASM does not recognise IFDIFI, so we convert |
| 539 | * it to %if 0. This is not used in NASM |
| 540 | * compatible code, but does need to parse for the |
| 541 | * TASM macro package. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 542 | */ |
H. Peter Anvin | 18f4879 | 2009-06-27 15:56:27 -0700 | [diff] [blame] | 543 | strcpy(line + 1, "if 0"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 544 | } else { |
| 545 | memcpy(line + 1, p, len + 1); |
| 546 | } |
| 547 | nasm_free(oldline); |
| 548 | return line; |
| 549 | } else if (m < 0) { |
| 550 | j = k; |
| 551 | } else |
| 552 | i = k; |
| 553 | } |
| 554 | p[len] = oldchar; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 555 | } |
| 556 | return line; |
| 557 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 558 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 559 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 560 | * The pre-preprocessing stage... This function translates line |
| 561 | * number indications as they emerge from GNU cpp (`# lineno "file" |
| 562 | * flags') into NASM preprocessor line number indications (`%line |
| 563 | * lineno file'). |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 564 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 565 | static char *prepreproc(char *line) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 566 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 567 | int lineno, fnlen; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 568 | char *fname, *oldline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 569 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 570 | if (line[0] == '#' && line[1] == ' ') { |
| 571 | oldline = line; |
| 572 | fname = oldline + 2; |
| 573 | lineno = atoi(fname); |
| 574 | fname += strspn(fname, "0123456789 "); |
| 575 | if (*fname == '"') |
| 576 | fname++; |
| 577 | fnlen = strcspn(fname, "\""); |
| 578 | line = nasm_malloc(20 + fnlen); |
| 579 | snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname); |
| 580 | nasm_free(oldline); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 581 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 582 | if (tasm_compatible_mode) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 583 | return check_tasm_directive(line); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 584 | return line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 588 | * Free a linked list of tokens. |
| 589 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 590 | static void free_tlist(Token * list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 591 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 592 | while (list) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 593 | list = delete_Token(list); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | /* |
| 597 | * Free a linked list of lines. |
| 598 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 599 | static void free_llist(Line * list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 600 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 601 | Line *l, *tmp; |
| 602 | list_for_each_safe(l, tmp, list) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 603 | free_tlist(l->first); |
| 604 | nasm_free(l); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | |
| 608 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 609 | * Free an MMacro |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 610 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 611 | static void free_mmacro(MMacro * m) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 612 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 613 | nasm_free(m->name); |
| 614 | free_tlist(m->dlist); |
| 615 | nasm_free(m->defaults); |
| 616 | free_llist(m->expansion); |
| 617 | nasm_free(m); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | /* |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 621 | * Free all currently defined macros, and free the hash tables |
| 622 | */ |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 623 | static void free_smacro_table(struct hash_table *smt) |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 624 | { |
H. Peter Anvin (Intel) | ebb05a0 | 2018-12-11 12:30:25 -0800 | [diff] [blame] | 625 | struct hash_iterator it; |
| 626 | const struct hash_node *np; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 627 | |
H. Peter Anvin (Intel) | ebb05a0 | 2018-12-11 12:30:25 -0800 | [diff] [blame] | 628 | hash_for_each(smt, it, np) { |
| 629 | SMacro *tmp; |
| 630 | SMacro *s = np->data; |
| 631 | nasm_free((void *)np->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 (Intel) | ebb05a0 | 2018-12-11 12:30:25 -0800 | [diff] [blame] | 643 | struct hash_iterator it; |
| 644 | const struct hash_node *np; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 645 | |
H. Peter Anvin (Intel) | ebb05a0 | 2018-12-11 12:30:25 -0800 | [diff] [blame] | 646 | hash_for_each(mmt, it, np) { |
| 647 | MMacro *tmp; |
| 648 | MMacro *m = np->data; |
| 649 | nasm_free((void *)np->key); |
| 650 | list_for_each_safe(m, tmp, m) |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 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 | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 670 | * Pop the context stack. |
| 671 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 672 | static void ctx_pop(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 673 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 674 | Context *c = cstk; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 675 | |
| 676 | cstk = cstk->next; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 677 | free_smacro_table(&c->localmac); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 678 | nasm_free(c->name); |
| 679 | nasm_free(c); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 680 | } |
| 681 | |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 682 | /* |
| 683 | * Search for a key in the hash index; adding it if necessary |
| 684 | * (in which case we initialize the data pointer to NULL.) |
| 685 | */ |
| 686 | static void ** |
| 687 | hash_findi_add(struct hash_table *hash, const char *str) |
| 688 | { |
| 689 | struct hash_insert hi; |
| 690 | void **r; |
| 691 | char *strx; |
H. Peter Anvin (Intel) | ebb05a0 | 2018-12-11 12:30:25 -0800 | [diff] [blame] | 692 | size_t l = strlen(str) + 1; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 693 | |
H. Peter Anvin (Intel) | ebb05a0 | 2018-12-11 12:30:25 -0800 | [diff] [blame] | 694 | r = hash_findib(hash, str, l, &hi); |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 695 | if (r) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 696 | return r; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 697 | |
H. Peter Anvin (Intel) | ebb05a0 | 2018-12-11 12:30:25 -0800 | [diff] [blame] | 698 | strx = nasm_malloc(l); /* Use a more efficient allocator here? */ |
| 699 | memcpy(strx, str, l); |
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 != '}') |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 911 | nasm_warn(WARN_OTHER, "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 { |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 999 | nasm_warn(WARN_OTHER, "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 | { |
H. Peter Anvin (Intel) | 6447109 | 2018-12-11 13:06:14 -0800 | [diff] [blame] | 1498 | const struct strlist_entry *ip = strlist_head(ipath_list); |
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 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1504 | while (1) { |
night199uk | fdb1a1b | 2018-10-18 23:19:47 +0200 | [diff] [blame] | 1505 | sp = nasm_catfile(prefix, file); |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1506 | if (omode == INC_PROBE) { |
| 1507 | fp = NULL; |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1508 | found = nasm_file_exists(sp); |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1509 | } else { |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1510 | fp = nasm_open_read(sp, fmode); |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1511 | found = (fp != NULL); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1512 | } |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1513 | if (found) { |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1514 | *slpath = sp; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1515 | return fp; |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1516 | } |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 1517 | |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1518 | nasm_free(sp); |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 1519 | |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1520 | if (!ip) { |
| 1521 | *slpath = NULL; |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1522 | return NULL; |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1523 | } |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1524 | |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1525 | prefix = ip->str; |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1526 | ip = ip->next; |
| 1527 | } |
| 1528 | } |
| 1529 | |
| 1530 | /* |
| 1531 | * Open a file, or test for the presence of one (depending on omode), |
| 1532 | * considering the include path. |
| 1533 | */ |
| 1534 | static FILE *inc_fopen(const char *file, |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 1535 | struct strlist *dhead, |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 1536 | const char **found_path, |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1537 | enum incopen_mode omode, |
| 1538 | enum file_flags fmode) |
| 1539 | { |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1540 | struct hash_insert hi; |
| 1541 | void **hp; |
| 1542 | char *path; |
| 1543 | FILE *fp = NULL; |
| 1544 | |
| 1545 | hp = hash_find(&FileHash, file, &hi); |
| 1546 | if (hp) { |
| 1547 | path = *hp; |
Martin Storsjö | f283c8f | 2017-08-13 17:28:46 +0300 | [diff] [blame] | 1548 | if (path || omode != INC_NEEDED) { |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 1549 | strlist_add(dhead, path ? path : file); |
Martin Storsjö | f283c8f | 2017-08-13 17:28:46 +0300 | [diff] [blame] | 1550 | } |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1551 | } else { |
| 1552 | /* Need to do the actual path search */ |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1553 | fp = inc_fopen_search(file, &path, omode, fmode); |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1554 | |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1555 | /* Positive or negative result */ |
| 1556 | hash_add(&hi, nasm_strdup(file), path); |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1557 | |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 1558 | /* |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1559 | * Add file to dependency path. |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 1560 | */ |
| 1561 | if (path || omode != INC_NEEDED) |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 1562 | strlist_add(dhead, file); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1563 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1564 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1565 | if (!path) { |
| 1566 | if (omode == INC_NEEDED) |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 1567 | nasm_fatal("unable to open include file `%s'", file); |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1568 | } else { |
| 1569 | if (!fp && omode != INC_PROBE) |
| 1570 | fp = nasm_open_read(path, fmode); |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1571 | } |
| 1572 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1573 | if (found_path) |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 1574 | *found_path = path; |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1575 | |
| 1576 | return fp; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | /* |
Fabian Giesen | 0bbc38d | 2016-04-28 13:48:14 -0700 | [diff] [blame] | 1580 | * Opens an include or input file. Public version, for use by modules |
| 1581 | * that get a file:lineno pair and need to look at the file again |
| 1582 | * (e.g. the CodeView debug backend). Returns NULL on failure. |
| 1583 | */ |
H. Peter Anvin | 3e83cec | 2016-05-25 04:28:46 -0700 | [diff] [blame] | 1584 | FILE *pp_input_fopen(const char *filename, enum file_flags mode) |
Fabian Giesen | 0bbc38d | 2016-04-28 13:48:14 -0700 | [diff] [blame] | 1585 | { |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 1586 | return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode); |
Fabian Giesen | 0bbc38d | 2016-04-28 13:48:14 -0700 | [diff] [blame] | 1587 | } |
| 1588 | |
| 1589 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1590 | * 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] | 1591 | * 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] | 1592 | * return true if _any_ single-line macro of that name is defined. |
| 1593 | * Otherwise, will return true if a single-line macro with either |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1594 | * `nparam' or no parameters is defined. |
| 1595 | * |
| 1596 | * If a macro with precisely the right number of parameters is |
H. Peter Anvin | ef7468f | 2002-04-30 20:57:59 +0000 | [diff] [blame] | 1597 | * defined, or nparam is -1, the address of the definition structure |
| 1598 | * 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] | 1599 | * is NULL, no action will be taken regarding its contents, and no |
| 1600 | * error will occur. |
| 1601 | * |
| 1602 | * Note that this is also called with nparam zero to resolve |
| 1603 | * `ifdef'. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1604 | * |
| 1605 | * If you already know which context macro belongs to, you can pass |
| 1606 | * the context pointer as first parameter; if you won't but name begins |
| 1607 | * with %$ the context will be automatically computed. If all_contexts |
| 1608 | * is true, macro will be searched in outer contexts as well. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1609 | */ |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1610 | static bool |
H. Peter Anvin | b2a5fda | 2008-06-19 21:42:42 -0700 | [diff] [blame] | 1611 | smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn, |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1612 | bool nocase) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1613 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1614 | struct hash_table *smtbl; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1615 | SMacro *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1616 | |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1617 | if (ctx) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1618 | smtbl = &ctx->localmac; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1619 | } else if (name[0] == '%' && name[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1620 | if (cstk) |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1621 | ctx = get_ctx(name, &name); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1622 | if (!ctx) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1623 | return false; /* got to return _something_ */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1624 | smtbl = &ctx->localmac; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1625 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1626 | smtbl = &smacros; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1627 | } |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1628 | m = (SMacro *) hash_findix(smtbl, name); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1629 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1630 | while (m) { |
| 1631 | if (!mstrcmp(m->name, name, m->casesense && nocase) && |
Charles Crayne | 192d5b5 | 2007-10-18 19:02:42 -0700 | [diff] [blame] | 1632 | (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1633 | if (defn) { |
Charles Crayne | 192d5b5 | 2007-10-18 19:02:42 -0700 | [diff] [blame] | 1634 | if (nparam == (int) m->nparam || nparam == -1) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1635 | *defn = m; |
| 1636 | else |
| 1637 | *defn = NULL; |
| 1638 | } |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1639 | return true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1640 | } |
| 1641 | m = m->next; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1642 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1643 | |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1644 | return false; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1645 | } |
| 1646 | |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 1647 | /* param should be a natural number [0; INT_MAX] */ |
| 1648 | static int read_param_count(const char *str) |
| 1649 | { |
| 1650 | int result; |
| 1651 | bool err; |
| 1652 | |
| 1653 | result = readnum(str, &err); |
| 1654 | if (result < 0 || result > INT_MAX) { |
| 1655 | result = 0; |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1656 | nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]", |
| 1657 | str, 0, INT_MAX); |
| 1658 | } else if (err) |
| 1659 | nasm_nonfatal("unable to parse parameter count `%s'", str); |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 1660 | return result; |
| 1661 | } |
| 1662 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1663 | /* |
| 1664 | * Count and mark off the parameters in a multi-line macro call. |
| 1665 | * This is called both from within the multi-line macro expansion |
| 1666 | * code, and also to mark off the default parameters when provided |
| 1667 | * in a %macro definition line. |
| 1668 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1669 | static void count_mmac_params(Token * t, int *nparam, Token *** params) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1670 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1671 | int paramsize, brace; |
| 1672 | |
| 1673 | *nparam = paramsize = 0; |
| 1674 | *params = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1675 | while (t) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1676 | /* +1: we need space for the final NULL */ |
H. Peter Anvin | 91fb6f1 | 2008-09-01 10:56:33 -0700 | [diff] [blame] | 1677 | if (*nparam+1 >= paramsize) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1678 | paramsize += PARAM_DELTA; |
| 1679 | *params = nasm_realloc(*params, sizeof(**params) * paramsize); |
| 1680 | } |
| 1681 | skip_white_(t); |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1682 | brace = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1683 | if (tok_is_(t, "{")) |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1684 | brace++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1685 | (*params)[(*nparam)++] = t; |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1686 | if (brace) { |
| 1687 | while (brace && (t = t->next) != NULL) { |
| 1688 | if (tok_is_(t, "{")) |
| 1689 | brace++; |
| 1690 | else if (tok_is_(t, "}")) |
| 1691 | brace--; |
| 1692 | } |
| 1693 | |
| 1694 | if (t) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1695 | /* |
| 1696 | * Now we've found the closing brace, look further |
| 1697 | * for the comma. |
| 1698 | */ |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1699 | t = t->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1700 | skip_white_(t); |
| 1701 | if (tok_isnt_(t, ",")) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1702 | nasm_nonfatal("braces do not enclose all of macro parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1703 | while (tok_isnt_(t, ",")) |
| 1704 | t = t->next; |
| 1705 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1706 | } |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1707 | } else { |
| 1708 | while (tok_isnt_(t, ",")) |
| 1709 | t = t->next; |
| 1710 | } |
| 1711 | if (t) { /* got a comma/brace */ |
| 1712 | t = t->next; /* eat the comma */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1713 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1714 | } |
| 1715 | } |
| 1716 | |
| 1717 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1718 | * Determine whether one of the various `if' conditions is true or |
| 1719 | * not. |
| 1720 | * |
| 1721 | * We must free the tline we get passed. |
| 1722 | */ |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1723 | static bool if_condition(Token * tline, enum preproc_token ct) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1724 | { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1725 | enum pp_conditional i = PP_COND(ct); |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1726 | bool j; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1727 | Token *t, *tt, **tptr, *origline; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1728 | struct tokenval tokval; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1729 | expr *evalresult; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1730 | enum pp_token_type needtype; |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1731 | char *p; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1732 | |
| 1733 | origline = tline; |
| 1734 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1735 | switch (i) { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1736 | case PPC_IFCTX: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1737 | j = false; /* have we matched yet? */ |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1738 | while (true) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1739 | skip_white_(tline); |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1740 | if (!tline) |
| 1741 | break; |
| 1742 | if (tline->type != TOK_ID) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1743 | nasm_nonfatal("`%s' expects context identifiers", |
| 1744 | pp_directives[ct]); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1745 | free_tlist(origline); |
| 1746 | return -1; |
| 1747 | } |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1748 | if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1749 | j = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1750 | tline = tline->next; |
| 1751 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1752 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1753 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1754 | case PPC_IFDEF: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1755 | j = false; /* have we matched yet? */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1756 | while (tline) { |
| 1757 | skip_white_(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1758 | if (!tline || (tline->type != TOK_ID && |
| 1759 | (tline->type != TOK_PREPROC_ID || |
| 1760 | tline->text[1] != '$'))) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1761 | nasm_nonfatal("`%s' expects macro identifiers", |
| 1762 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1763 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1764 | } |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1765 | if (smacro_defined(NULL, tline->text, 0, NULL, true)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1766 | j = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1767 | tline = tline->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1768 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1769 | break; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1770 | |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1771 | case PPC_IFENV: |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1772 | tline = expand_smacro(tline); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1773 | j = false; /* have we matched yet? */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1774 | while (tline) { |
| 1775 | skip_white_(tline); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1776 | if (!tline || (tline->type != TOK_ID && |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1777 | tline->type != TOK_STRING && |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1778 | (tline->type != TOK_PREPROC_ID || |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1779 | tline->text[1] != '!'))) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1780 | nasm_nonfatal("`%s' expects environment variable names", |
| 1781 | pp_directives[ct]); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1782 | goto fail; |
| 1783 | } |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1784 | p = tline->text; |
| 1785 | if (tline->type == TOK_PREPROC_ID) |
| 1786 | p += 2; /* Skip leading %! */ |
H. Peter Anvin | 53e2e4c | 2018-11-28 15:01:40 -0800 | [diff] [blame] | 1787 | if (nasm_isquote(*p)) |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1788 | nasm_unquote_cstr(p, ct); |
| 1789 | if (getenv(p)) |
| 1790 | j = true; |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1791 | tline = tline->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1792 | } |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1793 | break; |
| 1794 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1795 | case PPC_IFIDN: |
| 1796 | case PPC_IFIDNI: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1797 | tline = expand_smacro(tline); |
| 1798 | t = tt = tline; |
| 1799 | while (tok_isnt_(tt, ",")) |
| 1800 | tt = tt->next; |
| 1801 | if (!tt) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1802 | nasm_nonfatal("`%s' expects two comma-separated arguments", |
| 1803 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1804 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1805 | } |
| 1806 | tt = tt->next; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1807 | j = true; /* assume equality unless proved not */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1808 | while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) { |
| 1809 | if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1810 | nasm_nonfatal("`%s': more than one comma on line", |
| 1811 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1812 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1813 | } |
| 1814 | if (t->type == TOK_WHITESPACE) { |
| 1815 | t = t->next; |
| 1816 | continue; |
| 1817 | } |
| 1818 | if (tt->type == TOK_WHITESPACE) { |
| 1819 | tt = tt->next; |
| 1820 | continue; |
| 1821 | } |
| 1822 | if (tt->type != t->type) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1823 | j = false; /* found mismatching tokens */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1824 | break; |
| 1825 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1826 | /* When comparing strings, need to unquote them first */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1827 | if (t->type == TOK_STRING) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1828 | size_t l1 = nasm_unquote(t->text, NULL); |
| 1829 | size_t l2 = nasm_unquote(tt->text, NULL); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 1830 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1831 | if (l1 != l2) { |
| 1832 | j = false; |
| 1833 | break; |
| 1834 | } |
| 1835 | if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) { |
| 1836 | j = false; |
| 1837 | break; |
| 1838 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1839 | } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1840 | j = false; /* found mismatching tokens */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1841 | break; |
| 1842 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1843 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1844 | t = t->next; |
| 1845 | tt = tt->next; |
| 1846 | } |
| 1847 | if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1848 | j = false; /* trailing gunk on one end or other */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1849 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1850 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1851 | case PPC_IFMACRO: |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1852 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1853 | bool found = false; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1854 | MMacro searching, *mmac; |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 1855 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1856 | skip_white_(tline); |
| 1857 | tline = expand_id(tline); |
| 1858 | if (!tok_type_(tline, TOK_ID)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1859 | nasm_nonfatal("`%s' expects a macro name", pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1860 | goto fail; |
| 1861 | } |
| 1862 | searching.name = nasm_strdup(tline->text); |
| 1863 | searching.casesense = true; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1864 | searching.plus = false; |
| 1865 | searching.nolist = false; |
| 1866 | searching.in_progress = 0; |
| 1867 | searching.max_depth = 0; |
| 1868 | searching.rep_nest = NULL; |
| 1869 | searching.nparam_min = 0; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1870 | searching.nparam_max = INT_MAX; |
| 1871 | tline = expand_smacro(tline->next); |
| 1872 | skip_white_(tline); |
| 1873 | if (!tline) { |
| 1874 | } else if (!tok_type_(tline, TOK_NUMBER)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1875 | nasm_nonfatal("`%s' expects a parameter count or nothing", |
| 1876 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1877 | } else { |
| 1878 | searching.nparam_min = searching.nparam_max = |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 1879 | read_param_count(tline->text); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1880 | } |
| 1881 | if (tline && tok_is_(tline->next, "-")) { |
| 1882 | tline = tline->next->next; |
| 1883 | if (tok_is_(tline, "*")) |
| 1884 | searching.nparam_max = INT_MAX; |
| 1885 | else if (!tok_type_(tline, TOK_NUMBER)) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1886 | nasm_nonfatal("`%s' expects a parameter count after `-'", |
| 1887 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1888 | else { |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 1889 | searching.nparam_max = read_param_count(tline->text); |
Cyrill Gorcunov | c9244ea | 2017-10-22 15:25:48 +0300 | [diff] [blame] | 1890 | if (searching.nparam_min > searching.nparam_max) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1891 | nasm_nonfatal("minimum parameter count exceeds maximum"); |
Cyrill Gorcunov | c9244ea | 2017-10-22 15:25:48 +0300 | [diff] [blame] | 1892 | searching.nparam_max = searching.nparam_min; |
| 1893 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1894 | } |
| 1895 | } |
| 1896 | if (tline && tok_is_(tline->next, "+")) { |
| 1897 | tline = tline->next; |
| 1898 | searching.plus = true; |
| 1899 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1900 | mmac = (MMacro *) hash_findix(&mmacros, searching.name); |
| 1901 | while (mmac) { |
| 1902 | if (!strcmp(mmac->name, searching.name) && |
| 1903 | (mmac->nparam_min <= searching.nparam_max |
| 1904 | || searching.plus) |
| 1905 | && (searching.nparam_min <= mmac->nparam_max |
| 1906 | || mmac->plus)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1907 | found = true; |
| 1908 | break; |
| 1909 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1910 | mmac = mmac->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1911 | } |
| 1912 | if (tline && tline->next) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 1913 | nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1914 | nasm_free(searching.name); |
| 1915 | j = found; |
| 1916 | break; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1917 | } |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 1918 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1919 | case PPC_IFID: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1920 | needtype = TOK_ID; |
| 1921 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1922 | case PPC_IFNUM: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1923 | needtype = TOK_NUMBER; |
| 1924 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1925 | case PPC_IFSTR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1926 | needtype = TOK_STRING; |
| 1927 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1928 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1929 | iftype: |
| 1930 | t = tline = expand_smacro(tline); |
H. Peter Anvin | d85d250 | 2008-05-04 17:53:31 -0700 | [diff] [blame] | 1931 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1932 | while (tok_type_(t, TOK_WHITESPACE) || |
| 1933 | (needtype == TOK_NUMBER && |
| 1934 | tok_type_(t, TOK_OTHER) && |
| 1935 | (t->text[0] == '-' || t->text[0] == '+') && |
| 1936 | !t->text[1])) |
| 1937 | t = t->next; |
H. Peter Anvin | d85d250 | 2008-05-04 17:53:31 -0700 | [diff] [blame] | 1938 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1939 | j = tok_type_(t, needtype); |
| 1940 | break; |
H. Peter Anvin | cbf768d | 2008-02-16 16:41:25 -0800 | [diff] [blame] | 1941 | |
| 1942 | case PPC_IFTOKEN: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1943 | t = tline = expand_smacro(tline); |
| 1944 | while (tok_type_(t, TOK_WHITESPACE)) |
| 1945 | t = t->next; |
H. Peter Anvin | cbf768d | 2008-02-16 16:41:25 -0800 | [diff] [blame] | 1946 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1947 | j = false; |
| 1948 | if (t) { |
| 1949 | t = t->next; /* Skip the actual token */ |
| 1950 | while (tok_type_(t, TOK_WHITESPACE)) |
| 1951 | t = t->next; |
| 1952 | j = !t; /* Should be nothing left */ |
| 1953 | } |
| 1954 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1955 | |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 1956 | case PPC_IFEMPTY: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1957 | t = tline = expand_smacro(tline); |
| 1958 | while (tok_type_(t, TOK_WHITESPACE)) |
| 1959 | t = t->next; |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 1960 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1961 | j = !t; /* Should be empty */ |
| 1962 | break; |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 1963 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1964 | case PPC_IF: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1965 | t = tline = expand_smacro(tline); |
| 1966 | tptr = &t; |
| 1967 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 1968 | evalresult = evaluate(ppscan, tptr, &tokval, NULL, true, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1969 | if (!evalresult) |
| 1970 | return -1; |
| 1971 | if (tokval.t_type) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 1972 | nasm_warn(WARN_OTHER, "trailing garbage after expression ignored"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1973 | if (!is_simple(evalresult)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1974 | nasm_nonfatal("non-constant value given to `%s'", |
| 1975 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1976 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1977 | } |
Chuck Crayne | 60ae75d | 2007-05-02 01:59:16 +0000 | [diff] [blame] | 1978 | j = reloc_value(evalresult) != 0; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1979 | break; |
H. Peter Anvin | 95e2882 | 2007-09-12 04:20:08 +0000 | [diff] [blame] | 1980 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1981 | default: |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1982 | nasm_fatal("preprocessor directive `%s' not yet implemented", |
| 1983 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1984 | goto fail; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1985 | } |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1986 | |
| 1987 | free_tlist(origline); |
| 1988 | return j ^ PP_NEGATIVE(ct); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1989 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1990 | fail: |
| 1991 | free_tlist(origline); |
| 1992 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1993 | } |
| 1994 | |
| 1995 | /* |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 1996 | * Common code for defining an smacro |
| 1997 | */ |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1998 | static bool define_smacro(Context *ctx, const char *mname, bool casesense, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1999 | int nparam, Token *expansion) |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2000 | { |
| 2001 | SMacro *smac, **smhead; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2002 | struct hash_table *smtbl; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2003 | |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2004 | if (smacro_defined(ctx, mname, nparam, &smac, casesense)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2005 | if (!smac) { |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2006 | nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and" |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2007 | " without parameters", mname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2008 | /* |
| 2009 | * Some instances of the old code considered this a failure, |
| 2010 | * some others didn't. What is the right thing to do here? |
| 2011 | */ |
| 2012 | free_tlist(expansion); |
| 2013 | return false; /* Failure */ |
| 2014 | } else { |
| 2015 | /* |
| 2016 | * We're redefining, so we have to take over an |
| 2017 | * existing SMacro structure. This means freeing |
| 2018 | * what was already in it. |
| 2019 | */ |
| 2020 | nasm_free(smac->name); |
| 2021 | free_tlist(smac->expansion); |
| 2022 | } |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2023 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2024 | smtbl = ctx ? &ctx->localmac : &smacros; |
| 2025 | smhead = (SMacro **) hash_findi_add(smtbl, mname); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2026 | smac = nasm_malloc(sizeof(SMacro)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2027 | smac->next = *smhead; |
| 2028 | *smhead = smac; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2029 | } |
| 2030 | smac->name = nasm_strdup(mname); |
| 2031 | smac->casesense = casesense; |
| 2032 | smac->nparam = nparam; |
| 2033 | smac->expansion = expansion; |
| 2034 | smac->in_progress = false; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2035 | return true; /* Success */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2036 | } |
| 2037 | |
| 2038 | /* |
| 2039 | * Undefine an smacro |
| 2040 | */ |
| 2041 | static void undef_smacro(Context *ctx, const char *mname) |
| 2042 | { |
| 2043 | SMacro **smhead, *s, **sp; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2044 | struct hash_table *smtbl; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2045 | |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2046 | smtbl = ctx ? &ctx->localmac : &smacros; |
| 2047 | smhead = (SMacro **)hash_findi(smtbl, mname, NULL); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2048 | |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2049 | if (smhead) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2050 | /* |
| 2051 | * We now have a macro name... go hunt for it. |
| 2052 | */ |
| 2053 | sp = smhead; |
| 2054 | while ((s = *sp) != NULL) { |
| 2055 | if (!mstrcmp(s->name, mname, s->casesense)) { |
| 2056 | *sp = s->next; |
| 2057 | nasm_free(s->name); |
| 2058 | free_tlist(s->expansion); |
| 2059 | nasm_free(s); |
| 2060 | } else { |
| 2061 | sp = &s->next; |
| 2062 | } |
| 2063 | } |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2064 | } |
| 2065 | } |
| 2066 | |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2067 | /* |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2068 | * Parse a mmacro specification. |
| 2069 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2070 | 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] | 2071 | { |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2072 | tline = tline->next; |
| 2073 | skip_white_(tline); |
| 2074 | tline = expand_id(tline); |
| 2075 | if (!tok_type_(tline, TOK_ID)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2076 | nasm_nonfatal("`%s' expects a macro name", directive); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2077 | return false; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2078 | } |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2079 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2080 | def->prev = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2081 | def->name = nasm_strdup(tline->text); |
| 2082 | def->plus = false; |
| 2083 | def->nolist = false; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2084 | def->in_progress = 0; |
| 2085 | def->rep_nest = NULL; |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2086 | def->nparam_min = 0; |
| 2087 | def->nparam_max = 0; |
| 2088 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2089 | tline = expand_smacro(tline->next); |
| 2090 | skip_white_(tline); |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2091 | if (!tok_type_(tline, TOK_NUMBER)) |
| 2092 | nasm_nonfatal("`%s' expects a parameter count", directive); |
| 2093 | else |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 2094 | def->nparam_min = def->nparam_max = read_param_count(tline->text); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2095 | if (tline && tok_is_(tline->next, "-")) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2096 | tline = tline->next->next; |
| 2097 | if (tok_is_(tline, "*")) { |
| 2098 | def->nparam_max = INT_MAX; |
| 2099 | } else if (!tok_type_(tline, TOK_NUMBER)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2100 | nasm_nonfatal("`%s' expects a parameter count after `-'", directive); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2101 | } else { |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 2102 | def->nparam_max = read_param_count(tline->text); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2103 | if (def->nparam_min > def->nparam_max) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2104 | nasm_nonfatal("minimum parameter count exceeds maximum"); |
Cyrill Gorcunov | c9244ea | 2017-10-22 15:25:48 +0300 | [diff] [blame] | 2105 | def->nparam_max = def->nparam_min; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2106 | } |
| 2107 | } |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2108 | } |
| 2109 | if (tline && tok_is_(tline->next, "+")) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2110 | tline = tline->next; |
| 2111 | def->plus = true; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2112 | } |
| 2113 | if (tline && tok_type_(tline->next, TOK_ID) && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2114 | !nasm_stricmp(tline->next->text, ".nolist")) { |
| 2115 | tline = tline->next; |
| 2116 | def->nolist = true; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2117 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2118 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2119 | /* |
| 2120 | * Handle default parameters. |
| 2121 | */ |
| 2122 | if (tline && tline->next) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2123 | def->dlist = tline->next; |
| 2124 | tline->next = NULL; |
| 2125 | count_mmac_params(def->dlist, &def->ndefs, &def->defaults); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2126 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2127 | def->dlist = NULL; |
| 2128 | def->defaults = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2129 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2130 | def->expansion = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2131 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2132 | if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min && |
H. Peter Anvin (Intel) | 723ab48 | 2018-12-13 21:53:31 -0800 | [diff] [blame] | 2133 | !def->plus) { |
| 2134 | /* |
| 2135 | *!macro-defaults [on] macros with more default than optional parameters |
| 2136 | *! warns when a macro has more default parameters than optional parameters. |
| 2137 | *! See \k{mlmacdef} for why might want to disable this warning. |
| 2138 | */ |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2139 | nasm_warn(WARN_MACRO_DEFAULTS, |
H. Peter Anvin (Intel) | 723ab48 | 2018-12-13 21:53:31 -0800 | [diff] [blame] | 2140 | "too many default macro parameters in macro `%s'", def->name); |
| 2141 | } |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2142 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2143 | return true; |
| 2144 | } |
| 2145 | |
| 2146 | |
| 2147 | /* |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2148 | * Decode a size directive |
| 2149 | */ |
| 2150 | static int parse_size(const char *str) { |
| 2151 | static const char *size_names[] = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2152 | { "byte", "dword", "oword", "qword", "tword", "word", "yword" }; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2153 | static const int sizes[] = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2154 | { 0, 1, 4, 16, 8, 10, 2, 32 }; |
Cyrill Gorcunov | c713b5f | 2018-09-29 14:30:14 +0300 | [diff] [blame] | 2155 | 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] | 2156 | } |
| 2157 | |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2158 | /* |
| 2159 | * Process a preprocessor %pragma directive. Currently there are none. |
| 2160 | * Gets passed the token list starting with the "preproc" token from |
| 2161 | * "%pragma preproc". |
| 2162 | */ |
| 2163 | static void do_pragma_preproc(Token *tline) |
| 2164 | { |
| 2165 | /* Skip to the real stuff */ |
| 2166 | tline = tline->next; |
| 2167 | skip_white_(tline); |
| 2168 | if (!tline) |
| 2169 | return; |
| 2170 | |
| 2171 | (void)tline; /* Nothing else to do at present */ |
| 2172 | } |
| 2173 | |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2174 | /** |
| 2175 | * find and process preprocessor directive in passed line |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2176 | * Find out if a line contains a preprocessor directive, and deal |
| 2177 | * with it if so. |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2178 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2179 | * If a directive _is_ found, it is the responsibility of this routine |
| 2180 | * (and not the caller) to free_tlist() the line. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2181 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2182 | * @param tline a pointer to the current tokeninzed line linked list |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2183 | * @param output if this directive generated output |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2184 | * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2185 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2186 | */ |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2187 | static int do_directive(Token *tline, char **output) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2188 | { |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2189 | enum preproc_token i; |
| 2190 | int j; |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 2191 | bool err; |
| 2192 | int nparam; |
| 2193 | bool nolist; |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 2194 | bool casesense; |
H. Peter Anvin | 8cfdb9d | 2007-09-14 18:36:01 -0700 | [diff] [blame] | 2195 | int k, m; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2196 | int offset; |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 2197 | char *p, *pp; |
| 2198 | const char *found_path; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 2199 | const char *mname; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2200 | Include *inc; |
| 2201 | Context *ctx; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2202 | Cond *cond; |
| 2203 | MMacro *mmac, **mmhead; |
Jin Kyu Song | c9486b9 | 2013-10-28 17:07:57 -0700 | [diff] [blame] | 2204 | Token *t = NULL, *tt, *param_start, *macro_start, *last, **tptr, *origline; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2205 | Line *l; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2206 | struct tokenval tokval; |
| 2207 | expr *evalresult; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2208 | MMacro *tmp_defining; /* Used when manipulating rep_nest */ |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 2209 | int64_t count; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 2210 | size_t len; |
H. Peter Anvin (Intel) | 6bde2ed | 2018-12-13 19:39:41 -0800 | [diff] [blame] | 2211 | errflags severity; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2212 | |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2213 | *output = NULL; /* No output generated */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2214 | origline = tline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2215 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2216 | skip_white_(tline); |
H. Peter Anvin | f2936d7 | 2008-06-04 15:11:23 -0700 | [diff] [blame] | 2217 | if (!tline || !tok_type_(tline, TOK_PREPROC_ID) || |
Cyrill Gorcunov | 4b5b737 | 2018-10-29 22:54:08 +0300 | [diff] [blame] | 2218 | (tline->text[0] && (tline->text[1] == '%' || |
| 2219 | tline->text[1] == '$' || |
| 2220 | tline->text[1] == '!'))) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2221 | return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2222 | |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2223 | i = pp_token_hash(tline->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2224 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2225 | /* |
| 2226 | * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO |
| 2227 | * since they are known to be buggy at moment, we need to fix them |
| 2228 | * in future release (2.09-2.10) |
| 2229 | */ |
Philipp Kloke | b432f57 | 2013-03-31 12:01:23 +0200 | [diff] [blame] | 2230 | if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2231 | nasm_nonfatal("unknown preprocessor directive `%s'", tline->text); |
| 2232 | return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2233 | } |
| 2234 | |
| 2235 | /* |
| 2236 | * If we're in a non-emitting branch of a condition construct, |
| 2237 | * or walking to the end of an already terminated %rep block, |
| 2238 | * we should ignore all directives except for condition |
| 2239 | * directives. |
| 2240 | */ |
| 2241 | if (((istk->conds && !emitting(istk->conds->state)) || |
| 2242 | (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) { |
| 2243 | return NO_DIRECTIVE_FOUND; |
| 2244 | } |
| 2245 | |
| 2246 | /* |
| 2247 | * If we're defining a macro or reading a %rep block, we should |
| 2248 | * ignore all directives except for %macro/%imacro (which nest), |
| 2249 | * %endm/%endmacro, and (only if we're in a %rep block) %endrep. |
| 2250 | * If we're in a %rep block, another %rep nests, so should be let through. |
| 2251 | */ |
| 2252 | if (defining && i != PP_MACRO && i != PP_IMACRO && |
| 2253 | i != PP_RMACRO && i != PP_IRMACRO && |
| 2254 | i != PP_ENDMACRO && i != PP_ENDM && |
| 2255 | (defining->name || (i != PP_ENDREP && i != PP_REP))) { |
| 2256 | return NO_DIRECTIVE_FOUND; |
| 2257 | } |
| 2258 | |
| 2259 | if (defining) { |
| 2260 | if (i == PP_MACRO || i == PP_IMACRO || |
| 2261 | i == PP_RMACRO || i == PP_IRMACRO) { |
| 2262 | nested_mac_count++; |
| 2263 | return NO_DIRECTIVE_FOUND; |
| 2264 | } else if (nested_mac_count > 0) { |
| 2265 | if (i == PP_ENDMACRO) { |
| 2266 | nested_mac_count--; |
| 2267 | return NO_DIRECTIVE_FOUND; |
| 2268 | } |
| 2269 | } |
| 2270 | if (!defining->name) { |
| 2271 | if (i == PP_REP) { |
| 2272 | nested_rep_count++; |
| 2273 | return NO_DIRECTIVE_FOUND; |
| 2274 | } else if (nested_rep_count > 0) { |
| 2275 | if (i == PP_ENDREP) { |
| 2276 | nested_rep_count--; |
| 2277 | return NO_DIRECTIVE_FOUND; |
| 2278 | } |
| 2279 | } |
| 2280 | } |
| 2281 | } |
| 2282 | |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2283 | switch (i) { |
| 2284 | case PP_INVALID: |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2285 | nasm_nonfatal("unknown preprocessor directive `%s'", tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2286 | return NO_DIRECTIVE_FOUND; /* didn't get it */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2287 | |
H. Peter Anvin | 3f87a2a | 2016-10-04 14:07:19 -0700 | [diff] [blame] | 2288 | case PP_PRAGMA: |
| 2289 | /* |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2290 | * %pragma namespace options... |
| 2291 | * |
| 2292 | * The namespace "preproc" is reserved for the preprocessor; |
| 2293 | * all other namespaces generate a [pragma] assembly directive. |
| 2294 | * |
| 2295 | * Invalid %pragmas are ignored and may have different |
| 2296 | * meaning in future versions of NASM. |
H. Peter Anvin | 3f87a2a | 2016-10-04 14:07:19 -0700 | [diff] [blame] | 2297 | */ |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2298 | tline = tline->next; |
| 2299 | skip_white_(tline); |
| 2300 | tline = expand_smacro(tline); |
| 2301 | if (tok_type_(tline, TOK_ID)) { |
| 2302 | if (!nasm_stricmp(tline->text, "preproc")) { |
| 2303 | /* Preprocessor pragma */ |
| 2304 | do_pragma_preproc(tline); |
| 2305 | } else { |
| 2306 | /* Build the assembler directive */ |
| 2307 | t = new_Token(NULL, TOK_OTHER, "[", 1); |
| 2308 | t->next = new_Token(NULL, TOK_ID, "pragma", 6); |
| 2309 | t->next->next = new_Token(tline, TOK_WHITESPACE, NULL, 0); |
| 2310 | tline = t; |
| 2311 | for (t = tline; t->next; t = t->next) |
| 2312 | ; |
| 2313 | t->next = new_Token(NULL, TOK_OTHER, "]", 1); |
| 2314 | /* true here can be revisited in the future */ |
| 2315 | *output = detoken(tline, true); |
| 2316 | } |
| 2317 | } |
H. Peter Anvin | 3f87a2a | 2016-10-04 14:07:19 -0700 | [diff] [blame] | 2318 | free_tlist(origline); |
| 2319 | return DIRECTIVE_FOUND; |
| 2320 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2321 | case PP_STACKSIZE: |
| 2322 | /* Directive to tell NASM what the default stack size is. The |
| 2323 | * default is for a 16-bit stack, and this can be overriden with |
| 2324 | * %stacksize large. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2325 | */ |
| 2326 | tline = tline->next; |
| 2327 | if (tline && tline->type == TOK_WHITESPACE) |
| 2328 | tline = tline->next; |
| 2329 | if (!tline || tline->type != TOK_ID) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2330 | nasm_nonfatal("`%%stacksize' missing size parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2331 | free_tlist(origline); |
| 2332 | return DIRECTIVE_FOUND; |
| 2333 | } |
| 2334 | if (nasm_stricmp(tline->text, "flat") == 0) { |
| 2335 | /* All subsequent ARG directives are for a 32-bit stack */ |
| 2336 | StackSize = 4; |
| 2337 | StackPointer = "ebp"; |
| 2338 | ArgOffset = 8; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2339 | LocalOffset = 0; |
Charles Crayne | 7eaf919 | 2007-11-08 22:11:14 -0800 | [diff] [blame] | 2340 | } else if (nasm_stricmp(tline->text, "flat64") == 0) { |
| 2341 | /* All subsequent ARG directives are for a 64-bit stack */ |
| 2342 | StackSize = 8; |
| 2343 | StackPointer = "rbp"; |
Per Jessen | 53252e0 | 2010-02-11 00:16:59 +0300 | [diff] [blame] | 2344 | ArgOffset = 16; |
Charles Crayne | 7eaf919 | 2007-11-08 22:11:14 -0800 | [diff] [blame] | 2345 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2346 | } else if (nasm_stricmp(tline->text, "large") == 0) { |
| 2347 | /* All subsequent ARG directives are for a 16-bit stack, |
| 2348 | * far function call. |
| 2349 | */ |
| 2350 | StackSize = 2; |
| 2351 | StackPointer = "bp"; |
| 2352 | ArgOffset = 4; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2353 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2354 | } else if (nasm_stricmp(tline->text, "small") == 0) { |
| 2355 | /* All subsequent ARG directives are for a 16-bit stack, |
| 2356 | * far function call. We don't support near functions. |
| 2357 | */ |
| 2358 | StackSize = 2; |
| 2359 | StackPointer = "bp"; |
| 2360 | ArgOffset = 6; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2361 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2362 | } else { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2363 | nasm_nonfatal("`%%stacksize' invalid size type"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2364 | free_tlist(origline); |
| 2365 | return DIRECTIVE_FOUND; |
| 2366 | } |
| 2367 | free_tlist(origline); |
| 2368 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2369 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2370 | case PP_ARG: |
| 2371 | /* TASM like ARG directive to define arguments to functions, in |
| 2372 | * the following form: |
| 2373 | * |
| 2374 | * ARG arg1:WORD, arg2:DWORD, arg4:QWORD |
| 2375 | */ |
| 2376 | offset = ArgOffset; |
| 2377 | do { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 2378 | char *arg, directive[256]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2379 | int size = StackSize; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2380 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2381 | /* Find the argument name */ |
| 2382 | tline = tline->next; |
| 2383 | if (tline && tline->type == TOK_WHITESPACE) |
| 2384 | tline = tline->next; |
| 2385 | if (!tline || tline->type != TOK_ID) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2386 | nasm_nonfatal("`%%arg' missing argument parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2387 | free_tlist(origline); |
| 2388 | return DIRECTIVE_FOUND; |
| 2389 | } |
| 2390 | arg = tline->text; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2391 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2392 | /* Find the argument size type */ |
| 2393 | tline = tline->next; |
| 2394 | if (!tline || tline->type != TOK_OTHER |
| 2395 | || tline->text[0] != ':') { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2396 | nasm_nonfatal("Syntax error processing `%%arg' directive"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2397 | free_tlist(origline); |
| 2398 | return DIRECTIVE_FOUND; |
| 2399 | } |
| 2400 | tline = tline->next; |
| 2401 | if (!tline || tline->type != TOK_ID) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2402 | nasm_nonfatal("`%%arg' missing size type parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2403 | free_tlist(origline); |
| 2404 | return DIRECTIVE_FOUND; |
| 2405 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2406 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2407 | /* Allow macro expansion of type parameter */ |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2408 | tt = tokenize(tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2409 | tt = expand_smacro(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2410 | size = parse_size(tt->text); |
| 2411 | if (!size) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2412 | nasm_nonfatal("Invalid size type for `%%arg' missing directive"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2413 | free_tlist(tt); |
| 2414 | free_tlist(origline); |
| 2415 | return DIRECTIVE_FOUND; |
| 2416 | } |
| 2417 | free_tlist(tt); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2418 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2419 | /* Round up to even stack slots */ |
| 2420 | size = ALIGN(size, StackSize); |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2421 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2422 | /* Now define the macro for the argument */ |
| 2423 | snprintf(directive, sizeof(directive), "%%define %s (%s+%d)", |
| 2424 | arg, StackPointer, offset); |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2425 | do_directive(tokenize(directive), output); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2426 | offset += size; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2427 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2428 | /* Move to the next argument in the list */ |
| 2429 | tline = tline->next; |
| 2430 | if (tline && tline->type == TOK_WHITESPACE) |
| 2431 | tline = tline->next; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2432 | } while (tline && tline->type == TOK_OTHER && tline->text[0] == ','); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2433 | ArgOffset = offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2434 | free_tlist(origline); |
| 2435 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2436 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2437 | case PP_LOCAL: |
| 2438 | /* TASM like LOCAL directive to define local variables for a |
| 2439 | * function, in the following form: |
| 2440 | * |
| 2441 | * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize |
| 2442 | * |
| 2443 | * The '= LocalSize' at the end is ignored by NASM, but is |
| 2444 | * required by TASM to define the local parameter size (and used |
| 2445 | * by the TASM macro package). |
| 2446 | */ |
| 2447 | offset = LocalOffset; |
| 2448 | do { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 2449 | char *local, directive[256]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2450 | int size = StackSize; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2451 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2452 | /* Find the argument name */ |
| 2453 | tline = tline->next; |
| 2454 | if (tline && tline->type == TOK_WHITESPACE) |
| 2455 | tline = tline->next; |
| 2456 | if (!tline || tline->type != TOK_ID) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2457 | nasm_nonfatal("`%%local' missing argument parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2458 | free_tlist(origline); |
| 2459 | return DIRECTIVE_FOUND; |
| 2460 | } |
| 2461 | local = tline->text; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2462 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2463 | /* Find the argument size type */ |
| 2464 | tline = tline->next; |
| 2465 | if (!tline || tline->type != TOK_OTHER |
| 2466 | || tline->text[0] != ':') { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2467 | nasm_nonfatal("Syntax error processing `%%local' directive"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2468 | free_tlist(origline); |
| 2469 | return DIRECTIVE_FOUND; |
| 2470 | } |
| 2471 | tline = tline->next; |
| 2472 | if (!tline || tline->type != TOK_ID) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2473 | nasm_nonfatal("`%%local' missing size type parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2474 | free_tlist(origline); |
| 2475 | return DIRECTIVE_FOUND; |
| 2476 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2477 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2478 | /* Allow macro expansion of type parameter */ |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2479 | tt = tokenize(tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2480 | tt = expand_smacro(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2481 | size = parse_size(tt->text); |
| 2482 | if (!size) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2483 | nasm_nonfatal("Invalid size type for `%%local' missing directive"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2484 | free_tlist(tt); |
| 2485 | free_tlist(origline); |
| 2486 | return DIRECTIVE_FOUND; |
| 2487 | } |
| 2488 | free_tlist(tt); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2489 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2490 | /* Round up to even stack slots */ |
| 2491 | size = ALIGN(size, StackSize); |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2492 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2493 | offset += size; /* Negative offset, increment before */ |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2494 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2495 | /* Now define the macro for the argument */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2496 | snprintf(directive, sizeof(directive), "%%define %s (%s-%d)", |
| 2497 | local, StackPointer, offset); |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2498 | do_directive(tokenize(directive), output); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2499 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2500 | /* Now define the assign to setup the enter_c macro correctly */ |
| 2501 | snprintf(directive, sizeof(directive), |
| 2502 | "%%assign %%$localsize %%$localsize+%d", size); |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2503 | do_directive(tokenize(directive), output); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2504 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2505 | /* Move to the next argument in the list */ |
| 2506 | tline = tline->next; |
| 2507 | if (tline && tline->type == TOK_WHITESPACE) |
| 2508 | tline = tline->next; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2509 | } while (tline && tline->type == TOK_OTHER && tline->text[0] == ','); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2510 | LocalOffset = offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2511 | free_tlist(origline); |
| 2512 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2513 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2514 | case PP_CLEAR: |
| 2515 | if (tline->next) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2516 | nasm_warn(WARN_OTHER, "trailing garbage after `%%clear' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2517 | free_macros(); |
| 2518 | init_macros(); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2519 | free_tlist(origline); |
| 2520 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2521 | |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2522 | case PP_DEPEND: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2523 | t = tline->next = expand_smacro(tline->next); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2524 | skip_white_(t); |
| 2525 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2526 | t->type != TOK_INTERNAL_STRING)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2527 | nasm_nonfatal("`%%depend' expects a file name"); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2528 | free_tlist(origline); |
| 2529 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2530 | } |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2531 | if (t->next) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2532 | nasm_warn(WARN_OTHER, "trailing garbage after `%%depend' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2533 | p = t->text; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2534 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2535 | nasm_unquote_cstr(p, i); |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 2536 | strlist_add(deplist, p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2537 | free_tlist(origline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2538 | return DIRECTIVE_FOUND; |
| 2539 | |
| 2540 | case PP_INCLUDE: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2541 | t = tline->next = expand_smacro(tline->next); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2542 | skip_white_(t); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2543 | |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2544 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2545 | t->type != TOK_INTERNAL_STRING)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2546 | nasm_nonfatal("`%%include' expects a file name"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2547 | free_tlist(origline); |
| 2548 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2549 | } |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2550 | if (t->next) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2551 | nasm_warn(WARN_OTHER, "trailing garbage after `%%include' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2552 | p = t->text; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2553 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2554 | nasm_unquote_cstr(p, i); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2555 | inc = nasm_malloc(sizeof(Include)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2556 | inc->next = istk; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2557 | inc->conds = NULL; |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 2558 | found_path = NULL; |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 2559 | inc->fp = inc_fopen(p, deplist, &found_path, |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 2560 | (pp_mode == PP_DEPS) |
| 2561 | ? INC_OPTIONAL : INC_NEEDED, NF_TEXT); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2562 | if (!inc->fp) { |
| 2563 | /* -MG given but file not found */ |
| 2564 | nasm_free(inc); |
| 2565 | } else { |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 2566 | inc->fname = src_set_fname(found_path ? found_path : p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2567 | inc->lineno = src_set_linnum(0); |
| 2568 | inc->lineinc = 1; |
| 2569 | inc->expansion = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2570 | inc->mstk = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2571 | istk = inc; |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 2572 | lfmt->uplevel(LIST_INCLUDE); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2573 | } |
| 2574 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2575 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2576 | |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2577 | case PP_USE: |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2578 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2579 | static macros_t *use_pkg; |
| 2580 | const char *pkg_macro = NULL; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2581 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2582 | tline = tline->next; |
| 2583 | skip_white_(tline); |
| 2584 | tline = expand_id(tline); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2585 | |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2586 | if (!tline || (tline->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2587 | tline->type != TOK_INTERNAL_STRING && |
| 2588 | tline->type != TOK_ID)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2589 | nasm_nonfatal("`%%use' expects a package name"); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2590 | free_tlist(origline); |
| 2591 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2592 | } |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2593 | if (tline->next) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2594 | nasm_warn(WARN_OTHER, "trailing garbage after `%%use' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2595 | if (tline->type == TOK_STRING) |
| 2596 | nasm_unquote_cstr(tline->text, i); |
| 2597 | use_pkg = nasm_stdmac_find_package(tline->text); |
| 2598 | if (!use_pkg) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2599 | nasm_nonfatal("unknown `%%use' package: %s", tline->text); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2600 | else |
| 2601 | 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] | 2602 | if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2603 | /* Not already included, go ahead and include it */ |
| 2604 | stdmacpos = use_pkg; |
| 2605 | } |
| 2606 | free_tlist(origline); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2607 | return DIRECTIVE_FOUND; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2608 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2609 | case PP_PUSH: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2610 | case PP_REPL: |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2611 | case PP_POP: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2612 | tline = tline->next; |
| 2613 | skip_white_(tline); |
| 2614 | tline = expand_id(tline); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2615 | if (tline) { |
| 2616 | if (!tok_type_(tline, TOK_ID)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2617 | nasm_nonfatal("`%s' expects a context identifier", |
| 2618 | pp_directives[i]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2619 | free_tlist(origline); |
| 2620 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2621 | } |
| 2622 | if (tline->next) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2623 | nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2624 | pp_directives[i]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2625 | p = nasm_strdup(tline->text); |
| 2626 | } else { |
| 2627 | p = NULL; /* Anonymous */ |
| 2628 | } |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2629 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2630 | if (i == PP_PUSH) { |
H. Peter Anvin (Intel) | ebb05a0 | 2018-12-11 12:30:25 -0800 | [diff] [blame] | 2631 | nasm_new(ctx); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2632 | ctx->next = cstk; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2633 | ctx->name = p; |
| 2634 | ctx->number = unique++; |
| 2635 | cstk = ctx; |
| 2636 | } else { |
| 2637 | /* %pop or %repl */ |
| 2638 | if (!cstk) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2639 | nasm_nonfatal("`%s': context stack is empty", |
| 2640 | pp_directives[i]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2641 | } else if (i == PP_POP) { |
| 2642 | if (p && (!cstk->name || nasm_stricmp(p, cstk->name))) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2643 | nasm_nonfatal("`%%pop' in wrong context: %s, expected %s", |
| 2644 | cstk->name ? cstk->name : "anonymous", p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2645 | else |
| 2646 | ctx_pop(); |
| 2647 | } else { |
| 2648 | /* i == PP_REPL */ |
| 2649 | nasm_free(cstk->name); |
| 2650 | cstk->name = p; |
| 2651 | p = NULL; |
| 2652 | } |
| 2653 | nasm_free(p); |
| 2654 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2655 | free_tlist(origline); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2656 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2657 | case PP_FATAL: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2658 | severity = ERR_FATAL; |
| 2659 | goto issue_error; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2660 | case PP_ERROR: |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2661 | severity = ERR_NONFATAL|ERR_PASS2; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2662 | goto issue_error; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2663 | case PP_WARNING: |
H. Peter Anvin (Intel) | 723ab48 | 2018-12-13 21:53:31 -0800 | [diff] [blame] | 2664 | /*! |
| 2665 | *!user [on] %warning directives |
| 2666 | *! controls output of \c{%warning} directives (see \k{pperror}). |
| 2667 | */ |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2668 | severity = ERR_WARNING|WARN_USER|ERR_PASS2; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2669 | goto issue_error; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2670 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2671 | issue_error: |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2672 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2673 | /* Only error out if this is the final pass */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2674 | tline->next = expand_smacro(tline->next); |
| 2675 | tline = tline->next; |
| 2676 | skip_white_(tline); |
| 2677 | t = tline ? tline->next : NULL; |
| 2678 | skip_white_(t); |
| 2679 | if (tok_type_(tline, TOK_STRING) && !t) { |
| 2680 | /* The line contains only a quoted string */ |
| 2681 | p = tline->text; |
| 2682 | nasm_unquote(p, NULL); /* Ignore NUL character truncation */ |
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 | } else { |
| 2685 | /* Not a quoted string, or more than a quoted string */ |
| 2686 | p = detoken(tline, false); |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2687 | nasm_error(severity, "%s", p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2688 | nasm_free(p); |
| 2689 | } |
| 2690 | free_tlist(origline); |
| 2691 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2692 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2693 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2694 | CASE_PP_IF: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2695 | if (istk->conds && !emitting(istk->conds->state)) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2696 | j = COND_NEVER; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2697 | else { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2698 | j = if_condition(tline->next, i); |
| 2699 | tline->next = NULL; /* it got freed */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2700 | j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2701 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2702 | cond = nasm_malloc(sizeof(Cond)); |
| 2703 | cond->next = istk->conds; |
| 2704 | cond->state = j; |
| 2705 | istk->conds = cond; |
| 2706 | if(istk->mstk) |
| 2707 | istk->mstk->condcnt ++; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2708 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2709 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2710 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2711 | CASE_PP_ELIF: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2712 | if (!istk->conds) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2713 | nasm_fatal("`%s': no matching `%%if'", pp_directives[i]); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2714 | switch(istk->conds->state) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2715 | case COND_IF_TRUE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2716 | istk->conds->state = COND_DONE; |
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 | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2719 | case COND_DONE: |
| 2720 | case COND_NEVER: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2721 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2722 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2723 | case COND_ELSE_TRUE: |
| 2724 | case COND_ELSE_FALSE: |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2725 | nasm_warn(WARN_OTHER|ERR_PP_PRECOND, |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2726 | "`%%elif' after `%%else' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2727 | istk->conds->state = COND_NEVER; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2728 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2729 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2730 | case COND_IF_FALSE: |
| 2731 | /* |
| 2732 | * IMPORTANT: In the case of %if, we will already have |
| 2733 | * called expand_mmac_params(); however, if we're |
| 2734 | * processing an %elif we must have been in a |
| 2735 | * non-emitting mode, which would have inhibited |
| 2736 | * the normal invocation of expand_mmac_params(). |
| 2737 | * Therefore, we have to do it explicitly here. |
| 2738 | */ |
| 2739 | j = if_condition(expand_mmac_params(tline->next), i); |
| 2740 | tline->next = NULL; /* it got freed */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2741 | istk->conds->state = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2742 | j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; |
| 2743 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2744 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2745 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2746 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2747 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2748 | case PP_ELSE: |
| 2749 | if (tline->next) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2750 | nasm_warn(WARN_OTHER|ERR_PP_PRECOND, |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2751 | "trailing garbage after `%%else' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2752 | if (!istk->conds) |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 2753 | nasm_fatal("`%%else: no matching `%%if'"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2754 | switch(istk->conds->state) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2755 | case COND_IF_TRUE: |
| 2756 | case COND_DONE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2757 | istk->conds->state = COND_ELSE_FALSE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2758 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2759 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2760 | case COND_NEVER: |
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 | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2763 | case COND_IF_FALSE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2764 | istk->conds->state = COND_ELSE_TRUE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2765 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2766 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2767 | case COND_ELSE_TRUE: |
| 2768 | case COND_ELSE_FALSE: |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2769 | nasm_warn(WARN_OTHER|ERR_PP_PRECOND, |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2770 | "`%%else' after `%%else' ignored."); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2771 | istk->conds->state = COND_NEVER; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2772 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2773 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2774 | free_tlist(origline); |
| 2775 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2776 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2777 | case PP_ENDIF: |
| 2778 | if (tline->next) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2779 | nasm_warn(WARN_OTHER|ERR_PP_PRECOND, |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2780 | "trailing garbage after `%%endif' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2781 | if (!istk->conds) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2782 | nasm_fatal("`%%endif': no matching `%%if'"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2783 | cond = istk->conds; |
| 2784 | istk->conds = cond->next; |
| 2785 | nasm_free(cond); |
| 2786 | if(istk->mstk) |
| 2787 | istk->mstk->condcnt --; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2788 | free_tlist(origline); |
| 2789 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2790 | |
H. Peter Anvin | db8f96e | 2009-07-15 09:07:29 -0400 | [diff] [blame] | 2791 | case PP_RMACRO: |
| 2792 | case PP_IRMACRO: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2793 | case PP_MACRO: |
| 2794 | case PP_IMACRO: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2795 | if (defining) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2796 | nasm_fatal("`%s': already defining a macro", |
| 2797 | pp_directives[i]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2798 | return DIRECTIVE_FOUND; |
| 2799 | } |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 2800 | defining = nasm_zalloc(sizeof(MMacro)); |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 2801 | defining->max_depth = ((i == PP_RMACRO) || (i == PP_IRMACRO)) |
| 2802 | ? nasm_limit[LIMIT_MACROS] : 0; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2803 | defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO); |
| 2804 | if (!parse_mmacro_spec(tline, defining, pp_directives[i])) { |
| 2805 | nasm_free(defining); |
| 2806 | defining = NULL; |
| 2807 | return DIRECTIVE_FOUND; |
| 2808 | } |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2809 | |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 2810 | src_get(&defining->xline, &defining->fname); |
| 2811 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2812 | mmac = (MMacro *) hash_findix(&mmacros, defining->name); |
| 2813 | while (mmac) { |
| 2814 | if (!strcmp(mmac->name, defining->name) && |
| 2815 | (mmac->nparam_min <= defining->nparam_max |
| 2816 | || defining->plus) |
| 2817 | && (defining->nparam_min <= mmac->nparam_max |
| 2818 | || mmac->plus)) { |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2819 | nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'", |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2820 | defining->name); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2821 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2822 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2823 | mmac = mmac->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2824 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2825 | free_tlist(origline); |
| 2826 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2827 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2828 | case PP_ENDM: |
| 2829 | case PP_ENDMACRO: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2830 | if (! (defining && defining->name)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2831 | nasm_nonfatal("`%s': not defining a macro", tline->text); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2832 | return DIRECTIVE_FOUND; |
| 2833 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2834 | mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name); |
| 2835 | defining->next = *mmhead; |
| 2836 | *mmhead = defining; |
| 2837 | defining = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2838 | free_tlist(origline); |
| 2839 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2840 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2841 | case PP_EXITMACRO: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2842 | /* |
| 2843 | * We must search along istk->expansion until we hit a |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2844 | * macro-end marker for a macro with a name. Then we |
| 2845 | * bypass all lines between exitmacro and endmacro. |
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 | list_for_each(l, istk->expansion) |
| 2848 | if (l->finishes && l->finishes->name) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2849 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2850 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2851 | if (l) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2852 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2853 | * Remove all conditional entries relative to this |
| 2854 | * macro invocation. (safe to do in this context) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2855 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2856 | for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) { |
| 2857 | cond = istk->conds; |
| 2858 | istk->conds = cond->next; |
| 2859 | nasm_free(cond); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2860 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2861 | istk->expansion = l; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2862 | } else { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2863 | nasm_nonfatal("`%%exitmacro' not within `%%macro' block"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2864 | } |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 2865 | free_tlist(origline); |
| 2866 | return DIRECTIVE_FOUND; |
| 2867 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2868 | case PP_UNMACRO: |
| 2869 | case PP_UNIMACRO: |
| 2870 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2871 | MMacro **mmac_p; |
| 2872 | MMacro spec; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2873 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2874 | spec.casesense = (i == PP_UNMACRO); |
| 2875 | if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) { |
| 2876 | return DIRECTIVE_FOUND; |
| 2877 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2878 | mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL); |
| 2879 | while (mmac_p && *mmac_p) { |
| 2880 | mmac = *mmac_p; |
| 2881 | if (mmac->casesense == spec.casesense && |
| 2882 | !mstrcmp(mmac->name, spec.name, spec.casesense) && |
| 2883 | mmac->nparam_min == spec.nparam_min && |
| 2884 | mmac->nparam_max == spec.nparam_max && |
| 2885 | mmac->plus == spec.plus) { |
| 2886 | *mmac_p = mmac->next; |
| 2887 | free_mmacro(mmac); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2888 | } else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2889 | mmac_p = &mmac->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2890 | } |
| 2891 | } |
| 2892 | free_tlist(origline); |
| 2893 | free_tlist(spec.dlist); |
| 2894 | return DIRECTIVE_FOUND; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2895 | } |
| 2896 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2897 | case PP_ROTATE: |
| 2898 | if (tline->next && tline->next->type == TOK_WHITESPACE) |
| 2899 | tline = tline->next; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2900 | if (!tline->next) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2901 | free_tlist(origline); |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2902 | nasm_nonfatal("`%%rotate' missing rotate count"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2903 | return DIRECTIVE_FOUND; |
| 2904 | } |
| 2905 | t = expand_smacro(tline->next); |
| 2906 | tline->next = NULL; |
| 2907 | free_tlist(origline); |
| 2908 | tline = t; |
| 2909 | tptr = &t; |
| 2910 | tokval.t_type = TOKEN_INVALID; |
| 2911 | evalresult = |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 2912 | evaluate(ppscan, tptr, &tokval, NULL, true, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2913 | free_tlist(tline); |
| 2914 | if (!evalresult) |
| 2915 | return DIRECTIVE_FOUND; |
| 2916 | if (tokval.t_type) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2917 | nasm_warn(WARN_OTHER, "trailing garbage after expression ignored"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2918 | if (!is_simple(evalresult)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2919 | nasm_nonfatal("non-constant value given to `%%rotate'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2920 | return DIRECTIVE_FOUND; |
| 2921 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2922 | mmac = istk->mstk; |
| 2923 | while (mmac && !mmac->name) /* avoid mistaking %reps for macros */ |
| 2924 | mmac = mmac->next_active; |
| 2925 | if (!mmac) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2926 | nasm_nonfatal("`%%rotate' invoked outside a macro call"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2927 | } else if (mmac->nparam == 0) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2928 | nasm_nonfatal("`%%rotate' invoked within macro without parameters"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2929 | } else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2930 | int rotate = mmac->rotate + reloc_value(evalresult); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2931 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2932 | rotate %= (int)mmac->nparam; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2933 | if (rotate < 0) |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2934 | rotate += mmac->nparam; |
| 2935 | |
| 2936 | mmac->rotate = rotate; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2937 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2938 | return DIRECTIVE_FOUND; |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 2939 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2940 | case PP_REP: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 2941 | nolist = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2942 | do { |
| 2943 | tline = tline->next; |
| 2944 | } while (tok_type_(tline, TOK_WHITESPACE)); |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 2945 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2946 | if (tok_type_(tline, TOK_ID) && |
| 2947 | nasm_stricmp(tline->text, ".nolist") == 0) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 2948 | nolist = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2949 | do { |
| 2950 | tline = tline->next; |
| 2951 | } while (tok_type_(tline, TOK_WHITESPACE)); |
| 2952 | } |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 2953 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2954 | if (tline) { |
| 2955 | t = expand_smacro(tline); |
| 2956 | tptr = &t; |
| 2957 | tokval.t_type = TOKEN_INVALID; |
| 2958 | evalresult = |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 2959 | evaluate(ppscan, tptr, &tokval, NULL, true, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2960 | if (!evalresult) { |
| 2961 | free_tlist(origline); |
| 2962 | return DIRECTIVE_FOUND; |
| 2963 | } |
| 2964 | if (tokval.t_type) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2965 | nasm_warn(WARN_OTHER, "trailing garbage after expression ignored"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2966 | if (!is_simple(evalresult)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2967 | nasm_nonfatal("non-constant value given to `%%rep'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2968 | return DIRECTIVE_FOUND; |
| 2969 | } |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 2970 | count = reloc_value(evalresult); |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 2971 | if (count > nasm_limit[LIMIT_REP]) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2972 | nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")", |
| 2973 | count, nasm_limit[LIMIT_REP]); |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 2974 | count = 0; |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 2975 | } else if (count < 0) { |
H. Peter Anvin (Intel) | 723ab48 | 2018-12-13 21:53:31 -0800 | [diff] [blame] | 2976 | /*! |
| 2977 | *!negative-rep [on] regative %rep count |
| 2978 | *! warns about negative counts given to the \c{%rep} |
| 2979 | *! preprocessor directive. |
| 2980 | */ |
H. Peter Anvin (Intel) | 80c4f23 | 2018-12-14 13:33:24 -0800 | [diff] [blame] | 2981 | nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP, |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 2982 | "negative `%%rep' count: %"PRId64, count); |
| 2983 | count = 0; |
| 2984 | } else { |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 2985 | count++; |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 2986 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2987 | } else { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2988 | nasm_nonfatal("`%%rep' expects a repeat count"); |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 2989 | count = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2990 | } |
| 2991 | free_tlist(origline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2992 | |
| 2993 | tmp_defining = defining; |
| 2994 | defining = nasm_malloc(sizeof(MMacro)); |
| 2995 | defining->prev = NULL; |
| 2996 | defining->name = NULL; /* flags this macro as a %rep block */ |
| 2997 | defining->casesense = false; |
| 2998 | defining->plus = false; |
| 2999 | defining->nolist = nolist; |
| 3000 | defining->in_progress = count; |
| 3001 | defining->max_depth = 0; |
| 3002 | defining->nparam_min = defining->nparam_max = 0; |
| 3003 | defining->defaults = NULL; |
| 3004 | defining->dlist = NULL; |
| 3005 | defining->expansion = NULL; |
| 3006 | defining->next_active = istk->mstk; |
| 3007 | defining->rep_nest = tmp_defining; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3008 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3009 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3010 | case PP_ENDREP: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3011 | if (!defining || defining->name) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3012 | nasm_nonfatal("`%%endrep': no matching `%%rep'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3013 | return DIRECTIVE_FOUND; |
| 3014 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3015 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3016 | /* |
| 3017 | * Now we have a "macro" defined - although it has no name |
| 3018 | * and we won't be entering it in the hash tables - we must |
| 3019 | * push a macro-end marker for it on to istk->expansion. |
| 3020 | * After that, it will take care of propagating itself (a |
| 3021 | * macro-end marker line for a macro which is really a %rep |
| 3022 | * block will cause the macro to be re-expanded, complete |
| 3023 | * with another macro-end marker to ensure the process |
| 3024 | * continues) until the whole expansion is forcibly removed |
| 3025 | * from istk->expansion by a %exitrep. |
| 3026 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3027 | l = nasm_malloc(sizeof(Line)); |
| 3028 | l->next = istk->expansion; |
| 3029 | l->finishes = defining; |
| 3030 | l->first = NULL; |
| 3031 | istk->expansion = l; |
| 3032 | |
| 3033 | istk->mstk = defining; |
| 3034 | |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 3035 | lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3036 | tmp_defining = defining; |
| 3037 | defining = defining->rep_nest; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3038 | free_tlist(origline); |
| 3039 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3040 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3041 | case PP_EXITREP: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3042 | /* |
| 3043 | * We must search along istk->expansion until we hit a |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3044 | * macro-end marker for a macro with no name. Then we set |
| 3045 | * its `in_progress' flag to 0. |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3046 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3047 | list_for_each(l, istk->expansion) |
| 3048 | if (l->finishes && !l->finishes->name) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3049 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3050 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3051 | if (l) |
| 3052 | l->finishes->in_progress = 1; |
| 3053 | else |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3054 | nasm_nonfatal("`%%exitrep' not within `%%rep' block"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3055 | free_tlist(origline); |
| 3056 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3057 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3058 | case PP_XDEFINE: |
| 3059 | case PP_IXDEFINE: |
| 3060 | case PP_DEFINE: |
| 3061 | case PP_IDEFINE: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3062 | casesense = (i == PP_DEFINE || i == PP_XDEFINE); |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3063 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3064 | tline = tline->next; |
| 3065 | skip_white_(tline); |
| 3066 | tline = expand_id(tline); |
| 3067 | if (!tline || (tline->type != TOK_ID && |
| 3068 | (tline->type != TOK_PREPROC_ID || |
| 3069 | tline->text[1] != '$'))) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3070 | nasm_nonfatal("`%s' expects a macro identifier", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3071 | pp_directives[i]); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3072 | free_tlist(origline); |
| 3073 | return DIRECTIVE_FOUND; |
| 3074 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3075 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3076 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3077 | last = tline; |
| 3078 | param_start = tline = tline->next; |
| 3079 | nparam = 0; |
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 | /* Expand the macro definition now for %xdefine and %ixdefine */ |
| 3082 | if ((i == PP_XDEFINE) || (i == PP_IXDEFINE)) |
| 3083 | tline = expand_smacro(tline); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3084 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3085 | if (tok_is_(tline, "(")) { |
| 3086 | /* |
| 3087 | * This macro has parameters. |
| 3088 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3089 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3090 | tline = tline->next; |
| 3091 | while (1) { |
| 3092 | skip_white_(tline); |
| 3093 | if (!tline) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3094 | nasm_nonfatal("parameter identifier expected"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3095 | free_tlist(origline); |
| 3096 | return DIRECTIVE_FOUND; |
| 3097 | } |
| 3098 | if (tline->type != TOK_ID) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3099 | nasm_nonfatal("`%s': parameter identifier expected", |
| 3100 | tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3101 | free_tlist(origline); |
| 3102 | return DIRECTIVE_FOUND; |
| 3103 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3104 | tline->type = TOK_SMAC_PARAM + nparam++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3105 | tline = tline->next; |
| 3106 | skip_white_(tline); |
| 3107 | if (tok_is_(tline, ",")) { |
| 3108 | tline = tline->next; |
H. Peter Anvin | ca348b6 | 2008-07-23 10:49:26 -0400 | [diff] [blame] | 3109 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3110 | if (!tok_is_(tline, ")")) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3111 | nasm_nonfatal("`)' expected to terminate macro template"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3112 | free_tlist(origline); |
| 3113 | return DIRECTIVE_FOUND; |
| 3114 | } |
| 3115 | break; |
| 3116 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3117 | } |
| 3118 | last = tline; |
| 3119 | tline = tline->next; |
| 3120 | } |
| 3121 | if (tok_type_(tline, TOK_WHITESPACE)) |
| 3122 | last = tline, tline = tline->next; |
| 3123 | macro_start = NULL; |
| 3124 | last->next = NULL; |
| 3125 | t = tline; |
| 3126 | while (t) { |
| 3127 | if (t->type == TOK_ID) { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3128 | list_for_each(tt, param_start) |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3129 | if (tt->type >= TOK_SMAC_PARAM && |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3130 | !strcmp(tt->text, t->text)) |
| 3131 | t->type = tt->type; |
| 3132 | } |
| 3133 | tt = t->next; |
| 3134 | t->next = macro_start; |
| 3135 | macro_start = t; |
| 3136 | t = tt; |
| 3137 | } |
| 3138 | /* |
| 3139 | * Good. We now have a macro name, a parameter count, and a |
| 3140 | * token list (in reverse order) for an expansion. We ought |
| 3141 | * to be OK just to create an SMacro, store it, and let |
| 3142 | * free_tlist have the rest of the line (which we have |
| 3143 | * carefully re-terminated after chopping off the expansion |
| 3144 | * from the end). |
| 3145 | */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 3146 | define_smacro(ctx, mname, casesense, nparam, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3147 | free_tlist(origline); |
| 3148 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3149 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3150 | case PP_UNDEF: |
| 3151 | tline = tline->next; |
| 3152 | skip_white_(tline); |
| 3153 | tline = expand_id(tline); |
| 3154 | if (!tline || (tline->type != TOK_ID && |
| 3155 | (tline->type != TOK_PREPROC_ID || |
| 3156 | tline->text[1] != '$'))) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3157 | nasm_nonfatal("`%%undef' expects a macro identifier"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3158 | free_tlist(origline); |
| 3159 | return DIRECTIVE_FOUND; |
| 3160 | } |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3161 | if (tline->next) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 3162 | nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored"); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3163 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3164 | /* Find the context that symbol belongs to */ |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3165 | ctx = get_ctx(tline->text, &mname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3166 | undef_smacro(ctx, mname); |
| 3167 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3168 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3169 | |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3170 | case PP_DEFSTR: |
| 3171 | case PP_IDEFSTR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3172 | casesense = (i == PP_DEFSTR); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3173 | |
| 3174 | tline = tline->next; |
| 3175 | skip_white_(tline); |
| 3176 | tline = expand_id(tline); |
| 3177 | if (!tline || (tline->type != TOK_ID && |
| 3178 | (tline->type != TOK_PREPROC_ID || |
| 3179 | tline->text[1] != '$'))) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3180 | nasm_nonfatal("`%s' expects a macro identifier", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3181 | pp_directives[i]); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3182 | free_tlist(origline); |
| 3183 | return DIRECTIVE_FOUND; |
| 3184 | } |
| 3185 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3186 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3187 | last = tline; |
| 3188 | tline = expand_smacro(tline->next); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3189 | last->next = NULL; |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3190 | |
| 3191 | while (tok_type_(tline, TOK_WHITESPACE)) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3192 | tline = delete_Token(tline); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3193 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3194 | p = detoken(tline, false); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3195 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3196 | macro_start->next = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3197 | macro_start->text = nasm_quote(p, strlen(p)); |
| 3198 | macro_start->type = TOK_STRING; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3199 | macro_start->a.mac = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3200 | nasm_free(p); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3201 | |
| 3202 | /* |
| 3203 | * We now have a macro name, an implicit parameter count of |
| 3204 | * zero, and a string token to use as an expansion. Create |
| 3205 | * and store an SMacro. |
| 3206 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3207 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3208 | free_tlist(origline); |
| 3209 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3210 | |
H. Peter Anvin | 2f55bda | 2009-07-14 15:04:04 -0400 | [diff] [blame] | 3211 | case PP_DEFTOK: |
| 3212 | case PP_IDEFTOK: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3213 | casesense = (i == PP_DEFTOK); |
| 3214 | |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3215 | tline = tline->next; |
| 3216 | skip_white_(tline); |
| 3217 | tline = expand_id(tline); |
| 3218 | if (!tline || (tline->type != TOK_ID && |
| 3219 | (tline->type != TOK_PREPROC_ID || |
| 3220 | tline->text[1] != '$'))) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3221 | nasm_nonfatal("`%s' expects a macro identifier as first parameter", |
| 3222 | pp_directives[i]); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3223 | free_tlist(origline); |
| 3224 | return DIRECTIVE_FOUND; |
| 3225 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3226 | ctx = get_ctx(tline->text, &mname); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3227 | last = tline; |
| 3228 | tline = expand_smacro(tline->next); |
| 3229 | last->next = NULL; |
| 3230 | |
| 3231 | t = tline; |
| 3232 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3233 | t = t->next; |
| 3234 | /* t should now point to the string */ |
Cyrill Gorcunov | 6908e58 | 2010-09-06 19:36:15 +0400 | [diff] [blame] | 3235 | if (!tok_type_(t, TOK_STRING)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3236 | nasm_nonfatal("`%s` requires string as second parameter", |
| 3237 | pp_directives[i]); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3238 | free_tlist(tline); |
| 3239 | free_tlist(origline); |
| 3240 | return DIRECTIVE_FOUND; |
| 3241 | } |
| 3242 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 3243 | /* |
| 3244 | * Convert the string to a token stream. Note that smacros |
| 3245 | * are stored with the token stream reversed, so we have to |
| 3246 | * reverse the output of tokenize(). |
| 3247 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3248 | nasm_unquote_cstr(t->text, i); |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 3249 | macro_start = reverse_tokens(tokenize(t->text)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3250 | |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3251 | /* |
| 3252 | * We now have a macro name, an implicit parameter count of |
| 3253 | * zero, and a numeric token to use as an expansion. Create |
| 3254 | * and store an SMacro. |
| 3255 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3256 | define_smacro(ctx, mname, casesense, 0, macro_start); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3257 | free_tlist(tline); |
| 3258 | free_tlist(origline); |
| 3259 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3260 | |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3261 | case PP_PATHSEARCH: |
| 3262 | { |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 3263 | const char *found_path; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3264 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3265 | casesense = true; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3266 | |
| 3267 | tline = tline->next; |
| 3268 | skip_white_(tline); |
| 3269 | tline = expand_id(tline); |
| 3270 | if (!tline || (tline->type != TOK_ID && |
| 3271 | (tline->type != TOK_PREPROC_ID || |
| 3272 | tline->text[1] != '$'))) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3273 | nasm_nonfatal("`%%pathsearch' expects a macro identifier as first parameter"); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3274 | free_tlist(origline); |
| 3275 | return DIRECTIVE_FOUND; |
| 3276 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3277 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3278 | last = tline; |
| 3279 | tline = expand_smacro(tline->next); |
| 3280 | last->next = NULL; |
| 3281 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3282 | t = tline; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3283 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3284 | t = t->next; |
| 3285 | |
| 3286 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3287 | t->type != TOK_INTERNAL_STRING)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3288 | nasm_nonfatal("`%%pathsearch' expects a file name"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3289 | free_tlist(tline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3290 | free_tlist(origline); |
| 3291 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 3292 | } |
| 3293 | if (t->next) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 3294 | nasm_warn(WARN_OTHER, "trailing garbage after `%%pathsearch' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3295 | p = t->text; |
H. Peter Anvin | 427cc91 | 2008-06-01 21:43:03 -0700 | [diff] [blame] | 3296 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3297 | nasm_unquote(p, NULL); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3298 | |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 3299 | inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY); |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 3300 | if (!found_path) |
| 3301 | found_path = p; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3302 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3303 | macro_start->next = NULL; |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 3304 | macro_start->text = nasm_quote(found_path, strlen(found_path)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3305 | macro_start->type = TOK_STRING; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3306 | macro_start->a.mac = NULL; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3307 | |
| 3308 | /* |
| 3309 | * We now have a macro name, an implicit parameter count of |
| 3310 | * zero, and a string token to use as an expansion. Create |
| 3311 | * and store an SMacro. |
| 3312 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3313 | define_smacro(ctx, mname, casesense, 0, macro_start); |
| 3314 | free_tlist(tline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3315 | free_tlist(origline); |
| 3316 | return DIRECTIVE_FOUND; |
| 3317 | } |
| 3318 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3319 | case PP_STRLEN: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3320 | casesense = true; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 3321 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3322 | tline = tline->next; |
| 3323 | skip_white_(tline); |
| 3324 | tline = expand_id(tline); |
| 3325 | if (!tline || (tline->type != TOK_ID && |
| 3326 | (tline->type != TOK_PREPROC_ID || |
| 3327 | tline->text[1] != '$'))) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3328 | nasm_nonfatal("`%%strlen' expects a macro identifier as first parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3329 | free_tlist(origline); |
| 3330 | return DIRECTIVE_FOUND; |
| 3331 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3332 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3333 | last = tline; |
| 3334 | tline = expand_smacro(tline->next); |
| 3335 | last->next = NULL; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3336 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3337 | t = tline; |
| 3338 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3339 | t = t->next; |
| 3340 | /* t should now point to the string */ |
Cyrill Gorcunov | 4e1d5ab | 2010-07-23 18:51:51 +0400 | [diff] [blame] | 3341 | if (!tok_type_(t, TOK_STRING)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3342 | nasm_nonfatal("`%%strlen` requires string as second parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3343 | free_tlist(tline); |
| 3344 | free_tlist(origline); |
| 3345 | return DIRECTIVE_FOUND; |
| 3346 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3347 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3348 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3349 | macro_start->next = NULL; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 3350 | make_tok_num(macro_start, nasm_unquote(t->text, NULL)); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3351 | macro_start->a.mac = NULL; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3352 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3353 | /* |
| 3354 | * We now have a macro name, an implicit parameter count of |
| 3355 | * zero, and a numeric token to use as an expansion. Create |
| 3356 | * and store an SMacro. |
| 3357 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3358 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3359 | free_tlist(tline); |
| 3360 | free_tlist(origline); |
| 3361 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3362 | |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3363 | case PP_STRCAT: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3364 | casesense = true; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3365 | |
| 3366 | tline = tline->next; |
| 3367 | skip_white_(tline); |
| 3368 | tline = expand_id(tline); |
| 3369 | if (!tline || (tline->type != TOK_ID && |
| 3370 | (tline->type != TOK_PREPROC_ID || |
| 3371 | tline->text[1] != '$'))) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3372 | nasm_nonfatal("`%%strcat' expects a macro identifier as first parameter"); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3373 | free_tlist(origline); |
| 3374 | return DIRECTIVE_FOUND; |
| 3375 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3376 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3377 | last = tline; |
| 3378 | tline = expand_smacro(tline->next); |
| 3379 | last->next = NULL; |
| 3380 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3381 | len = 0; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3382 | list_for_each(t, tline) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3383 | switch (t->type) { |
| 3384 | case TOK_WHITESPACE: |
| 3385 | break; |
| 3386 | case TOK_STRING: |
| 3387 | len += t->a.len = nasm_unquote(t->text, NULL); |
| 3388 | break; |
| 3389 | case TOK_OTHER: |
| 3390 | if (!strcmp(t->text, ",")) /* permit comma separators */ |
| 3391 | break; |
| 3392 | /* else fall through */ |
| 3393 | default: |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3394 | nasm_nonfatal("non-string passed to `%%strcat' (%d)", t->type); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3395 | free_tlist(tline); |
| 3396 | free_tlist(origline); |
| 3397 | return DIRECTIVE_FOUND; |
| 3398 | } |
| 3399 | } |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3400 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3401 | p = pp = nasm_malloc(len); |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3402 | list_for_each(t, tline) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3403 | if (t->type == TOK_STRING) { |
| 3404 | memcpy(p, t->text, t->a.len); |
| 3405 | p += t->a.len; |
| 3406 | } |
| 3407 | } |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3408 | |
| 3409 | /* |
| 3410 | * We now have a macro name, an implicit parameter count of |
| 3411 | * zero, and a numeric token to use as an expansion. Create |
| 3412 | * and store an SMacro. |
| 3413 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3414 | macro_start = new_Token(NULL, TOK_STRING, NULL, 0); |
| 3415 | macro_start->text = nasm_quote(pp, len); |
| 3416 | nasm_free(pp); |
| 3417 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3418 | free_tlist(tline); |
| 3419 | free_tlist(origline); |
| 3420 | return DIRECTIVE_FOUND; |
| 3421 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3422 | case PP_SUBSTR: |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3423 | { |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3424 | int64_t start, count; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3425 | size_t len; |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 3426 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3427 | casesense = true; |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3428 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3429 | tline = tline->next; |
| 3430 | skip_white_(tline); |
| 3431 | tline = expand_id(tline); |
| 3432 | if (!tline || (tline->type != TOK_ID && |
| 3433 | (tline->type != TOK_PREPROC_ID || |
| 3434 | tline->text[1] != '$'))) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3435 | nasm_nonfatal("`%%substr' expects a macro identifier as first parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3436 | free_tlist(origline); |
| 3437 | return DIRECTIVE_FOUND; |
| 3438 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3439 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3440 | last = tline; |
| 3441 | tline = expand_smacro(tline->next); |
| 3442 | last->next = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3443 | |
Cyrill Gorcunov | 35519d6 | 2010-09-06 23:49:52 +0400 | [diff] [blame] | 3444 | if (tline) /* skip expanded id */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3445 | t = tline->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3446 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3447 | t = t->next; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3448 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3449 | /* t should now point to the string */ |
Cyrill Gorcunov | 35519d6 | 2010-09-06 23:49:52 +0400 | [diff] [blame] | 3450 | if (!tok_type_(t, TOK_STRING)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3451 | nasm_nonfatal("`%%substr` requires string as second parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3452 | free_tlist(tline); |
| 3453 | free_tlist(origline); |
| 3454 | return DIRECTIVE_FOUND; |
| 3455 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3456 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3457 | tt = t->next; |
| 3458 | tptr = &tt; |
| 3459 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 3460 | evalresult = evaluate(ppscan, tptr, &tokval, NULL, true, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3461 | if (!evalresult) { |
| 3462 | free_tlist(tline); |
| 3463 | free_tlist(origline); |
| 3464 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3465 | } else if (!is_simple(evalresult)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3466 | nasm_nonfatal("non-constant value given to `%%substr`"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3467 | free_tlist(tline); |
| 3468 | free_tlist(origline); |
| 3469 | return DIRECTIVE_FOUND; |
| 3470 | } |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3471 | start = evalresult->value - 1; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3472 | |
| 3473 | while (tok_type_(tt, TOK_WHITESPACE)) |
| 3474 | tt = tt->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3475 | if (!tt) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3476 | count = 1; /* Backwards compatibility: one character */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3477 | } else { |
| 3478 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 3479 | evalresult = evaluate(ppscan, tptr, &tokval, NULL, true, NULL); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3480 | if (!evalresult) { |
| 3481 | free_tlist(tline); |
| 3482 | free_tlist(origline); |
| 3483 | return DIRECTIVE_FOUND; |
| 3484 | } else if (!is_simple(evalresult)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3485 | nasm_nonfatal("non-constant value given to `%%substr`"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3486 | free_tlist(tline); |
| 3487 | free_tlist(origline); |
| 3488 | return DIRECTIVE_FOUND; |
| 3489 | } |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3490 | count = evalresult->value; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3491 | } |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3492 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3493 | len = nasm_unquote(t->text, NULL); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3494 | |
Cyrill Gorcunov | cff031e | 2010-09-07 20:31:11 +0400 | [diff] [blame] | 3495 | /* make start and count being in range */ |
| 3496 | if (start < 0) |
| 3497 | start = 0; |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3498 | if (count < 0) |
| 3499 | count = len + count + 1 - start; |
| 3500 | if (start + count > (int64_t)len) |
Cyrill Gorcunov | cff031e | 2010-09-07 20:31:11 +0400 | [diff] [blame] | 3501 | count = len - start; |
| 3502 | if (!len || count < 0 || start >=(int64_t)len) |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3503 | start = -1, count = 0; /* empty string */ |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3504 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3505 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3506 | macro_start->next = NULL; |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3507 | macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3508 | macro_start->type = TOK_STRING; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3509 | macro_start->a.mac = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3510 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3511 | /* |
| 3512 | * We now have a macro name, an implicit parameter count of |
| 3513 | * zero, and a numeric token to use as an expansion. Create |
| 3514 | * and store an SMacro. |
| 3515 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3516 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3517 | free_tlist(tline); |
| 3518 | free_tlist(origline); |
| 3519 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3520 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3521 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3522 | case PP_ASSIGN: |
| 3523 | case PP_IASSIGN: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3524 | casesense = (i == PP_ASSIGN); |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3525 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3526 | tline = tline->next; |
| 3527 | skip_white_(tline); |
| 3528 | tline = expand_id(tline); |
| 3529 | if (!tline || (tline->type != TOK_ID && |
| 3530 | (tline->type != TOK_PREPROC_ID || |
| 3531 | tline->text[1] != '$'))) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3532 | nasm_nonfatal("`%%%sassign' expects a macro identifier", |
| 3533 | (i == PP_IASSIGN ? "i" : "")); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3534 | free_tlist(origline); |
| 3535 | return DIRECTIVE_FOUND; |
| 3536 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3537 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3538 | last = tline; |
| 3539 | tline = expand_smacro(tline->next); |
| 3540 | last->next = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3541 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3542 | t = tline; |
| 3543 | tptr = &t; |
| 3544 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 3545 | evalresult = evaluate(ppscan, tptr, &tokval, NULL, true, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3546 | free_tlist(tline); |
| 3547 | if (!evalresult) { |
| 3548 | free_tlist(origline); |
| 3549 | return DIRECTIVE_FOUND; |
| 3550 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3551 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3552 | if (tokval.t_type) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 3553 | nasm_warn(WARN_OTHER, "trailing garbage after expression ignored"); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3554 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3555 | if (!is_simple(evalresult)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3556 | nasm_nonfatal("non-constant value given to `%%%sassign'", |
| 3557 | (i == PP_IASSIGN ? "i" : "")); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3558 | free_tlist(origline); |
| 3559 | return DIRECTIVE_FOUND; |
| 3560 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3561 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3562 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3563 | macro_start->next = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3564 | make_tok_num(macro_start, reloc_value(evalresult)); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3565 | macro_start->a.mac = NULL; |
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 | /* |
| 3568 | * We now have a macro name, an implicit parameter count of |
| 3569 | * zero, and a numeric token to use as an expansion. Create |
| 3570 | * and store an SMacro. |
| 3571 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3572 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3573 | free_tlist(origline); |
| 3574 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3575 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3576 | case PP_LINE: |
| 3577 | /* |
| 3578 | * Syntax is `%line nnn[+mmm] [filename]' |
| 3579 | */ |
H. Peter Anvin (Intel) | 800c168 | 2018-12-14 12:22:11 -0800 | [diff] [blame] | 3580 | if (unlikely(pp_noline)) { |
| 3581 | free_tlist(origline); |
| 3582 | return DIRECTIVE_FOUND; |
| 3583 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3584 | tline = tline->next; |
| 3585 | skip_white_(tline); |
| 3586 | if (!tok_type_(tline, TOK_NUMBER)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3587 | nasm_nonfatal("`%%line' expects line number"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3588 | free_tlist(origline); |
| 3589 | return DIRECTIVE_FOUND; |
| 3590 | } |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 3591 | k = readnum(tline->text, &err); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3592 | m = 1; |
| 3593 | tline = tline->next; |
| 3594 | if (tok_is_(tline, "+")) { |
| 3595 | tline = tline->next; |
| 3596 | if (!tok_type_(tline, TOK_NUMBER)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3597 | nasm_nonfatal("`%%line' expects line increment"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3598 | free_tlist(origline); |
| 3599 | return DIRECTIVE_FOUND; |
| 3600 | } |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 3601 | m = readnum(tline->text, &err); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3602 | tline = tline->next; |
| 3603 | } |
| 3604 | skip_white_(tline); |
| 3605 | src_set_linnum(k); |
| 3606 | istk->lineinc = m; |
| 3607 | if (tline) { |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 3608 | char *fname = detoken(tline, false); |
| 3609 | src_set_fname(fname); |
| 3610 | nasm_free(fname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3611 | } |
| 3612 | free_tlist(origline); |
| 3613 | return DIRECTIVE_FOUND; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3614 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3615 | default: |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3616 | nasm_fatal("preprocessor directive `%s' not yet implemented", |
| 3617 | pp_directives[i]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3618 | return DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3619 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3620 | } |
| 3621 | |
| 3622 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3623 | * Ensure that a macro parameter contains a condition code and |
| 3624 | * nothing else. Return the condition code index if so, or -1 |
| 3625 | * otherwise. |
| 3626 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3627 | static int find_cc(Token * t) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3628 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3629 | Token *tt; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3630 | |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3631 | if (!t) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3632 | return -1; /* Probably a %+ without a space */ |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3633 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3634 | skip_white_(t); |
Cyrill Gorcunov | 7524cfd | 2017-10-22 19:01:16 +0300 | [diff] [blame] | 3635 | if (!t) |
| 3636 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3637 | if (t->type != TOK_ID) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3638 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3639 | tt = t->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3640 | skip_white_(tt); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3641 | if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ","))) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3642 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3643 | |
Cyrill Gorcunov | 1945639 | 2012-05-02 00:18:56 +0400 | [diff] [blame] | 3644 | return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3645 | } |
| 3646 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3647 | /* |
| 3648 | * This routines walks over tokens strem and hadnles tokens |
| 3649 | * pasting, if @handle_explicit passed then explicit pasting |
| 3650 | * term is handled, otherwise -- implicit pastings only. |
| 3651 | */ |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 3652 | static bool paste_tokens(Token **head, const struct tokseq_match *m, |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3653 | size_t mnum, bool handle_explicit) |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3654 | { |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3655 | Token *tok, *next, **prev_next, **prev_nonspace; |
| 3656 | bool pasted = false; |
| 3657 | char *buf, *p; |
| 3658 | size_t len, i; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3659 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3660 | /* |
| 3661 | * The last token before pasting. We need it |
| 3662 | * to be able to connect new handled tokens. |
| 3663 | * In other words if there were a tokens stream |
| 3664 | * |
| 3665 | * A -> B -> C -> D |
| 3666 | * |
| 3667 | * and we've joined tokens B and C, the resulting |
| 3668 | * stream should be |
| 3669 | * |
| 3670 | * A -> BC -> D |
| 3671 | */ |
| 3672 | tok = *head; |
| 3673 | prev_next = NULL; |
| 3674 | |
| 3675 | if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE)) |
| 3676 | prev_nonspace = head; |
| 3677 | else |
| 3678 | prev_nonspace = NULL; |
| 3679 | |
| 3680 | while (tok && (next = tok->next)) { |
| 3681 | |
| 3682 | switch (tok->type) { |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3683 | case TOK_WHITESPACE: |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3684 | /* Zap redundant whitespaces */ |
| 3685 | while (tok_type_(next, TOK_WHITESPACE)) |
| 3686 | next = delete_Token(next); |
| 3687 | tok->next = next; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3688 | break; |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3689 | |
| 3690 | case TOK_PASTE: |
| 3691 | /* Explicit pasting */ |
| 3692 | if (!handle_explicit) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3693 | break; |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3694 | next = delete_Token(tok); |
| 3695 | |
| 3696 | while (tok_type_(next, TOK_WHITESPACE)) |
| 3697 | next = delete_Token(next); |
| 3698 | |
| 3699 | if (!pasted) |
| 3700 | pasted = true; |
| 3701 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3702 | /* Left pasting token is start of line */ |
| 3703 | if (!prev_nonspace) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3704 | nasm_fatal("No lvalue found on pasting"); |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3705 | |
Cyrill Gorcunov | 8b5c9fb | 2013-02-04 01:24:54 +0400 | [diff] [blame] | 3706 | /* |
| 3707 | * No ending token, this might happen in two |
| 3708 | * cases |
| 3709 | * |
| 3710 | * 1) There indeed no right token at all |
| 3711 | * 2) There is a bare "%define ID" statement, |
| 3712 | * and @ID does expand to whitespace. |
| 3713 | * |
| 3714 | * So technically we need to do a grammar analysis |
| 3715 | * in another stage of parsing, but for now lets don't |
| 3716 | * change the behaviour people used to. Simply allow |
| 3717 | * whitespace after paste token. |
| 3718 | */ |
| 3719 | if (!next) { |
| 3720 | /* |
| 3721 | * Zap ending space tokens and that's all. |
| 3722 | */ |
| 3723 | tok = (*prev_nonspace)->next; |
| 3724 | while (tok_type_(tok, TOK_WHITESPACE)) |
| 3725 | tok = delete_Token(tok); |
| 3726 | tok = *prev_nonspace; |
| 3727 | tok->next = NULL; |
| 3728 | break; |
| 3729 | } |
| 3730 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3731 | tok = *prev_nonspace; |
| 3732 | while (tok_type_(tok, TOK_WHITESPACE)) |
| 3733 | tok = delete_Token(tok); |
| 3734 | len = strlen(tok->text); |
| 3735 | len += strlen(next->text); |
| 3736 | |
| 3737 | p = buf = nasm_malloc(len + 1); |
| 3738 | strcpy(p, tok->text); |
| 3739 | p = strchr(p, '\0'); |
| 3740 | strcpy(p, next->text); |
| 3741 | |
| 3742 | delete_Token(tok); |
| 3743 | |
| 3744 | tok = tokenize(buf); |
| 3745 | nasm_free(buf); |
| 3746 | |
| 3747 | *prev_nonspace = tok; |
| 3748 | while (tok && tok->next) |
| 3749 | tok = tok->next; |
| 3750 | |
| 3751 | tok->next = delete_Token(next); |
| 3752 | |
| 3753 | /* Restart from pasted tokens head */ |
| 3754 | tok = *prev_nonspace; |
| 3755 | break; |
| 3756 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3757 | default: |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3758 | /* implicit pasting */ |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 3759 | for (i = 0; i < mnum; i++) { |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3760 | if (!(PP_CONCAT_MATCH(tok, m[i].mask_head))) |
| 3761 | continue; |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 3762 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3763 | len = 0; |
| 3764 | while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) { |
| 3765 | len += strlen(next->text); |
| 3766 | next = next->next; |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 3767 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3768 | |
Cyrill Gorcunov | 6f8109e | 2017-10-22 21:26:36 +0300 | [diff] [blame] | 3769 | /* No match or no text to process */ |
| 3770 | if (tok == next || len == 0) |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3771 | break; |
| 3772 | |
| 3773 | len += strlen(tok->text); |
| 3774 | p = buf = nasm_malloc(len + 1); |
| 3775 | |
Adam Majer | 1a06943 | 2017-07-25 11:12:35 +0200 | [diff] [blame] | 3776 | strcpy(p, tok->text); |
| 3777 | p = strchr(p, '\0'); |
| 3778 | tok = delete_Token(tok); |
| 3779 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3780 | while (tok != next) { |
Adam Majer | 1a06943 | 2017-07-25 11:12:35 +0200 | [diff] [blame] | 3781 | if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) { |
| 3782 | strcpy(p, tok->text); |
| 3783 | p = strchr(p, '\0'); |
| 3784 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3785 | tok = delete_Token(tok); |
| 3786 | } |
| 3787 | |
| 3788 | tok = tokenize(buf); |
| 3789 | nasm_free(buf); |
| 3790 | |
| 3791 | if (prev_next) |
| 3792 | *prev_next = tok; |
| 3793 | else |
| 3794 | *head = tok; |
| 3795 | |
| 3796 | /* |
| 3797 | * Connect pasted into original stream, |
| 3798 | * ie A -> new-tokens -> B |
| 3799 | */ |
| 3800 | while (tok && tok->next) |
| 3801 | tok = tok->next; |
| 3802 | tok->next = next; |
| 3803 | |
| 3804 | if (!pasted) |
| 3805 | pasted = true; |
| 3806 | |
| 3807 | /* Restart from pasted tokens head */ |
| 3808 | tok = prev_next ? *prev_next : *head; |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 3809 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3810 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3811 | break; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3812 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3813 | |
| 3814 | prev_next = &tok->next; |
| 3815 | |
| 3816 | if (tok->next && |
| 3817 | !tok_type_(tok->next, TOK_WHITESPACE) && |
| 3818 | !tok_type_(tok->next, TOK_PASTE)) |
| 3819 | prev_nonspace = prev_next; |
| 3820 | |
| 3821 | tok = tok->next; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3822 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3823 | |
| 3824 | return pasted; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3825 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3826 | |
| 3827 | /* |
| 3828 | * expands to a list of tokens from %{x:y} |
| 3829 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3830 | static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3831 | { |
| 3832 | Token *t = tline, **tt, *tm, *head; |
| 3833 | char *pos; |
| 3834 | int fst, lst, j, i; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3835 | |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3836 | pos = strchr(tline->text, ':'); |
| 3837 | nasm_assert(pos); |
| 3838 | |
| 3839 | lst = atoi(pos + 1); |
| 3840 | fst = atoi(tline->text + 1); |
| 3841 | |
| 3842 | /* |
| 3843 | * only macros params are accounted so |
| 3844 | * if someone passes %0 -- we reject such |
| 3845 | * value(s) |
| 3846 | */ |
| 3847 | if (lst == 0 || fst == 0) |
| 3848 | goto err; |
| 3849 | |
| 3850 | /* the values should be sane */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3851 | if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) || |
| 3852 | (lst > (int)mac->nparam || lst < (-(int)mac->nparam))) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3853 | goto err; |
| 3854 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3855 | fst = fst < 0 ? fst + (int)mac->nparam + 1: fst; |
| 3856 | lst = lst < 0 ? lst + (int)mac->nparam + 1: lst; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3857 | |
| 3858 | /* counted from zero */ |
| 3859 | fst--, lst--; |
| 3860 | |
| 3861 | /* |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3862 | * It will be at least one token. Note we |
| 3863 | * need to scan params until separator, otherwise |
| 3864 | * only first token will be passed. |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3865 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3866 | tm = mac->params[(fst + mac->rotate) % mac->nparam]; |
Cyrill Gorcunov | 67f2ca2 | 2018-10-13 19:41:01 +0300 | [diff] [blame] | 3867 | if (!tm) |
| 3868 | goto err; |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3869 | head = new_Token(NULL, tm->type, tm->text, 0); |
| 3870 | tt = &head->next, tm = tm->next; |
| 3871 | while (tok_isnt_(tm, ",")) { |
| 3872 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 3873 | *tt = t, tt = &t->next, tm = tm->next; |
| 3874 | } |
| 3875 | |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3876 | if (fst < lst) { |
| 3877 | for (i = fst + 1; i <= lst; i++) { |
| 3878 | t = new_Token(NULL, TOK_OTHER, ",", 0); |
| 3879 | *tt = t, tt = &t->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3880 | j = (i + mac->rotate) % mac->nparam; |
| 3881 | tm = mac->params[j]; |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3882 | while (tok_isnt_(tm, ",")) { |
| 3883 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 3884 | *tt = t, tt = &t->next, tm = tm->next; |
| 3885 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3886 | } |
| 3887 | } else { |
| 3888 | for (i = fst - 1; i >= lst; i--) { |
| 3889 | t = new_Token(NULL, TOK_OTHER, ",", 0); |
| 3890 | *tt = t, tt = &t->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3891 | j = (i + mac->rotate) % mac->nparam; |
| 3892 | tm = mac->params[j]; |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3893 | while (tok_isnt_(tm, ",")) { |
| 3894 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 3895 | *tt = t, tt = &t->next, tm = tm->next; |
| 3896 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3897 | } |
| 3898 | } |
| 3899 | |
| 3900 | *last = tt; |
| 3901 | return head; |
| 3902 | |
| 3903 | err: |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3904 | nasm_nonfatal("`%%{%s}': macro parameters out of range", |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3905 | &tline->text[1]); |
| 3906 | return tline; |
| 3907 | } |
| 3908 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3909 | /* |
| 3910 | * Expand MMacro-local things: parameter references (%0, %n, %+n, |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 3911 | * %-n) and MMacro-local identifiers (%%foo) as well as |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3912 | * macro indirection (%[...]) and range (%{..:..}). |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3913 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3914 | static Token *expand_mmac_params(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3915 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3916 | Token *t, *tt, **tail, *thead; |
H. Peter Anvin | 6125b62 | 2009-04-08 14:02:25 -0700 | [diff] [blame] | 3917 | bool changed = false; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3918 | char *pos; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3919 | |
| 3920 | tail = &thead; |
| 3921 | thead = NULL; |
| 3922 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3923 | while (tline) { |
Cyrill Gorcunov | 661f723 | 2018-10-28 20:39:34 +0300 | [diff] [blame] | 3924 | if (tline->type == TOK_PREPROC_ID && tline->text && tline->text[0] && |
Cyrill Gorcunov | ca61119 | 2010-06-04 09:22:12 +0400 | [diff] [blame] | 3925 | (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) || |
| 3926 | (tline->text[1] >= '0' && tline->text[1] <= '9') || |
| 3927 | tline->text[1] == '%')) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 3928 | char *text = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3929 | int type = 0, cc; /* type = 0 to placate optimisers */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 3930 | char tmpbuf[30]; |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3931 | unsigned int n; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3932 | int i; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3933 | MMacro *mac; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3934 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3935 | t = tline; |
| 3936 | tline = tline->next; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3937 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3938 | mac = istk->mstk; |
| 3939 | while (mac && !mac->name) /* avoid mistaking %reps for macros */ |
| 3940 | mac = mac->next_active; |
| 3941 | if (!mac) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3942 | nasm_nonfatal("`%s': not in a macro call", t->text); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3943 | } else { |
| 3944 | pos = strchr(t->text, ':'); |
| 3945 | if (!pos) { |
| 3946 | switch (t->text[1]) { |
| 3947 | /* |
| 3948 | * We have to make a substitution of one of the |
| 3949 | * forms %1, %-1, %+1, %%foo, %0. |
| 3950 | */ |
| 3951 | case '0': |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3952 | type = TOK_NUMBER; |
| 3953 | snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam); |
| 3954 | text = nasm_strdup(tmpbuf); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3955 | break; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3956 | case '%': |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3957 | type = TOK_ID; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3958 | snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".", |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3959 | mac->unique); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3960 | text = nasm_strcat(tmpbuf, t->text + 2); |
| 3961 | break; |
| 3962 | case '-': |
| 3963 | n = atoi(t->text + 2) - 1; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3964 | if (n >= mac->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3965 | tt = NULL; |
| 3966 | else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3967 | if (mac->nparam > 1) |
| 3968 | n = (n + mac->rotate) % mac->nparam; |
| 3969 | tt = mac->params[n]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3970 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3971 | cc = find_cc(tt); |
| 3972 | if (cc == -1) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3973 | nasm_nonfatal("macro parameter %d is not a condition code", |
| 3974 | n + 1); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3975 | text = NULL; |
| 3976 | } else { |
| 3977 | type = TOK_ID; |
| 3978 | if (inverse_ccs[cc] == -1) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3979 | nasm_nonfatal("condition code `%s' is not invertible", |
| 3980 | conditions[cc]); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3981 | text = NULL; |
| 3982 | } else |
| 3983 | text = nasm_strdup(conditions[inverse_ccs[cc]]); |
| 3984 | } |
| 3985 | break; |
| 3986 | case '+': |
| 3987 | n = atoi(t->text + 2) - 1; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3988 | if (n >= mac->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3989 | tt = NULL; |
| 3990 | else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3991 | if (mac->nparam > 1) |
| 3992 | n = (n + mac->rotate) % mac->nparam; |
| 3993 | tt = mac->params[n]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3994 | } |
| 3995 | cc = find_cc(tt); |
| 3996 | if (cc == -1) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3997 | nasm_nonfatal("macro parameter %d is not a condition code", |
| 3998 | n + 1); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3999 | text = NULL; |
| 4000 | } else { |
| 4001 | type = TOK_ID; |
| 4002 | text = nasm_strdup(conditions[cc]); |
| 4003 | } |
| 4004 | break; |
| 4005 | default: |
| 4006 | n = atoi(t->text + 1) - 1; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4007 | if (n >= mac->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4008 | tt = NULL; |
| 4009 | else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4010 | if (mac->nparam > 1) |
| 4011 | n = (n + mac->rotate) % mac->nparam; |
| 4012 | tt = mac->params[n]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4013 | } |
| 4014 | if (tt) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4015 | for (i = 0; i < mac->paramlen[n]; i++) { |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4016 | *tail = new_Token(NULL, tt->type, tt->text, 0); |
| 4017 | tail = &(*tail)->next; |
| 4018 | tt = tt->next; |
| 4019 | } |
| 4020 | } |
| 4021 | text = NULL; /* we've done it here */ |
| 4022 | break; |
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 | } else { |
| 4025 | /* |
| 4026 | * seems we have a parameters range here |
| 4027 | */ |
| 4028 | Token *head, **last; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4029 | head = expand_mmac_params_range(mac, t, &last); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4030 | if (head != t) { |
| 4031 | *tail = head; |
| 4032 | *last = tline; |
| 4033 | tline = head; |
| 4034 | text = NULL; |
| 4035 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4036 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4037 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4038 | if (!text) { |
| 4039 | delete_Token(t); |
| 4040 | } else { |
| 4041 | *tail = t; |
| 4042 | tail = &t->next; |
| 4043 | t->type = type; |
| 4044 | nasm_free(t->text); |
| 4045 | t->text = text; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4046 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4047 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4048 | changed = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4049 | continue; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4050 | } else if (tline->type == TOK_INDIRECT) { |
| 4051 | t = tline; |
| 4052 | tline = tline->next; |
| 4053 | tt = tokenize(t->text); |
| 4054 | tt = expand_mmac_params(tt); |
| 4055 | tt = expand_smacro(tt); |
| 4056 | *tail = tt; |
| 4057 | while (tt) { |
| 4058 | tt->a.mac = NULL; /* Necessary? */ |
| 4059 | tail = &tt->next; |
| 4060 | tt = tt->next; |
| 4061 | } |
| 4062 | delete_Token(t); |
| 4063 | changed = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4064 | } else { |
| 4065 | t = *tail = tline; |
| 4066 | tline = tline->next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4067 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4068 | tail = &t->next; |
| 4069 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4070 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4071 | *tail = NULL; |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 4072 | |
Cyrill Gorcunov | c6a742c | 2011-06-27 01:23:09 +0400 | [diff] [blame] | 4073 | if (changed) { |
| 4074 | const struct tokseq_match t[] = { |
| 4075 | { |
| 4076 | PP_CONCAT_MASK(TOK_ID) | |
| 4077 | PP_CONCAT_MASK(TOK_FLOAT), /* head */ |
| 4078 | PP_CONCAT_MASK(TOK_ID) | |
| 4079 | PP_CONCAT_MASK(TOK_NUMBER) | |
| 4080 | PP_CONCAT_MASK(TOK_FLOAT) | |
| 4081 | PP_CONCAT_MASK(TOK_OTHER) /* tail */ |
| 4082 | }, |
| 4083 | { |
| 4084 | PP_CONCAT_MASK(TOK_NUMBER), /* head */ |
| 4085 | PP_CONCAT_MASK(TOK_NUMBER) /* tail */ |
| 4086 | } |
| 4087 | }; |
| 4088 | paste_tokens(&thead, t, ARRAY_SIZE(t), false); |
| 4089 | } |
H. Peter Anvin | 6125b62 | 2009-04-08 14:02:25 -0700 | [diff] [blame] | 4090 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4091 | return thead; |
| 4092 | } |
| 4093 | |
| 4094 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4095 | * Expand all single-line macro calls made in the given line. |
| 4096 | * Return the expanded version of the line. The original is deemed |
| 4097 | * to be destroyed in the process. (In reality we'll just move |
| 4098 | * Tokens from input to output a lot of the time, rather than |
| 4099 | * actually bothering to destroy and replicate.) |
| 4100 | */ |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4101 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4102 | static Token *expand_smacro(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4103 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4104 | Token *t, *tt, *mstart, **tail, *thead; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4105 | SMacro *head = NULL, *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4106 | Token **params; |
| 4107 | int *paramsize; |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 4108 | unsigned int nparam, sparam; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 4109 | int brackets; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4110 | Token *org_tline = tline; |
| 4111 | Context *ctx; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 4112 | const char *mname; |
H. Peter Anvin | a3d96d0 | 2018-06-15 17:51:39 -0700 | [diff] [blame] | 4113 | int64_t deadman = nasm_limit[LIMIT_MACROS]; |
H. Peter Anvin | 8287daf | 2009-07-07 16:00:58 -0700 | [diff] [blame] | 4114 | bool expanded; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4115 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4116 | /* |
| 4117 | * Trick: we should avoid changing the start token pointer since it can |
| 4118 | * be contained in "next" field of other token. Because of this |
| 4119 | * we allocate a copy of first token and work with it; at the end of |
| 4120 | * routine we copy it back |
| 4121 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4122 | if (org_tline) { |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4123 | tline = new_Token(org_tline->next, org_tline->type, |
| 4124 | org_tline->text, 0); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4125 | tline->a.mac = org_tline->a.mac; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4126 | nasm_free(org_tline->text); |
| 4127 | org_tline->text = NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4128 | } |
| 4129 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4130 | expanded = true; /* Always expand %+ at least once */ |
H. Peter Anvin | 8287daf | 2009-07-07 16:00:58 -0700 | [diff] [blame] | 4131 | |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4132 | again: |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4133 | thead = NULL; |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4134 | tail = &thead; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4135 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4136 | while (tline) { /* main token loop */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4137 | if (!--deadman) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4138 | nasm_nonfatal("interminable macro recursion"); |
Cyrill Gorcunov | bd38c8f | 2009-11-21 11:11:23 +0300 | [diff] [blame] | 4139 | goto err; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4140 | } |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4141 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4142 | if ((mname = tline->text)) { |
| 4143 | /* if this token is a local macro, look in local context */ |
Cyrill Gorcunov | c56d9ad | 2010-02-11 15:12:19 +0300 | [diff] [blame] | 4144 | if (tline->type == TOK_ID) { |
| 4145 | head = (SMacro *)hash_findix(&smacros, mname); |
| 4146 | } else if (tline->type == TOK_PREPROC_ID) { |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 4147 | ctx = get_ctx(mname, &mname); |
Cyrill Gorcunov | c56d9ad | 2010-02-11 15:12:19 +0300 | [diff] [blame] | 4148 | head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL; |
| 4149 | } else |
| 4150 | head = NULL; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 4151 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4152 | /* |
| 4153 | * We've hit an identifier. As in is_mmacro below, we first |
| 4154 | * check whether the identifier is a single-line macro at |
| 4155 | * all, then think about checking for parameters if |
| 4156 | * necessary. |
| 4157 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4158 | list_for_each(m, head) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4159 | if (!mstrcmp(m->name, mname, m->casesense)) |
| 4160 | break; |
| 4161 | if (m) { |
| 4162 | mstart = tline; |
| 4163 | params = NULL; |
| 4164 | paramsize = NULL; |
| 4165 | if (m->nparam == 0) { |
| 4166 | /* |
| 4167 | * Simple case: the macro is parameterless. Discard the |
| 4168 | * one token that the macro call took, and push the |
| 4169 | * expansion back on the to-do stack. |
| 4170 | */ |
| 4171 | if (!m->expansion) { |
| 4172 | if (!strcmp("__FILE__", m->name)) { |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 4173 | const char *file = src_get_fname(); |
| 4174 | /* nasm_free(tline->text); here? */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4175 | tline->text = nasm_quote(file, strlen(file)); |
| 4176 | tline->type = TOK_STRING; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4177 | continue; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4178 | } |
| 4179 | if (!strcmp("__LINE__", m->name)) { |
| 4180 | nasm_free(tline->text); |
| 4181 | make_tok_num(tline, src_get_linnum()); |
| 4182 | continue; |
| 4183 | } |
| 4184 | if (!strcmp("__BITS__", m->name)) { |
| 4185 | nasm_free(tline->text); |
| 4186 | make_tok_num(tline, globalbits); |
| 4187 | continue; |
| 4188 | } |
| 4189 | tline = delete_Token(tline); |
| 4190 | continue; |
| 4191 | } |
| 4192 | } else { |
| 4193 | /* |
| 4194 | * Complicated case: at least one macro with this name |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4195 | * exists and takes parameters. We must find the |
| 4196 | * parameters in the call, count them, find the SMacro |
| 4197 | * that corresponds to that form of the macro call, and |
| 4198 | * substitute for the parameters when we expand. What a |
| 4199 | * pain. |
| 4200 | */ |
| 4201 | /*tline = tline->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4202 | skip_white_(tline); */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4203 | do { |
| 4204 | t = tline->next; |
| 4205 | while (tok_type_(t, TOK_SMAC_END)) { |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4206 | t->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4207 | t->text = NULL; |
| 4208 | t = tline->next = delete_Token(t); |
| 4209 | } |
| 4210 | tline = t; |
| 4211 | } while (tok_type_(tline, TOK_WHITESPACE)); |
| 4212 | if (!tok_is_(tline, "(")) { |
| 4213 | /* |
| 4214 | * This macro wasn't called with parameters: ignore |
| 4215 | * the call. (Behaviour borrowed from gnu cpp.) |
| 4216 | */ |
| 4217 | tline = mstart; |
| 4218 | m = NULL; |
| 4219 | } else { |
| 4220 | int paren = 0; |
| 4221 | int white = 0; |
| 4222 | brackets = 0; |
| 4223 | nparam = 0; |
| 4224 | sparam = PARAM_DELTA; |
| 4225 | params = nasm_malloc(sparam * sizeof(Token *)); |
| 4226 | params[0] = tline->next; |
| 4227 | paramsize = nasm_malloc(sparam * sizeof(int)); |
| 4228 | paramsize[0] = 0; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4229 | while (true) { /* parameter loop */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4230 | /* |
| 4231 | * For some unusual expansions |
| 4232 | * which concatenates function call |
| 4233 | */ |
| 4234 | t = tline->next; |
| 4235 | while (tok_type_(t, TOK_SMAC_END)) { |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4236 | t->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4237 | t->text = NULL; |
| 4238 | t = tline->next = delete_Token(t); |
| 4239 | } |
| 4240 | tline = t; |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 4241 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4242 | if (!tline) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4243 | nasm_nonfatal("macro call expects terminating `)'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4244 | break; |
| 4245 | } |
| 4246 | if (tline->type == TOK_WHITESPACE |
| 4247 | && brackets <= 0) { |
| 4248 | if (paramsize[nparam]) |
| 4249 | white++; |
| 4250 | else |
| 4251 | params[nparam] = tline->next; |
| 4252 | continue; /* parameter loop */ |
| 4253 | } |
| 4254 | if (tline->type == TOK_OTHER |
| 4255 | && tline->text[1] == 0) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4256 | char ch = tline->text[0]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4257 | if (ch == ',' && !paren && brackets <= 0) { |
| 4258 | if (++nparam >= sparam) { |
| 4259 | sparam += PARAM_DELTA; |
| 4260 | params = nasm_realloc(params, |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4261 | sparam * sizeof(Token *)); |
| 4262 | paramsize = nasm_realloc(paramsize, |
| 4263 | sparam * sizeof(int)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4264 | } |
| 4265 | params[nparam] = tline->next; |
| 4266 | paramsize[nparam] = 0; |
| 4267 | white = 0; |
| 4268 | continue; /* parameter loop */ |
| 4269 | } |
| 4270 | if (ch == '{' && |
| 4271 | (brackets > 0 || (brackets == 0 && |
| 4272 | !paramsize[nparam]))) |
| 4273 | { |
| 4274 | if (!(brackets++)) { |
| 4275 | params[nparam] = tline->next; |
| 4276 | continue; /* parameter loop */ |
| 4277 | } |
| 4278 | } |
| 4279 | if (ch == '}' && brackets > 0) |
| 4280 | if (--brackets == 0) { |
| 4281 | brackets = -1; |
| 4282 | continue; /* parameter loop */ |
| 4283 | } |
| 4284 | if (ch == '(' && !brackets) |
| 4285 | paren++; |
| 4286 | if (ch == ')' && brackets <= 0) |
| 4287 | if (--paren < 0) |
| 4288 | break; |
| 4289 | } |
| 4290 | if (brackets < 0) { |
| 4291 | brackets = 0; |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4292 | nasm_nonfatal("braces do not " |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4293 | "enclose all of macro parameter"); |
| 4294 | } |
| 4295 | paramsize[nparam] += white + 1; |
| 4296 | white = 0; |
| 4297 | } /* parameter loop */ |
| 4298 | nparam++; |
| 4299 | while (m && (m->nparam != nparam || |
| 4300 | mstrcmp(m->name, mname, |
| 4301 | m->casesense))) |
| 4302 | m = m->next; |
H. Peter Anvin (Intel) | 723ab48 | 2018-12-13 21:53:31 -0800 | [diff] [blame] | 4303 | if (!m) { |
| 4304 | /*! |
| 4305 | *!macro-params [on] macro calls with wrong parameter count |
| 4306 | *! covers warnings about \i{multi-line macros} being invoked |
| 4307 | *! with the wrong number of parameters. See \k{mlmacover} for an |
| 4308 | *! example of why you might want to disable this warning. |
| 4309 | */ |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 4310 | nasm_warn(WARN_MACRO_PARAMS, |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4311 | "macro `%s' exists, " |
| 4312 | "but not taking %d parameters", |
| 4313 | mstart->text, nparam); |
H. Peter Anvin (Intel) | 723ab48 | 2018-12-13 21:53:31 -0800 | [diff] [blame] | 4314 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4315 | } |
| 4316 | } |
| 4317 | if (m && m->in_progress) |
| 4318 | m = NULL; |
| 4319 | if (!m) { /* in progess or didn't find '(' or wrong nparam */ |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 4320 | /* |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4321 | * Design question: should we handle !tline, which |
| 4322 | * indicates missing ')' here, or expand those |
| 4323 | * macros anyway, which requires the (t) test a few |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 4324 | * lines down? |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4325 | */ |
| 4326 | nasm_free(params); |
| 4327 | nasm_free(paramsize); |
| 4328 | tline = mstart; |
| 4329 | } else { |
| 4330 | /* |
| 4331 | * Expand the macro: we are placed on the last token of the |
| 4332 | * call, so that we can easily split the call from the |
| 4333 | * following tokens. We also start by pushing an SMAC_END |
| 4334 | * token for the cycle removal. |
| 4335 | */ |
| 4336 | t = tline; |
| 4337 | if (t) { |
| 4338 | tline = t->next; |
| 4339 | t->next = NULL; |
| 4340 | } |
| 4341 | tt = new_Token(tline, TOK_SMAC_END, NULL, 0); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4342 | tt->a.mac = m; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4343 | m->in_progress = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4344 | tline = tt; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 4345 | list_for_each(t, m->expansion) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4346 | if (t->type >= TOK_SMAC_PARAM) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4347 | Token *pcopy = tline, **ptail = &pcopy; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4348 | Token *ttt, *pt; |
| 4349 | int i; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4350 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4351 | ttt = params[t->type - TOK_SMAC_PARAM]; |
| 4352 | i = paramsize[t->type - TOK_SMAC_PARAM]; |
| 4353 | while (--i >= 0) { |
| 4354 | pt = *ptail = new_Token(tline, ttt->type, |
| 4355 | ttt->text, 0); |
| 4356 | ptail = &pt->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4357 | ttt = ttt->next; |
Cyrill Gorcunov | 59ce1c6 | 2017-10-22 18:42:07 +0300 | [diff] [blame] | 4358 | if (!ttt && i > 0) { |
| 4359 | /* |
| 4360 | * FIXME: Need to handle more gracefully, |
| 4361 | * exiting early on agruments analysis. |
| 4362 | */ |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4363 | nasm_fatal("macro `%s' expects %d args", |
Cyrill Gorcunov | 59ce1c6 | 2017-10-22 18:42:07 +0300 | [diff] [blame] | 4364 | mstart->text, |
| 4365 | (int)paramsize[t->type - TOK_SMAC_PARAM]); |
| 4366 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4367 | } |
| 4368 | tline = pcopy; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4369 | } else if (t->type == TOK_PREPROC_Q) { |
| 4370 | tt = new_Token(tline, TOK_ID, mname, 0); |
| 4371 | tline = tt; |
| 4372 | } else if (t->type == TOK_PREPROC_QQ) { |
| 4373 | tt = new_Token(tline, TOK_ID, m->name, 0); |
| 4374 | tline = tt; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4375 | } else { |
| 4376 | tt = new_Token(tline, t->type, t->text, 0); |
| 4377 | tline = tt; |
| 4378 | } |
| 4379 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4380 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4381 | /* |
| 4382 | * Having done that, get rid of the macro call, and clean |
| 4383 | * up the parameters. |
| 4384 | */ |
| 4385 | nasm_free(params); |
| 4386 | nasm_free(paramsize); |
| 4387 | free_tlist(mstart); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4388 | expanded = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4389 | continue; /* main token loop */ |
| 4390 | } |
| 4391 | } |
| 4392 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4393 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4394 | if (tline->type == TOK_SMAC_END) { |
Cyrill Gorcunov | 980dd65 | 2018-10-14 19:25:32 +0300 | [diff] [blame] | 4395 | /* On error path it might already be dropped */ |
| 4396 | if (tline->a.mac) |
| 4397 | tline->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4398 | tline = delete_Token(tline); |
| 4399 | } else { |
| 4400 | t = *tail = tline; |
| 4401 | tline = tline->next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4402 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4403 | t->next = NULL; |
| 4404 | tail = &t->next; |
| 4405 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4406 | } |
| 4407 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4408 | /* |
| 4409 | * 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] | 4410 | * after expansion (they can't be produced by tokenize()). The successive |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4411 | * TOK_IDs should be concatenated. |
| 4412 | * Also we look for %+ tokens and concatenate the tokens before and after |
| 4413 | * them (without white spaces in between). |
| 4414 | */ |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4415 | if (expanded) { |
Cyrill Gorcunov | c6a742c | 2011-06-27 01:23:09 +0400 | [diff] [blame] | 4416 | const struct tokseq_match t[] = { |
| 4417 | { |
| 4418 | PP_CONCAT_MASK(TOK_ID) | |
| 4419 | PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */ |
| 4420 | PP_CONCAT_MASK(TOK_ID) | |
| 4421 | PP_CONCAT_MASK(TOK_PREPROC_ID) | |
| 4422 | PP_CONCAT_MASK(TOK_NUMBER) /* tail */ |
| 4423 | } |
| 4424 | }; |
| 4425 | if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) { |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4426 | /* |
| 4427 | * If we concatenated something, *and* we had previously expanded |
| 4428 | * an actual macro, scan the lines again for macros... |
| 4429 | */ |
| 4430 | tline = thead; |
| 4431 | expanded = false; |
| 4432 | goto again; |
| 4433 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4434 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4435 | |
Cyrill Gorcunov | bd38c8f | 2009-11-21 11:11:23 +0300 | [diff] [blame] | 4436 | err: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4437 | if (org_tline) { |
| 4438 | if (thead) { |
| 4439 | *org_tline = *thead; |
| 4440 | /* since we just gave text to org_line, don't free it */ |
| 4441 | thead->text = NULL; |
| 4442 | delete_Token(thead); |
| 4443 | } else { |
| 4444 | /* the expression expanded to empty line; |
| 4445 | we can't return NULL for some reasons |
| 4446 | we just set the line to a single WHITESPACE token. */ |
| 4447 | memset(org_tline, 0, sizeof(*org_tline)); |
| 4448 | org_tline->text = NULL; |
| 4449 | org_tline->type = TOK_WHITESPACE; |
| 4450 | } |
| 4451 | thead = org_tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4452 | } |
| 4453 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4454 | return thead; |
| 4455 | } |
| 4456 | |
| 4457 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4458 | * Similar to expand_smacro but used exclusively with macro identifiers |
| 4459 | * right before they are fetched in. The reason is that there can be |
| 4460 | * identifiers consisting of several subparts. We consider that if there |
| 4461 | * are more than one element forming the name, user wants a expansion, |
| 4462 | * otherwise it will be left as-is. Example: |
| 4463 | * |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4464 | * %define %$abc cde |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4465 | * |
| 4466 | * the identifier %$abc will be left as-is so that the handler for %define |
| 4467 | * will suck it and define the corresponding value. Other case: |
| 4468 | * |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4469 | * %define _%$abc cde |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4470 | * |
| 4471 | * In this case user wants name to be expanded *before* %define starts |
| 4472 | * working, so we'll expand %$abc into something (if it has a value; |
| 4473 | * otherwise it will be left as-is) then concatenate all successive |
| 4474 | * PP_IDs into one. |
| 4475 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4476 | static Token *expand_id(Token * tline) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4477 | { |
| 4478 | Token *cur, *oldnext = NULL; |
| 4479 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4480 | if (!tline || !tline->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4481 | return tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4482 | |
| 4483 | cur = tline; |
| 4484 | while (cur->next && |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4485 | (cur->next->type == TOK_ID || |
| 4486 | cur->next->type == TOK_PREPROC_ID |
| 4487 | || cur->next->type == TOK_NUMBER)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4488 | cur = cur->next; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4489 | |
| 4490 | /* If identifier consists of just one token, don't expand */ |
| 4491 | if (cur == tline) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4492 | return tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4493 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4494 | if (cur) { |
| 4495 | oldnext = cur->next; /* Detach the tail past identifier */ |
| 4496 | cur->next = NULL; /* so that expand_smacro stops here */ |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4497 | } |
| 4498 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4499 | tline = expand_smacro(tline); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4500 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4501 | if (cur) { |
| 4502 | /* expand_smacro possibly changhed tline; re-scan for EOL */ |
| 4503 | cur = tline; |
| 4504 | while (cur && cur->next) |
| 4505 | cur = cur->next; |
| 4506 | if (cur) |
| 4507 | cur->next = oldnext; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4508 | } |
| 4509 | |
| 4510 | return tline; |
| 4511 | } |
| 4512 | |
| 4513 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4514 | * Determine whether the given line constitutes a multi-line macro |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4515 | * 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] | 4516 | * to check for an initial label - that's taken care of in |
| 4517 | * expand_mmacro - but must check numbers of parameters. Guaranteed |
| 4518 | * to be called with tline->type == TOK_ID, so the putative macro |
| 4519 | * name is easy to find. |
| 4520 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4521 | static MMacro *is_mmacro(Token * tline, Token *** params_array) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4522 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4523 | MMacro *head, *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4524 | Token **params; |
| 4525 | int nparam; |
| 4526 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4527 | head = (MMacro *) hash_findix(&mmacros, tline->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4528 | |
| 4529 | /* |
| 4530 | * Efficiency: first we see if any macro exists with the given |
| 4531 | * name. If not, we can return NULL immediately. _Then_ we |
| 4532 | * count the parameters, and then we look further along the |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4533 | * list if necessary to find the proper MMacro. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4534 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4535 | list_for_each(m, head) |
| 4536 | if (!mstrcmp(m->name, tline->text, m->casesense)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4537 | break; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4538 | if (!m) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4539 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4540 | |
| 4541 | /* |
| 4542 | * OK, we have a potential macro. Count and demarcate the |
| 4543 | * parameters. |
| 4544 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4545 | count_mmac_params(tline->next, &nparam, ¶ms); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4546 | |
| 4547 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4548 | * 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] | 4549 | * structure that handles this number. |
| 4550 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4551 | while (m) { |
| 4552 | if (m->nparam_min <= nparam |
| 4553 | && (m->plus || nparam <= m->nparam_max)) { |
| 4554 | /* |
| 4555 | * This one is right. Just check if cycle removal |
| 4556 | * prohibits us using it before we actually celebrate... |
| 4557 | */ |
| 4558 | if (m->in_progress > m->max_depth) { |
| 4559 | if (m->max_depth > 0) { |
H. Peter Anvin (Intel) | 80c4f23 | 2018-12-14 13:33:24 -0800 | [diff] [blame] | 4560 | nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i", |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4561 | m->max_depth); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4562 | } |
| 4563 | nasm_free(params); |
| 4564 | return NULL; |
| 4565 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4566 | /* |
| 4567 | * It's right, and we can use it. Add its default |
| 4568 | * parameters to the end of our list if necessary. |
| 4569 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4570 | if (m->defaults && nparam < m->nparam_min + m->ndefs) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4571 | params = |
| 4572 | nasm_realloc(params, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4573 | ((m->nparam_min + m->ndefs + |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4574 | 1) * sizeof(*params))); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4575 | while (nparam < m->nparam_min + m->ndefs) { |
| 4576 | params[nparam] = m->defaults[nparam - m->nparam_min]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4577 | nparam++; |
| 4578 | } |
| 4579 | } |
| 4580 | /* |
| 4581 | * If we've gone over the maximum parameter count (and |
| 4582 | * we're in Plus mode), ignore parameters beyond |
| 4583 | * nparam_max. |
| 4584 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4585 | if (m->plus && nparam > m->nparam_max) |
| 4586 | nparam = m->nparam_max; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4587 | /* |
| 4588 | * Then terminate the parameter list, and leave. |
| 4589 | */ |
| 4590 | if (!params) { /* need this special case */ |
| 4591 | params = nasm_malloc(sizeof(*params)); |
| 4592 | nparam = 0; |
| 4593 | } |
| 4594 | params[nparam] = NULL; |
| 4595 | *params_array = params; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4596 | return m; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4597 | } |
| 4598 | /* |
| 4599 | * This one wasn't right: look for the next one with the |
| 4600 | * same name. |
| 4601 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4602 | list_for_each(m, m->next) |
| 4603 | if (!mstrcmp(m->name, tline->text, m->casesense)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4604 | break; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4605 | } |
| 4606 | |
| 4607 | /* |
| 4608 | * After all that, we didn't find one with the right number of |
| 4609 | * parameters. Issue a warning, and fail to expand the macro. |
| 4610 | */ |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 4611 | nasm_warn(WARN_MACRO_PARAMS, |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4612 | "macro `%s' exists, but not taking %d parameters", |
| 4613 | tline->text, nparam); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4614 | nasm_free(params); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4615 | return NULL; |
| 4616 | } |
| 4617 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4618 | |
| 4619 | /* |
| 4620 | * Save MMacro invocation specific fields in |
| 4621 | * preparation for a recursive macro expansion |
| 4622 | */ |
| 4623 | static void push_mmacro(MMacro *m) |
| 4624 | { |
| 4625 | MMacroInvocation *i; |
| 4626 | |
| 4627 | i = nasm_malloc(sizeof(MMacroInvocation)); |
| 4628 | i->prev = m->prev; |
| 4629 | i->params = m->params; |
| 4630 | i->iline = m->iline; |
| 4631 | i->nparam = m->nparam; |
| 4632 | i->rotate = m->rotate; |
| 4633 | i->paramlen = m->paramlen; |
| 4634 | i->unique = m->unique; |
| 4635 | i->condcnt = m->condcnt; |
| 4636 | m->prev = i; |
| 4637 | } |
| 4638 | |
| 4639 | |
| 4640 | /* |
| 4641 | * Restore MMacro invocation specific fields that were |
| 4642 | * saved during a previous recursive macro expansion |
| 4643 | */ |
| 4644 | static void pop_mmacro(MMacro *m) |
| 4645 | { |
| 4646 | MMacroInvocation *i; |
| 4647 | |
| 4648 | if (m->prev) { |
| 4649 | i = m->prev; |
| 4650 | m->prev = i->prev; |
| 4651 | m->params = i->params; |
| 4652 | m->iline = i->iline; |
| 4653 | m->nparam = i->nparam; |
| 4654 | m->rotate = i->rotate; |
| 4655 | m->paramlen = i->paramlen; |
| 4656 | m->unique = i->unique; |
| 4657 | m->condcnt = i->condcnt; |
| 4658 | nasm_free(i); |
| 4659 | } |
| 4660 | } |
| 4661 | |
| 4662 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4663 | /* |
| 4664 | * Expand the multi-line macro call made by the given line, if |
| 4665 | * 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] | 4666 | * istk->expansion and return 1. Otherwise return 0. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4667 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4668 | static int expand_mmacro(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4669 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4670 | Token *startline = tline; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4671 | Token *label = NULL; |
| 4672 | int dont_prepend = 0; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4673 | Token **params, *t, *tt; |
| 4674 | MMacro *m; |
| 4675 | Line *l, *ll; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4676 | int i, nparam, *paramlen; |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 4677 | const char *mname; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4678 | |
| 4679 | t = tline; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4680 | skip_white_(t); |
H. Peter Anvin | ce2233b | 2008-05-25 21:57:00 -0700 | [diff] [blame] | 4681 | /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */ |
H. Peter Anvin | dce1e2f | 2002-04-30 21:06:37 +0000 | [diff] [blame] | 4682 | 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] | 4683 | return 0; |
| 4684 | m = is_mmacro(t, ¶ms); |
| 4685 | if (m) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4686 | mname = t->text; |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 4687 | } else { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4688 | Token *last; |
| 4689 | /* |
| 4690 | * We have an id which isn't a macro call. We'll assume |
| 4691 | * it might be a label; we'll also check to see if a |
| 4692 | * colon follows it. Then, if there's another id after |
| 4693 | * that lot, we'll check it again for macro-hood. |
| 4694 | */ |
| 4695 | label = last = t; |
| 4696 | t = t->next; |
| 4697 | if (tok_type_(t, TOK_WHITESPACE)) |
| 4698 | last = t, t = t->next; |
| 4699 | if (tok_is_(t, ":")) { |
| 4700 | dont_prepend = 1; |
| 4701 | last = t, t = t->next; |
| 4702 | if (tok_type_(t, TOK_WHITESPACE)) |
| 4703 | last = t, t = t->next; |
| 4704 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4705 | if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, ¶ms))) |
| 4706 | return 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4707 | last->next = NULL; |
Keith Kanios | 891775e | 2009-07-11 06:08:54 -0500 | [diff] [blame] | 4708 | mname = t->text; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4709 | tline = t; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4710 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4711 | |
| 4712 | /* |
| 4713 | * Fix up the parameters: this involves stripping leading and |
| 4714 | * trailing whitespace, then stripping braces if they are |
| 4715 | * present. |
| 4716 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4717 | for (nparam = 0; params[nparam]; nparam++) ; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4718 | paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4719 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4720 | for (i = 0; params[i]; i++) { |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4721 | int brace = 0; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4722 | int comma = (!m->plus || i < nparam - 1); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4723 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4724 | t = params[i]; |
| 4725 | skip_white_(t); |
| 4726 | if (tok_is_(t, "{")) |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4727 | t = t->next, brace++, comma = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4728 | params[i] = t; |
| 4729 | paramlen[i] = 0; |
| 4730 | while (t) { |
| 4731 | if (comma && t->type == TOK_OTHER && !strcmp(t->text, ",")) |
| 4732 | break; /* ... because we have hit a comma */ |
| 4733 | if (comma && t->type == TOK_WHITESPACE |
| 4734 | && tok_is_(t->next, ",")) |
| 4735 | break; /* ... or a space then a comma */ |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4736 | if (brace && t->type == TOK_OTHER) { |
| 4737 | if (t->text[0] == '{') |
| 4738 | brace++; /* ... or a nested opening brace */ |
| 4739 | else if (t->text[0] == '}') |
| 4740 | if (!--brace) |
| 4741 | break; /* ... or a brace */ |
| 4742 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4743 | t = t->next; |
| 4744 | paramlen[i]++; |
| 4745 | } |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4746 | if (brace) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4747 | nasm_nonfatal("macro params should be enclosed in braces"); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4748 | } |
| 4749 | |
| 4750 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4751 | * OK, we have a MMacro structure together with a set of |
| 4752 | * parameters. We must now go through the expansion and push |
| 4753 | * copies of each Line on to istk->expansion. Substitution of |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4754 | * parameter tokens and macro-local tokens doesn't get done |
| 4755 | * until the single-line macro substitution process; this is |
| 4756 | * because delaying them allows us to change the semantics |
| 4757 | * later through %rotate. |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4758 | * |
| 4759 | * First, push an end marker on to istk->expansion, mark this |
| 4760 | * macro as in progress, and set up its invocation-specific |
| 4761 | * variables. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4762 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4763 | ll = nasm_malloc(sizeof(Line)); |
| 4764 | ll->next = istk->expansion; |
| 4765 | ll->finishes = m; |
| 4766 | ll->first = NULL; |
| 4767 | istk->expansion = ll; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4768 | |
| 4769 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4770 | * Save the previous MMacro expansion in the case of |
| 4771 | * macro recursion |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4772 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4773 | if (m->max_depth && m->in_progress) |
| 4774 | push_mmacro(m); |
| 4775 | |
| 4776 | m->in_progress ++; |
| 4777 | m->params = params; |
| 4778 | m->iline = tline; |
| 4779 | m->nparam = nparam; |
| 4780 | m->rotate = 0; |
| 4781 | m->paramlen = paramlen; |
| 4782 | m->unique = unique++; |
| 4783 | m->lineno = 0; |
| 4784 | m->condcnt = 0; |
| 4785 | |
| 4786 | m->next_active = istk->mstk; |
| 4787 | istk->mstk = m; |
| 4788 | |
| 4789 | list_for_each(l, m->expansion) { |
| 4790 | Token **tail; |
| 4791 | |
| 4792 | ll = nasm_malloc(sizeof(Line)); |
| 4793 | ll->finishes = NULL; |
| 4794 | ll->next = istk->expansion; |
| 4795 | istk->expansion = ll; |
| 4796 | tail = &ll->first; |
| 4797 | |
| 4798 | list_for_each(t, l->first) { |
| 4799 | Token *x = t; |
| 4800 | switch (t->type) { |
| 4801 | case TOK_PREPROC_Q: |
| 4802 | tt = *tail = new_Token(NULL, TOK_ID, mname, 0); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4803 | break; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4804 | case TOK_PREPROC_QQ: |
| 4805 | tt = *tail = new_Token(NULL, TOK_ID, m->name, 0); |
| 4806 | break; |
| 4807 | case TOK_PREPROC_ID: |
| 4808 | if (t->text[1] == '0' && t->text[2] == '0') { |
| 4809 | dont_prepend = -1; |
| 4810 | x = label; |
| 4811 | if (!x) |
| 4812 | continue; |
| 4813 | } |
| 4814 | /* fall through */ |
| 4815 | default: |
| 4816 | tt = *tail = new_Token(NULL, x->type, x->text, 0); |
| 4817 | break; |
| 4818 | } |
| 4819 | tail = &tt->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4820 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4821 | *tail = NULL; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4822 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4823 | |
| 4824 | /* |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4825 | * If we had a label, push it on as the first line of |
| 4826 | * the macro expansion. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4827 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4828 | if (label) { |
| 4829 | if (dont_prepend < 0) |
| 4830 | free_tlist(startline); |
| 4831 | else { |
| 4832 | ll = nasm_malloc(sizeof(Line)); |
| 4833 | ll->finishes = NULL; |
| 4834 | ll->next = istk->expansion; |
| 4835 | istk->expansion = ll; |
| 4836 | ll->first = startline; |
| 4837 | if (!dont_prepend) { |
| 4838 | while (label->next) |
| 4839 | label = label->next; |
| 4840 | label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4841 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4842 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4843 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4844 | |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 4845 | lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4846 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4847 | return 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4848 | } |
| 4849 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4850 | /* |
| 4851 | * This function adds macro names to error messages, and suppresses |
| 4852 | * them if necessary. |
| 4853 | */ |
H. Peter Anvin (Intel) | 6bde2ed | 2018-12-13 19:39:41 -0800 | [diff] [blame] | 4854 | static void pp_verror(errflags severity, const char *fmt, va_list arg) |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4855 | { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4856 | char buff[BUFSIZ]; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4857 | MMacro *mmac = NULL; |
| 4858 | int delta = 0; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4859 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4860 | /* |
| 4861 | * If we're in a dead branch of IF or something like it, ignore the error. |
| 4862 | * However, because %else etc are evaluated in the state context |
| 4863 | * of the previous branch, errors might get lost: |
| 4864 | * %if 0 ... %else trailing garbage ... %endif |
| 4865 | * So %else etc should set the ERR_PP_PRECOND flag. |
| 4866 | */ |
| 4867 | if ((severity & ERR_MASK) < ERR_FATAL && |
| 4868 | istk && istk->conds && |
| 4869 | ((severity & ERR_PP_PRECOND) ? |
| 4870 | istk->conds->state == COND_NEVER : |
H. Peter Anvin | eb6653f | 2016-04-05 13:03:10 -0700 | [diff] [blame] | 4871 | !emitting(istk->conds->state))) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4872 | return; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4873 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4874 | /* get %macro name */ |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4875 | if (!(severity & ERR_NOFILE) && istk && istk->mstk) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4876 | mmac = istk->mstk; |
| 4877 | /* but %rep blocks should be skipped */ |
| 4878 | while (mmac && !mmac->name) |
| 4879 | mmac = mmac->next_active, delta++; |
Cyrill Gorcunov | 9900c6b | 2011-10-09 18:58:46 +0400 | [diff] [blame] | 4880 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4881 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4882 | if (mmac) { |
| 4883 | vsnprintf(buff, sizeof(buff), fmt, arg); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4884 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4885 | nasm_set_verror(real_verror); |
| 4886 | nasm_error(severity, "(%s:%d) %s", |
| 4887 | mmac->name, mmac->lineno - delta, buff); |
| 4888 | nasm_set_verror(pp_verror); |
| 4889 | } else { |
| 4890 | real_verror(severity, fmt, arg); |
| 4891 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4892 | } |
| 4893 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4894 | static void |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 4895 | pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4896 | { |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 4897 | Token *t; |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 4898 | int apass; |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 4899 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4900 | cstk = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4901 | istk = nasm_malloc(sizeof(Include)); |
| 4902 | istk->next = NULL; |
| 4903 | istk->conds = NULL; |
| 4904 | istk->expansion = NULL; |
| 4905 | istk->mstk = NULL; |
H. Peter Anvin | 3e83cec | 2016-05-25 04:28:46 -0700 | [diff] [blame] | 4906 | istk->fp = nasm_open_read(file, NF_TEXT); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4907 | istk->fname = NULL; |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 4908 | src_set(0, file); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4909 | istk->lineinc = 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4910 | if (!istk->fp) |
Cyrill Gorcunov | c3527dd | 2018-11-24 22:17:47 +0300 | [diff] [blame] | 4911 | nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4912 | defining = NULL; |
Charles Crayne | d4200be | 2008-07-12 16:42:33 -0700 | [diff] [blame] | 4913 | nested_mac_count = 0; |
| 4914 | nested_rep_count = 0; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 4915 | init_macros(); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4916 | unique = 0; |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 4917 | deplist = dep_list; |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 4918 | pp_mode = mode; |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 4919 | |
| 4920 | if (tasm_compatible_mode) |
| 4921 | pp_add_stdmac(nasm_stdmac_tasm); |
| 4922 | |
| 4923 | pp_add_stdmac(nasm_stdmac_nasm); |
| 4924 | pp_add_stdmac(nasm_stdmac_version); |
| 4925 | |
Cyrill Gorcunov | 15ce78f | 2017-01-06 20:21:28 +0300 | [diff] [blame] | 4926 | if (extrastdmac) |
| 4927 | pp_add_stdmac(extrastdmac); |
| 4928 | |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 4929 | stdmacpos = stdmacros[0]; |
| 4930 | stdmacnext = &stdmacros[1]; |
| 4931 | |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 4932 | do_predef = true; |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 4933 | |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 4934 | strlist_add(deplist, file); |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 4935 | |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 4936 | /* |
| 4937 | * Define the __PASS__ macro. This is defined here unlike |
| 4938 | * all the other builtins, because it is special -- it varies between |
| 4939 | * passes. |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 4940 | * |
| 4941 | * 0 = dependencies only |
| 4942 | * 1 = preparatory passes |
| 4943 | * 2 = final pass |
| 4944 | * 3 = preproces only |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 4945 | */ |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 4946 | switch (mode) { |
| 4947 | case PP_NORMAL: |
| 4948 | apass = pass_final() ? 2 : 1; |
| 4949 | break; |
| 4950 | case PP_DEPS: |
| 4951 | apass = 0; |
| 4952 | break; |
| 4953 | case PP_PREPROC: |
| 4954 | apass = 3; |
| 4955 | break; |
| 4956 | default: |
| 4957 | panic(); |
| 4958 | } |
| 4959 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4960 | t = nasm_malloc(sizeof(*t)); |
| 4961 | t->next = NULL; |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 4962 | make_tok_num(t, apass); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4963 | t->a.mac = NULL; |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 4964 | define_smacro(NULL, "__PASS__", true, 0, t); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4965 | } |
| 4966 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 4967 | static void pp_init(void) |
| 4968 | { |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 4969 | } |
| 4970 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4971 | static char *pp_getline(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4972 | { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4973 | char *line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4974 | Token *tline; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4975 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4976 | real_verror = nasm_set_verror(pp_verror); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4977 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4978 | while (1) { |
| 4979 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4980 | * Fetch a tokenized line, either from the macro-expansion |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4981 | * buffer or from the input file. |
| 4982 | */ |
| 4983 | tline = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4984 | while (istk->expansion && istk->expansion->finishes) { |
| 4985 | Line *l = istk->expansion; |
| 4986 | if (!l->finishes->name && l->finishes->in_progress > 1) { |
| 4987 | Line *ll; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4988 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4989 | /* |
| 4990 | * This is a macro-end marker for a macro with no |
| 4991 | * name, which means it's not really a macro at all |
| 4992 | * but a %rep block, and the `in_progress' field is |
| 4993 | * more than 1, meaning that we still need to |
| 4994 | * repeat. (1 means the natural last repetition; 0 |
| 4995 | * means termination by %exitrep.) We have |
| 4996 | * therefore expanded up to the %endrep, and must |
| 4997 | * push the whole block on to the expansion buffer |
| 4998 | * again. We don't bother to remove the macro-end |
| 4999 | * marker: we'd only have to generate another one |
| 5000 | * if we did. |
| 5001 | */ |
| 5002 | l->finishes->in_progress--; |
| 5003 | list_for_each(l, l->finishes->expansion) { |
| 5004 | Token *t, *tt, **tail; |
| 5005 | |
| 5006 | ll = nasm_malloc(sizeof(Line)); |
| 5007 | ll->next = istk->expansion; |
| 5008 | ll->finishes = NULL; |
| 5009 | ll->first = NULL; |
| 5010 | tail = &ll->first; |
| 5011 | |
| 5012 | list_for_each(t, l->first) { |
| 5013 | if (t->text || t->type == TOK_WHITESPACE) { |
| 5014 | tt = *tail = new_Token(NULL, t->type, t->text, 0); |
| 5015 | tail = &tt->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5016 | } |
| 5017 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5018 | |
| 5019 | istk->expansion = ll; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5020 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5021 | } else { |
| 5022 | /* |
| 5023 | * Check whether a `%rep' was started and not ended |
| 5024 | * within this macro expansion. This can happen and |
| 5025 | * should be detected. It's a fatal error because |
| 5026 | * I'm too confused to work out how to recover |
| 5027 | * sensibly from it. |
| 5028 | */ |
| 5029 | if (defining) { |
| 5030 | if (defining->name) |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 5031 | nasm_panic("defining with name in expansion"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5032 | else if (istk->mstk->name) |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 5033 | nasm_fatal("`%%rep' without `%%endrep' within" |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5034 | " expansion of macro `%s'", |
| 5035 | istk->mstk->name); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5036 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5037 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5038 | /* |
| 5039 | * FIXME: investigate the relationship at this point between |
| 5040 | * istk->mstk and l->finishes |
| 5041 | */ |
| 5042 | { |
| 5043 | MMacro *m = istk->mstk; |
| 5044 | istk->mstk = m->next_active; |
| 5045 | if (m->name) { |
| 5046 | /* |
| 5047 | * This was a real macro call, not a %rep, and |
| 5048 | * therefore the parameter information needs to |
| 5049 | * be freed. |
| 5050 | */ |
| 5051 | if (m->prev) { |
| 5052 | pop_mmacro(m); |
| 5053 | l->finishes->in_progress --; |
| 5054 | } else { |
| 5055 | nasm_free(m->params); |
| 5056 | free_tlist(m->iline); |
| 5057 | nasm_free(m->paramlen); |
| 5058 | l->finishes->in_progress = 0; |
| 5059 | } |
Adam Majer | 91e7240 | 2017-07-25 10:42:01 +0200 | [diff] [blame] | 5060 | } |
| 5061 | |
| 5062 | /* |
| 5063 | * FIXME It is incorrect to always free_mmacro here. |
| 5064 | * It leads to usage-after-free. |
| 5065 | * |
| 5066 | * https://bugzilla.nasm.us/show_bug.cgi?id=3392414 |
| 5067 | */ |
| 5068 | #if 0 |
| 5069 | else |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5070 | free_mmacro(m); |
Adam Majer | 91e7240 | 2017-07-25 10:42:01 +0200 | [diff] [blame] | 5071 | #endif |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5072 | } |
| 5073 | istk->expansion = l->next; |
| 5074 | nasm_free(l); |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 5075 | lfmt->downlevel(LIST_MACRO); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5076 | } |
| 5077 | } |
| 5078 | while (1) { /* until we get a line we can use */ |
| 5079 | |
| 5080 | if (istk->expansion) { /* from a macro expansion */ |
| 5081 | char *p; |
| 5082 | Line *l = istk->expansion; |
| 5083 | if (istk->mstk) |
| 5084 | istk->mstk->lineno++; |
| 5085 | tline = l->first; |
| 5086 | istk->expansion = l->next; |
| 5087 | nasm_free(l); |
| 5088 | p = detoken(tline, false); |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 5089 | lfmt->line(LIST_MACRO, p); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5090 | nasm_free(p); |
| 5091 | break; |
| 5092 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5093 | line = read_line(); |
| 5094 | if (line) { /* from the current input file */ |
| 5095 | line = prepreproc(line); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5096 | tline = tokenize(line); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5097 | nasm_free(line); |
| 5098 | break; |
| 5099 | } |
| 5100 | /* |
| 5101 | * The current file has ended; work down the istk |
| 5102 | */ |
| 5103 | { |
| 5104 | Include *i = istk; |
| 5105 | fclose(i->fp); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5106 | if (i->conds) { |
| 5107 | /* nasm_error can't be conditionally suppressed */ |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 5108 | nasm_fatal("expected `%%endif' before end of file"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5109 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5110 | /* 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] | 5111 | if (i->next) |
| 5112 | src_set(i->lineno, i->fname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5113 | istk = i->next; |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 5114 | lfmt->downlevel(LIST_INCLUDE); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5115 | nasm_free(i); |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5116 | if (!istk) { |
| 5117 | line = NULL; |
| 5118 | goto done; |
| 5119 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5120 | if (istk->expansion && istk->expansion->finishes) |
| 5121 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5122 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5123 | } |
| 5124 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5125 | /* |
| 5126 | * We must expand MMacro parameters and MMacro-local labels |
| 5127 | * _before_ we plunge into directive processing, to cope |
| 5128 | * with things like `%define something %1' such as STRUC |
| 5129 | * uses. Unless we're _defining_ a MMacro, in which case |
| 5130 | * those tokens should be left alone to go into the |
| 5131 | * definition; and unless we're in a non-emitting |
| 5132 | * condition, in which case we don't want to meddle with |
| 5133 | * anything. |
| 5134 | */ |
| 5135 | if (!defining && !(istk->conds && !emitting(istk->conds->state)) |
| 5136 | && !(istk->mstk && !istk->mstk->in_progress)) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5137 | tline = expand_mmac_params(tline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5138 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5139 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5140 | /* |
| 5141 | * Check the line to see if it's a preprocessor directive. |
| 5142 | */ |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 5143 | if (do_directive(tline, &line) == DIRECTIVE_FOUND) { |
| 5144 | if (line) |
| 5145 | break; /* Directive generated output */ |
| 5146 | else |
| 5147 | continue; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5148 | } else if (defining) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5149 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5150 | * We're defining a multi-line macro. We emit nothing |
| 5151 | * at all, and just |
| 5152 | * shove the tokenized line on to the macro definition. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5153 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5154 | Line *l = nasm_malloc(sizeof(Line)); |
| 5155 | l->next = defining->expansion; |
| 5156 | l->first = tline; |
| 5157 | l->finishes = NULL; |
| 5158 | defining->expansion = l; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5159 | continue; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5160 | } else if (istk->conds && !emitting(istk->conds->state)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5161 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5162 | * We're in a non-emitting branch of a condition block. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5163 | * Emit nothing at all, not even a blank line: when we |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5164 | * emerge from the condition we'll give a line-number |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5165 | * directive so we keep our place correctly. |
| 5166 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5167 | free_tlist(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5168 | continue; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5169 | } else if (istk->mstk && !istk->mstk->in_progress) { |
| 5170 | /* |
| 5171 | * We're in a %rep block which has been terminated, so |
| 5172 | * we're walking through to the %endrep without |
| 5173 | * emitting anything. Emit nothing at all, not even a |
| 5174 | * blank line: when we emerge from the %rep block we'll |
| 5175 | * give a line-number directive so we keep our place |
| 5176 | * correctly. |
| 5177 | */ |
| 5178 | free_tlist(tline); |
| 5179 | continue; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5180 | } else { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5181 | tline = expand_smacro(tline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5182 | if (!expand_mmacro(tline)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5183 | /* |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5184 | * De-tokenize the line again, and emit it. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5185 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5186 | line = detoken(tline, true); |
| 5187 | free_tlist(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5188 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5189 | } else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5190 | continue; /* expand_mmacro calls free_tlist */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5191 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5192 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5193 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5194 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5195 | done: |
| 5196 | nasm_set_verror(real_verror); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5197 | return line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5198 | } |
| 5199 | |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5200 | static void pp_cleanup_pass(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5201 | { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5202 | real_verror = nasm_set_verror(pp_verror); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5203 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5204 | if (defining) { |
| 5205 | if (defining->name) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 5206 | nasm_nonfatal("end of file while still defining macro `%s'", |
| 5207 | defining->name); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5208 | } else { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 5209 | nasm_nonfatal("end of file while still in %%rep"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5210 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5211 | |
| 5212 | free_mmacro(defining); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5213 | defining = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5214 | } |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5215 | |
| 5216 | nasm_set_verror(real_verror); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5217 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5218 | while (cstk) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5219 | ctx_pop(); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 5220 | free_macros(); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5221 | while (istk) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5222 | Include *i = istk; |
| 5223 | istk = istk->next; |
| 5224 | fclose(i->fp); |
Cyrill Gorcunov | 8dcfd88 | 2011-03-03 09:18:56 +0300 | [diff] [blame] | 5225 | nasm_free(i); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5226 | } |
| 5227 | while (cstk) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5228 | ctx_pop(); |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 5229 | src_set_fname(NULL); |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5230 | } |
| 5231 | |
| 5232 | static void pp_cleanup_session(void) |
| 5233 | { |
| 5234 | free_llist(predef); |
| 5235 | predef = NULL; |
| 5236 | delete_Blocks(); |
| 5237 | freeTokens = NULL; |
| 5238 | ipath_list = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5239 | } |
| 5240 | |
Cyrill Gorcunov | 8c0666b | 2018-11-24 14:33:48 +0300 | [diff] [blame] | 5241 | static void pp_include_path(struct strlist *list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5242 | { |
Cyrill Gorcunov | 8c0666b | 2018-11-24 14:33:48 +0300 | [diff] [blame] | 5243 | ipath_list = list; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5244 | } |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5245 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5246 | static void pp_pre_include(char *fname) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5247 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5248 | Token *inc, *space, *name; |
| 5249 | Line *l; |
| 5250 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5251 | name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0); |
| 5252 | space = new_Token(name, TOK_WHITESPACE, NULL, 0); |
| 5253 | inc = new_Token(space, TOK_PREPROC_ID, "%include", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5254 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5255 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5256 | l->next = predef; |
| 5257 | l->first = inc; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5258 | l->finishes = NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5259 | predef = l; |
| 5260 | } |
| 5261 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5262 | static void pp_pre_define(char *definition) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5263 | { |
| 5264 | Token *def, *space; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5265 | Line *l; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5266 | char *equals; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5267 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5268 | real_verror = nasm_set_verror(pp_verror); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5269 | |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5270 | equals = strchr(definition, '='); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5271 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 5272 | def = new_Token(space, TOK_PREPROC_ID, "%define", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5273 | if (equals) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5274 | *equals = ' '; |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5275 | space->next = tokenize(definition); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5276 | if (equals) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5277 | *equals = '='; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5278 | |
Cyrill Gorcunov | 6d42e9b | 2015-02-08 11:07:17 +0300 | [diff] [blame] | 5279 | if (space->next->type != TOK_PREPROC_ID && |
| 5280 | space->next->type != TOK_ID) |
H. Peter Anvin (Intel) | 80c4f23 | 2018-12-14 13:33:24 -0800 | [diff] [blame] | 5281 | nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition); |
Cyrill Gorcunov | 6d42e9b | 2015-02-08 11:07:17 +0300 | [diff] [blame] | 5282 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5283 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5284 | l->next = predef; |
| 5285 | l->first = def; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5286 | l->finishes = NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5287 | predef = l; |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5288 | |
| 5289 | nasm_set_verror(real_verror); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5290 | } |
| 5291 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5292 | static void pp_pre_undefine(char *definition) |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5293 | { |
| 5294 | Token *def, *space; |
| 5295 | Line *l; |
| 5296 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5297 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 5298 | def = new_Token(space, TOK_PREPROC_ID, "%undef", 0); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5299 | space->next = tokenize(definition); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5300 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5301 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5302 | l->next = predef; |
| 5303 | l->first = def; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5304 | l->finishes = NULL; |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5305 | predef = l; |
| 5306 | } |
| 5307 | |
H. Peter Anvin | 0599034 | 2018-06-11 13:32:42 -0700 | [diff] [blame] | 5308 | /* Insert an early preprocessor command that doesn't need special handling */ |
| 5309 | static void pp_pre_command(const char *what, char *string) |
| 5310 | { |
| 5311 | char *cmd; |
| 5312 | Token *def, *space; |
| 5313 | Line *l; |
| 5314 | |
| 5315 | def = tokenize(string); |
| 5316 | if (what) { |
| 5317 | cmd = nasm_strcat(what[0] == '%' ? "" : "%", what); |
| 5318 | space = new_Token(def, TOK_WHITESPACE, NULL, 0); |
| 5319 | def = new_Token(space, TOK_PREPROC_ID, cmd, 0); |
| 5320 | } |
| 5321 | |
| 5322 | l = nasm_malloc(sizeof(Line)); |
| 5323 | l->next = predef; |
| 5324 | l->first = def; |
| 5325 | l->finishes = NULL; |
| 5326 | predef = l; |
| 5327 | } |
| 5328 | |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 5329 | static void pp_add_stdmac(macros_t *macros) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5330 | { |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 5331 | macros_t **mp; |
| 5332 | |
| 5333 | /* Find the end of the list and avoid duplicates */ |
| 5334 | for (mp = stdmacros; *mp; mp++) { |
| 5335 | if (*mp == macros) |
| 5336 | return; /* Nothing to do */ |
| 5337 | } |
| 5338 | |
| 5339 | nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]); |
| 5340 | |
| 5341 | *mp = macros; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 5342 | } |
| 5343 | |
Cyrill Gorcunov | 15ce78f | 2017-01-06 20:21:28 +0300 | [diff] [blame] | 5344 | static void pp_extra_stdmac(macros_t *macros) |
| 5345 | { |
| 5346 | extrastdmac = macros; |
| 5347 | } |
| 5348 | |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 5349 | static void make_tok_num(Token * tok, int64_t val) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5350 | { |
Cyrill Gorcunov | ce65274 | 2013-05-06 23:43:43 +0400 | [diff] [blame] | 5351 | char numbuf[32]; |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 5352 | snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5353 | tok->text = nasm_strdup(numbuf); |
| 5354 | tok->type = TOK_NUMBER; |
| 5355 | } |
| 5356 | |
H. Peter Anvin (Intel) | 6bde2ed | 2018-12-13 19:39:41 -0800 | [diff] [blame] | 5357 | static void pp_list_one_macro(MMacro *m, errflags severity) |
H. Peter Anvin | 3736895 | 2016-05-09 14:10:32 -0700 | [diff] [blame] | 5358 | { |
| 5359 | if (!m) |
| 5360 | return; |
| 5361 | |
| 5362 | /* We need to print the next_active list in reverse order */ |
| 5363 | pp_list_one_macro(m->next_active, severity); |
| 5364 | |
| 5365 | if (m->name && !m->nolist) { |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 5366 | src_set(m->xline + m->lineno, m->fname); |
H. Peter Anvin | ddb2906 | 2018-12-11 00:06:29 -0800 | [diff] [blame] | 5367 | nasm_error(severity, "... from macro `%s' defined", m->name); |
H. Peter Anvin | 3736895 | 2016-05-09 14:10:32 -0700 | [diff] [blame] | 5368 | } |
| 5369 | } |
| 5370 | |
H. Peter Anvin (Intel) | 6bde2ed | 2018-12-13 19:39:41 -0800 | [diff] [blame] | 5371 | static void pp_error_list_macros(errflags severity) |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5372 | { |
H. Peter Anvin | ddb2906 | 2018-12-11 00:06:29 -0800 | [diff] [blame] | 5373 | struct src_location saved; |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5374 | |
H. Peter Anvin | ddb2906 | 2018-12-11 00:06:29 -0800 | [diff] [blame] | 5375 | severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE; |
| 5376 | saved = src_where(); |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5377 | |
Cyrill Gorcunov | 771d04e | 2016-05-10 23:27:03 +0300 | [diff] [blame] | 5378 | if (istk) |
| 5379 | pp_list_one_macro(istk->mstk, severity); |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5380 | |
H. Peter Anvin | ddb2906 | 2018-12-11 00:06:29 -0800 | [diff] [blame] | 5381 | src_update(saved); |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5382 | } |
| 5383 | |
H. Peter Anvin | e746971 | 2016-02-18 02:20:59 -0800 | [diff] [blame] | 5384 | const struct preproc_ops nasmpp = { |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 5385 | pp_init, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5386 | pp_reset, |
| 5387 | pp_getline, |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5388 | pp_cleanup_pass, |
| 5389 | pp_cleanup_session, |
Cyrill Gorcunov | 15ce78f | 2017-01-06 20:21:28 +0300 | [diff] [blame] | 5390 | pp_extra_stdmac, |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5391 | pp_pre_define, |
| 5392 | pp_pre_undefine, |
| 5393 | pp_pre_include, |
H. Peter Anvin | 0599034 | 2018-06-11 13:32:42 -0700 | [diff] [blame] | 5394 | pp_pre_command, |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5395 | pp_include_path, |
| 5396 | pp_error_list_macros, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5397 | }; |