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 (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 3 | * Copyright 1996-2019 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 | c2f3f26 | 2018-12-27 12:37:25 -0800 | [diff] [blame] | 65 | #include "nctype.h" |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 66 | |
| 67 | #include "nasm.h" |
| 68 | #include "nasmlib.h" |
H. Peter Anvin | b20bc73 | 2017-03-07 19:23:03 -0800 | [diff] [blame] | 69 | #include "error.h" |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 70 | #include "preproc.h" |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 71 | #include "hashtbl.h" |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 72 | #include "quote.h" |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 73 | #include "stdscan.h" |
H. Peter Anvin | dbb640b | 2009-07-18 18:57:16 -0700 | [diff] [blame] | 74 | #include "eval.h" |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 75 | #include "tokens.h" |
H. Peter Anvin | a4835d4 | 2008-05-20 14:21:29 -0700 | [diff] [blame] | 76 | #include "tables.h" |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 77 | #include "listing.h" |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 78 | |
| 79 | typedef struct SMacro SMacro; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 80 | typedef struct MMacro MMacro; |
| 81 | typedef struct MMacroInvocation MMacroInvocation; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 82 | typedef struct Context Context; |
| 83 | typedef struct Token Token; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 84 | typedef struct Blocks Blocks; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 85 | typedef struct Line Line; |
| 86 | typedef struct Include Include; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 87 | typedef struct Cond Cond; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 88 | |
| 89 | /* |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 90 | * Note on the storage of both SMacro and MMacros: the hash table |
| 91 | * indexes them case-insensitively, and we then have to go through a |
| 92 | * linked list of potential case aliases (and, for MMacros, parameter |
| 93 | * ranges); this is to preserve the matching semantics of the earlier |
| 94 | * code. If the number of case aliases for a specific macro is a |
| 95 | * performance issue, you may want to reconsider your coding style. |
| 96 | */ |
| 97 | |
| 98 | /* |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 99 | * Function call tp obtain the expansion of an smacro |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 100 | */ |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 101 | typedef Token *(*ExpandSMacro)(const SMacro *s, Token **params, |
| 102 | unsigned int nparams); |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 103 | |
| 104 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 105 | * Store the definition of a single-line macro. |
| 106 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 107 | struct SMacro { |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 108 | SMacro *next; /* MUST BE FIRST - see free_smacro() */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 109 | char *name; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 110 | Token *expansion; |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 111 | ExpandSMacro expand; |
| 112 | intorptr expandpvt; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 113 | bool *eval_param; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 114 | unsigned int nparam; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 115 | bool casesense; |
| 116 | bool in_progress; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 120 | * Store the definition of a multi-line macro. This is also used to |
| 121 | * store the interiors of `%rep...%endrep' blocks, which are |
| 122 | * effectively self-re-invoking multi-line macros which simply |
| 123 | * don't have a name or bother to appear in the hash tables. %rep |
| 124 | * blocks are signified by having a NULL `name' field. |
| 125 | * |
| 126 | * In a MMacro describing a `%rep' block, the `in_progress' field |
| 127 | * isn't merely boolean, but gives the number of repeats left to |
| 128 | * run. |
| 129 | * |
| 130 | * The `next' field is used for storing MMacros in hash tables; the |
| 131 | * `next_active' field is for stacking them on istk entries. |
| 132 | * |
| 133 | * When a MMacro is being expanded, `params', `iline', `nparam', |
| 134 | * `paramlen', `rotate' and `unique' are local to the invocation. |
| 135 | */ |
| 136 | struct MMacro { |
| 137 | MMacro *next; |
| 138 | MMacroInvocation *prev; /* previous invocation */ |
| 139 | char *name; |
| 140 | int nparam_min, nparam_max; |
| 141 | bool casesense; |
| 142 | bool plus; /* is the last parameter greedy? */ |
| 143 | bool nolist; /* is this macro listing-inhibited? */ |
| 144 | int64_t in_progress; /* is this macro currently being expanded? */ |
| 145 | int32_t max_depth; /* maximum number of recursive expansions allowed */ |
| 146 | Token *dlist; /* All defaults as one list */ |
| 147 | Token **defaults; /* Parameter default pointers */ |
| 148 | int ndefs; /* number of default parameters */ |
| 149 | Line *expansion; |
| 150 | |
| 151 | MMacro *next_active; |
| 152 | MMacro *rep_nest; /* used for nesting %rep */ |
| 153 | Token **params; /* actual parameters */ |
| 154 | Token *iline; /* invocation line */ |
| 155 | unsigned int nparam, rotate; |
| 156 | int *paramlen; |
| 157 | uint64_t unique; |
| 158 | int lineno; /* Current line number on expansion */ |
| 159 | uint64_t condcnt; /* number of if blocks... */ |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 160 | |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 161 | const char *fname; /* File where defined */ |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 162 | int32_t xline; /* First line in macro */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | |
| 166 | /* Store the definition of a multi-line macro, as defined in a |
| 167 | * previous recursive macro expansion. |
| 168 | */ |
| 169 | struct MMacroInvocation { |
| 170 | MMacroInvocation *prev; /* previous invocation */ |
| 171 | Token **params; /* actual parameters */ |
| 172 | Token *iline; /* invocation line */ |
| 173 | unsigned int nparam, rotate; |
| 174 | int *paramlen; |
| 175 | uint64_t unique; |
| 176 | uint64_t condcnt; |
| 177 | }; |
| 178 | |
| 179 | |
| 180 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 181 | * The context stack is composed of a linked list of these. |
| 182 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 183 | struct Context { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 184 | Context *next; |
| 185 | char *name; |
| 186 | struct hash_table localmac; |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 187 | uint64_t number; |
| 188 | unsigned int depth; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 189 | }; |
| 190 | |
| 191 | /* |
| 192 | * This is the internal form which we break input lines up into. |
| 193 | * Typically stored in linked lists. |
| 194 | * |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 195 | * Note that `type' serves a double meaning: TOK_SMAC_START_PARAMS is |
| 196 | * not necessarily used as-is, but is also used to encode the number |
| 197 | * and expansion type of substituted parameter. So in the definition |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 198 | * |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 199 | * %define a(x,=y) ( (x) & ~(y) ) |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 200 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 201 | * the token representing `x' will have its type changed to |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 202 | * tok_smac_param(0) but the one representing `y' will be |
| 203 | * tok_smac_param(1); see the accessor functions below. |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 204 | * |
| 205 | * TOK_INTERNAL_STRING is a dirty hack: it's a single string token |
| 206 | * which doesn't need quotes around it. Used in the pre-include |
| 207 | * mechanism as an alternative to trying to find a sensible type of |
| 208 | * quote to use on the filename we were passed. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 209 | */ |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 210 | enum pp_token_type { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 211 | TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID, |
| 212 | TOK_PREPROC_ID, TOK_STRING, |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 213 | TOK_NUMBER, TOK_FLOAT, TOK_OTHER, |
H. Peter Anvin | 6c81f0a | 2008-05-25 21:46:17 -0700 | [diff] [blame] | 214 | TOK_INTERNAL_STRING, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 215 | TOK_PREPROC_Q, TOK_PREPROC_QQ, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 216 | TOK_PASTE, /* %+ */ |
| 217 | TOK_INDIRECT, /* %[...] */ |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 218 | TOK_SMAC_START_PARAMS, /* MUST BE LAST IN THE LIST!!! */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 219 | TOK_MAX = INT_MAX /* Keep compiler from reducing the range */ |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 220 | }; |
| 221 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 222 | static inline enum pp_token_type tok_smac_param(int param) |
| 223 | { |
| 224 | return TOK_SMAC_START_PARAMS + param; |
| 225 | } |
| 226 | static int smac_nparam(enum pp_token_type toktype) |
| 227 | { |
| 228 | return toktype - TOK_SMAC_START_PARAMS; |
| 229 | } |
| 230 | static bool is_smac_param(enum pp_token_type toktype) |
| 231 | { |
| 232 | return toktype >= TOK_SMAC_START_PARAMS; |
| 233 | } |
| 234 | |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 235 | #define PP_CONCAT_MASK(x) (1 << (x)) |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 236 | #define PP_CONCAT_MATCH(t, mask) (PP_CONCAT_MASK((t)->type) & mask) |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 237 | |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 238 | struct tokseq_match { |
| 239 | int mask_head; |
| 240 | int mask_tail; |
| 241 | }; |
| 242 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 243 | struct Token { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 244 | Token *next; |
| 245 | char *text; |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 246 | size_t len; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 247 | enum pp_token_type type; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 248 | }; |
| 249 | |
| 250 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 251 | * Multi-line macro definitions are stored as a linked list of |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 252 | * these, which is essentially a container to allow several linked |
| 253 | * lists of Tokens. |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 254 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 255 | * Note that in this module, linked lists are treated as stacks |
| 256 | * wherever possible. For this reason, Lines are _pushed_ on to the |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 257 | * `expansion' field in MMacro structures, so that the linked list, |
| 258 | * if walked, would give the macro lines in reverse order; this |
| 259 | * means that we can walk the list when expanding a macro, and thus |
| 260 | * push the lines on to the `expansion' field in _istk_ in reverse |
| 261 | * order (so that when popped back off they are in the right |
| 262 | * order). It may seem cockeyed, and it relies on my design having |
| 263 | * an even number of steps in, but it works... |
| 264 | * |
| 265 | * Some of these structures, rather than being actual lines, are |
| 266 | * markers delimiting the end of the expansion of a given macro. |
| 267 | * This is for use in the cycle-tracking and %rep-handling code. |
| 268 | * Such structures have `finishes' non-NULL, and `first' NULL. All |
| 269 | * others have `finishes' NULL, but `first' may still be NULL if |
| 270 | * the line is blank. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 271 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 272 | struct Line { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 273 | Line *next; |
| 274 | MMacro *finishes; |
| 275 | Token *first; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 276 | }; |
| 277 | |
| 278 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 279 | * To handle an arbitrary level of file inclusion, we maintain a |
| 280 | * stack (ie linked list) of these things. |
| 281 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 282 | struct Include { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 283 | Include *next; |
| 284 | FILE *fp; |
| 285 | Cond *conds; |
| 286 | Line *expansion; |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 287 | const char *fname; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 288 | int lineno, lineinc; |
| 289 | MMacro *mstk; /* stack of active macros/reps */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 290 | }; |
| 291 | |
| 292 | /* |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 293 | * File real name hash, so we don't have to re-search the include |
| 294 | * path for every pass (and potentially more than that if a file |
| 295 | * is used more than once.) |
| 296 | */ |
| 297 | struct hash_table FileHash; |
| 298 | |
| 299 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 300 | * Conditional assembly: we maintain a separate stack of these for |
| 301 | * each level of file inclusion. (The only reason we keep the |
| 302 | * stacks separate is to ensure that a stray `%endif' in a file |
| 303 | * included from within the true branch of a `%if' won't terminate |
| 304 | * it and cause confusion: instead, rightly, it'll cause an error.) |
| 305 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 306 | struct Cond { |
| 307 | Cond *next; |
| 308 | int state; |
| 309 | }; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 310 | enum { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 311 | /* |
| 312 | * These states are for use just after %if or %elif: IF_TRUE |
| 313 | * means the condition has evaluated to truth so we are |
| 314 | * currently emitting, whereas IF_FALSE means we are not |
| 315 | * currently emitting but will start doing so if a %else comes |
| 316 | * up. In these states, all directives are admissible: %elif, |
| 317 | * %else and %endif. (And of course %if.) |
| 318 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 319 | COND_IF_TRUE, COND_IF_FALSE, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 320 | /* |
| 321 | * These states come up after a %else: ELSE_TRUE means we're |
| 322 | * emitting, and ELSE_FALSE means we're not. In ELSE_* states, |
| 323 | * any %elif or %else will cause an error. |
| 324 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 325 | COND_ELSE_TRUE, COND_ELSE_FALSE, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 326 | /* |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 327 | * These states mean that we're not emitting now, and also that |
| 328 | * nothing until %endif will be emitted at all. COND_DONE is |
| 329 | * used when we've had our moment of emission |
| 330 | * and have now started seeing %elifs. COND_NEVER is used when |
| 331 | * the condition construct in question is contained within a |
| 332 | * non-emitting branch of a larger condition construct, |
| 333 | * or if there is an error. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 334 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 335 | COND_DONE, COND_NEVER |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 336 | }; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 337 | #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE ) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 338 | |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 339 | /* |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 340 | * These defines are used as the possible return values for do_directive |
| 341 | */ |
| 342 | #define NO_DIRECTIVE_FOUND 0 |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 343 | #define DIRECTIVE_FOUND 1 |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 344 | |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 345 | /* max reps */ |
| 346 | #define REP_LIMIT ((INT64_C(1) << 62)) |
| 347 | |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 348 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 349 | * Condition codes. Note that we use c_ prefix not C_ because C_ is |
| 350 | * used in nasm.h for the "real" condition codes. At _this_ level, |
| 351 | * we treat CXZ and ECXZ as condition codes, albeit non-invertible |
| 352 | * ones, so we need a different enum... |
| 353 | */ |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 354 | static const char * const conditions[] = { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 355 | "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le", |
| 356 | "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no", |
H. Peter Anvin | ce9be34 | 2007-09-12 00:22:29 +0000 | [diff] [blame] | 357 | "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z" |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 358 | }; |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 359 | enum pp_conds { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 360 | c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE, |
| 361 | 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] | 362 | c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z, |
| 363 | c_none = -1 |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 364 | }; |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 365 | static const enum pp_conds inverse_ccs[] = { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 366 | c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE, |
| 367 | 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] | 368 | 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] | 369 | }; |
| 370 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 371 | /* |
| 372 | * Directive names. |
| 373 | */ |
| 374 | /* If this is a an IF, ELIF, ELSE or ENDIF keyword */ |
| 375 | static int is_condition(enum preproc_token arg) |
| 376 | { |
| 377 | return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF); |
| 378 | } |
| 379 | |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 380 | /* For TASM compatibility we need to be able to recognise TASM compatible |
| 381 | * conditional compilation directives. Using the NASM pre-processor does |
| 382 | * not work, so we look for them specifically from the following list and |
| 383 | * then jam in the equivalent NASM directive into the input stream. |
| 384 | */ |
| 385 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 386 | enum { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 387 | TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI, |
| 388 | TM_IFNDEF, TM_INCLUDE, TM_LOCAL |
| 389 | }; |
| 390 | |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 391 | static const char * const tasm_directives[] = { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 392 | "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi", |
| 393 | "ifndef", "include", "local" |
| 394 | }; |
| 395 | |
| 396 | static int StackSize = 4; |
H. Peter Anvin | 6c8b2be | 2016-05-24 23:46:50 -0700 | [diff] [blame] | 397 | static const char *StackPointer = "ebp"; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 398 | static int ArgOffset = 8; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 399 | static int LocalOffset = 0; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 400 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 401 | static Context *cstk; |
| 402 | static Include *istk; |
Cyrill Gorcunov | 8c0666b | 2018-11-24 14:33:48 +0300 | [diff] [blame] | 403 | static const struct strlist *ipath_list; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 404 | |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 405 | static struct strlist *deplist; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 406 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 407 | static uint64_t unique; /* unique identifier numbers */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 408 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 409 | static Line *predef = NULL; |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 410 | static bool do_predef; |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 411 | static enum preproc_mode pp_mode; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 412 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 413 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 414 | * The current set of multi-line macros we have defined. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 415 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 416 | static struct hash_table mmacros; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 417 | |
| 418 | /* |
| 419 | * The current set of single-line macros we have defined. |
| 420 | */ |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 421 | static struct hash_table smacros; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 422 | |
| 423 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 424 | * The multi-line macro we are currently defining, or the %rep |
| 425 | * block we are currently reading, if any. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 426 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 427 | static MMacro *defining; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 428 | |
Charles Crayne | d4200be | 2008-07-12 16:42:33 -0700 | [diff] [blame] | 429 | static uint64_t nested_mac_count; |
| 430 | static uint64_t nested_rep_count; |
| 431 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 432 | /* |
| 433 | * The number of macro parameters to allocate space for at a time. |
| 434 | */ |
| 435 | #define PARAM_DELTA 16 |
| 436 | |
| 437 | /* |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 438 | * The standard macro set: defined in macros.c in a set of arrays. |
| 439 | * This gives our position in any macro set, while we are processing it. |
| 440 | * The stdmacset is an array of such macro sets. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 441 | */ |
H. Peter Anvin | a70547f | 2008-07-19 21:44:26 -0700 | [diff] [blame] | 442 | static macros_t *stdmacpos; |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 443 | static macros_t **stdmacnext; |
| 444 | static macros_t *stdmacros[8]; |
Cyrill Gorcunov | 15ce78f | 2017-01-06 20:21:28 +0300 | [diff] [blame] | 445 | static macros_t *extrastdmac; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 446 | |
| 447 | /* |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 448 | * Tokens are allocated in blocks to improve speed |
| 449 | */ |
| 450 | #define TOKEN_BLOCKSIZE 4096 |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 451 | static Token *freeTokens = NULL; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 452 | struct Blocks { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 453 | Blocks *next; |
| 454 | void *chunk; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 455 | }; |
| 456 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 457 | static Blocks blocks = { NULL, NULL }; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 458 | |
| 459 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 460 | * Forward declarations. |
| 461 | */ |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 462 | static void pp_add_stdmac(macros_t *macros); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 463 | static Token *expand_mmac_params(Token * tline); |
| 464 | static Token *expand_smacro(Token * tline); |
| 465 | static Token *expand_id(Token * tline); |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 466 | static Context *get_ctx(const char *name, const char **namep); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 467 | static Token *make_tok_num(int64_t val); |
| 468 | static Token *make_tok_qstr(const char *str); |
H. Peter Anvin (Intel) | 6bde2ed | 2018-12-13 19:39:41 -0800 | [diff] [blame] | 469 | 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] | 470 | static vefunc real_verror; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 471 | static void *new_Block(size_t size); |
| 472 | static void delete_Blocks(void); |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 473 | static Token *new_Token(Token * next, enum pp_token_type type, |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 474 | const char *text, size_t txtlen); |
| 475 | static Token *dup_Token(Token *next, const Token *src); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 476 | static Token *delete_Token(Token * t); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 477 | |
| 478 | /* |
| 479 | * Macros for safe checking of token pointers, avoid *(NULL) |
| 480 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 481 | #define tok_type_(x,t) ((x) && (x)->type == (t)) |
| 482 | #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next |
| 483 | #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v))) |
| 484 | #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] | 485 | |
Cyrill Gorcunov | 194ba89 | 2011-06-30 01:16:35 +0400 | [diff] [blame] | 486 | /* |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 487 | * In-place reverse a list of tokens. |
| 488 | */ |
| 489 | static Token *reverse_tokens(Token *t) |
| 490 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 491 | Token *prev = NULL; |
| 492 | Token *next; |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 493 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 494 | while (t) { |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 495 | next = t->next; |
| 496 | t->next = prev; |
| 497 | prev = t; |
| 498 | t = next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 499 | } |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 500 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 501 | return prev; |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | /* |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 505 | * Handle TASM specific directives, which do not contain a % in |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 506 | * front of them. We do it here because I could not find any other |
| 507 | * place to do it for the moment, and it is a hack (ideally it would |
| 508 | * be nice to be able to use the NASM pre-processor to do it). |
| 509 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 510 | static char *check_tasm_directive(char *line) |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 511 | { |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 512 | int32_t i, j, k, m, len; |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 513 | char *p, *q, *oldline, oldchar; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 514 | |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 515 | p = nasm_skip_spaces(line); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 516 | |
| 517 | /* Binary search for the directive name */ |
| 518 | i = -1; |
Cyrill Gorcunov | a731924 | 2010-06-03 22:04:36 +0400 | [diff] [blame] | 519 | j = ARRAY_SIZE(tasm_directives); |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 520 | q = nasm_skip_word(p); |
| 521 | len = q - p; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 522 | if (len) { |
| 523 | oldchar = p[len]; |
| 524 | p[len] = 0; |
| 525 | while (j - i > 1) { |
| 526 | k = (j + i) / 2; |
| 527 | m = nasm_stricmp(p, tasm_directives[k]); |
| 528 | if (m == 0) { |
| 529 | /* We have found a directive, so jam a % in front of it |
| 530 | * so that NASM will then recognise it as one if it's own. |
| 531 | */ |
| 532 | p[len] = oldchar; |
| 533 | len = strlen(p); |
| 534 | oldline = line; |
| 535 | line = nasm_malloc(len + 2); |
| 536 | line[0] = '%'; |
| 537 | if (k == TM_IFDIFI) { |
H. Peter Anvin | 18f4879 | 2009-06-27 15:56:27 -0700 | [diff] [blame] | 538 | /* |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 539 | * NASM does not recognise IFDIFI, so we convert |
| 540 | * it to %if 0. This is not used in NASM |
| 541 | * compatible code, but does need to parse for the |
| 542 | * TASM macro package. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 543 | */ |
H. Peter Anvin | 18f4879 | 2009-06-27 15:56:27 -0700 | [diff] [blame] | 544 | strcpy(line + 1, "if 0"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 545 | } else { |
| 546 | memcpy(line + 1, p, len + 1); |
| 547 | } |
| 548 | nasm_free(oldline); |
| 549 | return line; |
| 550 | } else if (m < 0) { |
| 551 | j = k; |
| 552 | } else |
| 553 | i = k; |
| 554 | } |
| 555 | p[len] = oldchar; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 556 | } |
| 557 | return line; |
| 558 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 559 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 560 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 561 | * The pre-preprocessing stage... This function translates line |
| 562 | * number indications as they emerge from GNU cpp (`# lineno "file" |
| 563 | * flags') into NASM preprocessor line number indications (`%line |
| 564 | * lineno file'). |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 565 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 566 | static char *prepreproc(char *line) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 567 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 568 | int lineno, fnlen; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 569 | char *fname, *oldline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 570 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 571 | if (line[0] == '#' && line[1] == ' ') { |
| 572 | oldline = line; |
| 573 | fname = oldline + 2; |
| 574 | lineno = atoi(fname); |
| 575 | fname += strspn(fname, "0123456789 "); |
| 576 | if (*fname == '"') |
| 577 | fname++; |
| 578 | fnlen = strcspn(fname, "\""); |
| 579 | line = nasm_malloc(20 + fnlen); |
| 580 | snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname); |
| 581 | nasm_free(oldline); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 582 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 583 | if (tasm_compatible_mode) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 584 | return check_tasm_directive(line); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 585 | return line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 589 | * Free a linked list of tokens. |
| 590 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 591 | static void free_tlist(Token * list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 592 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 593 | while (list) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 594 | list = delete_Token(list); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | /* |
| 598 | * Free a linked list of lines. |
| 599 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 600 | static void free_llist(Line * list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 601 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 602 | Line *l, *tmp; |
| 603 | list_for_each_safe(l, tmp, list) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 604 | free_tlist(l->first); |
| 605 | nasm_free(l); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 606 | } |
| 607 | } |
| 608 | |
| 609 | /* |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 610 | * Free an array of linked lists of tokens |
| 611 | */ |
| 612 | static void free_tlist_array(Token **array, size_t nlists) |
| 613 | { |
| 614 | Token **listp = array; |
| 615 | |
| 616 | while (nlists--) |
| 617 | free_tlist(*listp++); |
| 618 | |
| 619 | nasm_free(array); |
| 620 | } |
| 621 | |
| 622 | /* |
| 623 | * Duplicate a linked list of tokens. |
| 624 | */ |
| 625 | static Token *dup_tlist(const Token *list, Token ***tailp) |
| 626 | { |
| 627 | Token *newlist = NULL; |
| 628 | Token **tailpp = &newlist; |
| 629 | const Token *t; |
| 630 | |
| 631 | list_for_each(t, list) { |
| 632 | Token *nt; |
| 633 | *tailpp = nt = dup_Token(NULL, t); |
| 634 | tailpp = &nt->next; |
| 635 | } |
| 636 | |
| 637 | if (tailp) |
| 638 | *tailp = tailpp; |
| 639 | |
| 640 | return newlist; |
| 641 | } |
| 642 | |
| 643 | /* |
| 644 | * Duplicate a linked list of tokens in reverse order |
| 645 | */ |
| 646 | static Token *dup_tlist_reverse(const Token *list, Token *tail) |
| 647 | { |
| 648 | const Token *t; |
| 649 | |
| 650 | list_for_each(t, list) |
| 651 | tail = dup_Token(tail, t); |
| 652 | |
| 653 | return tail; |
| 654 | } |
| 655 | |
| 656 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 657 | * Free an MMacro |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 658 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 659 | static void free_mmacro(MMacro * m) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 660 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 661 | nasm_free(m->name); |
| 662 | free_tlist(m->dlist); |
| 663 | nasm_free(m->defaults); |
| 664 | free_llist(m->expansion); |
| 665 | nasm_free(m); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | /* |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 669 | * Clear or free an SMacro |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 670 | */ |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 671 | static void free_smacro_members(SMacro *s) |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 672 | { |
| 673 | nasm_free(s->name); |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 674 | free_tlist(s->expansion); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 675 | nasm_free(s->eval_param); |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | static void clear_smacro(SMacro *s) |
| 679 | { |
| 680 | free_smacro_members(s); |
| 681 | /* Wipe everything except the next pointer */ |
| 682 | memset(&s->next + 1, 0, sizeof *s - sizeof s->next); |
| 683 | } |
| 684 | |
| 685 | /* |
| 686 | * Free an SMacro |
| 687 | */ |
| 688 | static void free_smacro(SMacro *s) |
| 689 | { |
| 690 | free_smacro_members(s); |
| 691 | nasm_free(s); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | /* |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 695 | * Free all currently defined macros, and free the hash tables |
| 696 | */ |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 697 | static void free_smacro_table(struct hash_table *smt) |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 698 | { |
H. Peter Anvin (Intel) | ebb05a0 | 2018-12-11 12:30:25 -0800 | [diff] [blame] | 699 | struct hash_iterator it; |
| 700 | const struct hash_node *np; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 701 | |
H. Peter Anvin (Intel) | ebb05a0 | 2018-12-11 12:30:25 -0800 | [diff] [blame] | 702 | hash_for_each(smt, it, np) { |
| 703 | SMacro *tmp; |
| 704 | SMacro *s = np->data; |
| 705 | nasm_free((void *)np->key); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 706 | list_for_each_safe(s, tmp, s) |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 707 | free_smacro(s); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 708 | } |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 709 | hash_free(smt); |
| 710 | } |
| 711 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 712 | static void free_mmacro_table(struct hash_table *mmt) |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 713 | { |
H. Peter Anvin (Intel) | ebb05a0 | 2018-12-11 12:30:25 -0800 | [diff] [blame] | 714 | struct hash_iterator it; |
| 715 | const struct hash_node *np; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 716 | |
H. Peter Anvin (Intel) | ebb05a0 | 2018-12-11 12:30:25 -0800 | [diff] [blame] | 717 | hash_for_each(mmt, it, np) { |
| 718 | MMacro *tmp; |
| 719 | MMacro *m = np->data; |
| 720 | nasm_free((void *)np->key); |
| 721 | list_for_each_safe(m, tmp, m) |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 722 | free_mmacro(m); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 723 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 724 | hash_free(mmt); |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | static void free_macros(void) |
| 728 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 729 | free_smacro_table(&smacros); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 730 | free_mmacro_table(&mmacros); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | /* |
| 734 | * Initialize the hash tables |
| 735 | */ |
| 736 | static void init_macros(void) |
| 737 | { |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 741 | * Pop the context stack. |
| 742 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 743 | static void ctx_pop(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 744 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 745 | Context *c = cstk; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 746 | |
| 747 | cstk = cstk->next; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 748 | free_smacro_table(&c->localmac); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 749 | nasm_free(c->name); |
| 750 | nasm_free(c); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 751 | } |
| 752 | |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 753 | /* |
| 754 | * Search for a key in the hash index; adding it if necessary |
| 755 | * (in which case we initialize the data pointer to NULL.) |
| 756 | */ |
| 757 | static void ** |
| 758 | hash_findi_add(struct hash_table *hash, const char *str) |
| 759 | { |
| 760 | struct hash_insert hi; |
| 761 | void **r; |
| 762 | char *strx; |
H. Peter Anvin (Intel) | ebb05a0 | 2018-12-11 12:30:25 -0800 | [diff] [blame] | 763 | size_t l = strlen(str) + 1; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 764 | |
H. Peter Anvin (Intel) | ebb05a0 | 2018-12-11 12:30:25 -0800 | [diff] [blame] | 765 | r = hash_findib(hash, str, l, &hi); |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 766 | if (r) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 767 | return r; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 768 | |
H. Peter Anvin (Intel) | ebb05a0 | 2018-12-11 12:30:25 -0800 | [diff] [blame] | 769 | strx = nasm_malloc(l); /* Use a more efficient allocator here? */ |
| 770 | memcpy(strx, str, l); |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 771 | return hash_add(&hi, strx, NULL); |
| 772 | } |
| 773 | |
| 774 | /* |
| 775 | * Like hash_findi, but returns the data element rather than a pointer |
| 776 | * to it. Used only when not adding a new element, hence no third |
| 777 | * argument. |
| 778 | */ |
| 779 | static void * |
| 780 | hash_findix(struct hash_table *hash, const char *str) |
| 781 | { |
| 782 | void **p; |
| 783 | |
| 784 | p = hash_findi(hash, str, NULL); |
| 785 | return p ? *p : NULL; |
| 786 | } |
| 787 | |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 788 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 789 | * read line from standart macros set, |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 790 | * if there no more left -- return NULL |
| 791 | */ |
| 792 | static char *line_from_stdmac(void) |
| 793 | { |
| 794 | unsigned char c; |
| 795 | const unsigned char *p = stdmacpos; |
| 796 | char *line, *q; |
| 797 | size_t len = 0; |
| 798 | |
| 799 | if (!stdmacpos) |
| 800 | return NULL; |
| 801 | |
| 802 | while ((c = *p++)) { |
| 803 | if (c >= 0x80) |
| 804 | len += pp_directives_len[c - 0x80] + 1; |
| 805 | else |
| 806 | len++; |
| 807 | } |
| 808 | |
| 809 | line = nasm_malloc(len + 1); |
| 810 | q = line; |
| 811 | while ((c = *stdmacpos++)) { |
| 812 | if (c >= 0x80) { |
| 813 | memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]); |
| 814 | q += pp_directives_len[c - 0x80]; |
| 815 | *q++ = ' '; |
| 816 | } else { |
| 817 | *q++ = c; |
| 818 | } |
| 819 | } |
| 820 | stdmacpos = p; |
| 821 | *q = '\0'; |
| 822 | |
| 823 | if (!*stdmacpos) { |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 824 | /* This was the last of this particular macro set */ |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 825 | stdmacpos = NULL; |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 826 | if (*stdmacnext) { |
| 827 | stdmacpos = *stdmacnext++; |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 828 | } else if (do_predef) { |
| 829 | Line *pd, *l; |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 830 | |
| 831 | /* |
| 832 | * Nasty hack: here we push the contents of |
| 833 | * `predef' on to the top-level expansion stack, |
| 834 | * since this is the most convenient way to |
| 835 | * implement the pre-include and pre-define |
| 836 | * features. |
| 837 | */ |
| 838 | list_for_each(pd, predef) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 839 | l = nasm_malloc(sizeof(Line)); |
| 840 | l->next = istk->expansion; |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 841 | l->first = dup_tlist(pd->first, NULL); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 842 | l->finishes = NULL; |
| 843 | |
| 844 | istk->expansion = l; |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 845 | } |
| 846 | do_predef = false; |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | return line; |
| 851 | } |
| 852 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 853 | static char *read_line(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 854 | { |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 855 | int c; |
| 856 | unsigned int size, next; |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 857 | const unsigned int delta = 512; |
| 858 | const unsigned int pad = 8; |
| 859 | unsigned int nr_cont = 0; |
| 860 | bool cont = false; |
| 861 | char *buffer, *p; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 862 | |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 863 | /* Standart macros set (predefined) goes first */ |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 864 | p = line_from_stdmac(); |
| 865 | if (p) |
| 866 | return p; |
H. Peter Anvin | 72edbb8 | 2008-06-19 16:00:04 -0700 | [diff] [blame] | 867 | |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 868 | size = delta; |
| 869 | p = buffer = nasm_malloc(size); |
| 870 | |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 871 | do { |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 872 | c = fgetc(istk->fp); |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 873 | |
| 874 | switch (c) { |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 875 | case EOF: |
| 876 | if (p == buffer) { |
| 877 | nasm_free(buffer); |
| 878 | return NULL; |
| 879 | } |
| 880 | c = 0; |
| 881 | break; |
| 882 | |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 883 | case '\r': |
| 884 | next = fgetc(istk->fp); |
| 885 | if (next != '\n') |
| 886 | ungetc(next, istk->fp); |
| 887 | if (cont) { |
| 888 | cont = false; |
| 889 | continue; |
| 890 | } |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 891 | c = 0; |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 892 | break; |
| 893 | |
| 894 | case '\n': |
| 895 | if (cont) { |
| 896 | cont = false; |
| 897 | continue; |
| 898 | } |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 899 | c = 0; |
| 900 | break; |
| 901 | |
| 902 | case 032: /* ^Z = legacy MS-DOS end of file mark */ |
| 903 | c = 0; |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 904 | break; |
| 905 | |
| 906 | case '\\': |
| 907 | next = fgetc(istk->fp); |
| 908 | ungetc(next, istk->fp); |
Cyrill Gorcunov | 490f85e | 2012-12-27 20:02:17 +0400 | [diff] [blame] | 909 | if (next == '\r' || next == '\n') { |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 910 | cont = true; |
| 911 | nr_cont++; |
| 912 | continue; |
| 913 | } |
| 914 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 915 | } |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 916 | |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 917 | if (p >= (buffer + size - pad)) { |
| 918 | buffer = nasm_realloc(buffer, size + delta); |
| 919 | p = buffer + size - pad; |
| 920 | size += delta; |
| 921 | } |
| 922 | |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 923 | *p++ = c; |
| 924 | } while (c); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 925 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 926 | src_set_linnum(src_get_linnum() + istk->lineinc + |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 927 | (nr_cont * istk->lineinc)); |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 928 | lfmt->line(LIST_READ, buffer); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 929 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 930 | return buffer; |
| 931 | } |
| 932 | |
| 933 | /* |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 934 | * 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] | 935 | * don't need to parse the value out of e.g. numeric tokens: we |
| 936 | * simply split one string into many. |
| 937 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 938 | static Token *tokenize(char *line) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 939 | { |
H. Peter Anvin (Intel) | 98031bf | 2019-08-09 16:11:28 -0700 | [diff] [blame^] | 940 | char c; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 941 | enum pp_token_type type; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 942 | Token *list = NULL; |
| 943 | Token *t, **tail = &list; |
| 944 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 945 | while (*line) { |
H. Peter Anvin (Intel) | 98031bf | 2019-08-09 16:11:28 -0700 | [diff] [blame^] | 946 | char *p = line; |
| 947 | char *ep = NULL; /* End of token, for trimming the end */ |
| 948 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 949 | if (*p == '%') { |
| 950 | p++; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 951 | if (*p == '+' && !nasm_isdigit(p[1])) { |
| 952 | p++; |
| 953 | type = TOK_PASTE; |
| 954 | } else if (nasm_isdigit(*p) || |
| 955 | ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 956 | do { |
| 957 | p++; |
| 958 | } |
H. Peter Anvin | bda7a6e | 2008-06-21 10:23:17 -0700 | [diff] [blame] | 959 | while (nasm_isdigit(*p)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 960 | type = TOK_PREPROC_ID; |
| 961 | } else if (*p == '{') { |
| 962 | p++; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 963 | while (*p) { |
| 964 | if (*p == '}') |
| 965 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 966 | p[-1] = *p; |
| 967 | p++; |
| 968 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 969 | if (*p != '}') |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 970 | nasm_warn(WARN_OTHER, "unterminated %%{ construct"); |
H. Peter Anvin (Intel) | 98031bf | 2019-08-09 16:11:28 -0700 | [diff] [blame^] | 971 | ep = &p[-1]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 972 | if (*p) |
| 973 | p++; |
| 974 | type = TOK_PREPROC_ID; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 975 | } else if (*p == '[') { |
| 976 | int lvl = 1; |
| 977 | line += 2; /* Skip the leading %[ */ |
| 978 | p++; |
| 979 | while (lvl && (c = *p++)) { |
| 980 | switch (c) { |
| 981 | case ']': |
| 982 | lvl--; |
| 983 | break; |
| 984 | case '%': |
| 985 | if (*p == '[') |
| 986 | lvl++; |
| 987 | break; |
| 988 | case '\'': |
| 989 | case '\"': |
| 990 | case '`': |
Cyrill Gorcunov | 3144e84 | 2017-10-22 10:50:55 +0300 | [diff] [blame] | 991 | p = nasm_skip_string(p - 1); |
| 992 | if (*p) |
| 993 | p++; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 994 | break; |
| 995 | default: |
| 996 | break; |
| 997 | } |
| 998 | } |
| 999 | p--; |
H. Peter Anvin (Intel) | 98031bf | 2019-08-09 16:11:28 -0700 | [diff] [blame^] | 1000 | ep = p; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1001 | if (*p) |
H. Peter Anvin (Intel) | 98031bf | 2019-08-09 16:11:28 -0700 | [diff] [blame^] | 1002 | p++; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1003 | if (lvl) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1004 | nasm_nonfatalf(ERR_PASS1, "unterminated %%[ construct"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1005 | type = TOK_INDIRECT; |
| 1006 | } else if (*p == '?') { |
| 1007 | type = TOK_PREPROC_Q; /* %? */ |
| 1008 | p++; |
| 1009 | if (*p == '?') { |
| 1010 | type = TOK_PREPROC_QQ; /* %?? */ |
| 1011 | p++; |
| 1012 | } |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1013 | } else if (*p == '!') { |
| 1014 | type = TOK_PREPROC_ID; |
| 1015 | p++; |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 1016 | if (nasm_isidchar(*p)) { |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1017 | do { |
| 1018 | p++; |
| 1019 | } |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 1020 | while (nasm_isidchar(*p)); |
H. Peter Anvin | 53e2e4c | 2018-11-28 15:01:40 -0800 | [diff] [blame] | 1021 | } else if (nasm_isquote(*p)) { |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1022 | p = nasm_skip_string(p); |
| 1023 | if (*p) |
| 1024 | p++; |
| 1025 | else |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1026 | nasm_nonfatalf(ERR_PASS1, "unterminated %%! string"); |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1027 | } else { |
| 1028 | /* %! without string or identifier */ |
| 1029 | type = TOK_OTHER; /* Legacy behavior... */ |
| 1030 | } |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 1031 | } else if (nasm_isidchar(*p) || |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1032 | ((*p == '!' || *p == '%' || *p == '$') && |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 1033 | nasm_isidchar(p[1]))) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1034 | do { |
| 1035 | p++; |
| 1036 | } |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 1037 | while (nasm_isidchar(*p)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1038 | type = TOK_PREPROC_ID; |
| 1039 | } else { |
| 1040 | type = TOK_OTHER; |
| 1041 | if (*p == '%') |
| 1042 | p++; |
| 1043 | } |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 1044 | } else if (nasm_isidstart(*p) || (*p == '$' && nasm_isidstart(p[1]))) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1045 | type = TOK_ID; |
| 1046 | p++; |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 1047 | while (*p && nasm_isidchar(*p)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1048 | p++; |
H. Peter Anvin | 53e2e4c | 2018-11-28 15:01:40 -0800 | [diff] [blame] | 1049 | } else if (nasm_isquote(*p)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1050 | /* |
| 1051 | * A string token. |
| 1052 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1053 | type = TOK_STRING; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1054 | p = nasm_skip_string(p); |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1055 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1056 | if (*p) { |
| 1057 | p++; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1058 | } else { |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 1059 | nasm_warn(WARN_OTHER, "unterminated string"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1060 | /* Handling unterminated strings by UNV */ |
| 1061 | /* type = -1; */ |
| 1062 | } |
Victor van den Elzen | fb5f251 | 2009-04-17 16:17:59 +0200 | [diff] [blame] | 1063 | } else if (p[0] == '$' && p[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1064 | type = TOK_OTHER; /* TOKEN_BASE */ |
Victor van den Elzen | fb5f251 | 2009-04-17 16:17:59 +0200 | [diff] [blame] | 1065 | p += 2; |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 1066 | } else if (nasm_isnumstart(*p)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1067 | bool is_hex = false; |
| 1068 | bool is_float = false; |
| 1069 | bool has_e = false; |
| 1070 | char c, *r; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1071 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1072 | /* |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1073 | * A numeric token. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1074 | */ |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1075 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1076 | if (*p == '$') { |
| 1077 | p++; |
| 1078 | is_hex = true; |
| 1079 | } |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1080 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1081 | for (;;) { |
| 1082 | c = *p++; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1083 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1084 | if (!is_hex && (c == 'e' || c == 'E')) { |
| 1085 | has_e = true; |
| 1086 | if (*p == '+' || *p == '-') { |
| 1087 | /* |
| 1088 | * e can only be followed by +/- if it is either a |
| 1089 | * prefixed hex number or a floating-point number |
| 1090 | */ |
| 1091 | p++; |
| 1092 | is_float = true; |
| 1093 | } |
| 1094 | } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') { |
| 1095 | is_hex = true; |
| 1096 | } else if (c == 'P' || c == 'p') { |
| 1097 | is_float = true; |
| 1098 | if (*p == '+' || *p == '-') |
| 1099 | p++; |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 1100 | } else if (nasm_isnumchar(c)) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1101 | ; /* just advance */ |
| 1102 | else if (c == '.') { |
| 1103 | /* |
| 1104 | * we need to deal with consequences of the legacy |
| 1105 | * parser, like "1.nolist" being two tokens |
| 1106 | * (TOK_NUMBER, TOK_ID) here; at least give it |
| 1107 | * a shot for now. In the future, we probably need |
| 1108 | * a flex-based scanner with proper pattern matching |
| 1109 | * to do it as well as it can be done. Nothing in |
| 1110 | * the world is going to help the person who wants |
| 1111 | * 0x123.p16 interpreted as two tokens, though. |
| 1112 | */ |
| 1113 | r = p; |
| 1114 | while (*r == '_') |
| 1115 | r++; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1116 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1117 | if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) || |
| 1118 | (!is_hex && (*r == 'e' || *r == 'E')) || |
| 1119 | (*r == 'p' || *r == 'P')) { |
| 1120 | p = r; |
| 1121 | is_float = true; |
| 1122 | } else |
| 1123 | break; /* Terminate the token */ |
| 1124 | } else |
| 1125 | break; |
| 1126 | } |
| 1127 | p--; /* Point to first character beyond number */ |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1128 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1129 | if (p == line+1 && *line == '$') { |
| 1130 | type = TOK_OTHER; /* TOKEN_HERE */ |
| 1131 | } else { |
| 1132 | if (has_e && !is_hex) { |
| 1133 | /* 1e13 is floating-point, but 1e13h is not */ |
| 1134 | is_float = true; |
| 1135 | } |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 1136 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1137 | type = is_float ? TOK_FLOAT : TOK_NUMBER; |
| 1138 | } |
H. Peter Anvin | bda7a6e | 2008-06-21 10:23:17 -0700 | [diff] [blame] | 1139 | } else if (nasm_isspace(*p)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1140 | type = TOK_WHITESPACE; |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 1141 | p = nasm_skip_spaces(p); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1142 | /* |
| 1143 | * Whitespace just before end-of-line is discarded by |
| 1144 | * pretending it's a comment; whitespace just before a |
| 1145 | * comment gets lumped into the comment. |
| 1146 | */ |
| 1147 | if (!*p || *p == ';') { |
| 1148 | type = TOK_COMMENT; |
| 1149 | while (*p) |
| 1150 | p++; |
| 1151 | } |
| 1152 | } else if (*p == ';') { |
| 1153 | type = TOK_COMMENT; |
| 1154 | while (*p) |
| 1155 | p++; |
| 1156 | } else { |
| 1157 | /* |
| 1158 | * Anything else is an operator of some kind. We check |
| 1159 | * for all the double-character operators (>>, <<, //, |
| 1160 | * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1161 | * else is a single-character operator. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1162 | */ |
| 1163 | type = TOK_OTHER; |
| 1164 | if ((p[0] == '>' && p[1] == '>') || |
| 1165 | (p[0] == '<' && p[1] == '<') || |
| 1166 | (p[0] == '/' && p[1] == '/') || |
| 1167 | (p[0] == '<' && p[1] == '=') || |
| 1168 | (p[0] == '>' && p[1] == '=') || |
| 1169 | (p[0] == '=' && p[1] == '=') || |
| 1170 | (p[0] == '!' && p[1] == '=') || |
| 1171 | (p[0] == '<' && p[1] == '>') || |
| 1172 | (p[0] == '&' && p[1] == '&') || |
| 1173 | (p[0] == '|' && p[1] == '|') || |
| 1174 | (p[0] == '^' && p[1] == '^')) { |
| 1175 | p++; |
| 1176 | } |
| 1177 | p++; |
| 1178 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1179 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1180 | /* Handling unterminated string by UNV */ |
| 1181 | /*if (type == -1) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1182 | { |
| 1183 | *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1); |
| 1184 | t->text[p-line] = *line; |
| 1185 | tail = &t->next; |
| 1186 | } |
| 1187 | else */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1188 | if (type != TOK_COMMENT) { |
H. Peter Anvin (Intel) | 98031bf | 2019-08-09 16:11:28 -0700 | [diff] [blame^] | 1189 | if (!ep) |
| 1190 | ep = p; |
| 1191 | *tail = t = new_Token(NULL, type, line, ep - line); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1192 | tail = &t->next; |
| 1193 | } |
| 1194 | line = p; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1195 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1196 | return list; |
| 1197 | } |
| 1198 | |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1199 | /* |
| 1200 | * this function allocates a new managed block of memory and |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1201 | * returns a pointer to the block. The managed blocks are |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1202 | * deleted only all at once by the delete_Blocks function. |
| 1203 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1204 | static void *new_Block(size_t size) |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1205 | { |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1206 | Blocks *b = &blocks; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1207 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1208 | /* first, get to the end of the linked list */ |
| 1209 | while (b->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1210 | b = b->next; |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1211 | /* now allocate the requested chunk */ |
| 1212 | b->chunk = nasm_malloc(size); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1213 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1214 | /* now allocate a new block for the next request */ |
Cyrill Gorcunov | c31767c | 2014-06-28 02:22:17 +0400 | [diff] [blame] | 1215 | b->next = nasm_zalloc(sizeof(Blocks)); |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1216 | return b->chunk; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | /* |
| 1220 | * this function deletes all managed blocks of memory |
| 1221 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1222 | static void delete_Blocks(void) |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1223 | { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1224 | Blocks *a, *b = &blocks; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1225 | |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1226 | /* |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1227 | * keep in mind that the first block, pointed to by blocks |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1228 | * is a static and not dynamically allocated, so we don't |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1229 | * free it. |
| 1230 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1231 | while (b) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1232 | if (b->chunk) |
| 1233 | nasm_free(b->chunk); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1234 | a = b; |
| 1235 | b = b->next; |
| 1236 | if (a != &blocks) |
| 1237 | nasm_free(a); |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1238 | } |
Cyrill Gorcunov | dae24d7 | 2014-06-28 10:17:39 +0400 | [diff] [blame] | 1239 | memset(&blocks, 0, sizeof(blocks)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1240 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1241 | |
| 1242 | /* |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1243 | * this function creates a new Token and passes a pointer to it |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 1244 | * back to the caller. It sets the type, text, and next pointer elements. |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1245 | */ |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1246 | static Token *new_Token(Token * next, enum pp_token_type type, |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 1247 | const char *text, size_t txtlen) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1248 | { |
| 1249 | Token *t; |
| 1250 | int i; |
| 1251 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1252 | if (!freeTokens) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1253 | freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token)); |
| 1254 | for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++) |
| 1255 | freeTokens[i].next = &freeTokens[i + 1]; |
| 1256 | freeTokens[i].next = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1257 | } |
| 1258 | t = freeTokens; |
| 1259 | freeTokens = t->next; |
| 1260 | t->next = next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1261 | t->type = type; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1262 | if (type == TOK_WHITESPACE || !text) { |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 1263 | t->len = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1264 | t->text = NULL; |
| 1265 | } else { |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 1266 | if (txtlen == 0 && text[0]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1267 | txtlen = strlen(text); |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 1268 | t->len = txtlen; |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1269 | t->text = nasm_malloc(txtlen+1); |
| 1270 | memcpy(t->text, text, txtlen); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1271 | t->text[txtlen] = '\0'; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1272 | } |
| 1273 | return t; |
| 1274 | } |
| 1275 | |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 1276 | static Token *dup_Token(Token *next, const Token *src) |
| 1277 | { |
| 1278 | return new_Token(next, src->type, src->text, src->len); |
| 1279 | } |
| 1280 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1281 | static Token *delete_Token(Token * t) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1282 | { |
| 1283 | Token *next = t->next; |
| 1284 | nasm_free(t->text); |
H. Peter Anvin | 788e6c1 | 2002-04-30 21:02:01 +0000 | [diff] [blame] | 1285 | t->next = freeTokens; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1286 | freeTokens = t; |
| 1287 | return next; |
| 1288 | } |
| 1289 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1290 | /* |
| 1291 | * Convert a line of tokens back into text. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1292 | * If expand_locals is not zero, identifiers of the form "%$*xxx" |
| 1293 | * will be transformed into ..@ctxnum.xxx |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1294 | */ |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 1295 | static char *detoken(Token * tlist, bool expand_locals) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1296 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1297 | Token *t; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1298 | char *line, *p; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1299 | int len = 0; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1300 | |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1301 | list_for_each(t, tlist) { |
Cyrill Gorcunov | 9b7ee09 | 2017-10-22 21:42:59 +0300 | [diff] [blame] | 1302 | if (t->type == TOK_PREPROC_ID && t->text && |
H. Peter Anvin (Intel) | 98031bf | 2019-08-09 16:11:28 -0700 | [diff] [blame^] | 1303 | t->text[0] == '%' && t->text[1] == '!') { |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1304 | char *v; |
| 1305 | char *q = t->text; |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1306 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1307 | v = t->text + 2; |
H. Peter Anvin (Intel) | 98031bf | 2019-08-09 16:11:28 -0700 | [diff] [blame^] | 1308 | if (nasm_isquote(*v)) |
| 1309 | nasm_unquote_cstr(v, NULL); |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1310 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1311 | if (v) { |
| 1312 | char *p = getenv(v); |
| 1313 | if (!p) { |
H. Peter Anvin (Intel) | 98031bf | 2019-08-09 16:11:28 -0700 | [diff] [blame^] | 1314 | /*! |
| 1315 | *!environment [on] nonexistent environment variable |
| 1316 | *! warns if a nonexistent environment variable |
| 1317 | *! is accessed using the \c{%!} preprocessor |
| 1318 | *! construct (see \k{getenv}.) Such environment |
| 1319 | *! variables are treated as empty (with this |
| 1320 | *! warning issued) starting in NASM 2.15; |
| 1321 | *! earlier versions of NASM would treat this as |
| 1322 | *! an error. |
Cyrill Gorcunov | bbb7a1a | 2016-06-19 12:15:24 +0300 | [diff] [blame] | 1323 | */ |
H. Peter Anvin (Intel) | 98031bf | 2019-08-09 16:11:28 -0700 | [diff] [blame^] | 1324 | nasm_warn(WARN_ENVIRONMENT, "nonexistent environment variable `%s'", v); |
| 1325 | p = ""; |
| 1326 | } |
| 1327 | t->text = nasm_strdup(p); |
| 1328 | t->len = nasm_last_string_len(); |
Cyrill Gorcunov | 7500487 | 2017-07-26 01:21:16 +0300 | [diff] [blame] | 1329 | nasm_free(q); |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1330 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1331 | } |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1332 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1333 | /* Expand local macros here and not during preprocessing */ |
| 1334 | if (expand_locals && |
| 1335 | t->type == TOK_PREPROC_ID && t->text && |
| 1336 | t->text[0] == '%' && t->text[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1337 | const char *q; |
| 1338 | char *p; |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1339 | Context *ctx = get_ctx(t->text, &q); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1340 | if (ctx) { |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 1341 | p = nasm_asprintf("..@%"PRIu64".%s", ctx->number, q); |
| 1342 | t->len = nasm_last_string_len(); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1343 | nasm_free(t->text); |
| 1344 | t->text = p; |
| 1345 | } |
| 1346 | } |
H. Peter Anvin (Intel) | 98031bf | 2019-08-09 16:11:28 -0700 | [diff] [blame^] | 1347 | if (t->text) { |
| 1348 | if (debug_level(2)) { |
| 1349 | unsigned long t_len = t->len; |
| 1350 | unsigned long s_len = strlen(t->text); |
| 1351 | if (t_len != s_len) { |
| 1352 | nasm_panic("assertion failed: token \"%s\" type %u len %lu has t->len %lu\n", |
| 1353 | t->text, t->type, s_len, t_len); |
| 1354 | t->len = s_len; |
| 1355 | } |
| 1356 | } |
| 1357 | len += t->len; |
| 1358 | } else if (t->type == TOK_WHITESPACE) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1359 | len++; |
H. Peter Anvin (Intel) | 98031bf | 2019-08-09 16:11:28 -0700 | [diff] [blame^] | 1360 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1361 | } |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1362 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1363 | p = line = nasm_malloc(len + 1); |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1364 | |
| 1365 | list_for_each(t, tlist) { |
H. Peter Anvin (Intel) | 98031bf | 2019-08-09 16:11:28 -0700 | [diff] [blame^] | 1366 | if (t->text) { |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 1367 | memcpy(p, t->text, t->len); |
| 1368 | p += t->len; |
H. Peter Anvin (Intel) | 98031bf | 2019-08-09 16:11:28 -0700 | [diff] [blame^] | 1369 | } else if (t->type == TOK_WHITESPACE) { |
| 1370 | *p++ = ' '; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1371 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1372 | } |
| 1373 | *p = '\0'; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1374 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1375 | return line; |
| 1376 | } |
| 1377 | |
| 1378 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1379 | * A scanner, suitable for use by the expression evaluator, which |
| 1380 | * operates on a line of Tokens. Expects a pointer to a pointer to |
| 1381 | * the first token in the line to be passed in as its private_data |
| 1382 | * field. |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1383 | * |
| 1384 | * FIX: This really needs to be unified with stdscan. |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1385 | */ |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 1386 | struct ppscan { |
| 1387 | Token *tptr; |
| 1388 | int ntokens; |
| 1389 | }; |
| 1390 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1391 | static int ppscan(void *private_data, struct tokenval *tokval) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1392 | { |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 1393 | struct ppscan *pps = private_data; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1394 | Token *tline; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1395 | char ourcopy[MAX_KEYWORD+1], *p, *r, *s; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1396 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1397 | do { |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 1398 | if (pps->ntokens && (tline = pps->tptr)) { |
| 1399 | pps->ntokens--; |
| 1400 | pps->tptr = tline->next; |
| 1401 | } else { |
| 1402 | pps->tptr = NULL; |
| 1403 | pps->ntokens = 0; |
| 1404 | return tokval->t_type = TOKEN_EOS; |
| 1405 | } |
| 1406 | } while (tline->type == TOK_WHITESPACE || tline->type == TOK_COMMENT); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1407 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1408 | tokval->t_charptr = tline->text; |
| 1409 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1410 | if (tline->text[0] == '$' && !tline->text[1]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1411 | return tokval->t_type = TOKEN_HERE; |
H. Peter Anvin | 7cf897e | 2002-05-30 21:30:33 +0000 | [diff] [blame] | 1412 | if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1413 | return tokval->t_type = TOKEN_BASE; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1414 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1415 | if (tline->type == TOK_ID) { |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1416 | p = tokval->t_charptr = tline->text; |
| 1417 | if (p[0] == '$') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1418 | tokval->t_charptr++; |
| 1419 | return tokval->t_type = TOKEN_ID; |
| 1420 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1421 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1422 | for (r = p, s = ourcopy; *r; r++) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1423 | if (r >= p+MAX_KEYWORD) |
| 1424 | return tokval->t_type = TOKEN_ID; /* Not a keyword */ |
H. Peter Anvin | ac8f8fc | 2008-06-11 15:49:41 -0700 | [diff] [blame] | 1425 | *s++ = nasm_tolower(*r); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1426 | } |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1427 | *s = '\0'; |
| 1428 | /* right, so we have an identifier sitting in temp storage. now, |
| 1429 | * is it actually a register or instruction name, or what? */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1430 | return nasm_token_hash(ourcopy, tokval); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1433 | if (tline->type == TOK_NUMBER) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1434 | bool rn_error; |
| 1435 | tokval->t_integer = readnum(tline->text, &rn_error); |
| 1436 | tokval->t_charptr = tline->text; |
| 1437 | if (rn_error) |
| 1438 | return tokval->t_type = TOKEN_ERRNUM; |
| 1439 | else |
| 1440 | return tokval->t_type = TOKEN_NUM; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1441 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1442 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1443 | if (tline->type == TOK_FLOAT) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1444 | return tokval->t_type = TOKEN_FLOAT; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1447 | if (tline->type == TOK_STRING) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1448 | char bq, *ep; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1449 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1450 | bq = tline->text[0]; |
H. Peter Anvin | 1162704 | 2008-06-09 20:45:19 -0700 | [diff] [blame] | 1451 | tokval->t_charptr = tline->text; |
| 1452 | tokval->t_inttwo = nasm_unquote(tline->text, &ep); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 1453 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1454 | if (ep[0] != bq || ep[1] != '\0') |
| 1455 | return tokval->t_type = TOKEN_ERRSTR; |
| 1456 | else |
| 1457 | return tokval->t_type = TOKEN_STR; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1458 | } |
| 1459 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1460 | if (tline->type == TOK_OTHER) { |
| 1461 | if (!strcmp(tline->text, "<<")) |
| 1462 | return tokval->t_type = TOKEN_SHL; |
| 1463 | if (!strcmp(tline->text, ">>")) |
| 1464 | return tokval->t_type = TOKEN_SHR; |
| 1465 | if (!strcmp(tline->text, "//")) |
| 1466 | return tokval->t_type = TOKEN_SDIV; |
| 1467 | if (!strcmp(tline->text, "%%")) |
| 1468 | return tokval->t_type = TOKEN_SMOD; |
| 1469 | if (!strcmp(tline->text, "==")) |
| 1470 | return tokval->t_type = TOKEN_EQ; |
| 1471 | if (!strcmp(tline->text, "<>")) |
| 1472 | return tokval->t_type = TOKEN_NE; |
| 1473 | if (!strcmp(tline->text, "!=")) |
| 1474 | return tokval->t_type = TOKEN_NE; |
| 1475 | if (!strcmp(tline->text, "<=")) |
| 1476 | return tokval->t_type = TOKEN_LE; |
| 1477 | if (!strcmp(tline->text, ">=")) |
| 1478 | return tokval->t_type = TOKEN_GE; |
| 1479 | if (!strcmp(tline->text, "&&")) |
| 1480 | return tokval->t_type = TOKEN_DBL_AND; |
| 1481 | if (!strcmp(tline->text, "^^")) |
| 1482 | return tokval->t_type = TOKEN_DBL_XOR; |
| 1483 | if (!strcmp(tline->text, "||")) |
| 1484 | return tokval->t_type = TOKEN_DBL_OR; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |
| 1487 | /* |
| 1488 | * We have no other options: just return the first character of |
| 1489 | * the token text. |
| 1490 | */ |
| 1491 | return tokval->t_type = tline->text[0]; |
| 1492 | } |
| 1493 | |
| 1494 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1495 | * Compare a string to the name of an existing macro; this is a |
| 1496 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 1497 | * depending on the value of the `casesense' parameter. |
| 1498 | */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 1499 | static int mstrcmp(const char *p, const char *q, bool casesense) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1500 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1501 | return casesense ? strcmp(p, q) : nasm_stricmp(p, q); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1502 | } |
| 1503 | |
| 1504 | /* |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1505 | * Compare a string to the name of an existing macro; this is a |
| 1506 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 1507 | * depending on the value of the `casesense' parameter. |
| 1508 | */ |
| 1509 | static int mmemcmp(const char *p, const char *q, size_t l, bool casesense) |
| 1510 | { |
| 1511 | return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l); |
| 1512 | } |
| 1513 | |
| 1514 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1515 | * Return the Context structure associated with a %$ token. Return |
| 1516 | * NULL, having _already_ reported an error condition, if the |
| 1517 | * context stack isn't deep enough for the supplied number of $ |
| 1518 | * signs. |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1519 | * |
| 1520 | * If "namep" is non-NULL, set it to the pointer to the macro name |
| 1521 | * tail, i.e. the part beyond %$... |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1522 | */ |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1523 | static Context *get_ctx(const char *name, const char **namep) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1524 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1525 | Context *ctx; |
| 1526 | int i; |
| 1527 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1528 | if (namep) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1529 | *namep = name; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1530 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1531 | if (!name || name[0] != '%' || name[1] != '$') |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1532 | return NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1533 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1534 | if (!cstk) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1535 | nasm_nonfatal("`%s': context stack is empty", name); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1536 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1539 | name += 2; |
| 1540 | ctx = cstk; |
| 1541 | i = 0; |
| 1542 | while (ctx && *name == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1543 | name++; |
| 1544 | i++; |
| 1545 | ctx = ctx->next; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1546 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1547 | if (!ctx) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1548 | nasm_nonfatal("`%s': context stack is only" |
| 1549 | " %d level%s deep", name, i, (i == 1 ? "" : "s")); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1550 | return NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1551 | } |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1552 | |
| 1553 | if (namep) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1554 | *namep = name; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1555 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1556 | return ctx; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1557 | } |
| 1558 | |
| 1559 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1560 | * Open an include file. This routine must always return a valid |
| 1561 | * file pointer if it returns - it's responsible for throwing an |
| 1562 | * ERR_FATAL and bombing out completely if not. It should also try |
| 1563 | * the include path one by one until it finds the file or reaches |
| 1564 | * the end of the path. |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1565 | * |
| 1566 | * Note: for INC_PROBE the function returns NULL at all times; |
| 1567 | * instead look for the |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1568 | */ |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1569 | enum incopen_mode { |
| 1570 | INC_NEEDED, /* File must exist */ |
| 1571 | INC_OPTIONAL, /* Missing is OK */ |
| 1572 | INC_PROBE /* Only an existence probe */ |
| 1573 | }; |
| 1574 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1575 | /* This is conducts a full pathname search */ |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1576 | static FILE *inc_fopen_search(const char *file, char **slpath, |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1577 | enum incopen_mode omode, enum file_flags fmode) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1578 | { |
H. Peter Anvin (Intel) | 6447109 | 2018-12-11 13:06:14 -0800 | [diff] [blame] | 1579 | const struct strlist_entry *ip = strlist_head(ipath_list); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1580 | FILE *fp; |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1581 | const char *prefix = ""; |
night199uk | fdb1a1b | 2018-10-18 23:19:47 +0200 | [diff] [blame] | 1582 | char *sp; |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1583 | bool found; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1584 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1585 | while (1) { |
night199uk | fdb1a1b | 2018-10-18 23:19:47 +0200 | [diff] [blame] | 1586 | sp = nasm_catfile(prefix, file); |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1587 | if (omode == INC_PROBE) { |
| 1588 | fp = NULL; |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1589 | found = nasm_file_exists(sp); |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1590 | } else { |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1591 | fp = nasm_open_read(sp, fmode); |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1592 | found = (fp != NULL); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1593 | } |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1594 | if (found) { |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1595 | *slpath = sp; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1596 | return fp; |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1597 | } |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 1598 | |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1599 | nasm_free(sp); |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 1600 | |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1601 | if (!ip) { |
| 1602 | *slpath = NULL; |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1603 | return NULL; |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1604 | } |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1605 | |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1606 | prefix = ip->str; |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1607 | ip = ip->next; |
| 1608 | } |
| 1609 | } |
| 1610 | |
| 1611 | /* |
| 1612 | * Open a file, or test for the presence of one (depending on omode), |
| 1613 | * considering the include path. |
| 1614 | */ |
| 1615 | static FILE *inc_fopen(const char *file, |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 1616 | struct strlist *dhead, |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 1617 | const char **found_path, |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1618 | enum incopen_mode omode, |
| 1619 | enum file_flags fmode) |
| 1620 | { |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1621 | struct hash_insert hi; |
| 1622 | void **hp; |
| 1623 | char *path; |
| 1624 | FILE *fp = NULL; |
| 1625 | |
| 1626 | hp = hash_find(&FileHash, file, &hi); |
| 1627 | if (hp) { |
| 1628 | path = *hp; |
Martin Storsjö | f283c8f | 2017-08-13 17:28:46 +0300 | [diff] [blame] | 1629 | if (path || omode != INC_NEEDED) { |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 1630 | strlist_add(dhead, path ? path : file); |
Martin Storsjö | f283c8f | 2017-08-13 17:28:46 +0300 | [diff] [blame] | 1631 | } |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1632 | } else { |
| 1633 | /* Need to do the actual path search */ |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1634 | fp = inc_fopen_search(file, &path, omode, fmode); |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1635 | |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1636 | /* Positive or negative result */ |
| 1637 | hash_add(&hi, nasm_strdup(file), path); |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1638 | |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 1639 | /* |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1640 | * Add file to dependency path. |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 1641 | */ |
| 1642 | if (path || omode != INC_NEEDED) |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 1643 | strlist_add(dhead, file); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1644 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1645 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1646 | if (!path) { |
| 1647 | if (omode == INC_NEEDED) |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 1648 | nasm_fatal("unable to open include file `%s'", file); |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1649 | } else { |
| 1650 | if (!fp && omode != INC_PROBE) |
| 1651 | fp = nasm_open_read(path, fmode); |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1652 | } |
| 1653 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1654 | if (found_path) |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 1655 | *found_path = path; |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1656 | |
| 1657 | return fp; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
| 1660 | /* |
Fabian Giesen | 0bbc38d | 2016-04-28 13:48:14 -0700 | [diff] [blame] | 1661 | * Opens an include or input file. Public version, for use by modules |
| 1662 | * that get a file:lineno pair and need to look at the file again |
| 1663 | * (e.g. the CodeView debug backend). Returns NULL on failure. |
| 1664 | */ |
H. Peter Anvin | 3e83cec | 2016-05-25 04:28:46 -0700 | [diff] [blame] | 1665 | FILE *pp_input_fopen(const char *filename, enum file_flags mode) |
Fabian Giesen | 0bbc38d | 2016-04-28 13:48:14 -0700 | [diff] [blame] | 1666 | { |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 1667 | return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode); |
Fabian Giesen | 0bbc38d | 2016-04-28 13:48:14 -0700 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1671 | * 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] | 1672 | * 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] | 1673 | * return true if _any_ single-line macro of that name is defined. |
| 1674 | * Otherwise, will return true if a single-line macro with either |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1675 | * `nparam' or no parameters is defined. |
| 1676 | * |
| 1677 | * If a macro with precisely the right number of parameters is |
H. Peter Anvin | ef7468f | 2002-04-30 20:57:59 +0000 | [diff] [blame] | 1678 | * defined, or nparam is -1, the address of the definition structure |
| 1679 | * 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] | 1680 | * is NULL, no action will be taken regarding its contents, and no |
| 1681 | * error will occur. |
| 1682 | * |
| 1683 | * Note that this is also called with nparam zero to resolve |
| 1684 | * `ifdef'. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1685 | * |
| 1686 | * If you already know which context macro belongs to, you can pass |
| 1687 | * the context pointer as first parameter; if you won't but name begins |
| 1688 | * with %$ the context will be automatically computed. If all_contexts |
| 1689 | * is true, macro will be searched in outer contexts as well. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1690 | */ |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1691 | static bool |
H. Peter Anvin | b2a5fda | 2008-06-19 21:42:42 -0700 | [diff] [blame] | 1692 | smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn, |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1693 | bool nocase) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1694 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1695 | struct hash_table *smtbl; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1696 | SMacro *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1697 | |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1698 | if (ctx) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1699 | smtbl = &ctx->localmac; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1700 | } else if (name[0] == '%' && name[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1701 | if (cstk) |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1702 | ctx = get_ctx(name, &name); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1703 | if (!ctx) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1704 | return false; /* got to return _something_ */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1705 | smtbl = &ctx->localmac; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1706 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1707 | smtbl = &smacros; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1708 | } |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1709 | m = (SMacro *) hash_findix(smtbl, name); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1710 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1711 | while (m) { |
| 1712 | if (!mstrcmp(m->name, name, m->casesense && nocase) && |
Charles Crayne | 192d5b5 | 2007-10-18 19:02:42 -0700 | [diff] [blame] | 1713 | (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1714 | if (defn) { |
Charles Crayne | 192d5b5 | 2007-10-18 19:02:42 -0700 | [diff] [blame] | 1715 | if (nparam == (int) m->nparam || nparam == -1) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1716 | *defn = m; |
| 1717 | else |
| 1718 | *defn = NULL; |
| 1719 | } |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1720 | return true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1721 | } |
| 1722 | m = m->next; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1723 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1724 | |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1725 | return false; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1726 | } |
| 1727 | |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 1728 | /* param should be a natural number [0; INT_MAX] */ |
| 1729 | static int read_param_count(const char *str) |
| 1730 | { |
| 1731 | int result; |
| 1732 | bool err; |
| 1733 | |
| 1734 | result = readnum(str, &err); |
| 1735 | if (result < 0 || result > INT_MAX) { |
| 1736 | result = 0; |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1737 | nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]", |
| 1738 | str, 0, INT_MAX); |
| 1739 | } else if (err) |
| 1740 | nasm_nonfatal("unable to parse parameter count `%s'", str); |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 1741 | return result; |
| 1742 | } |
| 1743 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1744 | /* |
| 1745 | * Count and mark off the parameters in a multi-line macro call. |
| 1746 | * This is called both from within the multi-line macro expansion |
| 1747 | * code, and also to mark off the default parameters when provided |
| 1748 | * in a %macro definition line. |
| 1749 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1750 | static void count_mmac_params(Token * t, int *nparam, Token *** params) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1751 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1752 | int paramsize, brace; |
| 1753 | |
| 1754 | *nparam = paramsize = 0; |
| 1755 | *params = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1756 | while (t) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1757 | /* +1: we need space for the final NULL */ |
H. Peter Anvin | 91fb6f1 | 2008-09-01 10:56:33 -0700 | [diff] [blame] | 1758 | if (*nparam+1 >= paramsize) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1759 | paramsize += PARAM_DELTA; |
| 1760 | *params = nasm_realloc(*params, sizeof(**params) * paramsize); |
| 1761 | } |
| 1762 | skip_white_(t); |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1763 | brace = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1764 | if (tok_is_(t, "{")) |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1765 | brace++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1766 | (*params)[(*nparam)++] = t; |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1767 | if (brace) { |
| 1768 | while (brace && (t = t->next) != NULL) { |
| 1769 | if (tok_is_(t, "{")) |
| 1770 | brace++; |
| 1771 | else if (tok_is_(t, "}")) |
| 1772 | brace--; |
| 1773 | } |
| 1774 | |
| 1775 | if (t) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1776 | /* |
| 1777 | * Now we've found the closing brace, look further |
| 1778 | * for the comma. |
| 1779 | */ |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1780 | t = t->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1781 | skip_white_(t); |
| 1782 | if (tok_isnt_(t, ",")) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1783 | nasm_nonfatal("braces do not enclose all of macro parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1784 | while (tok_isnt_(t, ",")) |
| 1785 | t = t->next; |
| 1786 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1787 | } |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1788 | } else { |
| 1789 | while (tok_isnt_(t, ",")) |
| 1790 | t = t->next; |
| 1791 | } |
| 1792 | if (t) { /* got a comma/brace */ |
| 1793 | t = t->next; /* eat the comma */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1794 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1795 | } |
| 1796 | } |
| 1797 | |
| 1798 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1799 | * Determine whether one of the various `if' conditions is true or |
| 1800 | * not. |
| 1801 | * |
| 1802 | * We must free the tline we get passed. |
| 1803 | */ |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1804 | static bool if_condition(Token * tline, enum preproc_token ct) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1805 | { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1806 | enum pp_conditional i = PP_COND(ct); |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1807 | bool j; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 1808 | Token *t, *tt, *origline; |
| 1809 | struct ppscan pps; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1810 | struct tokenval tokval; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1811 | expr *evalresult; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1812 | enum pp_token_type needtype; |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1813 | char *p; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1814 | |
| 1815 | origline = tline; |
| 1816 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1817 | switch (i) { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1818 | case PPC_IFCTX: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1819 | j = false; /* have we matched yet? */ |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1820 | while (true) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1821 | skip_white_(tline); |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1822 | if (!tline) |
| 1823 | break; |
| 1824 | if (tline->type != TOK_ID) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1825 | nasm_nonfatal("`%s' expects context identifiers", |
| 1826 | pp_directives[ct]); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1827 | free_tlist(origline); |
| 1828 | return -1; |
| 1829 | } |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1830 | if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1831 | j = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1832 | tline = tline->next; |
| 1833 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1834 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1835 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1836 | case PPC_IFDEF: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1837 | j = false; /* have we matched yet? */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1838 | while (tline) { |
| 1839 | skip_white_(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1840 | if (!tline || (tline->type != TOK_ID && |
| 1841 | (tline->type != TOK_PREPROC_ID || |
| 1842 | tline->text[1] != '$'))) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1843 | nasm_nonfatal("`%s' expects macro identifiers", |
| 1844 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1845 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1846 | } |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1847 | if (smacro_defined(NULL, tline->text, 0, NULL, true)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1848 | j = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1849 | tline = tline->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1850 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1851 | break; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1852 | |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1853 | case PPC_IFENV: |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1854 | tline = expand_smacro(tline); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1855 | j = false; /* have we matched yet? */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1856 | while (tline) { |
| 1857 | skip_white_(tline); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1858 | if (!tline || (tline->type != TOK_ID && |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1859 | tline->type != TOK_STRING && |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1860 | (tline->type != TOK_PREPROC_ID || |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1861 | tline->text[1] != '!'))) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1862 | nasm_nonfatal("`%s' expects environment variable names", |
| 1863 | pp_directives[ct]); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1864 | goto fail; |
| 1865 | } |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1866 | p = tline->text; |
| 1867 | if (tline->type == TOK_PREPROC_ID) |
| 1868 | p += 2; /* Skip leading %! */ |
H. Peter Anvin | 53e2e4c | 2018-11-28 15:01:40 -0800 | [diff] [blame] | 1869 | if (nasm_isquote(*p)) |
H. Peter Anvin | bb42d30 | 2019-04-22 14:29:29 -0700 | [diff] [blame] | 1870 | nasm_unquote_cstr(p, NULL); |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1871 | if (getenv(p)) |
| 1872 | j = true; |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1873 | tline = tline->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1874 | } |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1875 | break; |
| 1876 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1877 | case PPC_IFIDN: |
| 1878 | case PPC_IFIDNI: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1879 | tline = expand_smacro(tline); |
| 1880 | t = tt = tline; |
| 1881 | while (tok_isnt_(tt, ",")) |
| 1882 | tt = tt->next; |
| 1883 | if (!tt) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1884 | nasm_nonfatal("`%s' expects two comma-separated arguments", |
| 1885 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1886 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1887 | } |
| 1888 | tt = tt->next; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1889 | j = true; /* assume equality unless proved not */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1890 | while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) { |
| 1891 | if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1892 | nasm_nonfatal("`%s': more than one comma on line", |
| 1893 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1894 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1895 | } |
| 1896 | if (t->type == TOK_WHITESPACE) { |
| 1897 | t = t->next; |
| 1898 | continue; |
| 1899 | } |
| 1900 | if (tt->type == TOK_WHITESPACE) { |
| 1901 | tt = tt->next; |
| 1902 | continue; |
| 1903 | } |
| 1904 | if (tt->type != t->type) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1905 | j = false; /* found mismatching tokens */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1906 | break; |
| 1907 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1908 | /* When comparing strings, need to unquote them first */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1909 | if (t->type == TOK_STRING) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1910 | size_t l1 = nasm_unquote(t->text, NULL); |
| 1911 | size_t l2 = nasm_unquote(tt->text, NULL); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 1912 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1913 | if (l1 != l2) { |
| 1914 | j = false; |
| 1915 | break; |
| 1916 | } |
| 1917 | if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) { |
| 1918 | j = false; |
| 1919 | break; |
| 1920 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1921 | } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1922 | j = false; /* found mismatching tokens */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1923 | break; |
| 1924 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1925 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1926 | t = t->next; |
| 1927 | tt = tt->next; |
| 1928 | } |
| 1929 | if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1930 | j = false; /* trailing gunk on one end or other */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1931 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1932 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1933 | case PPC_IFMACRO: |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1934 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1935 | bool found = false; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1936 | MMacro searching, *mmac; |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 1937 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1938 | skip_white_(tline); |
| 1939 | tline = expand_id(tline); |
| 1940 | if (!tok_type_(tline, TOK_ID)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1941 | nasm_nonfatal("`%s' expects a macro name", pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1942 | goto fail; |
| 1943 | } |
| 1944 | searching.name = nasm_strdup(tline->text); |
| 1945 | searching.casesense = true; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1946 | searching.plus = false; |
| 1947 | searching.nolist = false; |
| 1948 | searching.in_progress = 0; |
| 1949 | searching.max_depth = 0; |
| 1950 | searching.rep_nest = NULL; |
| 1951 | searching.nparam_min = 0; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1952 | searching.nparam_max = INT_MAX; |
| 1953 | tline = expand_smacro(tline->next); |
| 1954 | skip_white_(tline); |
| 1955 | if (!tline) { |
| 1956 | } else if (!tok_type_(tline, TOK_NUMBER)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1957 | nasm_nonfatal("`%s' expects a parameter count or nothing", |
| 1958 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1959 | } else { |
| 1960 | searching.nparam_min = searching.nparam_max = |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 1961 | read_param_count(tline->text); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1962 | } |
| 1963 | if (tline && tok_is_(tline->next, "-")) { |
| 1964 | tline = tline->next->next; |
| 1965 | if (tok_is_(tline, "*")) |
| 1966 | searching.nparam_max = INT_MAX; |
| 1967 | else if (!tok_type_(tline, TOK_NUMBER)) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1968 | nasm_nonfatal("`%s' expects a parameter count after `-'", |
| 1969 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1970 | else { |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 1971 | searching.nparam_max = read_param_count(tline->text); |
Cyrill Gorcunov | c9244ea | 2017-10-22 15:25:48 +0300 | [diff] [blame] | 1972 | if (searching.nparam_min > searching.nparam_max) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1973 | nasm_nonfatal("minimum parameter count exceeds maximum"); |
Cyrill Gorcunov | c9244ea | 2017-10-22 15:25:48 +0300 | [diff] [blame] | 1974 | searching.nparam_max = searching.nparam_min; |
| 1975 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1976 | } |
| 1977 | } |
| 1978 | if (tline && tok_is_(tline->next, "+")) { |
| 1979 | tline = tline->next; |
| 1980 | searching.plus = true; |
| 1981 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1982 | mmac = (MMacro *) hash_findix(&mmacros, searching.name); |
| 1983 | while (mmac) { |
| 1984 | if (!strcmp(mmac->name, searching.name) && |
| 1985 | (mmac->nparam_min <= searching.nparam_max |
| 1986 | || searching.plus) |
| 1987 | && (searching.nparam_min <= mmac->nparam_max |
| 1988 | || mmac->plus)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1989 | found = true; |
| 1990 | break; |
| 1991 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1992 | mmac = mmac->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1993 | } |
| 1994 | if (tline && tline->next) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 1995 | nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1996 | nasm_free(searching.name); |
| 1997 | j = found; |
| 1998 | break; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1999 | } |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 2000 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2001 | case PPC_IFID: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2002 | needtype = TOK_ID; |
| 2003 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2004 | case PPC_IFNUM: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2005 | needtype = TOK_NUMBER; |
| 2006 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2007 | case PPC_IFSTR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2008 | needtype = TOK_STRING; |
| 2009 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2010 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2011 | iftype: |
| 2012 | t = tline = expand_smacro(tline); |
H. Peter Anvin | d85d250 | 2008-05-04 17:53:31 -0700 | [diff] [blame] | 2013 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2014 | while (tok_type_(t, TOK_WHITESPACE) || |
| 2015 | (needtype == TOK_NUMBER && |
| 2016 | tok_type_(t, TOK_OTHER) && |
| 2017 | (t->text[0] == '-' || t->text[0] == '+') && |
| 2018 | !t->text[1])) |
| 2019 | t = t->next; |
H. Peter Anvin | d85d250 | 2008-05-04 17:53:31 -0700 | [diff] [blame] | 2020 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2021 | j = tok_type_(t, needtype); |
| 2022 | break; |
H. Peter Anvin | cbf768d | 2008-02-16 16:41:25 -0800 | [diff] [blame] | 2023 | |
| 2024 | case PPC_IFTOKEN: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2025 | t = tline = expand_smacro(tline); |
| 2026 | while (tok_type_(t, TOK_WHITESPACE)) |
| 2027 | t = t->next; |
H. Peter Anvin | cbf768d | 2008-02-16 16:41:25 -0800 | [diff] [blame] | 2028 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2029 | j = false; |
| 2030 | if (t) { |
| 2031 | t = t->next; /* Skip the actual token */ |
| 2032 | while (tok_type_(t, TOK_WHITESPACE)) |
| 2033 | t = t->next; |
| 2034 | j = !t; /* Should be nothing left */ |
| 2035 | } |
| 2036 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2037 | |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 2038 | case PPC_IFEMPTY: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2039 | t = tline = expand_smacro(tline); |
| 2040 | while (tok_type_(t, TOK_WHITESPACE)) |
| 2041 | t = t->next; |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 2042 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2043 | j = !t; /* Should be empty */ |
| 2044 | break; |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 2045 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2046 | case PPC_IF: |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2047 | pps.tptr = tline = expand_smacro(tline); |
| 2048 | pps.ntokens = -1; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2049 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2050 | evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2051 | if (!evalresult) |
| 2052 | return -1; |
| 2053 | if (tokval.t_type) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2054 | nasm_warn(WARN_OTHER, "trailing garbage after expression ignored"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2055 | if (!is_simple(evalresult)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2056 | nasm_nonfatal("non-constant value given to `%s'", |
| 2057 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2058 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2059 | } |
Chuck Crayne | 60ae75d | 2007-05-02 01:59:16 +0000 | [diff] [blame] | 2060 | j = reloc_value(evalresult) != 0; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2061 | break; |
H. Peter Anvin | 95e2882 | 2007-09-12 04:20:08 +0000 | [diff] [blame] | 2062 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2063 | default: |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2064 | nasm_fatal("preprocessor directive `%s' not yet implemented", |
| 2065 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2066 | goto fail; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2067 | } |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2068 | |
| 2069 | free_tlist(origline); |
| 2070 | return j ^ PP_NEGATIVE(ct); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2071 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2072 | fail: |
| 2073 | free_tlist(origline); |
| 2074 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2075 | } |
| 2076 | |
| 2077 | /* |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 2078 | * Default smacro expansion routine: just returns a copy of the |
| 2079 | * expansion list. |
| 2080 | */ |
| 2081 | static Token * |
| 2082 | smacro_expand_default(const SMacro *s, Token **params, unsigned int nparams) |
| 2083 | { |
| 2084 | (void)params; |
| 2085 | (void)nparams; |
| 2086 | |
| 2087 | return dup_tlist(s->expansion, NULL); |
| 2088 | } |
| 2089 | |
| 2090 | /* |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 2091 | * Emit a macro defintion or undef to the listing file, if |
| 2092 | * desired. This is similar to detoken(), but it handles the reverse |
| 2093 | * expansion list, does not expand %! or local variable tokens, and |
| 2094 | * does some special handling for macro parameters. |
| 2095 | */ |
| 2096 | static void |
| 2097 | list_smacro_def(enum preproc_token op, const Context *ctx, const SMacro *m) |
| 2098 | { |
| 2099 | static const Token unused_arg_name = { NULL, "", 0, TOK_OTHER }; |
| 2100 | const Token **param_names; |
| 2101 | Token *junk_names = NULL; |
| 2102 | Token *t; |
| 2103 | size_t namelen, size; |
| 2104 | unsigned int i; |
| 2105 | char *def, *p; |
| 2106 | unsigned int context_depth = 0; |
| 2107 | |
| 2108 | nasm_newn(param_names, m->nparam); |
| 2109 | |
| 2110 | namelen = strlen(m->name); |
| 2111 | size = namelen + 2; /* Include room for space after name + NUL */ |
| 2112 | |
| 2113 | if (ctx) { |
| 2114 | context_depth = cstk->depth - ctx->depth + 1; |
| 2115 | size += context_depth + 1; /* % + some number of $ */ |
| 2116 | } |
| 2117 | |
| 2118 | list_for_each(t, m->expansion) { |
| 2119 | if (!t->text) { |
| 2120 | size++; /* Whitespace, presumably */ |
| 2121 | } else { |
| 2122 | size += t->len; |
| 2123 | if (is_smac_param(t->type)) |
| 2124 | param_names[smac_nparam(t->type)] = t; |
| 2125 | } |
| 2126 | } |
| 2127 | |
| 2128 | if (m->nparam) { |
| 2129 | /* |
| 2130 | * Space for ( and either , or ) around each |
| 2131 | * parameter, plus an optional =. |
| 2132 | */ |
| 2133 | size += 1 + 2 * m->nparam; |
| 2134 | for (i = 0; i < m->nparam; i++) { |
| 2135 | if (!param_names[i]) |
| 2136 | param_names[i] = &unused_arg_name; |
| 2137 | size += param_names[i]->len; |
| 2138 | } |
| 2139 | } |
| 2140 | |
| 2141 | def = nasm_malloc(size); |
| 2142 | p = def+size; |
| 2143 | *--p = '\0'; |
| 2144 | |
| 2145 | list_for_each(t, m->expansion) { |
| 2146 | if (!t->text) { |
| 2147 | *--p = ' '; |
| 2148 | } else { |
| 2149 | p -= t->len; |
| 2150 | memcpy(p, t->text, t->len); |
| 2151 | } |
| 2152 | } |
| 2153 | |
| 2154 | *--p = ' '; |
| 2155 | |
| 2156 | if (m->nparam) { |
| 2157 | *--p = ')'; |
| 2158 | for (i = m->nparam; i--;) { |
| 2159 | p -= param_names[i]->len; |
| 2160 | memcpy(p, param_names[i]->text, param_names[i]->len); |
| 2161 | if (m->eval_param && m->eval_param[i]) |
| 2162 | *--p = '='; |
| 2163 | *--p = ','; |
| 2164 | } |
| 2165 | *p = '('; /* First parameter starts with ( not , */ |
| 2166 | |
| 2167 | free_tlist(junk_names); |
| 2168 | } |
| 2169 | |
| 2170 | p -= namelen; |
| 2171 | memcpy(p, m->name, namelen); |
| 2172 | |
| 2173 | if (context_depth) { |
| 2174 | for (i = 0; i < context_depth; i++) |
| 2175 | *--p = '$'; |
| 2176 | *--p = '%'; |
| 2177 | } |
| 2178 | |
| 2179 | nasm_listmsg("%s %s", pp_directives[op], p); |
| 2180 | |
| 2181 | nasm_free(def); |
| 2182 | } |
| 2183 | |
| 2184 | /* |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2185 | * Common code for defining an smacro |
| 2186 | */ |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2187 | static SMacro *define_smacro(Context *ctx, const char *mname, |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 2188 | bool casesense, int nparam, |
| 2189 | bool *eval_param, Token *expansion) |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2190 | { |
| 2191 | SMacro *smac, **smhead; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2192 | struct hash_table *smtbl; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2193 | |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2194 | if (smacro_defined(ctx, mname, nparam, &smac, casesense)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2195 | if (!smac) { |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2196 | nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and" |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2197 | " without parameters", mname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2198 | /* |
| 2199 | * Some instances of the old code considered this a failure, |
| 2200 | * some others didn't. What is the right thing to do here? |
| 2201 | */ |
| 2202 | free_tlist(expansion); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2203 | return NULL; /* Failure */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2204 | } else { |
| 2205 | /* |
| 2206 | * We're redefining, so we have to take over an |
| 2207 | * existing SMacro structure. This means freeing |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2208 | * what was already in it, but not the structure itself. |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2209 | */ |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 2210 | clear_smacro(smac); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2211 | } |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2212 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2213 | smtbl = ctx ? &ctx->localmac : &smacros; |
| 2214 | smhead = (SMacro **) hash_findi_add(smtbl, mname); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2215 | nasm_new(smac); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2216 | smac->next = *smhead; |
| 2217 | *smhead = smac; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2218 | } |
| 2219 | smac->name = nasm_strdup(mname); |
| 2220 | smac->casesense = casesense; |
| 2221 | smac->nparam = nparam; |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 2222 | smac->eval_param = eval_param; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2223 | smac->expansion = expansion; |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 2224 | smac->expand = smacro_expand_default; |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 2225 | |
| 2226 | if (list_option('m')) |
| 2227 | list_smacro_def(casesense ? PP_DEFINE : PP_IDEFINE, ctx, smac); |
| 2228 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2229 | return smac; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2230 | } |
| 2231 | |
| 2232 | /* |
| 2233 | * Undefine an smacro |
| 2234 | */ |
| 2235 | static void undef_smacro(Context *ctx, const char *mname) |
| 2236 | { |
| 2237 | SMacro **smhead, *s, **sp; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2238 | struct hash_table *smtbl; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2239 | |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2240 | smtbl = ctx ? &ctx->localmac : &smacros; |
| 2241 | smhead = (SMacro **)hash_findi(smtbl, mname, NULL); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2242 | |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2243 | if (smhead) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2244 | /* |
| 2245 | * We now have a macro name... go hunt for it. |
| 2246 | */ |
| 2247 | sp = smhead; |
| 2248 | while ((s = *sp) != NULL) { |
| 2249 | if (!mstrcmp(s->name, mname, s->casesense)) { |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 2250 | if (list_option('m')) |
| 2251 | list_smacro_def(PP_UNDEF, ctx, s); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2252 | *sp = s->next; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 2253 | free_smacro(s); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2254 | } else { |
| 2255 | sp = &s->next; |
| 2256 | } |
| 2257 | } |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2258 | } |
| 2259 | } |
| 2260 | |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2261 | /* |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2262 | * Parse a mmacro specification. |
| 2263 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2264 | 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] | 2265 | { |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2266 | tline = tline->next; |
| 2267 | skip_white_(tline); |
| 2268 | tline = expand_id(tline); |
| 2269 | if (!tok_type_(tline, TOK_ID)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2270 | nasm_nonfatal("`%s' expects a macro name", directive); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2271 | return false; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2272 | } |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2273 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2274 | def->prev = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2275 | def->name = nasm_strdup(tline->text); |
| 2276 | def->plus = false; |
| 2277 | def->nolist = false; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2278 | def->in_progress = 0; |
| 2279 | def->rep_nest = NULL; |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2280 | def->nparam_min = 0; |
| 2281 | def->nparam_max = 0; |
| 2282 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2283 | tline = expand_smacro(tline->next); |
| 2284 | skip_white_(tline); |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2285 | if (!tok_type_(tline, TOK_NUMBER)) |
| 2286 | nasm_nonfatal("`%s' expects a parameter count", directive); |
| 2287 | else |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 2288 | def->nparam_min = def->nparam_max = read_param_count(tline->text); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2289 | if (tline && tok_is_(tline->next, "-")) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2290 | tline = tline->next->next; |
| 2291 | if (tok_is_(tline, "*")) { |
| 2292 | def->nparam_max = INT_MAX; |
| 2293 | } else if (!tok_type_(tline, TOK_NUMBER)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2294 | nasm_nonfatal("`%s' expects a parameter count after `-'", directive); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2295 | } else { |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 2296 | def->nparam_max = read_param_count(tline->text); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2297 | if (def->nparam_min > def->nparam_max) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2298 | nasm_nonfatal("minimum parameter count exceeds maximum"); |
Cyrill Gorcunov | c9244ea | 2017-10-22 15:25:48 +0300 | [diff] [blame] | 2299 | def->nparam_max = def->nparam_min; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2300 | } |
| 2301 | } |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2302 | } |
| 2303 | if (tline && tok_is_(tline->next, "+")) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2304 | tline = tline->next; |
| 2305 | def->plus = true; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2306 | } |
| 2307 | if (tline && tok_type_(tline->next, TOK_ID) && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2308 | !nasm_stricmp(tline->next->text, ".nolist")) { |
| 2309 | tline = tline->next; |
| 2310 | def->nolist = true; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2311 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2312 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2313 | /* |
| 2314 | * Handle default parameters. |
| 2315 | */ |
| 2316 | if (tline && tline->next) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2317 | def->dlist = tline->next; |
| 2318 | tline->next = NULL; |
| 2319 | count_mmac_params(def->dlist, &def->ndefs, &def->defaults); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2320 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2321 | def->dlist = NULL; |
| 2322 | def->defaults = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2323 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2324 | def->expansion = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2325 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2326 | 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] | 2327 | !def->plus) { |
| 2328 | /* |
| 2329 | *!macro-defaults [on] macros with more default than optional parameters |
| 2330 | *! warns when a macro has more default parameters than optional parameters. |
| 2331 | *! See \k{mlmacdef} for why might want to disable this warning. |
| 2332 | */ |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2333 | nasm_warn(WARN_MACRO_DEFAULTS, |
H. Peter Anvin (Intel) | 723ab48 | 2018-12-13 21:53:31 -0800 | [diff] [blame] | 2334 | "too many default macro parameters in macro `%s'", def->name); |
| 2335 | } |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2336 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2337 | return true; |
| 2338 | } |
| 2339 | |
| 2340 | |
| 2341 | /* |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2342 | * Decode a size directive |
| 2343 | */ |
| 2344 | static int parse_size(const char *str) { |
| 2345 | static const char *size_names[] = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2346 | { "byte", "dword", "oword", "qword", "tword", "word", "yword" }; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2347 | static const int sizes[] = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2348 | { 0, 1, 4, 16, 8, 10, 2, 32 }; |
Cyrill Gorcunov | c713b5f | 2018-09-29 14:30:14 +0300 | [diff] [blame] | 2349 | 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] | 2350 | } |
| 2351 | |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2352 | /* |
| 2353 | * Process a preprocessor %pragma directive. Currently there are none. |
| 2354 | * Gets passed the token list starting with the "preproc" token from |
| 2355 | * "%pragma preproc". |
| 2356 | */ |
| 2357 | static void do_pragma_preproc(Token *tline) |
| 2358 | { |
| 2359 | /* Skip to the real stuff */ |
| 2360 | tline = tline->next; |
| 2361 | skip_white_(tline); |
| 2362 | if (!tline) |
| 2363 | return; |
| 2364 | |
| 2365 | (void)tline; /* Nothing else to do at present */ |
| 2366 | } |
| 2367 | |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2368 | /** |
| 2369 | * find and process preprocessor directive in passed line |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2370 | * Find out if a line contains a preprocessor directive, and deal |
| 2371 | * with it if so. |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2372 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2373 | * If a directive _is_ found, it is the responsibility of this routine |
| 2374 | * (and not the caller) to free_tlist() the line. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2375 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2376 | * @param tline a pointer to the current tokeninzed line linked list |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2377 | * @param output if this directive generated output |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2378 | * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2379 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2380 | */ |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 2381 | static int do_directive(Token *tline, Token **output) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2382 | { |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2383 | enum preproc_token i; |
| 2384 | int j; |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 2385 | bool err; |
| 2386 | int nparam; |
| 2387 | bool nolist; |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 2388 | bool casesense; |
H. Peter Anvin | 8cfdb9d | 2007-09-14 18:36:01 -0700 | [diff] [blame] | 2389 | int k, m; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2390 | int offset; |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 2391 | char *p, *pp; |
| 2392 | const char *found_path; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 2393 | const char *mname; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2394 | struct ppscan pps; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2395 | Include *inc; |
| 2396 | Context *ctx; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2397 | Cond *cond; |
| 2398 | MMacro *mmac, **mmhead; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2399 | Token *t = NULL, *tt, *param_start, *macro_start, *last, *origline; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2400 | Line *l; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2401 | struct tokenval tokval; |
| 2402 | expr *evalresult; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2403 | MMacro *tmp_defining; /* Used when manipulating rep_nest */ |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 2404 | int64_t count; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 2405 | size_t len; |
H. Peter Anvin (Intel) | 6bde2ed | 2018-12-13 19:39:41 -0800 | [diff] [blame] | 2406 | errflags severity; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2407 | const char *dname; /* Name of directive, for messages */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2408 | |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2409 | *output = NULL; /* No output generated */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2410 | origline = tline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2411 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2412 | skip_white_(tline); |
H. Peter Anvin | f2936d7 | 2008-06-04 15:11:23 -0700 | [diff] [blame] | 2413 | if (!tline || !tok_type_(tline, TOK_PREPROC_ID) || |
Cyrill Gorcunov | 4b5b737 | 2018-10-29 22:54:08 +0300 | [diff] [blame] | 2414 | (tline->text[0] && (tline->text[1] == '%' || |
| 2415 | tline->text[1] == '$' || |
| 2416 | tline->text[1] == '!'))) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2417 | return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2418 | |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2419 | i = pp_token_hash(tline->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2420 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2421 | /* |
| 2422 | * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO |
| 2423 | * since they are known to be buggy at moment, we need to fix them |
| 2424 | * in future release (2.09-2.10) |
| 2425 | */ |
Philipp Kloke | b432f57 | 2013-03-31 12:01:23 +0200 | [diff] [blame] | 2426 | if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2427 | nasm_nonfatal("unknown preprocessor directive `%s'", tline->text); |
| 2428 | return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2429 | } |
| 2430 | |
| 2431 | /* |
| 2432 | * If we're in a non-emitting branch of a condition construct, |
| 2433 | * or walking to the end of an already terminated %rep block, |
| 2434 | * we should ignore all directives except for condition |
| 2435 | * directives. |
| 2436 | */ |
| 2437 | if (((istk->conds && !emitting(istk->conds->state)) || |
| 2438 | (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) { |
| 2439 | return NO_DIRECTIVE_FOUND; |
| 2440 | } |
| 2441 | |
| 2442 | /* |
| 2443 | * If we're defining a macro or reading a %rep block, we should |
| 2444 | * ignore all directives except for %macro/%imacro (which nest), |
| 2445 | * %endm/%endmacro, and (only if we're in a %rep block) %endrep. |
| 2446 | * If we're in a %rep block, another %rep nests, so should be let through. |
| 2447 | */ |
| 2448 | if (defining && i != PP_MACRO && i != PP_IMACRO && |
| 2449 | i != PP_RMACRO && i != PP_IRMACRO && |
| 2450 | i != PP_ENDMACRO && i != PP_ENDM && |
| 2451 | (defining->name || (i != PP_ENDREP && i != PP_REP))) { |
| 2452 | return NO_DIRECTIVE_FOUND; |
| 2453 | } |
| 2454 | |
| 2455 | if (defining) { |
| 2456 | if (i == PP_MACRO || i == PP_IMACRO || |
| 2457 | i == PP_RMACRO || i == PP_IRMACRO) { |
| 2458 | nested_mac_count++; |
| 2459 | return NO_DIRECTIVE_FOUND; |
| 2460 | } else if (nested_mac_count > 0) { |
| 2461 | if (i == PP_ENDMACRO) { |
| 2462 | nested_mac_count--; |
| 2463 | return NO_DIRECTIVE_FOUND; |
| 2464 | } |
| 2465 | } |
| 2466 | if (!defining->name) { |
| 2467 | if (i == PP_REP) { |
| 2468 | nested_rep_count++; |
| 2469 | return NO_DIRECTIVE_FOUND; |
| 2470 | } else if (nested_rep_count > 0) { |
| 2471 | if (i == PP_ENDREP) { |
| 2472 | nested_rep_count--; |
| 2473 | return NO_DIRECTIVE_FOUND; |
| 2474 | } |
| 2475 | } |
| 2476 | } |
| 2477 | } |
| 2478 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2479 | dname = pp_directives[i]; /* Directive name, for error messages */ |
| 2480 | casesense = true; /* Default to case sensitive */ |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2481 | switch (i) { |
| 2482 | case PP_INVALID: |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2483 | nasm_nonfatal("unknown preprocessor directive `%s'", tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2484 | return NO_DIRECTIVE_FOUND; /* didn't get it */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2485 | |
H. Peter Anvin | 3f87a2a | 2016-10-04 14:07:19 -0700 | [diff] [blame] | 2486 | case PP_PRAGMA: |
| 2487 | /* |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2488 | * %pragma namespace options... |
| 2489 | * |
| 2490 | * The namespace "preproc" is reserved for the preprocessor; |
| 2491 | * all other namespaces generate a [pragma] assembly directive. |
| 2492 | * |
| 2493 | * Invalid %pragmas are ignored and may have different |
| 2494 | * meaning in future versions of NASM. |
H. Peter Anvin | 3f87a2a | 2016-10-04 14:07:19 -0700 | [diff] [blame] | 2495 | */ |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2496 | tline = tline->next; |
| 2497 | skip_white_(tline); |
| 2498 | tline = expand_smacro(tline); |
| 2499 | if (tok_type_(tline, TOK_ID)) { |
| 2500 | if (!nasm_stricmp(tline->text, "preproc")) { |
| 2501 | /* Preprocessor pragma */ |
| 2502 | do_pragma_preproc(tline); |
| 2503 | } else { |
| 2504 | /* Build the assembler directive */ |
| 2505 | t = new_Token(NULL, TOK_OTHER, "[", 1); |
| 2506 | t->next = new_Token(NULL, TOK_ID, "pragma", 6); |
| 2507 | t->next->next = new_Token(tline, TOK_WHITESPACE, NULL, 0); |
| 2508 | tline = t; |
| 2509 | for (t = tline; t->next; t = t->next) |
| 2510 | ; |
| 2511 | t->next = new_Token(NULL, TOK_OTHER, "]", 1); |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 2512 | *output = tline; |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2513 | } |
| 2514 | } |
H. Peter Anvin | 3f87a2a | 2016-10-04 14:07:19 -0700 | [diff] [blame] | 2515 | free_tlist(origline); |
| 2516 | return DIRECTIVE_FOUND; |
| 2517 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2518 | case PP_STACKSIZE: |
| 2519 | /* Directive to tell NASM what the default stack size is. The |
| 2520 | * default is for a 16-bit stack, and this can be overriden with |
| 2521 | * %stacksize large. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2522 | */ |
| 2523 | tline = tline->next; |
| 2524 | if (tline && tline->type == TOK_WHITESPACE) |
| 2525 | tline = tline->next; |
| 2526 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2527 | nasm_nonfatal("`%s' missing size parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2528 | free_tlist(origline); |
| 2529 | return DIRECTIVE_FOUND; |
| 2530 | } |
| 2531 | if (nasm_stricmp(tline->text, "flat") == 0) { |
| 2532 | /* All subsequent ARG directives are for a 32-bit stack */ |
| 2533 | StackSize = 4; |
| 2534 | StackPointer = "ebp"; |
| 2535 | ArgOffset = 8; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2536 | LocalOffset = 0; |
Charles Crayne | 7eaf919 | 2007-11-08 22:11:14 -0800 | [diff] [blame] | 2537 | } else if (nasm_stricmp(tline->text, "flat64") == 0) { |
| 2538 | /* All subsequent ARG directives are for a 64-bit stack */ |
| 2539 | StackSize = 8; |
| 2540 | StackPointer = "rbp"; |
Per Jessen | 53252e0 | 2010-02-11 00:16:59 +0300 | [diff] [blame] | 2541 | ArgOffset = 16; |
Charles Crayne | 7eaf919 | 2007-11-08 22:11:14 -0800 | [diff] [blame] | 2542 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2543 | } else if (nasm_stricmp(tline->text, "large") == 0) { |
| 2544 | /* All subsequent ARG directives are for a 16-bit stack, |
| 2545 | * far function call. |
| 2546 | */ |
| 2547 | StackSize = 2; |
| 2548 | StackPointer = "bp"; |
| 2549 | ArgOffset = 4; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2550 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2551 | } else if (nasm_stricmp(tline->text, "small") == 0) { |
| 2552 | /* All subsequent ARG directives are for a 16-bit stack, |
| 2553 | * far function call. We don't support near functions. |
| 2554 | */ |
| 2555 | StackSize = 2; |
| 2556 | StackPointer = "bp"; |
| 2557 | ArgOffset = 6; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2558 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2559 | } else { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2560 | nasm_nonfatal("`%s' invalid size type", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2561 | free_tlist(origline); |
| 2562 | return DIRECTIVE_FOUND; |
| 2563 | } |
| 2564 | free_tlist(origline); |
| 2565 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2566 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2567 | case PP_ARG: |
| 2568 | /* TASM like ARG directive to define arguments to functions, in |
| 2569 | * the following form: |
| 2570 | * |
| 2571 | * ARG arg1:WORD, arg2:DWORD, arg4:QWORD |
| 2572 | */ |
| 2573 | offset = ArgOffset; |
| 2574 | do { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 2575 | char *arg, directive[256]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2576 | int size = StackSize; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2577 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2578 | /* Find the argument name */ |
| 2579 | tline = tline->next; |
| 2580 | if (tline && tline->type == TOK_WHITESPACE) |
| 2581 | tline = tline->next; |
| 2582 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2583 | nasm_nonfatal("`%s' missing argument parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2584 | free_tlist(origline); |
| 2585 | return DIRECTIVE_FOUND; |
| 2586 | } |
| 2587 | arg = tline->text; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2588 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2589 | /* Find the argument size type */ |
| 2590 | tline = tline->next; |
| 2591 | if (!tline || tline->type != TOK_OTHER |
| 2592 | || tline->text[0] != ':') { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2593 | nasm_nonfatal("syntax error processing `%s' directive", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2594 | free_tlist(origline); |
| 2595 | return DIRECTIVE_FOUND; |
| 2596 | } |
| 2597 | tline = tline->next; |
| 2598 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2599 | nasm_nonfatal("`%s' missing size type parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2600 | free_tlist(origline); |
| 2601 | return DIRECTIVE_FOUND; |
| 2602 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2603 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2604 | /* Allow macro expansion of type parameter */ |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2605 | tt = tokenize(tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2606 | tt = expand_smacro(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2607 | size = parse_size(tt->text); |
| 2608 | if (!size) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2609 | nasm_nonfatal("invalid size type for `%s' missing directive", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2610 | free_tlist(tt); |
| 2611 | free_tlist(origline); |
| 2612 | return DIRECTIVE_FOUND; |
| 2613 | } |
| 2614 | free_tlist(tt); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2615 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2616 | /* Round up to even stack slots */ |
| 2617 | size = ALIGN(size, StackSize); |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2618 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2619 | /* Now define the macro for the argument */ |
| 2620 | snprintf(directive, sizeof(directive), "%%define %s (%s+%d)", |
| 2621 | arg, StackPointer, offset); |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2622 | do_directive(tokenize(directive), output); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2623 | offset += size; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2624 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2625 | /* Move to the next argument in the list */ |
| 2626 | tline = tline->next; |
| 2627 | if (tline && tline->type == TOK_WHITESPACE) |
| 2628 | tline = tline->next; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2629 | } while (tline && tline->type == TOK_OTHER && tline->text[0] == ','); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2630 | ArgOffset = offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2631 | free_tlist(origline); |
| 2632 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2633 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2634 | case PP_LOCAL: |
| 2635 | /* TASM like LOCAL directive to define local variables for a |
| 2636 | * function, in the following form: |
| 2637 | * |
| 2638 | * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize |
| 2639 | * |
| 2640 | * The '= LocalSize' at the end is ignored by NASM, but is |
| 2641 | * required by TASM to define the local parameter size (and used |
| 2642 | * by the TASM macro package). |
| 2643 | */ |
| 2644 | offset = LocalOffset; |
| 2645 | do { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 2646 | char *local, directive[256]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2647 | int size = StackSize; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2648 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2649 | /* Find the argument name */ |
| 2650 | tline = tline->next; |
| 2651 | if (tline && tline->type == TOK_WHITESPACE) |
| 2652 | tline = tline->next; |
| 2653 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2654 | nasm_nonfatal("`%s' missing argument parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2655 | free_tlist(origline); |
| 2656 | return DIRECTIVE_FOUND; |
| 2657 | } |
| 2658 | local = tline->text; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2659 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2660 | /* Find the argument size type */ |
| 2661 | tline = tline->next; |
| 2662 | if (!tline || tline->type != TOK_OTHER |
| 2663 | || tline->text[0] != ':') { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2664 | nasm_nonfatal("syntax error processing `%s' directive", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2665 | free_tlist(origline); |
| 2666 | return DIRECTIVE_FOUND; |
| 2667 | } |
| 2668 | tline = tline->next; |
| 2669 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2670 | nasm_nonfatal("`%s' missing size type parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2671 | free_tlist(origline); |
| 2672 | return DIRECTIVE_FOUND; |
| 2673 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2674 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2675 | /* Allow macro expansion of type parameter */ |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2676 | tt = tokenize(tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2677 | tt = expand_smacro(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2678 | size = parse_size(tt->text); |
| 2679 | if (!size) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2680 | nasm_nonfatal("invalid size type for `%s' missing directive", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2681 | free_tlist(tt); |
| 2682 | free_tlist(origline); |
| 2683 | return DIRECTIVE_FOUND; |
| 2684 | } |
| 2685 | free_tlist(tt); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2686 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2687 | /* Round up to even stack slots */ |
| 2688 | size = ALIGN(size, StackSize); |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2689 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2690 | offset += size; /* Negative offset, increment before */ |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2691 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2692 | /* Now define the macro for the argument */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2693 | snprintf(directive, sizeof(directive), "%%define %s (%s-%d)", |
| 2694 | local, StackPointer, offset); |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2695 | do_directive(tokenize(directive), output); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2696 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2697 | /* Now define the assign to setup the enter_c macro correctly */ |
| 2698 | snprintf(directive, sizeof(directive), |
| 2699 | "%%assign %%$localsize %%$localsize+%d", size); |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2700 | do_directive(tokenize(directive), output); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2701 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2702 | /* Move to the next argument in the list */ |
| 2703 | tline = tline->next; |
| 2704 | if (tline && tline->type == TOK_WHITESPACE) |
| 2705 | tline = tline->next; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2706 | } while (tline && tline->type == TOK_OTHER && tline->text[0] == ','); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2707 | LocalOffset = offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2708 | free_tlist(origline); |
| 2709 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2710 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2711 | case PP_CLEAR: |
| 2712 | if (tline->next) |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2713 | nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2714 | free_macros(); |
| 2715 | init_macros(); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2716 | free_tlist(origline); |
| 2717 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2718 | |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2719 | case PP_DEPEND: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2720 | t = tline->next = expand_smacro(tline->next); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2721 | skip_white_(t); |
| 2722 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2723 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2724 | nasm_nonfatal("`%s' expects a file name", dname); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2725 | free_tlist(origline); |
| 2726 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2727 | } |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2728 | if (t->next) |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2729 | nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2730 | p = t->text; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2731 | if (t->type != TOK_INTERNAL_STRING) |
H. Peter Anvin | bb42d30 | 2019-04-22 14:29:29 -0700 | [diff] [blame] | 2732 | nasm_unquote_cstr(p, NULL); |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 2733 | strlist_add(deplist, p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2734 | free_tlist(origline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2735 | return DIRECTIVE_FOUND; |
| 2736 | |
| 2737 | case PP_INCLUDE: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2738 | t = tline->next = expand_smacro(tline->next); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2739 | skip_white_(t); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2740 | |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2741 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2742 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2743 | nasm_nonfatal("`%s' expects a file name", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2744 | free_tlist(origline); |
| 2745 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2746 | } |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2747 | if (t->next) |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2748 | nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2749 | p = t->text; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2750 | if (t->type != TOK_INTERNAL_STRING) |
H. Peter Anvin | bb42d30 | 2019-04-22 14:29:29 -0700 | [diff] [blame] | 2751 | nasm_unquote_cstr(p, NULL); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2752 | inc = nasm_malloc(sizeof(Include)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2753 | inc->next = istk; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2754 | inc->conds = NULL; |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 2755 | found_path = NULL; |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 2756 | inc->fp = inc_fopen(p, deplist, &found_path, |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 2757 | (pp_mode == PP_DEPS) |
| 2758 | ? INC_OPTIONAL : INC_NEEDED, NF_TEXT); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2759 | if (!inc->fp) { |
| 2760 | /* -MG given but file not found */ |
| 2761 | nasm_free(inc); |
| 2762 | } else { |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 2763 | inc->fname = src_set_fname(found_path ? found_path : p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2764 | inc->lineno = src_set_linnum(0); |
| 2765 | inc->lineinc = 1; |
| 2766 | inc->expansion = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2767 | inc->mstk = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2768 | istk = inc; |
H. Peter Anvin | 0d4d431 | 2019-08-07 00:46:27 -0700 | [diff] [blame] | 2769 | lfmt->uplevel(LIST_INCLUDE, 0); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2770 | } |
| 2771 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2772 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2773 | |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2774 | case PP_USE: |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2775 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2776 | static macros_t *use_pkg; |
| 2777 | const char *pkg_macro = NULL; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2778 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2779 | tline = tline->next; |
| 2780 | skip_white_(tline); |
| 2781 | tline = expand_id(tline); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2782 | |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2783 | if (!tline || (tline->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2784 | tline->type != TOK_INTERNAL_STRING && |
| 2785 | tline->type != TOK_ID)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2786 | nasm_nonfatal("`%s' expects a package name", dname); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2787 | free_tlist(origline); |
| 2788 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2789 | } |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2790 | if (tline->next) |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2791 | nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2792 | if (tline->type == TOK_STRING) |
H. Peter Anvin | bb42d30 | 2019-04-22 14:29:29 -0700 | [diff] [blame] | 2793 | nasm_unquote_cstr(tline->text, NULL); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2794 | use_pkg = nasm_stdmac_find_package(tline->text); |
| 2795 | if (!use_pkg) |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2796 | nasm_nonfatal("unknown `%s' package: %s", dname, tline->text); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2797 | else |
| 2798 | 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] | 2799 | if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2800 | /* Not already included, go ahead and include it */ |
| 2801 | stdmacpos = use_pkg; |
| 2802 | } |
| 2803 | free_tlist(origline); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2804 | return DIRECTIVE_FOUND; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2805 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2806 | case PP_PUSH: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2807 | case PP_REPL: |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2808 | case PP_POP: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2809 | tline = tline->next; |
| 2810 | skip_white_(tline); |
| 2811 | tline = expand_id(tline); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2812 | if (tline) { |
| 2813 | if (!tok_type_(tline, TOK_ID)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2814 | nasm_nonfatal("`%s' expects a context identifier", |
| 2815 | pp_directives[i]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2816 | free_tlist(origline); |
| 2817 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2818 | } |
| 2819 | if (tline->next) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2820 | nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2821 | pp_directives[i]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2822 | p = nasm_strdup(tline->text); |
| 2823 | } else { |
| 2824 | p = NULL; /* Anonymous */ |
| 2825 | } |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2826 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2827 | if (i == PP_PUSH) { |
H. Peter Anvin (Intel) | ebb05a0 | 2018-12-11 12:30:25 -0800 | [diff] [blame] | 2828 | nasm_new(ctx); |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 2829 | ctx->depth = cstk ? cstk->depth + 1 : 1; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2830 | ctx->next = cstk; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2831 | ctx->name = p; |
| 2832 | ctx->number = unique++; |
| 2833 | cstk = ctx; |
| 2834 | } else { |
| 2835 | /* %pop or %repl */ |
| 2836 | if (!cstk) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2837 | nasm_nonfatal("`%s': context stack is empty", |
| 2838 | pp_directives[i]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2839 | } else if (i == PP_POP) { |
| 2840 | if (p && (!cstk->name || nasm_stricmp(p, cstk->name))) |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2841 | nasm_nonfatal("`%s' in wrong context: %s, " |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2842 | "expected %s", |
| 2843 | dname, cstk->name ? cstk->name : "anonymous", p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2844 | else |
| 2845 | ctx_pop(); |
| 2846 | } else { |
| 2847 | /* i == PP_REPL */ |
| 2848 | nasm_free(cstk->name); |
| 2849 | cstk->name = p; |
| 2850 | p = NULL; |
| 2851 | } |
| 2852 | nasm_free(p); |
| 2853 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2854 | free_tlist(origline); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2855 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2856 | case PP_FATAL: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2857 | severity = ERR_FATAL; |
| 2858 | goto issue_error; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2859 | case PP_ERROR: |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2860 | severity = ERR_NONFATAL|ERR_PASS2; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2861 | goto issue_error; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2862 | case PP_WARNING: |
H. Peter Anvin (Intel) | 723ab48 | 2018-12-13 21:53:31 -0800 | [diff] [blame] | 2863 | /*! |
| 2864 | *!user [on] %warning directives |
| 2865 | *! controls output of \c{%warning} directives (see \k{pperror}). |
| 2866 | */ |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2867 | severity = ERR_WARNING|WARN_USER|ERR_PASS2; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2868 | goto issue_error; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2869 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2870 | issue_error: |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2871 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2872 | /* Only error out if this is the final pass */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2873 | tline->next = expand_smacro(tline->next); |
| 2874 | tline = tline->next; |
| 2875 | skip_white_(tline); |
| 2876 | t = tline ? tline->next : NULL; |
| 2877 | skip_white_(t); |
| 2878 | if (tok_type_(tline, TOK_STRING) && !t) { |
| 2879 | /* The line contains only a quoted string */ |
| 2880 | p = tline->text; |
| 2881 | nasm_unquote(p, NULL); /* Ignore NUL character truncation */ |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2882 | nasm_error(severity, "%s", p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2883 | } else { |
| 2884 | /* Not a quoted string, or more than a quoted string */ |
| 2885 | p = detoken(tline, false); |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2886 | nasm_error(severity, "%s", p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2887 | nasm_free(p); |
| 2888 | } |
| 2889 | free_tlist(origline); |
| 2890 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2891 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2892 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2893 | CASE_PP_IF: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2894 | if (istk->conds && !emitting(istk->conds->state)) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2895 | j = COND_NEVER; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2896 | else { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2897 | j = if_condition(tline->next, i); |
| 2898 | tline->next = NULL; /* it got freed */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2899 | j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2900 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2901 | cond = nasm_malloc(sizeof(Cond)); |
| 2902 | cond->next = istk->conds; |
| 2903 | cond->state = j; |
| 2904 | istk->conds = cond; |
| 2905 | if(istk->mstk) |
| 2906 | istk->mstk->condcnt ++; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2907 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2908 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2909 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2910 | CASE_PP_ELIF: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2911 | if (!istk->conds) |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2912 | nasm_fatal("`%s': no matching `%%if'", dname); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2913 | switch(istk->conds->state) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2914 | case COND_IF_TRUE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2915 | istk->conds->state = COND_DONE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2916 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2917 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2918 | case COND_DONE: |
| 2919 | case COND_NEVER: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2920 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2921 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2922 | case COND_ELSE_TRUE: |
| 2923 | case COND_ELSE_FALSE: |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2924 | nasm_warn(WARN_OTHER|ERR_PP_PRECOND, |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2925 | "`%%elif' after `%%else' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2926 | istk->conds->state = COND_NEVER; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2927 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2928 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2929 | case COND_IF_FALSE: |
| 2930 | /* |
| 2931 | * IMPORTANT: In the case of %if, we will already have |
| 2932 | * called expand_mmac_params(); however, if we're |
| 2933 | * processing an %elif we must have been in a |
| 2934 | * non-emitting mode, which would have inhibited |
| 2935 | * the normal invocation of expand_mmac_params(). |
| 2936 | * Therefore, we have to do it explicitly here. |
| 2937 | */ |
| 2938 | j = if_condition(expand_mmac_params(tline->next), i); |
| 2939 | tline->next = NULL; /* it got freed */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2940 | istk->conds->state = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2941 | j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; |
| 2942 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2943 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2944 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2945 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2946 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2947 | case PP_ELSE: |
| 2948 | if (tline->next) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2949 | nasm_warn(WARN_OTHER|ERR_PP_PRECOND, |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2950 | "trailing garbage after `%%else' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2951 | if (!istk->conds) |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 2952 | nasm_fatal("`%%else: no matching `%%if'"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2953 | switch(istk->conds->state) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2954 | case COND_IF_TRUE: |
| 2955 | case COND_DONE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2956 | istk->conds->state = COND_ELSE_FALSE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2957 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2958 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2959 | case COND_NEVER: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2960 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2961 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2962 | case COND_IF_FALSE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2963 | istk->conds->state = COND_ELSE_TRUE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2964 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2965 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2966 | case COND_ELSE_TRUE: |
| 2967 | case COND_ELSE_FALSE: |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2968 | nasm_warn(WARN_OTHER|ERR_PP_PRECOND, |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2969 | "`%%else' after `%%else' ignored."); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2970 | istk->conds->state = COND_NEVER; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2971 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2972 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2973 | free_tlist(origline); |
| 2974 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2975 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2976 | case PP_ENDIF: |
| 2977 | if (tline->next) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2978 | nasm_warn(WARN_OTHER|ERR_PP_PRECOND, |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2979 | "trailing garbage after `%%endif' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2980 | if (!istk->conds) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2981 | nasm_fatal("`%%endif': no matching `%%if'"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2982 | cond = istk->conds; |
| 2983 | istk->conds = cond->next; |
| 2984 | nasm_free(cond); |
| 2985 | if(istk->mstk) |
| 2986 | istk->mstk->condcnt --; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2987 | free_tlist(origline); |
| 2988 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2989 | |
H. Peter Anvin | db8f96e | 2009-07-15 09:07:29 -0400 | [diff] [blame] | 2990 | case PP_IRMACRO: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2991 | case PP_IMACRO: |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2992 | casesense = false; |
| 2993 | /* fall through */ |
| 2994 | case PP_RMACRO: |
| 2995 | case PP_MACRO: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2996 | if (defining) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2997 | nasm_fatal("`%s': already defining a macro", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2998 | return DIRECTIVE_FOUND; |
| 2999 | } |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 3000 | defining = nasm_zalloc(sizeof(MMacro)); |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 3001 | defining->max_depth = ((i == PP_RMACRO) || (i == PP_IRMACRO)) |
| 3002 | ? nasm_limit[LIMIT_MACROS] : 0; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3003 | defining->casesense = casesense; |
| 3004 | if (!parse_mmacro_spec(tline, defining, dname)) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3005 | nasm_free(defining); |
| 3006 | defining = NULL; |
| 3007 | return DIRECTIVE_FOUND; |
| 3008 | } |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 3009 | |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 3010 | src_get(&defining->xline, &defining->fname); |
| 3011 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3012 | mmac = (MMacro *) hash_findix(&mmacros, defining->name); |
| 3013 | while (mmac) { |
| 3014 | if (!strcmp(mmac->name, defining->name) && |
| 3015 | (mmac->nparam_min <= defining->nparam_max |
| 3016 | || defining->plus) |
| 3017 | && (defining->nparam_min <= mmac->nparam_max |
| 3018 | || mmac->plus)) { |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 3019 | nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'", |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3020 | defining->name); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3021 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3022 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3023 | mmac = mmac->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3024 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3025 | free_tlist(origline); |
| 3026 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3027 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3028 | case PP_ENDM: |
| 3029 | case PP_ENDMACRO: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3030 | if (! (defining && defining->name)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3031 | nasm_nonfatal("`%s': not defining a macro", tline->text); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3032 | return DIRECTIVE_FOUND; |
| 3033 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3034 | mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name); |
| 3035 | defining->next = *mmhead; |
| 3036 | *mmhead = defining; |
| 3037 | defining = NULL; |
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 | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 3041 | case PP_EXITMACRO: |
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 a name. Then we |
| 3045 | * bypass all lines between exitmacro and endmacro. |
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) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3052 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3053 | * Remove all conditional entries relative to this |
| 3054 | * macro invocation. (safe to do in this context) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3055 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3056 | for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) { |
| 3057 | cond = istk->conds; |
| 3058 | istk->conds = cond->next; |
| 3059 | nasm_free(cond); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3060 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3061 | istk->expansion = l; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3062 | } else { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3063 | nasm_nonfatal("`%%exitmacro' not within `%%macro' block"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3064 | } |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 3065 | free_tlist(origline); |
| 3066 | return DIRECTIVE_FOUND; |
| 3067 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 3068 | case PP_UNIMACRO: |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3069 | casesense = false; |
| 3070 | /* fall through */ |
| 3071 | case PP_UNMACRO: |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 3072 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3073 | MMacro **mmac_p; |
| 3074 | MMacro spec; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 3075 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3076 | spec.casesense = casesense; |
| 3077 | if (!parse_mmacro_spec(tline, &spec, dname)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3078 | return DIRECTIVE_FOUND; |
| 3079 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3080 | mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL); |
| 3081 | while (mmac_p && *mmac_p) { |
| 3082 | mmac = *mmac_p; |
| 3083 | if (mmac->casesense == spec.casesense && |
| 3084 | !mstrcmp(mmac->name, spec.name, spec.casesense) && |
| 3085 | mmac->nparam_min == spec.nparam_min && |
| 3086 | mmac->nparam_max == spec.nparam_max && |
| 3087 | mmac->plus == spec.plus) { |
| 3088 | *mmac_p = mmac->next; |
| 3089 | free_mmacro(mmac); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3090 | } else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3091 | mmac_p = &mmac->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3092 | } |
| 3093 | } |
| 3094 | free_tlist(origline); |
| 3095 | free_tlist(spec.dlist); |
| 3096 | return DIRECTIVE_FOUND; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 3097 | } |
| 3098 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3099 | case PP_ROTATE: |
| 3100 | if (tline->next && tline->next->type == TOK_WHITESPACE) |
| 3101 | tline = tline->next; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 3102 | if (!tline->next) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3103 | free_tlist(origline); |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3104 | nasm_nonfatal("`%%rotate' missing rotate count"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3105 | return DIRECTIVE_FOUND; |
| 3106 | } |
| 3107 | t = expand_smacro(tline->next); |
| 3108 | tline->next = NULL; |
| 3109 | free_tlist(origline); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3110 | pps.tptr = tline = t; |
| 3111 | pps.ntokens = -1; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3112 | tokval.t_type = TOKEN_INVALID; |
| 3113 | evalresult = |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3114 | evaluate(ppscan, &pps, &tokval, NULL, true, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3115 | free_tlist(tline); |
| 3116 | if (!evalresult) |
| 3117 | return DIRECTIVE_FOUND; |
| 3118 | if (tokval.t_type) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 3119 | nasm_warn(WARN_OTHER, "trailing garbage after expression ignored"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3120 | if (!is_simple(evalresult)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3121 | nasm_nonfatal("non-constant value given to `%%rotate'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3122 | return DIRECTIVE_FOUND; |
| 3123 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3124 | mmac = istk->mstk; |
| 3125 | while (mmac && !mmac->name) /* avoid mistaking %reps for macros */ |
| 3126 | mmac = mmac->next_active; |
| 3127 | if (!mmac) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3128 | nasm_nonfatal("`%%rotate' invoked outside a macro call"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3129 | } else if (mmac->nparam == 0) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3130 | nasm_nonfatal("`%%rotate' invoked within macro without parameters"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3131 | } else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3132 | int rotate = mmac->rotate + reloc_value(evalresult); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3133 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3134 | rotate %= (int)mmac->nparam; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3135 | if (rotate < 0) |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3136 | rotate += mmac->nparam; |
| 3137 | |
| 3138 | mmac->rotate = rotate; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3139 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3140 | return DIRECTIVE_FOUND; |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 3141 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3142 | case PP_REP: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 3143 | nolist = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3144 | do { |
| 3145 | tline = tline->next; |
| 3146 | } while (tok_type_(tline, TOK_WHITESPACE)); |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 3147 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3148 | if (tok_type_(tline, TOK_ID) && |
| 3149 | nasm_stricmp(tline->text, ".nolist") == 0) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 3150 | nolist = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3151 | do { |
| 3152 | tline = tline->next; |
| 3153 | } while (tok_type_(tline, TOK_WHITESPACE)); |
| 3154 | } |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 3155 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3156 | if (tline) { |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3157 | pps.tptr = expand_smacro(tline); |
| 3158 | pps.ntokens = -1; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3159 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3160 | /* XXX: really critical?! */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3161 | evalresult = |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3162 | evaluate(ppscan, &pps, &tokval, NULL, true, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3163 | if (!evalresult) { |
| 3164 | free_tlist(origline); |
| 3165 | return DIRECTIVE_FOUND; |
| 3166 | } |
| 3167 | if (tokval.t_type) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 3168 | nasm_warn(WARN_OTHER, "trailing garbage after expression ignored"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3169 | if (!is_simple(evalresult)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3170 | nasm_nonfatal("non-constant value given to `%%rep'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3171 | return DIRECTIVE_FOUND; |
| 3172 | } |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 3173 | count = reloc_value(evalresult); |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 3174 | if (count > nasm_limit[LIMIT_REP]) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3175 | nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")", |
| 3176 | count, nasm_limit[LIMIT_REP]); |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 3177 | count = 0; |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 3178 | } else if (count < 0) { |
H. Peter Anvin (Intel) | 723ab48 | 2018-12-13 21:53:31 -0800 | [diff] [blame] | 3179 | /*! |
| 3180 | *!negative-rep [on] regative %rep count |
| 3181 | *! warns about negative counts given to the \c{%rep} |
| 3182 | *! preprocessor directive. |
| 3183 | */ |
H. Peter Anvin (Intel) | 80c4f23 | 2018-12-14 13:33:24 -0800 | [diff] [blame] | 3184 | nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP, |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 3185 | "negative `%%rep' count: %"PRId64, count); |
| 3186 | count = 0; |
| 3187 | } else { |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 3188 | count++; |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 3189 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3190 | } else { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3191 | nasm_nonfatal("`%%rep' expects a repeat count"); |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 3192 | count = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3193 | } |
| 3194 | free_tlist(origline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3195 | |
| 3196 | tmp_defining = defining; |
| 3197 | defining = nasm_malloc(sizeof(MMacro)); |
| 3198 | defining->prev = NULL; |
| 3199 | defining->name = NULL; /* flags this macro as a %rep block */ |
| 3200 | defining->casesense = false; |
| 3201 | defining->plus = false; |
| 3202 | defining->nolist = nolist; |
| 3203 | defining->in_progress = count; |
| 3204 | defining->max_depth = 0; |
| 3205 | defining->nparam_min = defining->nparam_max = 0; |
| 3206 | defining->defaults = NULL; |
| 3207 | defining->dlist = NULL; |
| 3208 | defining->expansion = NULL; |
| 3209 | defining->next_active = istk->mstk; |
| 3210 | defining->rep_nest = tmp_defining; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3211 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3212 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3213 | case PP_ENDREP: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3214 | if (!defining || defining->name) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3215 | nasm_nonfatal("`%%endrep': no matching `%%rep'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3216 | return DIRECTIVE_FOUND; |
| 3217 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3218 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3219 | /* |
| 3220 | * Now we have a "macro" defined - although it has no name |
| 3221 | * and we won't be entering it in the hash tables - we must |
| 3222 | * push a macro-end marker for it on to istk->expansion. |
| 3223 | * After that, it will take care of propagating itself (a |
| 3224 | * macro-end marker line for a macro which is really a %rep |
| 3225 | * block will cause the macro to be re-expanded, complete |
| 3226 | * with another macro-end marker to ensure the process |
| 3227 | * continues) until the whole expansion is forcibly removed |
| 3228 | * from istk->expansion by a %exitrep. |
| 3229 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3230 | l = nasm_malloc(sizeof(Line)); |
| 3231 | l->next = istk->expansion; |
| 3232 | l->finishes = defining; |
| 3233 | l->first = NULL; |
| 3234 | istk->expansion = l; |
| 3235 | |
| 3236 | istk->mstk = defining; |
| 3237 | |
H. Peter Anvin | 0d4d431 | 2019-08-07 00:46:27 -0700 | [diff] [blame] | 3238 | lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3239 | tmp_defining = defining; |
| 3240 | defining = defining->rep_nest; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3241 | free_tlist(origline); |
| 3242 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3243 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3244 | case PP_EXITREP: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3245 | /* |
| 3246 | * We must search along istk->expansion until we hit a |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3247 | * macro-end marker for a macro with no name. Then we set |
| 3248 | * its `in_progress' flag to 0. |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3249 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3250 | list_for_each(l, istk->expansion) |
| 3251 | if (l->finishes && !l->finishes->name) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3252 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3253 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3254 | if (l) |
| 3255 | l->finishes->in_progress = 1; |
| 3256 | else |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3257 | nasm_nonfatal("`%%exitrep' not within `%%rep' block"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3258 | free_tlist(origline); |
| 3259 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3260 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3261 | case PP_IDEFINE: |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3262 | case PP_IXDEFINE: |
| 3263 | casesense = false; |
| 3264 | /* fall through */ |
| 3265 | case PP_DEFINE: |
| 3266 | case PP_XDEFINE: |
| 3267 | { |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3268 | bool have_eval_params = false; |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 3269 | bool *eval_params = NULL; |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3270 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3271 | tline = tline->next; |
| 3272 | skip_white_(tline); |
| 3273 | tline = expand_id(tline); |
| 3274 | if (!tline || (tline->type != TOK_ID && |
| 3275 | (tline->type != TOK_PREPROC_ID || |
| 3276 | tline->text[1] != '$'))) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3277 | nasm_nonfatal("`%s' expects a macro identifier", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3278 | free_tlist(origline); |
| 3279 | return DIRECTIVE_FOUND; |
| 3280 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3281 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3282 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3283 | last = tline; |
| 3284 | param_start = tline = tline->next; |
| 3285 | nparam = 0; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3286 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3287 | if (tok_is_(tline, "(")) { |
| 3288 | /* |
| 3289 | * This macro has parameters. |
| 3290 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3291 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3292 | tline = tline->next; |
| 3293 | while (1) { |
| 3294 | skip_white_(tline); |
| 3295 | if (!tline) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3296 | nasm_nonfatal("parameter identifier expected"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3297 | free_tlist(origline); |
| 3298 | return DIRECTIVE_FOUND; |
| 3299 | } |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3300 | if (tok_is_(tline, "=")) { |
| 3301 | have_eval_params = true; |
| 3302 | tline = tline->next; |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 3303 | skip_white_(tline); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3304 | } |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 3305 | |
| 3306 | /* |
| 3307 | * "*" means an unnamed, ignored argument; it can never |
| 3308 | * match anything because "*" is not TOK_ID, and so |
| 3309 | * none of the expansion entries will be converted |
| 3310 | * to parameters. |
| 3311 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3312 | if (tline->type != TOK_ID) { |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 3313 | if (tok_is_(tline, ",") || tok_is_(tline, ")")) { |
| 3314 | /* |
| 3315 | * Empty name; duplicate the termination |
| 3316 | * token and use the first copy as a placeholder. |
| 3317 | * It will never match anything, since the |
| 3318 | * associated text doesn't correspond to a TOK_ID. |
| 3319 | */ |
| 3320 | tline = dup_Token(tline, tline); |
| 3321 | } else { |
| 3322 | nasm_nonfatal("`%s': parameter identifier expected", |
| 3323 | tline->text); |
| 3324 | free_tlist(origline); |
| 3325 | return DIRECTIVE_FOUND; |
| 3326 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3327 | } |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3328 | tline->type = tok_smac_param(nparam++); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3329 | tline = tline->next; |
| 3330 | skip_white_(tline); |
| 3331 | if (tok_is_(tline, ",")) { |
| 3332 | tline = tline->next; |
H. Peter Anvin | ca348b6 | 2008-07-23 10:49:26 -0400 | [diff] [blame] | 3333 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3334 | if (!tok_is_(tline, ")")) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3335 | nasm_nonfatal("`)' expected to terminate macro template"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3336 | free_tlist(origline); |
| 3337 | return DIRECTIVE_FOUND; |
| 3338 | } |
| 3339 | break; |
| 3340 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3341 | } |
| 3342 | last = tline; |
| 3343 | tline = tline->next; |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 3344 | |
| 3345 | if (have_eval_params) { |
| 3346 | /* Create evaluated parameters table */ |
| 3347 | bool is_eval = false; |
| 3348 | |
| 3349 | nasm_newn(eval_params, nparam); |
| 3350 | list_for_each(tt, param_start) { |
| 3351 | if (is_smac_param(tt->type)) |
| 3352 | eval_params[smac_nparam(tt->type)] = is_eval; |
| 3353 | is_eval = tok_is_(tt, "="); |
| 3354 | } |
| 3355 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3356 | } |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 3357 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3358 | if (tok_type_(tline, TOK_WHITESPACE)) |
| 3359 | last = tline, tline = tline->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3360 | last->next = NULL; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3361 | |
| 3362 | /* Expand the macro definition now for %xdefine and %ixdefine */ |
| 3363 | if ((i == PP_XDEFINE) || (i == PP_IXDEFINE)) |
| 3364 | tline = expand_smacro(tline); |
| 3365 | |
| 3366 | macro_start = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3367 | t = tline; |
| 3368 | while (t) { |
| 3369 | if (t->type == TOK_ID) { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3370 | list_for_each(tt, param_start) |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 3371 | if (is_smac_param(tt->type) && |
| 3372 | tt->len == t->len && |
| 3373 | !memcmp(tt->text, t->text, tt->len)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3374 | t->type = tt->type; |
| 3375 | } |
| 3376 | tt = t->next; |
| 3377 | t->next = macro_start; |
| 3378 | macro_start = t; |
| 3379 | t = tt; |
| 3380 | } |
| 3381 | /* |
| 3382 | * Good. We now have a macro name, a parameter count, and a |
| 3383 | * token list (in reverse order) for an expansion. We ought |
| 3384 | * to be OK just to create an SMacro, store it, and let |
| 3385 | * free_tlist have the rest of the line (which we have |
| 3386 | * carefully re-terminated after chopping off the expansion |
| 3387 | * from the end). |
| 3388 | */ |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 3389 | define_smacro(ctx, mname, casesense, nparam, eval_params, macro_start); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3390 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3391 | free_tlist(origline); |
| 3392 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3393 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3394 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3395 | case PP_UNDEF: |
| 3396 | tline = tline->next; |
| 3397 | skip_white_(tline); |
| 3398 | tline = expand_id(tline); |
| 3399 | if (!tline || (tline->type != TOK_ID && |
| 3400 | (tline->type != TOK_PREPROC_ID || |
| 3401 | tline->text[1] != '$'))) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3402 | nasm_nonfatal("`%%undef' expects a macro identifier"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3403 | free_tlist(origline); |
| 3404 | return DIRECTIVE_FOUND; |
| 3405 | } |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3406 | if (tline->next) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 3407 | nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored"); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3408 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3409 | /* Find the context that symbol belongs to */ |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3410 | ctx = get_ctx(tline->text, &mname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3411 | undef_smacro(ctx, mname); |
| 3412 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3413 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3414 | |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3415 | case PP_IDEFSTR: |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3416 | casesense = false; |
| 3417 | /* fall through */ |
| 3418 | case PP_DEFSTR: |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3419 | tline = tline->next; |
| 3420 | skip_white_(tline); |
| 3421 | tline = expand_id(tline); |
| 3422 | if (!tline || (tline->type != TOK_ID && |
| 3423 | (tline->type != TOK_PREPROC_ID || |
| 3424 | tline->text[1] != '$'))) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3425 | nasm_nonfatal("`%s' expects a macro identifier", dname); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3426 | free_tlist(origline); |
| 3427 | return DIRECTIVE_FOUND; |
| 3428 | } |
| 3429 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3430 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3431 | last = tline; |
| 3432 | tline = expand_smacro(tline->next); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3433 | last->next = NULL; |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3434 | |
| 3435 | while (tok_type_(tline, TOK_WHITESPACE)) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3436 | tline = delete_Token(tline); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3437 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3438 | p = detoken(tline, false); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3439 | macro_start = make_tok_qstr(p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3440 | nasm_free(p); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3441 | |
| 3442 | /* |
| 3443 | * We now have a macro name, an implicit parameter count of |
| 3444 | * zero, and a string token to use as an expansion. Create |
| 3445 | * and store an SMacro. |
| 3446 | */ |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 3447 | define_smacro(ctx, mname, casesense, 0, NULL, macro_start); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3448 | free_tlist(origline); |
| 3449 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3450 | |
H. Peter Anvin | 2f55bda | 2009-07-14 15:04:04 -0400 | [diff] [blame] | 3451 | case PP_IDEFTOK: |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3452 | casesense = false; |
| 3453 | /* fall through */ |
| 3454 | case PP_DEFTOK: |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3455 | tline = tline->next; |
| 3456 | skip_white_(tline); |
| 3457 | tline = expand_id(tline); |
| 3458 | if (!tline || (tline->type != TOK_ID && |
| 3459 | (tline->type != TOK_PREPROC_ID || |
| 3460 | tline->text[1] != '$'))) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3461 | nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3462 | free_tlist(origline); |
| 3463 | return DIRECTIVE_FOUND; |
| 3464 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3465 | ctx = get_ctx(tline->text, &mname); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3466 | last = tline; |
| 3467 | tline = expand_smacro(tline->next); |
| 3468 | last->next = NULL; |
| 3469 | |
| 3470 | t = tline; |
| 3471 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3472 | t = t->next; |
| 3473 | /* t should now point to the string */ |
Cyrill Gorcunov | 6908e58 | 2010-09-06 19:36:15 +0400 | [diff] [blame] | 3474 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3475 | nasm_nonfatal("`%s` requires string as second parameter", dname); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3476 | free_tlist(tline); |
| 3477 | free_tlist(origline); |
| 3478 | return DIRECTIVE_FOUND; |
| 3479 | } |
| 3480 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 3481 | /* |
| 3482 | * Convert the string to a token stream. Note that smacros |
| 3483 | * are stored with the token stream reversed, so we have to |
| 3484 | * reverse the output of tokenize(). |
| 3485 | */ |
H. Peter Anvin | bb42d30 | 2019-04-22 14:29:29 -0700 | [diff] [blame] | 3486 | nasm_unquote_cstr(t->text, NULL); |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 3487 | macro_start = reverse_tokens(tokenize(t->text)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3488 | |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3489 | /* |
| 3490 | * We now have a macro name, an implicit parameter count of |
| 3491 | * zero, and a numeric token to use as an expansion. Create |
| 3492 | * and store an SMacro. |
| 3493 | */ |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 3494 | define_smacro(ctx, mname, casesense, 0, NULL, macro_start); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3495 | free_tlist(tline); |
| 3496 | free_tlist(origline); |
| 3497 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3498 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3499 | case PP_IPATHSEARCH: |
| 3500 | casesense = false; |
| 3501 | /* fall through */ |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3502 | case PP_PATHSEARCH: |
| 3503 | { |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 3504 | const char *found_path; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3505 | |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3506 | tline = tline->next; |
| 3507 | skip_white_(tline); |
| 3508 | tline = expand_id(tline); |
| 3509 | if (!tline || (tline->type != TOK_ID && |
| 3510 | (tline->type != TOK_PREPROC_ID || |
| 3511 | tline->text[1] != '$'))) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3512 | nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3513 | free_tlist(origline); |
| 3514 | return DIRECTIVE_FOUND; |
| 3515 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3516 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3517 | last = tline; |
| 3518 | tline = expand_smacro(tline->next); |
| 3519 | last->next = NULL; |
| 3520 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3521 | t = tline; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3522 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3523 | t = t->next; |
| 3524 | |
| 3525 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3526 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3527 | nasm_nonfatal("`%s' expects a file name", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3528 | free_tlist(tline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3529 | free_tlist(origline); |
| 3530 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 3531 | } |
| 3532 | if (t->next) |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3533 | nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3534 | p = t->text; |
H. Peter Anvin | 427cc91 | 2008-06-01 21:43:03 -0700 | [diff] [blame] | 3535 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3536 | nasm_unquote(p, NULL); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3537 | |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 3538 | inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY); |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 3539 | if (!found_path) |
| 3540 | found_path = p; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3541 | macro_start = make_tok_qstr(found_path); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3542 | |
| 3543 | /* |
| 3544 | * We now have a macro name, an implicit parameter count of |
| 3545 | * zero, and a string token to use as an expansion. Create |
| 3546 | * and store an SMacro. |
| 3547 | */ |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 3548 | define_smacro(ctx, mname, casesense, 0, NULL, macro_start); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3549 | free_tlist(tline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3550 | free_tlist(origline); |
| 3551 | return DIRECTIVE_FOUND; |
| 3552 | } |
| 3553 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3554 | case PP_ISTRLEN: |
| 3555 | casesense = false; |
| 3556 | /* fall through */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3557 | case PP_STRLEN: |
| 3558 | tline = tline->next; |
| 3559 | skip_white_(tline); |
| 3560 | tline = expand_id(tline); |
| 3561 | if (!tline || (tline->type != TOK_ID && |
| 3562 | (tline->type != TOK_PREPROC_ID || |
| 3563 | tline->text[1] != '$'))) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3564 | nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3565 | free_tlist(origline); |
| 3566 | return DIRECTIVE_FOUND; |
| 3567 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3568 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3569 | last = tline; |
| 3570 | tline = expand_smacro(tline->next); |
| 3571 | last->next = NULL; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3572 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3573 | t = tline; |
| 3574 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3575 | t = t->next; |
| 3576 | /* t should now point to the string */ |
Cyrill Gorcunov | 4e1d5ab | 2010-07-23 18:51:51 +0400 | [diff] [blame] | 3577 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3578 | nasm_nonfatal("`%s' requires string as second parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3579 | free_tlist(tline); |
| 3580 | free_tlist(origline); |
| 3581 | return DIRECTIVE_FOUND; |
| 3582 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3583 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3584 | macro_start = make_tok_num(nasm_unquote(t->text, NULL)); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3585 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3586 | /* |
| 3587 | * We now have a macro name, an implicit parameter count of |
| 3588 | * zero, and a numeric token to use as an expansion. Create |
| 3589 | * and store an SMacro. |
| 3590 | */ |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 3591 | define_smacro(ctx, mname, casesense, 0, NULL, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3592 | free_tlist(tline); |
| 3593 | free_tlist(origline); |
| 3594 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3595 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3596 | case PP_ISTRCAT: |
| 3597 | casesense = false; |
| 3598 | /* fall through */ |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3599 | case PP_STRCAT: |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3600 | tline = tline->next; |
| 3601 | skip_white_(tline); |
| 3602 | tline = expand_id(tline); |
| 3603 | if (!tline || (tline->type != TOK_ID && |
| 3604 | (tline->type != TOK_PREPROC_ID || |
| 3605 | tline->text[1] != '$'))) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3606 | nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3607 | free_tlist(origline); |
| 3608 | return DIRECTIVE_FOUND; |
| 3609 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3610 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3611 | last = tline; |
| 3612 | tline = expand_smacro(tline->next); |
| 3613 | last->next = NULL; |
| 3614 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3615 | len = 0; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3616 | list_for_each(t, tline) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3617 | switch (t->type) { |
| 3618 | case TOK_WHITESPACE: |
| 3619 | break; |
| 3620 | case TOK_STRING: |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 3621 | len += t->len = nasm_unquote(t->text, NULL); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3622 | break; |
| 3623 | case TOK_OTHER: |
| 3624 | if (!strcmp(t->text, ",")) /* permit comma separators */ |
| 3625 | break; |
| 3626 | /* else fall through */ |
| 3627 | default: |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3628 | nasm_nonfatal("non-string passed to `%s': %s", dname, t->text); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3629 | free_tlist(tline); |
| 3630 | free_tlist(origline); |
| 3631 | return DIRECTIVE_FOUND; |
| 3632 | } |
| 3633 | } |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3634 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3635 | p = pp = nasm_malloc(len); |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3636 | list_for_each(t, tline) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3637 | if (t->type == TOK_STRING) { |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 3638 | memcpy(p, t->text, t->len); |
| 3639 | p += t->len; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3640 | } |
| 3641 | } |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3642 | |
| 3643 | /* |
| 3644 | * We now have a macro name, an implicit parameter count of |
| 3645 | * zero, and a numeric token to use as an expansion. Create |
| 3646 | * and store an SMacro. |
| 3647 | */ |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3648 | macro_start = make_tok_qstr(pp); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3649 | nasm_free(pp); |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 3650 | define_smacro(ctx, mname, casesense, 0, NULL, macro_start); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3651 | free_tlist(tline); |
| 3652 | free_tlist(origline); |
| 3653 | return DIRECTIVE_FOUND; |
| 3654 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3655 | case PP_ISUBSTR: |
| 3656 | casesense = false; |
| 3657 | /* fall through */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3658 | case PP_SUBSTR: |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3659 | { |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3660 | int64_t start, count; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3661 | size_t len; |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 3662 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3663 | tline = tline->next; |
| 3664 | skip_white_(tline); |
| 3665 | tline = expand_id(tline); |
| 3666 | if (!tline || (tline->type != TOK_ID && |
| 3667 | (tline->type != TOK_PREPROC_ID || |
| 3668 | tline->text[1] != '$'))) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3669 | nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3670 | free_tlist(origline); |
| 3671 | return DIRECTIVE_FOUND; |
| 3672 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3673 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3674 | last = tline; |
| 3675 | tline = expand_smacro(tline->next); |
| 3676 | last->next = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3677 | |
Cyrill Gorcunov | 35519d6 | 2010-09-06 23:49:52 +0400 | [diff] [blame] | 3678 | if (tline) /* skip expanded id */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3679 | t = tline->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3680 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3681 | t = t->next; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3682 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3683 | /* t should now point to the string */ |
Cyrill Gorcunov | 35519d6 | 2010-09-06 23:49:52 +0400 | [diff] [blame] | 3684 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3685 | nasm_nonfatal("`%s' requires string as second parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3686 | free_tlist(tline); |
| 3687 | free_tlist(origline); |
| 3688 | return DIRECTIVE_FOUND; |
| 3689 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3690 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3691 | pps.tptr = t->next; |
| 3692 | pps.ntokens = -1; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3693 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3694 | evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3695 | if (!evalresult) { |
| 3696 | free_tlist(tline); |
| 3697 | free_tlist(origline); |
| 3698 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3699 | } else if (!is_simple(evalresult)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3700 | nasm_nonfatal("non-constant value given to `%s'", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3701 | free_tlist(tline); |
| 3702 | free_tlist(origline); |
| 3703 | return DIRECTIVE_FOUND; |
| 3704 | } |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3705 | start = evalresult->value - 1; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3706 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3707 | while (tok_type_(pps.tptr, TOK_WHITESPACE)) |
| 3708 | pps.tptr = pps.tptr->next; |
| 3709 | if (!pps.tptr) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3710 | count = 1; /* Backwards compatibility: one character */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3711 | } else { |
| 3712 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3713 | evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3714 | if (!evalresult) { |
| 3715 | free_tlist(tline); |
| 3716 | free_tlist(origline); |
| 3717 | return DIRECTIVE_FOUND; |
| 3718 | } else if (!is_simple(evalresult)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3719 | nasm_nonfatal("non-constant value given to `%s'", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3720 | free_tlist(tline); |
| 3721 | free_tlist(origline); |
| 3722 | return DIRECTIVE_FOUND; |
| 3723 | } |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3724 | count = evalresult->value; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3725 | } |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3726 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3727 | len = nasm_unquote(t->text, NULL); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3728 | |
Cyrill Gorcunov | cff031e | 2010-09-07 20:31:11 +0400 | [diff] [blame] | 3729 | /* make start and count being in range */ |
| 3730 | if (start < 0) |
| 3731 | start = 0; |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3732 | if (count < 0) |
| 3733 | count = len + count + 1 - start; |
| 3734 | if (start + count > (int64_t)len) |
Cyrill Gorcunov | cff031e | 2010-09-07 20:31:11 +0400 | [diff] [blame] | 3735 | count = len - start; |
| 3736 | if (!len || count < 0 || start >=(int64_t)len) |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3737 | start = -1, count = 0; /* empty string */ |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3738 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3739 | macro_start = new_Token(NULL, TOK_STRING, NULL, 0); |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 3740 | macro_start->len = count; |
| 3741 | macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, |
| 3742 | ¯o_start->len); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3743 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3744 | /* |
| 3745 | * We now have a macro name, an implicit parameter count of |
| 3746 | * zero, and a numeric token to use as an expansion. Create |
| 3747 | * and store an SMacro. |
| 3748 | */ |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 3749 | define_smacro(ctx, mname, casesense, 0, NULL, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3750 | free_tlist(tline); |
| 3751 | free_tlist(origline); |
| 3752 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3753 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3754 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3755 | case PP_IASSIGN: |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3756 | casesense = false; |
| 3757 | /* fall through */ |
| 3758 | case PP_ASSIGN: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3759 | tline = tline->next; |
| 3760 | skip_white_(tline); |
| 3761 | tline = expand_id(tline); |
| 3762 | if (!tline || (tline->type != TOK_ID && |
| 3763 | (tline->type != TOK_PREPROC_ID || |
| 3764 | tline->text[1] != '$'))) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3765 | nasm_nonfatal("`%s' expects a macro identifier", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3766 | free_tlist(origline); |
| 3767 | return DIRECTIVE_FOUND; |
| 3768 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3769 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3770 | last = tline; |
| 3771 | tline = expand_smacro(tline->next); |
| 3772 | last->next = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3773 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3774 | pps.tptr = tline; |
| 3775 | pps.ntokens = -1; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3776 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3777 | evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3778 | free_tlist(tline); |
| 3779 | if (!evalresult) { |
| 3780 | free_tlist(origline); |
| 3781 | return DIRECTIVE_FOUND; |
| 3782 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3783 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3784 | if (tokval.t_type) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 3785 | nasm_warn(WARN_OTHER, "trailing garbage after expression ignored"); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3786 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3787 | if (!is_simple(evalresult)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3788 | nasm_nonfatal("non-constant value given to `%s'", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3789 | free_tlist(origline); |
| 3790 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3791 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3792 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3793 | macro_start = make_tok_num(reloc_value(evalresult)); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3794 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3795 | /* |
| 3796 | * We now have a macro name, an implicit parameter count of |
| 3797 | * zero, and a numeric token to use as an expansion. Create |
| 3798 | * and store an SMacro. |
| 3799 | */ |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 3800 | define_smacro(ctx, mname, casesense, 0, NULL, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3801 | free_tlist(origline); |
| 3802 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3803 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3804 | case PP_LINE: |
| 3805 | /* |
| 3806 | * Syntax is `%line nnn[+mmm] [filename]' |
| 3807 | */ |
H. Peter Anvin (Intel) | 800c168 | 2018-12-14 12:22:11 -0800 | [diff] [blame] | 3808 | if (unlikely(pp_noline)) { |
| 3809 | free_tlist(origline); |
| 3810 | return DIRECTIVE_FOUND; |
| 3811 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3812 | tline = tline->next; |
| 3813 | skip_white_(tline); |
| 3814 | if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3815 | nasm_nonfatal("`%s' expects line number", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3816 | free_tlist(origline); |
| 3817 | return DIRECTIVE_FOUND; |
| 3818 | } |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 3819 | k = readnum(tline->text, &err); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3820 | m = 1; |
| 3821 | tline = tline->next; |
| 3822 | if (tok_is_(tline, "+")) { |
| 3823 | tline = tline->next; |
| 3824 | if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3825 | nasm_nonfatal("`%s' expects line increment", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3826 | free_tlist(origline); |
| 3827 | return DIRECTIVE_FOUND; |
| 3828 | } |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 3829 | m = readnum(tline->text, &err); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3830 | tline = tline->next; |
| 3831 | } |
| 3832 | skip_white_(tline); |
| 3833 | src_set_linnum(k); |
| 3834 | istk->lineinc = m; |
| 3835 | if (tline) { |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 3836 | char *fname = detoken(tline, false); |
| 3837 | src_set_fname(fname); |
| 3838 | nasm_free(fname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3839 | } |
| 3840 | free_tlist(origline); |
| 3841 | return DIRECTIVE_FOUND; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3842 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3843 | default: |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3844 | nasm_nonfatal("preprocessor directive `%s' not yet implemented", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3845 | return DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3846 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3847 | } |
| 3848 | |
| 3849 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3850 | * Ensure that a macro parameter contains a condition code and |
| 3851 | * nothing else. Return the condition code index if so, or -1 |
| 3852 | * otherwise. |
| 3853 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3854 | static int find_cc(Token * t) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3855 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3856 | Token *tt; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3857 | |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3858 | if (!t) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3859 | return -1; /* Probably a %+ without a space */ |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3860 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3861 | skip_white_(t); |
Cyrill Gorcunov | 7524cfd | 2017-10-22 19:01:16 +0300 | [diff] [blame] | 3862 | if (!t) |
| 3863 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3864 | if (t->type != TOK_ID) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3865 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3866 | tt = t->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3867 | skip_white_(tt); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3868 | if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ","))) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3869 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3870 | |
Cyrill Gorcunov | 1945639 | 2012-05-02 00:18:56 +0400 | [diff] [blame] | 3871 | return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3872 | } |
| 3873 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3874 | /* |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 3875 | * This routines walks over tokens strem and handles tokens |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3876 | * pasting, if @handle_explicit passed then explicit pasting |
| 3877 | * term is handled, otherwise -- implicit pastings only. |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 3878 | * The @m array can contain a series of token types which are |
| 3879 | * executed as separate passes. |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3880 | */ |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 3881 | static bool paste_tokens(Token **head, const struct tokseq_match *m, |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3882 | size_t mnum, bool handle_explicit) |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3883 | { |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3884 | Token *tok, *next, **prev_next, **prev_nonspace; |
| 3885 | bool pasted = false; |
| 3886 | char *buf, *p; |
| 3887 | size_t len, i; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3888 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3889 | /* |
| 3890 | * The last token before pasting. We need it |
| 3891 | * to be able to connect new handled tokens. |
| 3892 | * In other words if there were a tokens stream |
| 3893 | * |
| 3894 | * A -> B -> C -> D |
| 3895 | * |
| 3896 | * and we've joined tokens B and C, the resulting |
| 3897 | * stream should be |
| 3898 | * |
| 3899 | * A -> BC -> D |
| 3900 | */ |
| 3901 | tok = *head; |
| 3902 | prev_next = NULL; |
| 3903 | |
| 3904 | if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE)) |
| 3905 | prev_nonspace = head; |
| 3906 | else |
| 3907 | prev_nonspace = NULL; |
| 3908 | |
| 3909 | while (tok && (next = tok->next)) { |
| 3910 | |
| 3911 | switch (tok->type) { |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3912 | case TOK_WHITESPACE: |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3913 | /* Zap redundant whitespaces */ |
| 3914 | while (tok_type_(next, TOK_WHITESPACE)) |
| 3915 | next = delete_Token(next); |
| 3916 | tok->next = next; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3917 | break; |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3918 | |
| 3919 | case TOK_PASTE: |
| 3920 | /* Explicit pasting */ |
| 3921 | if (!handle_explicit) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3922 | break; |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3923 | next = delete_Token(tok); |
| 3924 | |
| 3925 | while (tok_type_(next, TOK_WHITESPACE)) |
| 3926 | next = delete_Token(next); |
| 3927 | |
| 3928 | if (!pasted) |
| 3929 | pasted = true; |
| 3930 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3931 | /* Left pasting token is start of line */ |
| 3932 | if (!prev_nonspace) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3933 | nasm_fatal("No lvalue found on pasting"); |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3934 | |
Cyrill Gorcunov | 8b5c9fb | 2013-02-04 01:24:54 +0400 | [diff] [blame] | 3935 | /* |
| 3936 | * No ending token, this might happen in two |
| 3937 | * cases |
| 3938 | * |
| 3939 | * 1) There indeed no right token at all |
| 3940 | * 2) There is a bare "%define ID" statement, |
| 3941 | * and @ID does expand to whitespace. |
| 3942 | * |
| 3943 | * So technically we need to do a grammar analysis |
| 3944 | * in another stage of parsing, but for now lets don't |
| 3945 | * change the behaviour people used to. Simply allow |
| 3946 | * whitespace after paste token. |
| 3947 | */ |
| 3948 | if (!next) { |
| 3949 | /* |
| 3950 | * Zap ending space tokens and that's all. |
| 3951 | */ |
| 3952 | tok = (*prev_nonspace)->next; |
| 3953 | while (tok_type_(tok, TOK_WHITESPACE)) |
| 3954 | tok = delete_Token(tok); |
| 3955 | tok = *prev_nonspace; |
| 3956 | tok->next = NULL; |
| 3957 | break; |
| 3958 | } |
| 3959 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3960 | tok = *prev_nonspace; |
| 3961 | while (tok_type_(tok, TOK_WHITESPACE)) |
| 3962 | tok = delete_Token(tok); |
| 3963 | len = strlen(tok->text); |
| 3964 | len += strlen(next->text); |
| 3965 | |
| 3966 | p = buf = nasm_malloc(len + 1); |
| 3967 | strcpy(p, tok->text); |
| 3968 | p = strchr(p, '\0'); |
| 3969 | strcpy(p, next->text); |
| 3970 | |
| 3971 | delete_Token(tok); |
| 3972 | |
| 3973 | tok = tokenize(buf); |
| 3974 | nasm_free(buf); |
| 3975 | |
| 3976 | *prev_nonspace = tok; |
| 3977 | while (tok && tok->next) |
| 3978 | tok = tok->next; |
| 3979 | |
| 3980 | tok->next = delete_Token(next); |
| 3981 | |
| 3982 | /* Restart from pasted tokens head */ |
| 3983 | tok = *prev_nonspace; |
| 3984 | break; |
| 3985 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3986 | default: |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3987 | /* implicit pasting */ |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 3988 | for (i = 0; i < mnum; i++) { |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3989 | if (!(PP_CONCAT_MATCH(tok, m[i].mask_head))) |
| 3990 | continue; |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 3991 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3992 | len = 0; |
| 3993 | while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) { |
| 3994 | len += strlen(next->text); |
| 3995 | next = next->next; |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 3996 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3997 | |
Cyrill Gorcunov | 6f8109e | 2017-10-22 21:26:36 +0300 | [diff] [blame] | 3998 | /* No match or no text to process */ |
| 3999 | if (tok == next || len == 0) |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 4000 | break; |
| 4001 | |
| 4002 | len += strlen(tok->text); |
| 4003 | p = buf = nasm_malloc(len + 1); |
| 4004 | |
Adam Majer | 1a06943 | 2017-07-25 11:12:35 +0200 | [diff] [blame] | 4005 | strcpy(p, tok->text); |
| 4006 | p = strchr(p, '\0'); |
| 4007 | tok = delete_Token(tok); |
| 4008 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 4009 | while (tok != next) { |
Adam Majer | 1a06943 | 2017-07-25 11:12:35 +0200 | [diff] [blame] | 4010 | if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) { |
| 4011 | strcpy(p, tok->text); |
| 4012 | p = strchr(p, '\0'); |
| 4013 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 4014 | tok = delete_Token(tok); |
| 4015 | } |
| 4016 | |
| 4017 | tok = tokenize(buf); |
| 4018 | nasm_free(buf); |
| 4019 | |
| 4020 | if (prev_next) |
| 4021 | *prev_next = tok; |
| 4022 | else |
| 4023 | *head = tok; |
| 4024 | |
| 4025 | /* |
| 4026 | * Connect pasted into original stream, |
| 4027 | * ie A -> new-tokens -> B |
| 4028 | */ |
| 4029 | while (tok && tok->next) |
| 4030 | tok = tok->next; |
| 4031 | tok->next = next; |
| 4032 | |
| 4033 | if (!pasted) |
| 4034 | pasted = true; |
| 4035 | |
| 4036 | /* Restart from pasted tokens head */ |
| 4037 | tok = prev_next ? *prev_next : *head; |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4038 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 4039 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4040 | break; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 4041 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 4042 | |
| 4043 | prev_next = &tok->next; |
| 4044 | |
| 4045 | if (tok->next && |
| 4046 | !tok_type_(tok->next, TOK_WHITESPACE) && |
| 4047 | !tok_type_(tok->next, TOK_PASTE)) |
| 4048 | prev_nonspace = prev_next; |
| 4049 | |
| 4050 | tok = tok->next; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 4051 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 4052 | |
| 4053 | return pasted; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 4054 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4055 | |
| 4056 | /* |
| 4057 | * expands to a list of tokens from %{x:y} |
| 4058 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4059 | static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4060 | { |
| 4061 | Token *t = tline, **tt, *tm, *head; |
| 4062 | char *pos; |
| 4063 | int fst, lst, j, i; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4064 | |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4065 | pos = strchr(tline->text, ':'); |
| 4066 | nasm_assert(pos); |
| 4067 | |
| 4068 | lst = atoi(pos + 1); |
| 4069 | fst = atoi(tline->text + 1); |
| 4070 | |
| 4071 | /* |
| 4072 | * only macros params are accounted so |
| 4073 | * if someone passes %0 -- we reject such |
| 4074 | * value(s) |
| 4075 | */ |
| 4076 | if (lst == 0 || fst == 0) |
| 4077 | goto err; |
| 4078 | |
| 4079 | /* the values should be sane */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4080 | if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) || |
| 4081 | (lst > (int)mac->nparam || lst < (-(int)mac->nparam))) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4082 | goto err; |
| 4083 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4084 | fst = fst < 0 ? fst + (int)mac->nparam + 1: fst; |
| 4085 | lst = lst < 0 ? lst + (int)mac->nparam + 1: lst; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4086 | |
| 4087 | /* counted from zero */ |
| 4088 | fst--, lst--; |
| 4089 | |
| 4090 | /* |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 4091 | * It will be at least one token. Note we |
| 4092 | * need to scan params until separator, otherwise |
| 4093 | * only first token will be passed. |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4094 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4095 | tm = mac->params[(fst + mac->rotate) % mac->nparam]; |
Cyrill Gorcunov | 67f2ca2 | 2018-10-13 19:41:01 +0300 | [diff] [blame] | 4096 | if (!tm) |
| 4097 | goto err; |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4098 | head = dup_Token(NULL, tm); |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 4099 | tt = &head->next, tm = tm->next; |
| 4100 | while (tok_isnt_(tm, ",")) { |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4101 | t = dup_Token(NULL, tm); |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 4102 | *tt = t, tt = &t->next, tm = tm->next; |
| 4103 | } |
| 4104 | |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4105 | if (fst < lst) { |
| 4106 | for (i = fst + 1; i <= lst; i++) { |
| 4107 | t = new_Token(NULL, TOK_OTHER, ",", 0); |
| 4108 | *tt = t, tt = &t->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4109 | j = (i + mac->rotate) % mac->nparam; |
| 4110 | tm = mac->params[j]; |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 4111 | while (tok_isnt_(tm, ",")) { |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4112 | t = dup_Token(NULL, tm); |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 4113 | *tt = t, tt = &t->next, tm = tm->next; |
| 4114 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4115 | } |
| 4116 | } else { |
| 4117 | for (i = fst - 1; i >= lst; i--) { |
| 4118 | t = new_Token(NULL, TOK_OTHER, ",", 0); |
| 4119 | *tt = t, tt = &t->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4120 | j = (i + mac->rotate) % mac->nparam; |
| 4121 | tm = mac->params[j]; |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 4122 | while (tok_isnt_(tm, ",")) { |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4123 | t = dup_Token(NULL, tm); |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 4124 | *tt = t, tt = &t->next, tm = tm->next; |
| 4125 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4126 | } |
| 4127 | } |
| 4128 | |
| 4129 | *last = tt; |
| 4130 | return head; |
| 4131 | |
| 4132 | err: |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4133 | nasm_nonfatal("`%%{%s}': macro parameters out of range", |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4134 | &tline->text[1]); |
| 4135 | return tline; |
| 4136 | } |
| 4137 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4138 | /* |
| 4139 | * Expand MMacro-local things: parameter references (%0, %n, %+n, |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 4140 | * %-n) and MMacro-local identifiers (%%foo) as well as |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4141 | * macro indirection (%[...]) and range (%{..:..}). |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4142 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4143 | static Token *expand_mmac_params(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4144 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4145 | Token *t, *tt, **tail, *thead; |
H. Peter Anvin | 6125b62 | 2009-04-08 14:02:25 -0700 | [diff] [blame] | 4146 | bool changed = false; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4147 | char *pos; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4148 | |
| 4149 | tail = &thead; |
| 4150 | thead = NULL; |
| 4151 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4152 | while (tline) { |
Cyrill Gorcunov | 661f723 | 2018-10-28 20:39:34 +0300 | [diff] [blame] | 4153 | if (tline->type == TOK_PREPROC_ID && tline->text && tline->text[0] && |
Cyrill Gorcunov | ca61119 | 2010-06-04 09:22:12 +0400 | [diff] [blame] | 4154 | (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) || |
| 4155 | (tline->text[1] >= '0' && tline->text[1] <= '9') || |
| 4156 | tline->text[1] == '%')) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4157 | char *text = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4158 | int type = 0, cc; /* type = 0 to placate optimisers */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4159 | char tmpbuf[30]; |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 4160 | unsigned int n; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4161 | int i; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4162 | MMacro *mac; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4163 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4164 | t = tline; |
| 4165 | tline = tline->next; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4166 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4167 | mac = istk->mstk; |
| 4168 | while (mac && !mac->name) /* avoid mistaking %reps for macros */ |
| 4169 | mac = mac->next_active; |
| 4170 | if (!mac) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4171 | nasm_nonfatal("`%s': not in a macro call", t->text); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4172 | } else { |
| 4173 | pos = strchr(t->text, ':'); |
| 4174 | if (!pos) { |
| 4175 | switch (t->text[1]) { |
| 4176 | /* |
| 4177 | * We have to make a substitution of one of the |
| 4178 | * forms %1, %-1, %+1, %%foo, %0. |
| 4179 | */ |
| 4180 | case '0': |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4181 | type = TOK_NUMBER; |
| 4182 | snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam); |
| 4183 | text = nasm_strdup(tmpbuf); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4184 | break; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4185 | case '%': |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4186 | type = TOK_ID; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4187 | snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".", |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4188 | mac->unique); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4189 | text = nasm_strcat(tmpbuf, t->text + 2); |
| 4190 | break; |
| 4191 | case '-': |
| 4192 | n = atoi(t->text + 2) - 1; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4193 | if (n >= mac->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4194 | tt = NULL; |
| 4195 | else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4196 | if (mac->nparam > 1) |
| 4197 | n = (n + mac->rotate) % mac->nparam; |
| 4198 | tt = mac->params[n]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4199 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4200 | cc = find_cc(tt); |
| 4201 | if (cc == -1) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4202 | nasm_nonfatal("macro parameter %d is not a condition code", |
| 4203 | n + 1); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4204 | text = NULL; |
| 4205 | } else { |
| 4206 | type = TOK_ID; |
| 4207 | if (inverse_ccs[cc] == -1) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4208 | nasm_nonfatal("condition code `%s' is not invertible", |
| 4209 | conditions[cc]); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4210 | text = NULL; |
| 4211 | } else |
| 4212 | text = nasm_strdup(conditions[inverse_ccs[cc]]); |
| 4213 | } |
| 4214 | break; |
| 4215 | case '+': |
| 4216 | n = atoi(t->text + 2) - 1; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4217 | if (n >= mac->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4218 | tt = NULL; |
| 4219 | else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4220 | if (mac->nparam > 1) |
| 4221 | n = (n + mac->rotate) % mac->nparam; |
| 4222 | tt = mac->params[n]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4223 | } |
| 4224 | cc = find_cc(tt); |
| 4225 | if (cc == -1) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4226 | nasm_nonfatal("macro parameter %d is not a condition code", |
| 4227 | n + 1); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4228 | text = NULL; |
| 4229 | } else { |
| 4230 | type = TOK_ID; |
| 4231 | text = nasm_strdup(conditions[cc]); |
| 4232 | } |
| 4233 | break; |
| 4234 | default: |
| 4235 | n = atoi(t->text + 1) - 1; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4236 | if (n >= mac->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4237 | tt = NULL; |
| 4238 | else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4239 | if (mac->nparam > 1) |
| 4240 | n = (n + mac->rotate) % mac->nparam; |
| 4241 | tt = mac->params[n]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4242 | } |
| 4243 | if (tt) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4244 | for (i = 0; i < mac->paramlen[n]; i++) { |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4245 | *tail = dup_Token(NULL, tt); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4246 | tail = &(*tail)->next; |
| 4247 | tt = tt->next; |
| 4248 | } |
| 4249 | } |
| 4250 | text = NULL; /* we've done it here */ |
| 4251 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4252 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4253 | } else { |
| 4254 | /* |
| 4255 | * seems we have a parameters range here |
| 4256 | */ |
| 4257 | Token *head, **last; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4258 | head = expand_mmac_params_range(mac, t, &last); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4259 | if (head != t) { |
| 4260 | *tail = head; |
| 4261 | *last = tline; |
| 4262 | tline = head; |
| 4263 | text = NULL; |
| 4264 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4265 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4266 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4267 | if (!text) { |
| 4268 | delete_Token(t); |
| 4269 | } else { |
| 4270 | *tail = t; |
| 4271 | tail = &t->next; |
| 4272 | t->type = type; |
| 4273 | nasm_free(t->text); |
| 4274 | t->text = text; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4275 | t->len = strlen(text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4276 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4277 | changed = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4278 | continue; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4279 | } else if (tline->type == TOK_INDIRECT) { |
| 4280 | t = tline; |
| 4281 | tline = tline->next; |
| 4282 | tt = tokenize(t->text); |
| 4283 | tt = expand_mmac_params(tt); |
| 4284 | tt = expand_smacro(tt); |
| 4285 | *tail = tt; |
| 4286 | while (tt) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4287 | tail = &tt->next; |
| 4288 | tt = tt->next; |
| 4289 | } |
| 4290 | delete_Token(t); |
| 4291 | changed = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4292 | } else { |
| 4293 | t = *tail = tline; |
| 4294 | tline = tline->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4295 | tail = &t->next; |
| 4296 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4297 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4298 | *tail = NULL; |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 4299 | |
Cyrill Gorcunov | c6a742c | 2011-06-27 01:23:09 +0400 | [diff] [blame] | 4300 | if (changed) { |
| 4301 | const struct tokseq_match t[] = { |
| 4302 | { |
| 4303 | PP_CONCAT_MASK(TOK_ID) | |
| 4304 | PP_CONCAT_MASK(TOK_FLOAT), /* head */ |
| 4305 | PP_CONCAT_MASK(TOK_ID) | |
| 4306 | PP_CONCAT_MASK(TOK_NUMBER) | |
| 4307 | PP_CONCAT_MASK(TOK_FLOAT) | |
| 4308 | PP_CONCAT_MASK(TOK_OTHER) /* tail */ |
| 4309 | }, |
| 4310 | { |
| 4311 | PP_CONCAT_MASK(TOK_NUMBER), /* head */ |
| 4312 | PP_CONCAT_MASK(TOK_NUMBER) /* tail */ |
| 4313 | } |
| 4314 | }; |
| 4315 | paste_tokens(&thead, t, ARRAY_SIZE(t), false); |
| 4316 | } |
H. Peter Anvin | 6125b62 | 2009-04-08 14:02:25 -0700 | [diff] [blame] | 4317 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4318 | return thead; |
| 4319 | } |
| 4320 | |
| 4321 | /* |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4322 | * Expand *one* single-line macro instance. If the first token is not |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4323 | * a macro at all, it is simply copied to the output and the pointer |
| 4324 | * advanced. tpp should be a pointer to a pointer (usually the next |
| 4325 | * pointer of the previous token) to the first token. **tpp is updated |
| 4326 | * to point to the last token of the expansion, and *tpp updated to |
| 4327 | * point to the next pointer of the first token of the expansion. |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4328 | * |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4329 | * If the expansion is empty, *tpp will be unchanged but **tpp will |
| 4330 | * be advanced past the macro call. |
| 4331 | * |
| 4332 | * The return value equals **tpp. |
| 4333 | * |
| 4334 | * Return false if no expansion took place; true if it did. |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4335 | * |
| 4336 | */ |
| 4337 | static Token *expand_smacro_noreset(Token * tline); |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4338 | static int64_t smacro_deadman; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4339 | |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4340 | static bool expand_one_smacro(Token ***tpp) |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4341 | { |
| 4342 | Token **params = NULL; |
| 4343 | const char *mname; |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4344 | Token *tline = **tpp; |
| 4345 | Token *mstart = **tpp; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4346 | SMacro *head, *m; |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4347 | unsigned int i; |
| 4348 | Token *t, *tup, *ttail; |
| 4349 | unsigned int nparam = 0; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4350 | |
| 4351 | if (!tline) |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4352 | return false; /* Empty line, nothing to do */ |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4353 | |
| 4354 | mname = tline->text; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4355 | |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4356 | if (--smacro_deadman <= 0) { |
| 4357 | if (smacro_deadman == 0) |
| 4358 | nasm_nonfatal("interminable macro recursion"); |
| 4359 | goto not_a_macro; |
| 4360 | } else if (tline->type == TOK_ID) { |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4361 | head = (SMacro *)hash_findix(&smacros, mname); |
| 4362 | } else if (tline->type == TOK_PREPROC_ID) { |
| 4363 | Context *ctx = get_ctx(mname, &mname); |
| 4364 | head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL; |
| 4365 | } else { |
| 4366 | goto not_a_macro; |
| 4367 | } |
| 4368 | |
| 4369 | /* |
| 4370 | * We've hit an identifier of some sort. First check whether the |
| 4371 | * identifier is a single-line macro at all, then think about |
| 4372 | * checking for parameters if necessary. |
| 4373 | */ |
| 4374 | list_for_each(m, head) { |
| 4375 | if (!mstrcmp(m->name, mname, m->casesense)) |
| 4376 | break; |
| 4377 | } |
| 4378 | |
| 4379 | if (!m) { |
| 4380 | goto not_a_macro; |
| 4381 | } |
| 4382 | |
| 4383 | /* Parse parameters, if applicable */ |
| 4384 | |
| 4385 | params = NULL; |
| 4386 | nparam = 0; |
| 4387 | |
| 4388 | if (m->nparam == 0) { |
| 4389 | /* |
| 4390 | * Simple case: the macro is parameterless. |
| 4391 | * Nothing to parse; the expansion code will |
| 4392 | * drop the macro name token. |
| 4393 | */ |
| 4394 | } else { |
| 4395 | /* |
| 4396 | * Complicated case: at least one macro with this name |
| 4397 | * exists and takes parameters. We must find the |
| 4398 | * parameters in the call, count them, find the SMacro |
| 4399 | * that corresponds to that form of the macro call, and |
| 4400 | * substitute for the parameters when we expand. What a |
| 4401 | * pain. |
| 4402 | */ |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4403 | Token **phead, **pep; |
| 4404 | int paren = 1; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4405 | int white = 0; |
| 4406 | int brackets = 0; |
| 4407 | bool bracketed = false; |
| 4408 | bool bad_bracket = false; |
| 4409 | unsigned int sparam = PARAM_DELTA; |
| 4410 | |
| 4411 | tline = tline->next; |
| 4412 | |
| 4413 | while (tok_type_(tline, TOK_WHITESPACE)) { |
| 4414 | tline = tline->next; |
| 4415 | } |
| 4416 | if (!tok_is_(tline, "(")) { |
| 4417 | /* |
| 4418 | * This macro wasn't called with parameters: ignore |
| 4419 | * the call. (Behaviour borrowed from gnu cpp.) |
| 4420 | */ |
| 4421 | goto not_a_macro; |
| 4422 | } |
| 4423 | |
| 4424 | paren = 1; |
| 4425 | nparam = 0; |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4426 | nasm_newn(params, sparam); |
| 4427 | phead = pep = ¶ms[0]; |
| 4428 | *pep = NULL; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4429 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4430 | while (paren) { |
| 4431 | bool skip; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4432 | char ch; |
| 4433 | |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4434 | tline = tline->next; |
| 4435 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4436 | if (!tline) { |
| 4437 | nasm_nonfatal("macro call expects terminating `)'"); |
| 4438 | goto not_a_macro; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4439 | } |
| 4440 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4441 | ch = 0; |
| 4442 | skip = false; |
| 4443 | |
| 4444 | switch (tline->type) { |
| 4445 | case TOK_OTHER: |
| 4446 | if (!tline->text[1]) |
| 4447 | ch = tline->text[0]; |
| 4448 | break; |
| 4449 | |
| 4450 | case TOK_WHITESPACE: |
| 4451 | if (brackets || *phead) |
| 4452 | white++; /* Keep interior whitespace */ |
| 4453 | skip = true; |
| 4454 | break; |
| 4455 | |
| 4456 | default: |
| 4457 | break; |
| 4458 | } |
| 4459 | |
| 4460 | switch (ch) { |
| 4461 | case ',': |
| 4462 | if (!brackets) { |
| 4463 | if (++nparam >= sparam) { |
| 4464 | sparam += PARAM_DELTA; |
| 4465 | params = nasm_realloc(params, sparam * sizeof *params); |
| 4466 | } |
| 4467 | pep = ¶ms[nparam]; |
| 4468 | *pep = NULL; |
| 4469 | bracketed = false; |
| 4470 | skip = true; |
| 4471 | } |
| 4472 | break; |
| 4473 | |
| 4474 | case '{': |
| 4475 | if (!bracketed) { |
| 4476 | bracketed = !*phead; |
| 4477 | skip = true; |
| 4478 | } |
| 4479 | brackets++; |
| 4480 | break; |
| 4481 | |
| 4482 | case '}': |
| 4483 | if (brackets > 0) { |
| 4484 | if (!--brackets) |
| 4485 | skip = bracketed; |
| 4486 | } |
| 4487 | break; |
| 4488 | |
| 4489 | case '(': |
| 4490 | if (!brackets) |
| 4491 | paren++; |
| 4492 | break; |
| 4493 | |
| 4494 | case ')': |
| 4495 | if (!brackets) { |
| 4496 | paren--; |
| 4497 | if (!paren) { |
| 4498 | skip = true; |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 4499 | /* Count the final argument */ |
| 4500 | nparam++; |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4501 | } |
| 4502 | } |
| 4503 | break; |
| 4504 | |
| 4505 | default: |
| 4506 | break; /* Normal token */ |
| 4507 | } |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4508 | |
| 4509 | if (!skip) { |
| 4510 | Token *t; |
| 4511 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4512 | bad_bracket |= bracketed && !brackets; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4513 | |
| 4514 | if (white) { |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4515 | *pep = t = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4516 | pep = &t->next; |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4517 | white = 0; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4518 | } |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4519 | *pep = t = dup_Token(NULL, tline); |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4520 | pep = &t->next; |
| 4521 | white = 0; |
| 4522 | } |
| 4523 | } |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4524 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4525 | /* |
| 4526 | * Look for a macro matching in both name and parameter count. |
| 4527 | * We already know any matches cannot be anywhere before the |
| 4528 | * current position of "m", so there is no reason to |
| 4529 | * backtrack. |
| 4530 | */ |
| 4531 | while (1) { |
| 4532 | if (!m) { |
| 4533 | /*! |
| 4534 | *!macro-params-single [on] single-line macro calls with wrong parameter count |
| 4535 | *! warns about \i{single-line macros} being invoked |
| 4536 | *! with the wrong number of parameters. |
| 4537 | */ |
| 4538 | nasm_warn(WARN_MACRO_PARAMS_SINGLE, |
| 4539 | "single-line macro `%s' exists, " |
| 4540 | "but not taking %d parameter%s", |
| 4541 | mname, nparam, (nparam == 1) ? "" : "s"); |
| 4542 | goto not_a_macro; |
| 4543 | } |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4544 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4545 | if (m->nparam == nparam && !mstrcmp(m->name, mname, m->casesense)) |
| 4546 | break; /* It's good */ |
| 4547 | |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4548 | m = m->next; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4549 | } |
| 4550 | } |
| 4551 | |
| 4552 | if (m->in_progress) |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4553 | goto not_a_macro; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4554 | |
| 4555 | /* Expand each parameter */ |
| 4556 | m->in_progress = true; |
| 4557 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4558 | if (m->eval_param) { |
| 4559 | for (i = 0; i < nparam; i++) { |
| 4560 | if (m->eval_param[i]) { |
| 4561 | /* Evaluate this parameter as a number */ |
| 4562 | struct ppscan pps; |
| 4563 | struct tokenval tokval; |
| 4564 | expr *evalresult; |
| 4565 | Token *eval_param; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4566 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4567 | pps.tptr = eval_param = expand_smacro_noreset(params[i]); |
| 4568 | pps.ntokens = -1; |
| 4569 | tokval.t_type = TOKEN_INVALID; |
| 4570 | evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL); |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4571 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4572 | free_tlist(eval_param); |
| 4573 | params[i] = NULL; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4574 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4575 | if (!evalresult) { |
| 4576 | /* Nothing meaningful to do */ |
| 4577 | } else if (tokval.t_type) { |
| 4578 | nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name); |
| 4579 | } else if (!is_simple(evalresult)) { |
| 4580 | nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name); |
| 4581 | } else { |
| 4582 | params[i] = make_tok_num(reloc_value(evalresult)); |
| 4583 | } |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4584 | } |
| 4585 | } |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4586 | } |
| 4587 | |
| 4588 | t = tline; |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4589 | tline = tline->next; /* Remove the macro call from the input */ |
| 4590 | t->next = NULL; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4591 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4592 | /* Note: we own the expansion this returns. */ |
| 4593 | t = m->expand(m, params, nparam); |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4594 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4595 | tup = NULL; |
| 4596 | ttail = NULL; /* Pointer to the last token of the expansion */ |
| 4597 | while (t) { |
| 4598 | enum pp_token_type type = t->type; |
| 4599 | Token *tnext = t->next; |
| 4600 | Token **tp; |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4601 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4602 | switch (type) { |
| 4603 | case TOK_PREPROC_Q: |
| 4604 | case TOK_PREPROC_QQ: |
| 4605 | t->type = TOK_ID; |
| 4606 | nasm_free(t->text); |
| 4607 | t->text = nasm_strdup(type == TOK_PREPROC_QQ ? m->name : mname); |
| 4608 | t->len = nasm_last_string_len(); |
| 4609 | t->next = tline; |
| 4610 | break; |
| 4611 | |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4612 | case TOK_ID: |
| 4613 | case TOK_PREPROC_ID: |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4614 | /* |
| 4615 | * Chain this into the target line *before* expanding, |
| 4616 | * that way we pick up any arguments to the new macro call, |
| 4617 | * if applicable. |
| 4618 | */ |
| 4619 | t->next = tline; |
| 4620 | tp = &t; |
| 4621 | expand_one_smacro(&tp); |
| 4622 | if (t == tline) |
| 4623 | t = NULL; /* Null expansion */ |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4624 | break; |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4625 | |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4626 | default: |
| 4627 | if (is_smac_param(t->type)) { |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4628 | unsigned int param = smac_nparam(t->type); |
| 4629 | nasm_assert(!tup && param < nparam); |
| 4630 | delete_Token(t); |
| 4631 | t = NULL; |
| 4632 | tup = tnext; |
| 4633 | tnext = dup_tlist_reverse(params[param], NULL); |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4634 | } else { |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4635 | t->next = tline; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4636 | } |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4637 | } |
| 4638 | |
| 4639 | if (t) { |
| 4640 | tline = t; |
| 4641 | if (!ttail) |
| 4642 | ttail = t; |
| 4643 | } |
| 4644 | |
| 4645 | if (tnext) { |
| 4646 | t = tnext; |
| 4647 | } else { |
| 4648 | t = tup; |
| 4649 | tup = NULL; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4650 | } |
| 4651 | } |
| 4652 | |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4653 | **tpp = tline; |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4654 | if (ttail) |
| 4655 | *tpp = &ttail->next; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4656 | |
| 4657 | m->in_progress = false; |
| 4658 | |
| 4659 | /* Don't do this until after expansion or we will clobber mname */ |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4660 | free_tlist(mstart); |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4661 | free_tlist_array(params, nparam); |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4662 | return true; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4663 | |
| 4664 | /* |
| 4665 | * No macro expansion needed; roll back to mstart (if necessary) |
| 4666 | * and then advance to the next input token. |
| 4667 | */ |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4668 | not_a_macro: |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4669 | *tpp = &mstart->next; |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4670 | free_tlist_array(params, nparam); |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4671 | return false; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4672 | } |
| 4673 | |
| 4674 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4675 | * Expand all single-line macro calls made in the given line. |
| 4676 | * Return the expanded version of the line. The original is deemed |
| 4677 | * to be destroyed in the process. (In reality we'll just move |
| 4678 | * Tokens from input to output a lot of the time, rather than |
| 4679 | * actually bothering to destroy and replicate.) |
| 4680 | */ |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4681 | static Token *expand_smacro(Token *tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4682 | { |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4683 | smacro_deadman = nasm_limit[LIMIT_MACROS]; |
| 4684 | return expand_smacro_noreset(tline); |
| 4685 | } |
| 4686 | |
| 4687 | static Token *expand_smacro_noreset(Token * tline) |
| 4688 | { |
| 4689 | Token *t, **tail, *thead; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4690 | Token *org_tline = tline; |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4691 | bool expanded; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4692 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4693 | /* |
| 4694 | * Trick: we should avoid changing the start token pointer since it can |
| 4695 | * be contained in "next" field of other token. Because of this |
| 4696 | * we allocate a copy of first token and work with it; at the end of |
| 4697 | * routine we copy it back |
| 4698 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4699 | if (org_tline) { |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4700 | tline = new_Token(org_tline->next, org_tline->type, |
| 4701 | org_tline->text, 0); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4702 | nasm_free(org_tline->text); |
| 4703 | org_tline->text = NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4704 | } |
| 4705 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4706 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4707 | /* |
| 4708 | * Pretend that we always end up doing expansion on the first pass; |
| 4709 | * that way %+ get processed. However, if we process %+ before the |
| 4710 | * first pass, we end up with things like MACRO %+ TAIL trying to |
| 4711 | * look up the macro "MACROTAIL", which we don't want. |
| 4712 | */ |
| 4713 | expanded = true; |
| 4714 | thead = tline; |
| 4715 | while (true) { |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4716 | static const struct tokseq_match tmatch[] = { |
Cyrill Gorcunov | c6a742c | 2011-06-27 01:23:09 +0400 | [diff] [blame] | 4717 | { |
| 4718 | PP_CONCAT_MASK(TOK_ID) | |
| 4719 | PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */ |
| 4720 | PP_CONCAT_MASK(TOK_ID) | |
| 4721 | PP_CONCAT_MASK(TOK_PREPROC_ID) | |
| 4722 | PP_CONCAT_MASK(TOK_NUMBER) /* tail */ |
| 4723 | } |
| 4724 | }; |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4725 | |
| 4726 | tail = &thead; |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4727 | while ((t = *tail)) { /* main token loop */ |
| 4728 | |
| 4729 | expanded |= expand_one_smacro(&tail); |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4730 | } |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4731 | |
| 4732 | if (!expanded) { |
| 4733 | tline = thead; |
| 4734 | break; /* Done! */ |
| 4735 | } |
| 4736 | |
| 4737 | /* |
| 4738 | * Now scan the entire line and look for successive TOK_IDs |
| 4739 | * that resulted after expansion (they can't be produced by |
| 4740 | * tokenize()). The successive TOK_IDs should be concatenated. |
| 4741 | * Also we look for %+ tokens and concatenate the tokens |
| 4742 | * before and after them (without white spaces in between). |
| 4743 | */ |
| 4744 | paste_tokens(&thead, tmatch, ARRAY_SIZE(tmatch), true); |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4745 | |
| 4746 | expanded = false; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4747 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4748 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4749 | if (org_tline) { |
| 4750 | if (thead) { |
| 4751 | *org_tline = *thead; |
| 4752 | /* since we just gave text to org_line, don't free it */ |
| 4753 | thead->text = NULL; |
| 4754 | delete_Token(thead); |
| 4755 | } else { |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4756 | /* |
| 4757 | * The expression expanded to empty line; |
| 4758 | * we can't return NULL because of the "trick" above. |
| 4759 | * Just set the line to a single WHITESPACE token. |
| 4760 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4761 | memset(org_tline, 0, sizeof(*org_tline)); |
| 4762 | org_tline->text = NULL; |
| 4763 | org_tline->type = TOK_WHITESPACE; |
| 4764 | } |
| 4765 | thead = org_tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4766 | } |
| 4767 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4768 | return thead; |
| 4769 | } |
| 4770 | |
| 4771 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4772 | * Similar to expand_smacro but used exclusively with macro identifiers |
| 4773 | * right before they are fetched in. The reason is that there can be |
| 4774 | * identifiers consisting of several subparts. We consider that if there |
| 4775 | * are more than one element forming the name, user wants a expansion, |
| 4776 | * otherwise it will be left as-is. Example: |
| 4777 | * |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4778 | * %define %$abc cde |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4779 | * |
| 4780 | * the identifier %$abc will be left as-is so that the handler for %define |
| 4781 | * will suck it and define the corresponding value. Other case: |
| 4782 | * |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4783 | * %define _%$abc cde |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4784 | * |
| 4785 | * In this case user wants name to be expanded *before* %define starts |
| 4786 | * working, so we'll expand %$abc into something (if it has a value; |
| 4787 | * otherwise it will be left as-is) then concatenate all successive |
| 4788 | * PP_IDs into one. |
| 4789 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4790 | static Token *expand_id(Token * tline) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4791 | { |
| 4792 | Token *cur, *oldnext = NULL; |
| 4793 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4794 | if (!tline || !tline->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4795 | return tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4796 | |
| 4797 | cur = tline; |
| 4798 | while (cur->next && |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4799 | (cur->next->type == TOK_ID || |
| 4800 | cur->next->type == TOK_PREPROC_ID |
| 4801 | || cur->next->type == TOK_NUMBER)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4802 | cur = cur->next; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4803 | |
| 4804 | /* If identifier consists of just one token, don't expand */ |
| 4805 | if (cur == tline) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4806 | return tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4807 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4808 | if (cur) { |
| 4809 | oldnext = cur->next; /* Detach the tail past identifier */ |
| 4810 | cur->next = NULL; /* so that expand_smacro stops here */ |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4811 | } |
| 4812 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4813 | tline = expand_smacro(tline); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4814 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4815 | if (cur) { |
| 4816 | /* expand_smacro possibly changhed tline; re-scan for EOL */ |
| 4817 | cur = tline; |
| 4818 | while (cur && cur->next) |
| 4819 | cur = cur->next; |
| 4820 | if (cur) |
| 4821 | cur->next = oldnext; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4822 | } |
| 4823 | |
| 4824 | return tline; |
| 4825 | } |
| 4826 | |
| 4827 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4828 | * Determine whether the given line constitutes a multi-line macro |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4829 | * 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] | 4830 | * to check for an initial label - that's taken care of in |
| 4831 | * expand_mmacro - but must check numbers of parameters. Guaranteed |
| 4832 | * to be called with tline->type == TOK_ID, so the putative macro |
| 4833 | * name is easy to find. |
| 4834 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4835 | static MMacro *is_mmacro(Token * tline, Token *** params_array) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4836 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4837 | MMacro *head, *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4838 | Token **params; |
| 4839 | int nparam; |
| 4840 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4841 | head = (MMacro *) hash_findix(&mmacros, tline->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4842 | |
| 4843 | /* |
| 4844 | * Efficiency: first we see if any macro exists with the given |
| 4845 | * name. If not, we can return NULL immediately. _Then_ we |
| 4846 | * count the parameters, and then we look further along the |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4847 | * list if necessary to find the proper MMacro. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4848 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4849 | list_for_each(m, head) |
| 4850 | if (!mstrcmp(m->name, tline->text, m->casesense)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4851 | break; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4852 | if (!m) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4853 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4854 | |
| 4855 | /* |
| 4856 | * OK, we have a potential macro. Count and demarcate the |
| 4857 | * parameters. |
| 4858 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4859 | count_mmac_params(tline->next, &nparam, ¶ms); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4860 | |
| 4861 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4862 | * 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] | 4863 | * structure that handles this number. |
| 4864 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4865 | while (m) { |
| 4866 | if (m->nparam_min <= nparam |
| 4867 | && (m->plus || nparam <= m->nparam_max)) { |
| 4868 | /* |
| 4869 | * This one is right. Just check if cycle removal |
| 4870 | * prohibits us using it before we actually celebrate... |
| 4871 | */ |
| 4872 | if (m->in_progress > m->max_depth) { |
| 4873 | if (m->max_depth > 0) { |
H. Peter Anvin (Intel) | 80c4f23 | 2018-12-14 13:33:24 -0800 | [diff] [blame] | 4874 | nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i", |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4875 | m->max_depth); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4876 | } |
| 4877 | nasm_free(params); |
| 4878 | return NULL; |
| 4879 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4880 | /* |
| 4881 | * It's right, and we can use it. Add its default |
| 4882 | * parameters to the end of our list if necessary. |
| 4883 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4884 | if (m->defaults && nparam < m->nparam_min + m->ndefs) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4885 | params = |
| 4886 | nasm_realloc(params, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4887 | ((m->nparam_min + m->ndefs + |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4888 | 1) * sizeof(*params))); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4889 | while (nparam < m->nparam_min + m->ndefs) { |
| 4890 | params[nparam] = m->defaults[nparam - m->nparam_min]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4891 | nparam++; |
| 4892 | } |
| 4893 | } |
| 4894 | /* |
| 4895 | * If we've gone over the maximum parameter count (and |
| 4896 | * we're in Plus mode), ignore parameters beyond |
| 4897 | * nparam_max. |
| 4898 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4899 | if (m->plus && nparam > m->nparam_max) |
| 4900 | nparam = m->nparam_max; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4901 | /* |
| 4902 | * Then terminate the parameter list, and leave. |
| 4903 | */ |
| 4904 | if (!params) { /* need this special case */ |
| 4905 | params = nasm_malloc(sizeof(*params)); |
| 4906 | nparam = 0; |
| 4907 | } |
| 4908 | params[nparam] = NULL; |
| 4909 | *params_array = params; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4910 | return m; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4911 | } |
| 4912 | /* |
| 4913 | * This one wasn't right: look for the next one with the |
| 4914 | * same name. |
| 4915 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4916 | list_for_each(m, m->next) |
| 4917 | if (!mstrcmp(m->name, tline->text, m->casesense)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4918 | break; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4919 | } |
| 4920 | |
| 4921 | /* |
| 4922 | * After all that, we didn't find one with the right number of |
| 4923 | * parameters. Issue a warning, and fail to expand the macro. |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4924 | *! |
| 4925 | *!macro-params-multi [on] multi-line macro calls with wrong parameter count |
| 4926 | *! warns about \i{multi-line macros} being invoked |
| 4927 | *! with the wrong number of parameters. See \k{mlmacover} for an |
| 4928 | *! example of why you might want to disable this warning. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4929 | */ |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4930 | nasm_warn(WARN_MACRO_PARAMS_MULTI, |
| 4931 | "multi-line macro `%s' exists, but not taking %d parameter%s", |
| 4932 | tline->text, nparam, (nparam == 1) ? "" : "s"); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4933 | nasm_free(params); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4934 | return NULL; |
| 4935 | } |
| 4936 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4937 | |
| 4938 | /* |
| 4939 | * Save MMacro invocation specific fields in |
| 4940 | * preparation for a recursive macro expansion |
| 4941 | */ |
| 4942 | static void push_mmacro(MMacro *m) |
| 4943 | { |
| 4944 | MMacroInvocation *i; |
| 4945 | |
| 4946 | i = nasm_malloc(sizeof(MMacroInvocation)); |
| 4947 | i->prev = m->prev; |
| 4948 | i->params = m->params; |
| 4949 | i->iline = m->iline; |
| 4950 | i->nparam = m->nparam; |
| 4951 | i->rotate = m->rotate; |
| 4952 | i->paramlen = m->paramlen; |
| 4953 | i->unique = m->unique; |
| 4954 | i->condcnt = m->condcnt; |
| 4955 | m->prev = i; |
| 4956 | } |
| 4957 | |
| 4958 | |
| 4959 | /* |
| 4960 | * Restore MMacro invocation specific fields that were |
| 4961 | * saved during a previous recursive macro expansion |
| 4962 | */ |
| 4963 | static void pop_mmacro(MMacro *m) |
| 4964 | { |
| 4965 | MMacroInvocation *i; |
| 4966 | |
| 4967 | if (m->prev) { |
| 4968 | i = m->prev; |
| 4969 | m->prev = i->prev; |
| 4970 | m->params = i->params; |
| 4971 | m->iline = i->iline; |
| 4972 | m->nparam = i->nparam; |
| 4973 | m->rotate = i->rotate; |
| 4974 | m->paramlen = i->paramlen; |
| 4975 | m->unique = i->unique; |
| 4976 | m->condcnt = i->condcnt; |
| 4977 | nasm_free(i); |
| 4978 | } |
| 4979 | } |
| 4980 | |
| 4981 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4982 | /* |
| 4983 | * Expand the multi-line macro call made by the given line, if |
| 4984 | * 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] | 4985 | * istk->expansion and return 1. Otherwise return 0. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4986 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4987 | static int expand_mmacro(Token * tline) |
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 | Token *startline = tline; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4990 | Token *label = NULL; |
| 4991 | int dont_prepend = 0; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4992 | Token **params, *t, *tt; |
| 4993 | MMacro *m; |
| 4994 | Line *l, *ll; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4995 | int i, nparam, *paramlen; |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 4996 | const char *mname; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4997 | |
| 4998 | t = tline; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4999 | skip_white_(t); |
H. Peter Anvin | ce2233b | 2008-05-25 21:57:00 -0700 | [diff] [blame] | 5000 | /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */ |
H. Peter Anvin | dce1e2f | 2002-04-30 21:06:37 +0000 | [diff] [blame] | 5001 | 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] | 5002 | return 0; |
| 5003 | m = is_mmacro(t, ¶ms); |
| 5004 | if (m) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5005 | mname = t->text; |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 5006 | } else { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5007 | Token *last; |
| 5008 | /* |
| 5009 | * We have an id which isn't a macro call. We'll assume |
| 5010 | * it might be a label; we'll also check to see if a |
| 5011 | * colon follows it. Then, if there's another id after |
| 5012 | * that lot, we'll check it again for macro-hood. |
| 5013 | */ |
| 5014 | label = last = t; |
| 5015 | t = t->next; |
| 5016 | if (tok_type_(t, TOK_WHITESPACE)) |
| 5017 | last = t, t = t->next; |
| 5018 | if (tok_is_(t, ":")) { |
| 5019 | dont_prepend = 1; |
| 5020 | last = t, t = t->next; |
| 5021 | if (tok_type_(t, TOK_WHITESPACE)) |
| 5022 | last = t, t = t->next; |
| 5023 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5024 | if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, ¶ms))) |
| 5025 | return 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5026 | last->next = NULL; |
Keith Kanios | 891775e | 2009-07-11 06:08:54 -0500 | [diff] [blame] | 5027 | mname = t->text; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5028 | tline = t; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5029 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5030 | |
| 5031 | /* |
| 5032 | * Fix up the parameters: this involves stripping leading and |
| 5033 | * trailing whitespace, then stripping braces if they are |
| 5034 | * present. |
| 5035 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5036 | for (nparam = 0; params[nparam]; nparam++) ; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5037 | paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5038 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5039 | for (i = 0; params[i]; i++) { |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 5040 | int brace = 0; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5041 | int comma = (!m->plus || i < nparam - 1); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5042 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5043 | t = params[i]; |
| 5044 | skip_white_(t); |
| 5045 | if (tok_is_(t, "{")) |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 5046 | t = t->next, brace++, comma = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5047 | params[i] = t; |
| 5048 | paramlen[i] = 0; |
| 5049 | while (t) { |
| 5050 | if (comma && t->type == TOK_OTHER && !strcmp(t->text, ",")) |
| 5051 | break; /* ... because we have hit a comma */ |
| 5052 | if (comma && t->type == TOK_WHITESPACE |
| 5053 | && tok_is_(t->next, ",")) |
| 5054 | break; /* ... or a space then a comma */ |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 5055 | if (brace && t->type == TOK_OTHER) { |
| 5056 | if (t->text[0] == '{') |
| 5057 | brace++; /* ... or a nested opening brace */ |
| 5058 | else if (t->text[0] == '}') |
| 5059 | if (!--brace) |
| 5060 | break; /* ... or a brace */ |
| 5061 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5062 | t = t->next; |
| 5063 | paramlen[i]++; |
| 5064 | } |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 5065 | if (brace) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 5066 | nasm_nonfatal("macro params should be enclosed in braces"); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5067 | } |
| 5068 | |
| 5069 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5070 | * OK, we have a MMacro structure together with a set of |
| 5071 | * parameters. We must now go through the expansion and push |
| 5072 | * copies of each Line on to istk->expansion. Substitution of |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 5073 | * parameter tokens and macro-local tokens doesn't get done |
| 5074 | * until the single-line macro substitution process; this is |
| 5075 | * because delaying them allows us to change the semantics |
| 5076 | * later through %rotate. |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5077 | * |
| 5078 | * First, push an end marker on to istk->expansion, mark this |
| 5079 | * macro as in progress, and set up its invocation-specific |
| 5080 | * variables. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5081 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5082 | ll = nasm_malloc(sizeof(Line)); |
| 5083 | ll->next = istk->expansion; |
| 5084 | ll->finishes = m; |
| 5085 | ll->first = NULL; |
| 5086 | istk->expansion = ll; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5087 | |
| 5088 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5089 | * Save the previous MMacro expansion in the case of |
| 5090 | * macro recursion |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5091 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5092 | if (m->max_depth && m->in_progress) |
| 5093 | push_mmacro(m); |
| 5094 | |
| 5095 | m->in_progress ++; |
| 5096 | m->params = params; |
| 5097 | m->iline = tline; |
| 5098 | m->nparam = nparam; |
| 5099 | m->rotate = 0; |
| 5100 | m->paramlen = paramlen; |
| 5101 | m->unique = unique++; |
| 5102 | m->lineno = 0; |
| 5103 | m->condcnt = 0; |
| 5104 | |
| 5105 | m->next_active = istk->mstk; |
| 5106 | istk->mstk = m; |
| 5107 | |
| 5108 | list_for_each(l, m->expansion) { |
| 5109 | Token **tail; |
| 5110 | |
| 5111 | ll = nasm_malloc(sizeof(Line)); |
| 5112 | ll->finishes = NULL; |
| 5113 | ll->next = istk->expansion; |
| 5114 | istk->expansion = ll; |
| 5115 | tail = &ll->first; |
| 5116 | |
| 5117 | list_for_each(t, l->first) { |
| 5118 | Token *x = t; |
| 5119 | switch (t->type) { |
| 5120 | case TOK_PREPROC_Q: |
| 5121 | tt = *tail = new_Token(NULL, TOK_ID, mname, 0); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5122 | break; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5123 | case TOK_PREPROC_QQ: |
| 5124 | tt = *tail = new_Token(NULL, TOK_ID, m->name, 0); |
| 5125 | break; |
| 5126 | case TOK_PREPROC_ID: |
| 5127 | if (t->text[1] == '0' && t->text[2] == '0') { |
| 5128 | dont_prepend = -1; |
| 5129 | x = label; |
| 5130 | if (!x) |
| 5131 | continue; |
| 5132 | } |
| 5133 | /* fall through */ |
| 5134 | default: |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5135 | tt = *tail = dup_Token(NULL, x); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5136 | break; |
| 5137 | } |
| 5138 | tail = &tt->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5139 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5140 | *tail = NULL; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5141 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5142 | |
| 5143 | /* |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5144 | * If we had a label, push it on as the first line of |
| 5145 | * the macro expansion. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5146 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5147 | if (label) { |
| 5148 | if (dont_prepend < 0) |
| 5149 | free_tlist(startline); |
| 5150 | else { |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5151 | nasm_new(ll); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5152 | ll->finishes = NULL; |
| 5153 | ll->next = istk->expansion; |
| 5154 | istk->expansion = ll; |
| 5155 | ll->first = startline; |
| 5156 | if (!dont_prepend) { |
| 5157 | while (label->next) |
| 5158 | label = label->next; |
| 5159 | label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5160 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5161 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 5162 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5163 | |
H. Peter Anvin | 0d4d431 | 2019-08-07 00:46:27 -0700 | [diff] [blame] | 5164 | lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5165 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5166 | return 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5167 | } |
| 5168 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5169 | /* |
| 5170 | * This function adds macro names to error messages, and suppresses |
| 5171 | * them if necessary. |
| 5172 | */ |
H. Peter Anvin (Intel) | 6bde2ed | 2018-12-13 19:39:41 -0800 | [diff] [blame] | 5173 | 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] | 5174 | { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5175 | /* |
| 5176 | * If we're in a dead branch of IF or something like it, ignore the error. |
| 5177 | * However, because %else etc are evaluated in the state context |
| 5178 | * of the previous branch, errors might get lost: |
| 5179 | * %if 0 ... %else trailing garbage ... %endif |
| 5180 | * So %else etc should set the ERR_PP_PRECOND flag. |
| 5181 | */ |
| 5182 | if ((severity & ERR_MASK) < ERR_FATAL && |
| 5183 | istk && istk->conds && |
| 5184 | ((severity & ERR_PP_PRECOND) ? |
| 5185 | istk->conds->state == COND_NEVER : |
H. Peter Anvin | eb6653f | 2016-04-05 13:03:10 -0700 | [diff] [blame] | 5186 | !emitting(istk->conds->state))) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5187 | return; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 5188 | |
H. Peter Anvin (Intel) | 98031bf | 2019-08-09 16:11:28 -0700 | [diff] [blame^] | 5189 | /* This doesn't make sense with the macro stack unwinding */ |
| 5190 | if (0) { |
| 5191 | MMacro *mmac = NULL; |
| 5192 | int32_t delta = 0; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5193 | |
H. Peter Anvin (Intel) | 98031bf | 2019-08-09 16:11:28 -0700 | [diff] [blame^] | 5194 | /* get %macro name */ |
| 5195 | if (!(severity & ERR_NOFILE) && istk && istk->mstk) { |
| 5196 | mmac = istk->mstk; |
| 5197 | /* but %rep blocks should be skipped */ |
| 5198 | while (mmac && !mmac->name) |
| 5199 | mmac = mmac->next_active, delta++; |
| 5200 | } |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 5201 | |
H. Peter Anvin (Intel) | 98031bf | 2019-08-09 16:11:28 -0700 | [diff] [blame^] | 5202 | if (mmac) { |
| 5203 | char *buf; |
| 5204 | nasm_set_verror(real_verror); |
| 5205 | buf = nasm_vasprintf(fmt, arg); |
| 5206 | nasm_error(severity, "(%s:%"PRId32") %s", |
| 5207 | mmac->name, mmac->lineno - delta, buf); |
| 5208 | nasm_set_verror(pp_verror); |
| 5209 | nasm_free(buf); |
| 5210 | return; |
| 5211 | } |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5212 | } |
H. Peter Anvin (Intel) | 98031bf | 2019-08-09 16:11:28 -0700 | [diff] [blame^] | 5213 | real_verror(severity, fmt, arg); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 5214 | } |
| 5215 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 5216 | static Token * |
| 5217 | stdmac_file(const SMacro *s, Token **params, unsigned int nparams) |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5218 | { |
| 5219 | (void)s; |
| 5220 | (void)params; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 5221 | (void)nparams; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5222 | |
| 5223 | return make_tok_qstr(src_get_fname()); |
| 5224 | } |
| 5225 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 5226 | static Token * |
| 5227 | stdmac_line(const SMacro *s, Token **params, unsigned int nparams) |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5228 | { |
| 5229 | (void)s; |
| 5230 | (void)params; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 5231 | (void)nparams; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5232 | |
| 5233 | return make_tok_num(src_get_linnum()); |
| 5234 | } |
| 5235 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 5236 | static Token * |
| 5237 | stdmac_bits(const SMacro *s, Token **params, unsigned int nparams) |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5238 | { |
| 5239 | (void)s; |
| 5240 | (void)params; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 5241 | (void)nparams; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5242 | |
| 5243 | return make_tok_num(globalbits); |
| 5244 | } |
| 5245 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 5246 | static Token * |
| 5247 | stdmac_ptr(const SMacro *s, Token **params, unsigned int nparams) |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5248 | { |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 5249 | const Token *t; |
Chang S. Bae | fea2269 | 2019-05-28 12:25:16 -0700 | [diff] [blame] | 5250 | static const Token ptr_word = { NULL, "word", 4, TOK_ID }; |
| 5251 | static const Token ptr_dword = { NULL, "dword", 5, TOK_ID }; |
| 5252 | static const Token ptr_qword = { NULL, "qword", 5, TOK_ID }; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5253 | |
| 5254 | (void)s; |
| 5255 | (void)params; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 5256 | (void)nparams; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5257 | |
| 5258 | switch (globalbits) { |
| 5259 | case 16: |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 5260 | t = &ptr_word; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5261 | break; |
| 5262 | case 32: |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 5263 | t = &ptr_dword; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5264 | break; |
| 5265 | case 64: |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 5266 | t = &ptr_qword; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5267 | break; |
| 5268 | default: |
| 5269 | panic(); |
| 5270 | } |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 5271 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 5272 | return dup_Token(NULL, t); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5273 | } |
| 5274 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5275 | /* Add magic standard macros */ |
| 5276 | struct magic_macros { |
| 5277 | const char *name; |
| 5278 | int nparams; |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 5279 | ExpandSMacro func; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5280 | }; |
| 5281 | static const struct magic_macros magic_macros[] = |
| 5282 | { |
| 5283 | { "__FILE__", 0, stdmac_file }, |
| 5284 | { "__LINE__", 0, stdmac_line }, |
| 5285 | { "__BITS__", 0, stdmac_bits }, |
| 5286 | { "__PTR__", 0, stdmac_ptr }, |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5287 | { NULL, 0, NULL } |
| 5288 | }; |
| 5289 | |
| 5290 | static void pp_add_magic_stdmac(void) |
| 5291 | { |
| 5292 | const struct magic_macros *m; |
| 5293 | SMacro *s; |
| 5294 | |
| 5295 | for (m = magic_macros; m->name; m++) { |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 5296 | s = define_smacro(NULL, m->name, true, m->nparams, NULL, NULL); |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 5297 | s->expand = m->func; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5298 | } |
| 5299 | } |
| 5300 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5301 | static void |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5302 | 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] | 5303 | { |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5304 | int apass; |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 5305 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5306 | cstk = NULL; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5307 | nasm_new(istk); |
H. Peter Anvin | 3e83cec | 2016-05-25 04:28:46 -0700 | [diff] [blame] | 5308 | istk->fp = nasm_open_read(file, NF_TEXT); |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 5309 | src_set(0, file); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5310 | istk->lineinc = 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5311 | if (!istk->fp) |
Cyrill Gorcunov | c3527dd | 2018-11-24 22:17:47 +0300 | [diff] [blame] | 5312 | nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5313 | defining = NULL; |
Charles Crayne | d4200be | 2008-07-12 16:42:33 -0700 | [diff] [blame] | 5314 | nested_mac_count = 0; |
| 5315 | nested_rep_count = 0; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 5316 | init_macros(); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5317 | unique = 0; |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 5318 | deplist = dep_list; |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5319 | pp_mode = mode; |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 5320 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5321 | pp_add_magic_stdmac(); |
| 5322 | |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 5323 | if (tasm_compatible_mode) |
| 5324 | pp_add_stdmac(nasm_stdmac_tasm); |
| 5325 | |
| 5326 | pp_add_stdmac(nasm_stdmac_nasm); |
| 5327 | pp_add_stdmac(nasm_stdmac_version); |
| 5328 | |
Cyrill Gorcunov | 15ce78f | 2017-01-06 20:21:28 +0300 | [diff] [blame] | 5329 | if (extrastdmac) |
| 5330 | pp_add_stdmac(extrastdmac); |
| 5331 | |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 5332 | stdmacpos = stdmacros[0]; |
| 5333 | stdmacnext = &stdmacros[1]; |
| 5334 | |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 5335 | do_predef = true; |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 5336 | |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 5337 | strlist_add(deplist, file); |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 5338 | |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 5339 | /* |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 5340 | * Define the __PASS__ macro. This is defined here unlike all the |
| 5341 | * other builtins, because it is special -- it varies between |
| 5342 | * passes -- but there is really no particular reason to make it |
| 5343 | * magic. |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5344 | * |
| 5345 | * 0 = dependencies only |
| 5346 | * 1 = preparatory passes |
| 5347 | * 2 = final pass |
| 5348 | * 3 = preproces only |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 5349 | */ |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5350 | switch (mode) { |
| 5351 | case PP_NORMAL: |
| 5352 | apass = pass_final() ? 2 : 1; |
| 5353 | break; |
| 5354 | case PP_DEPS: |
| 5355 | apass = 0; |
| 5356 | break; |
| 5357 | case PP_PREPROC: |
| 5358 | apass = 3; |
| 5359 | break; |
| 5360 | default: |
| 5361 | panic(); |
| 5362 | } |
| 5363 | |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 5364 | define_smacro(NULL, "__PASS__", true, 0, NULL, make_tok_num(apass)); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5365 | } |
| 5366 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 5367 | static void pp_init(void) |
| 5368 | { |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 5369 | } |
| 5370 | |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5371 | /* |
| 5372 | * Get a line of tokens. If we popped the macro expansion/include stack, |
| 5373 | * we return a pointer to the dummy token tok_pop; at that point if |
| 5374 | * istk is NULL then we have reached end of input; |
| 5375 | */ |
| 5376 | static Token tok_pop; /* Dummy token placeholder */ |
| 5377 | |
| 5378 | static Token *pp_tokline(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5379 | { |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5380 | Token *tline, *dtline; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5381 | |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5382 | while (true) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5383 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5384 | * Fetch a tokenized line, either from the macro-expansion |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5385 | * buffer or from the input file. |
| 5386 | */ |
| 5387 | tline = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5388 | while (istk->expansion && istk->expansion->finishes) { |
| 5389 | Line *l = istk->expansion; |
| 5390 | if (!l->finishes->name && l->finishes->in_progress > 1) { |
| 5391 | Line *ll; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5392 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5393 | /* |
| 5394 | * This is a macro-end marker for a macro with no |
| 5395 | * name, which means it's not really a macro at all |
| 5396 | * but a %rep block, and the `in_progress' field is |
| 5397 | * more than 1, meaning that we still need to |
| 5398 | * repeat. (1 means the natural last repetition; 0 |
| 5399 | * means termination by %exitrep.) We have |
| 5400 | * therefore expanded up to the %endrep, and must |
| 5401 | * push the whole block on to the expansion buffer |
| 5402 | * again. We don't bother to remove the macro-end |
| 5403 | * marker: we'd only have to generate another one |
| 5404 | * if we did. |
| 5405 | */ |
| 5406 | l->finishes->in_progress--; |
| 5407 | list_for_each(l, l->finishes->expansion) { |
| 5408 | Token *t, *tt, **tail; |
| 5409 | |
| 5410 | ll = nasm_malloc(sizeof(Line)); |
| 5411 | ll->next = istk->expansion; |
| 5412 | ll->finishes = NULL; |
| 5413 | ll->first = NULL; |
| 5414 | tail = &ll->first; |
| 5415 | |
| 5416 | list_for_each(t, l->first) { |
| 5417 | if (t->text || t->type == TOK_WHITESPACE) { |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 5418 | tt = *tail = dup_Token(NULL, t); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5419 | tail = &tt->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5420 | } |
| 5421 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5422 | istk->expansion = ll; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5423 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5424 | } else { |
| 5425 | /* |
| 5426 | * Check whether a `%rep' was started and not ended |
| 5427 | * within this macro expansion. This can happen and |
| 5428 | * should be detected. It's a fatal error because |
| 5429 | * I'm too confused to work out how to recover |
| 5430 | * sensibly from it. |
| 5431 | */ |
| 5432 | if (defining) { |
| 5433 | if (defining->name) |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 5434 | nasm_panic("defining with name in expansion"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5435 | else if (istk->mstk->name) |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 5436 | nasm_fatal("`%%rep' without `%%endrep' within" |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5437 | " expansion of macro `%s'", |
| 5438 | istk->mstk->name); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5439 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5440 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5441 | /* |
| 5442 | * FIXME: investigate the relationship at this point between |
| 5443 | * istk->mstk and l->finishes |
| 5444 | */ |
| 5445 | { |
| 5446 | MMacro *m = istk->mstk; |
| 5447 | istk->mstk = m->next_active; |
| 5448 | if (m->name) { |
| 5449 | /* |
| 5450 | * This was a real macro call, not a %rep, and |
| 5451 | * therefore the parameter information needs to |
| 5452 | * be freed. |
| 5453 | */ |
| 5454 | if (m->prev) { |
| 5455 | pop_mmacro(m); |
| 5456 | l->finishes->in_progress --; |
| 5457 | } else { |
| 5458 | nasm_free(m->params); |
| 5459 | free_tlist(m->iline); |
| 5460 | nasm_free(m->paramlen); |
| 5461 | l->finishes->in_progress = 0; |
| 5462 | } |
Adam Majer | 91e7240 | 2017-07-25 10:42:01 +0200 | [diff] [blame] | 5463 | } |
| 5464 | |
| 5465 | /* |
| 5466 | * FIXME It is incorrect to always free_mmacro here. |
| 5467 | * It leads to usage-after-free. |
| 5468 | * |
| 5469 | * https://bugzilla.nasm.us/show_bug.cgi?id=3392414 |
| 5470 | */ |
| 5471 | #if 0 |
| 5472 | else |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5473 | free_mmacro(m); |
Adam Majer | 91e7240 | 2017-07-25 10:42:01 +0200 | [diff] [blame] | 5474 | #endif |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5475 | } |
| 5476 | istk->expansion = l->next; |
| 5477 | nasm_free(l); |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 5478 | lfmt->downlevel(LIST_MACRO); |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5479 | return &tok_pop; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5480 | } |
| 5481 | } |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5482 | do { /* until we get a line we can use */ |
| 5483 | char *line; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5484 | |
| 5485 | if (istk->expansion) { /* from a macro expansion */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5486 | Line *l = istk->expansion; |
| 5487 | if (istk->mstk) |
| 5488 | istk->mstk->lineno++; |
| 5489 | tline = l->first; |
| 5490 | istk->expansion = l->next; |
| 5491 | nasm_free(l); |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5492 | line = detoken(tline, false); |
| 5493 | lfmt->line(LIST_MACRO, line); |
| 5494 | nasm_free(line); |
| 5495 | } else if ((line = read_line())) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5496 | line = prepreproc(line); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5497 | tline = tokenize(line); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5498 | nasm_free(line); |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5499 | } else { |
| 5500 | /* |
| 5501 | * The current file has ended; work down the istk |
| 5502 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5503 | Include *i = istk; |
| 5504 | fclose(i->fp); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5505 | if (i->conds) { |
| 5506 | /* nasm_error can't be conditionally suppressed */ |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 5507 | nasm_fatal("expected `%%endif' before end of file"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5508 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5509 | /* 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] | 5510 | if (i->next) |
| 5511 | src_set(i->lineno, i->fname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5512 | istk = i->next; |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 5513 | lfmt->downlevel(LIST_INCLUDE); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5514 | nasm_free(i); |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5515 | return &tok_pop; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5516 | } |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5517 | } while (0); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5518 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5519 | /* |
| 5520 | * We must expand MMacro parameters and MMacro-local labels |
| 5521 | * _before_ we plunge into directive processing, to cope |
| 5522 | * with things like `%define something %1' such as STRUC |
| 5523 | * uses. Unless we're _defining_ a MMacro, in which case |
| 5524 | * those tokens should be left alone to go into the |
| 5525 | * definition; and unless we're in a non-emitting |
| 5526 | * condition, in which case we don't want to meddle with |
| 5527 | * anything. |
| 5528 | */ |
| 5529 | if (!defining && !(istk->conds && !emitting(istk->conds->state)) |
| 5530 | && !(istk->mstk && !istk->mstk->in_progress)) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5531 | tline = expand_mmac_params(tline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5532 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5533 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5534 | /* |
| 5535 | * Check the line to see if it's a preprocessor directive. |
| 5536 | */ |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5537 | if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) { |
| 5538 | if (dtline) |
| 5539 | return dtline; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5540 | } else if (defining) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5541 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5542 | * We're defining a multi-line macro. We emit nothing |
| 5543 | * at all, and just |
| 5544 | * shove the tokenized line on to the macro definition. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5545 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5546 | Line *l = nasm_malloc(sizeof(Line)); |
| 5547 | l->next = defining->expansion; |
| 5548 | l->first = tline; |
| 5549 | l->finishes = NULL; |
| 5550 | defining->expansion = l; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5551 | } else if (istk->conds && !emitting(istk->conds->state)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5552 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5553 | * We're in a non-emitting branch of a condition block. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5554 | * Emit nothing at all, not even a blank line: when we |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5555 | * emerge from the condition we'll give a line-number |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5556 | * directive so we keep our place correctly. |
| 5557 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5558 | free_tlist(tline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5559 | } else if (istk->mstk && !istk->mstk->in_progress) { |
| 5560 | /* |
| 5561 | * We're in a %rep block which has been terminated, so |
| 5562 | * we're walking through to the %endrep without |
| 5563 | * emitting anything. Emit nothing at all, not even a |
| 5564 | * blank line: when we emerge from the %rep block we'll |
| 5565 | * give a line-number directive so we keep our place |
| 5566 | * correctly. |
| 5567 | */ |
| 5568 | free_tlist(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5569 | } else { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5570 | tline = expand_smacro(tline); |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5571 | if (!expand_mmacro(tline)) |
| 5572 | return tline; |
| 5573 | } |
| 5574 | } |
| 5575 | } |
| 5576 | |
| 5577 | static char *pp_getline(void) |
| 5578 | { |
| 5579 | char *line = NULL; |
| 5580 | Token *tline; |
| 5581 | |
| 5582 | real_verror = nasm_set_verror(pp_verror); |
| 5583 | |
| 5584 | while (true) { |
| 5585 | tline = pp_tokline(); |
| 5586 | if (tline == &tok_pop) { |
| 5587 | /* |
| 5588 | * We popped the macro/include stack. If istk is empty, |
| 5589 | * we are at end of input, otherwise just loop back. |
| 5590 | */ |
| 5591 | if (!istk) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5592 | break; |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5593 | } else { |
| 5594 | /* |
| 5595 | * De-tokenize the line and emit it. |
| 5596 | */ |
| 5597 | line = detoken(tline, true); |
| 5598 | free_tlist(tline); |
| 5599 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5600 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5601 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5602 | |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 5603 | if (list_option('e') && line && line[0]) { |
| 5604 | char *buf = nasm_strcat(" ;;; ", line); |
| 5605 | lfmt->line(LIST_MACRO, buf); |
| 5606 | nasm_free(buf); |
| 5607 | } |
| 5608 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5609 | nasm_set_verror(real_verror); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5610 | return line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5611 | } |
| 5612 | |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5613 | static void pp_cleanup_pass(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5614 | { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5615 | real_verror = nasm_set_verror(pp_verror); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5616 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5617 | if (defining) { |
| 5618 | if (defining->name) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 5619 | nasm_nonfatal("end of file while still defining macro `%s'", |
| 5620 | defining->name); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5621 | } else { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 5622 | nasm_nonfatal("end of file while still in %%rep"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5623 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5624 | |
| 5625 | free_mmacro(defining); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5626 | defining = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5627 | } |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5628 | |
| 5629 | nasm_set_verror(real_verror); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5630 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5631 | while (cstk) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5632 | ctx_pop(); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 5633 | free_macros(); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5634 | while (istk) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5635 | Include *i = istk; |
| 5636 | istk = istk->next; |
| 5637 | fclose(i->fp); |
Cyrill Gorcunov | 8dcfd88 | 2011-03-03 09:18:56 +0300 | [diff] [blame] | 5638 | nasm_free(i); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5639 | } |
| 5640 | while (cstk) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5641 | ctx_pop(); |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 5642 | src_set_fname(NULL); |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5643 | } |
| 5644 | |
| 5645 | static void pp_cleanup_session(void) |
| 5646 | { |
| 5647 | free_llist(predef); |
| 5648 | predef = NULL; |
| 5649 | delete_Blocks(); |
| 5650 | freeTokens = NULL; |
| 5651 | ipath_list = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5652 | } |
| 5653 | |
Cyrill Gorcunov | 8c0666b | 2018-11-24 14:33:48 +0300 | [diff] [blame] | 5654 | static void pp_include_path(struct strlist *list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5655 | { |
Cyrill Gorcunov | 8c0666b | 2018-11-24 14:33:48 +0300 | [diff] [blame] | 5656 | ipath_list = list; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5657 | } |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5658 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5659 | static void pp_pre_include(char *fname) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5660 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5661 | Token *inc, *space, *name; |
| 5662 | Line *l; |
| 5663 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5664 | name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0); |
| 5665 | space = new_Token(name, TOK_WHITESPACE, NULL, 0); |
| 5666 | inc = new_Token(space, TOK_PREPROC_ID, "%include", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5667 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5668 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5669 | l->next = predef; |
| 5670 | l->first = inc; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5671 | l->finishes = NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5672 | predef = l; |
| 5673 | } |
| 5674 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5675 | static void pp_pre_define(char *definition) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5676 | { |
| 5677 | Token *def, *space; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5678 | Line *l; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5679 | char *equals; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5680 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5681 | real_verror = nasm_set_verror(pp_verror); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5682 | |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5683 | equals = strchr(definition, '='); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5684 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 5685 | def = new_Token(space, TOK_PREPROC_ID, "%define", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5686 | if (equals) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5687 | *equals = ' '; |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5688 | space->next = tokenize(definition); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5689 | if (equals) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5690 | *equals = '='; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5691 | |
Cyrill Gorcunov | 6d42e9b | 2015-02-08 11:07:17 +0300 | [diff] [blame] | 5692 | if (space->next->type != TOK_PREPROC_ID && |
| 5693 | space->next->type != TOK_ID) |
H. Peter Anvin (Intel) | 80c4f23 | 2018-12-14 13:33:24 -0800 | [diff] [blame] | 5694 | nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition); |
Cyrill Gorcunov | 6d42e9b | 2015-02-08 11:07:17 +0300 | [diff] [blame] | 5695 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5696 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5697 | l->next = predef; |
| 5698 | l->first = def; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5699 | l->finishes = NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5700 | predef = l; |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5701 | |
| 5702 | nasm_set_verror(real_verror); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5703 | } |
| 5704 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5705 | static void pp_pre_undefine(char *definition) |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5706 | { |
| 5707 | Token *def, *space; |
| 5708 | Line *l; |
| 5709 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5710 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 5711 | def = new_Token(space, TOK_PREPROC_ID, "%undef", 0); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5712 | space->next = tokenize(definition); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5713 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5714 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5715 | l->next = predef; |
| 5716 | l->first = def; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5717 | l->finishes = NULL; |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5718 | predef = l; |
| 5719 | } |
| 5720 | |
H. Peter Anvin | 0599034 | 2018-06-11 13:32:42 -0700 | [diff] [blame] | 5721 | /* Insert an early preprocessor command that doesn't need special handling */ |
| 5722 | static void pp_pre_command(const char *what, char *string) |
| 5723 | { |
| 5724 | char *cmd; |
| 5725 | Token *def, *space; |
| 5726 | Line *l; |
| 5727 | |
| 5728 | def = tokenize(string); |
| 5729 | if (what) { |
| 5730 | cmd = nasm_strcat(what[0] == '%' ? "" : "%", what); |
| 5731 | space = new_Token(def, TOK_WHITESPACE, NULL, 0); |
| 5732 | def = new_Token(space, TOK_PREPROC_ID, cmd, 0); |
| 5733 | } |
| 5734 | |
| 5735 | l = nasm_malloc(sizeof(Line)); |
| 5736 | l->next = predef; |
| 5737 | l->first = def; |
| 5738 | l->finishes = NULL; |
| 5739 | predef = l; |
| 5740 | } |
| 5741 | |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 5742 | static void pp_add_stdmac(macros_t *macros) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5743 | { |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 5744 | macros_t **mp; |
| 5745 | |
| 5746 | /* Find the end of the list and avoid duplicates */ |
| 5747 | for (mp = stdmacros; *mp; mp++) { |
| 5748 | if (*mp == macros) |
| 5749 | return; /* Nothing to do */ |
| 5750 | } |
| 5751 | |
| 5752 | nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]); |
| 5753 | |
| 5754 | *mp = macros; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 5755 | } |
| 5756 | |
Cyrill Gorcunov | 15ce78f | 2017-01-06 20:21:28 +0300 | [diff] [blame] | 5757 | static void pp_extra_stdmac(macros_t *macros) |
| 5758 | { |
| 5759 | extrastdmac = macros; |
| 5760 | } |
| 5761 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5762 | static Token *make_tok_num(int64_t val) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5763 | { |
Cyrill Gorcunov | ce65274 | 2013-05-06 23:43:43 +0400 | [diff] [blame] | 5764 | char numbuf[32]; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5765 | int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val); |
| 5766 | return new_Token(NULL, TOK_NUMBER, numbuf, len); |
| 5767 | } |
| 5768 | |
| 5769 | static Token *make_tok_qstr(const char *str) |
| 5770 | { |
| 5771 | Token *t = new_Token(NULL, TOK_STRING, NULL, 0); |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 5772 | t->text = nasm_quote_cstr(str, &t->len); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5773 | return t; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5774 | } |
| 5775 | |
H. Peter Anvin (Intel) | 6bde2ed | 2018-12-13 19:39:41 -0800 | [diff] [blame] | 5776 | static void pp_list_one_macro(MMacro *m, errflags severity) |
H. Peter Anvin | 3736895 | 2016-05-09 14:10:32 -0700 | [diff] [blame] | 5777 | { |
| 5778 | if (!m) |
| 5779 | return; |
| 5780 | |
| 5781 | /* We need to print the next_active list in reverse order */ |
| 5782 | pp_list_one_macro(m->next_active, severity); |
| 5783 | |
| 5784 | if (m->name && !m->nolist) { |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 5785 | src_set(m->xline + m->lineno, m->fname); |
H. Peter Anvin | ddb2906 | 2018-12-11 00:06:29 -0800 | [diff] [blame] | 5786 | nasm_error(severity, "... from macro `%s' defined", m->name); |
H. Peter Anvin | 3736895 | 2016-05-09 14:10:32 -0700 | [diff] [blame] | 5787 | } |
| 5788 | } |
| 5789 | |
H. Peter Anvin (Intel) | 6bde2ed | 2018-12-13 19:39:41 -0800 | [diff] [blame] | 5790 | static void pp_error_list_macros(errflags severity) |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5791 | { |
H. Peter Anvin | ddb2906 | 2018-12-11 00:06:29 -0800 | [diff] [blame] | 5792 | struct src_location saved; |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5793 | |
H. Peter Anvin | ddb2906 | 2018-12-11 00:06:29 -0800 | [diff] [blame] | 5794 | severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE; |
| 5795 | saved = src_where(); |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5796 | |
Cyrill Gorcunov | 771d04e | 2016-05-10 23:27:03 +0300 | [diff] [blame] | 5797 | if (istk) |
| 5798 | pp_list_one_macro(istk->mstk, severity); |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5799 | |
H. Peter Anvin | ddb2906 | 2018-12-11 00:06:29 -0800 | [diff] [blame] | 5800 | src_update(saved); |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5801 | } |
| 5802 | |
H. Peter Anvin | e746971 | 2016-02-18 02:20:59 -0800 | [diff] [blame] | 5803 | const struct preproc_ops nasmpp = { |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 5804 | pp_init, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5805 | pp_reset, |
| 5806 | pp_getline, |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5807 | pp_cleanup_pass, |
| 5808 | pp_cleanup_session, |
Cyrill Gorcunov | 15ce78f | 2017-01-06 20:21:28 +0300 | [diff] [blame] | 5809 | pp_extra_stdmac, |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5810 | pp_pre_define, |
| 5811 | pp_pre_undefine, |
| 5812 | pp_pre_include, |
H. Peter Anvin | 0599034 | 2018-06-11 13:32:42 -0700 | [diff] [blame] | 5813 | pp_pre_command, |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5814 | pp_include_path, |
| 5815 | pp_error_list_macros, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5816 | }; |