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 | ca544db | 2008-10-19 19:30:11 -0700 | [diff] [blame] | 940 | char c, *p = line; |
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) { |
| 946 | p = line; |
| 947 | if (*p == '%') { |
| 948 | p++; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 949 | if (*p == '+' && !nasm_isdigit(p[1])) { |
| 950 | p++; |
| 951 | type = TOK_PASTE; |
| 952 | } else if (nasm_isdigit(*p) || |
| 953 | ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 954 | do { |
| 955 | p++; |
| 956 | } |
H. Peter Anvin | bda7a6e | 2008-06-21 10:23:17 -0700 | [diff] [blame] | 957 | while (nasm_isdigit(*p)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 958 | type = TOK_PREPROC_ID; |
| 959 | } else if (*p == '{') { |
| 960 | p++; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 961 | while (*p) { |
| 962 | if (*p == '}') |
| 963 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 964 | p[-1] = *p; |
| 965 | p++; |
| 966 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 967 | if (*p != '}') |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 968 | nasm_warn(WARN_OTHER, "unterminated %%{ construct"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 969 | p[-1] = '\0'; |
| 970 | if (*p) |
| 971 | p++; |
| 972 | type = TOK_PREPROC_ID; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 973 | } else if (*p == '[') { |
| 974 | int lvl = 1; |
| 975 | line += 2; /* Skip the leading %[ */ |
| 976 | p++; |
| 977 | while (lvl && (c = *p++)) { |
| 978 | switch (c) { |
| 979 | case ']': |
| 980 | lvl--; |
| 981 | break; |
| 982 | case '%': |
| 983 | if (*p == '[') |
| 984 | lvl++; |
| 985 | break; |
| 986 | case '\'': |
| 987 | case '\"': |
| 988 | case '`': |
Cyrill Gorcunov | 3144e84 | 2017-10-22 10:50:55 +0300 | [diff] [blame] | 989 | p = nasm_skip_string(p - 1); |
| 990 | if (*p) |
| 991 | p++; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 992 | break; |
| 993 | default: |
| 994 | break; |
| 995 | } |
| 996 | } |
| 997 | p--; |
| 998 | if (*p) |
| 999 | *p++ = '\0'; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1000 | if (lvl) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1001 | nasm_nonfatalf(ERR_PASS1, "unterminated %%[ construct"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1002 | type = TOK_INDIRECT; |
| 1003 | } else if (*p == '?') { |
| 1004 | type = TOK_PREPROC_Q; /* %? */ |
| 1005 | p++; |
| 1006 | if (*p == '?') { |
| 1007 | type = TOK_PREPROC_QQ; /* %?? */ |
| 1008 | p++; |
| 1009 | } |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1010 | } else if (*p == '!') { |
| 1011 | type = TOK_PREPROC_ID; |
| 1012 | p++; |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 1013 | if (nasm_isidchar(*p)) { |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1014 | do { |
| 1015 | p++; |
| 1016 | } |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 1017 | while (nasm_isidchar(*p)); |
H. Peter Anvin | 53e2e4c | 2018-11-28 15:01:40 -0800 | [diff] [blame] | 1018 | } else if (nasm_isquote(*p)) { |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1019 | p = nasm_skip_string(p); |
| 1020 | if (*p) |
| 1021 | p++; |
| 1022 | else |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1023 | nasm_nonfatalf(ERR_PASS1, "unterminated %%! string"); |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1024 | } else { |
| 1025 | /* %! without string or identifier */ |
| 1026 | type = TOK_OTHER; /* Legacy behavior... */ |
| 1027 | } |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 1028 | } else if (nasm_isidchar(*p) || |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1029 | ((*p == '!' || *p == '%' || *p == '$') && |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 1030 | nasm_isidchar(p[1]))) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1031 | do { |
| 1032 | p++; |
| 1033 | } |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 1034 | while (nasm_isidchar(*p)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1035 | type = TOK_PREPROC_ID; |
| 1036 | } else { |
| 1037 | type = TOK_OTHER; |
| 1038 | if (*p == '%') |
| 1039 | p++; |
| 1040 | } |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 1041 | } else if (nasm_isidstart(*p) || (*p == '$' && nasm_isidstart(p[1]))) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1042 | type = TOK_ID; |
| 1043 | p++; |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 1044 | while (*p && nasm_isidchar(*p)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1045 | p++; |
H. Peter Anvin | 53e2e4c | 2018-11-28 15:01:40 -0800 | [diff] [blame] | 1046 | } else if (nasm_isquote(*p)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1047 | /* |
| 1048 | * A string token. |
| 1049 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1050 | type = TOK_STRING; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1051 | p = nasm_skip_string(p); |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1052 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1053 | if (*p) { |
| 1054 | p++; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1055 | } else { |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 1056 | nasm_warn(WARN_OTHER, "unterminated string"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1057 | /* Handling unterminated strings by UNV */ |
| 1058 | /* type = -1; */ |
| 1059 | } |
Victor van den Elzen | fb5f251 | 2009-04-17 16:17:59 +0200 | [diff] [blame] | 1060 | } else if (p[0] == '$' && p[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1061 | type = TOK_OTHER; /* TOKEN_BASE */ |
Victor van den Elzen | fb5f251 | 2009-04-17 16:17:59 +0200 | [diff] [blame] | 1062 | p += 2; |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 1063 | } else if (nasm_isnumstart(*p)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1064 | bool is_hex = false; |
| 1065 | bool is_float = false; |
| 1066 | bool has_e = false; |
| 1067 | char c, *r; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1068 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1069 | /* |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1070 | * A numeric token. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1071 | */ |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1072 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1073 | if (*p == '$') { |
| 1074 | p++; |
| 1075 | is_hex = true; |
| 1076 | } |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1077 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1078 | for (;;) { |
| 1079 | c = *p++; |
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 | if (!is_hex && (c == 'e' || c == 'E')) { |
| 1082 | has_e = true; |
| 1083 | if (*p == '+' || *p == '-') { |
| 1084 | /* |
| 1085 | * e can only be followed by +/- if it is either a |
| 1086 | * prefixed hex number or a floating-point number |
| 1087 | */ |
| 1088 | p++; |
| 1089 | is_float = true; |
| 1090 | } |
| 1091 | } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') { |
| 1092 | is_hex = true; |
| 1093 | } else if (c == 'P' || c == 'p') { |
| 1094 | is_float = true; |
| 1095 | if (*p == '+' || *p == '-') |
| 1096 | p++; |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 1097 | } else if (nasm_isnumchar(c)) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1098 | ; /* just advance */ |
| 1099 | else if (c == '.') { |
| 1100 | /* |
| 1101 | * we need to deal with consequences of the legacy |
| 1102 | * parser, like "1.nolist" being two tokens |
| 1103 | * (TOK_NUMBER, TOK_ID) here; at least give it |
| 1104 | * a shot for now. In the future, we probably need |
| 1105 | * a flex-based scanner with proper pattern matching |
| 1106 | * to do it as well as it can be done. Nothing in |
| 1107 | * the world is going to help the person who wants |
| 1108 | * 0x123.p16 interpreted as two tokens, though. |
| 1109 | */ |
| 1110 | r = p; |
| 1111 | while (*r == '_') |
| 1112 | r++; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1113 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1114 | if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) || |
| 1115 | (!is_hex && (*r == 'e' || *r == 'E')) || |
| 1116 | (*r == 'p' || *r == 'P')) { |
| 1117 | p = r; |
| 1118 | is_float = true; |
| 1119 | } else |
| 1120 | break; /* Terminate the token */ |
| 1121 | } else |
| 1122 | break; |
| 1123 | } |
| 1124 | p--; /* Point to first character beyond number */ |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1125 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1126 | if (p == line+1 && *line == '$') { |
| 1127 | type = TOK_OTHER; /* TOKEN_HERE */ |
| 1128 | } else { |
| 1129 | if (has_e && !is_hex) { |
| 1130 | /* 1e13 is floating-point, but 1e13h is not */ |
| 1131 | is_float = true; |
| 1132 | } |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 1133 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1134 | type = is_float ? TOK_FLOAT : TOK_NUMBER; |
| 1135 | } |
H. Peter Anvin | bda7a6e | 2008-06-21 10:23:17 -0700 | [diff] [blame] | 1136 | } else if (nasm_isspace(*p)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1137 | type = TOK_WHITESPACE; |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 1138 | p = nasm_skip_spaces(p); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1139 | /* |
| 1140 | * Whitespace just before end-of-line is discarded by |
| 1141 | * pretending it's a comment; whitespace just before a |
| 1142 | * comment gets lumped into the comment. |
| 1143 | */ |
| 1144 | if (!*p || *p == ';') { |
| 1145 | type = TOK_COMMENT; |
| 1146 | while (*p) |
| 1147 | p++; |
| 1148 | } |
| 1149 | } else if (*p == ';') { |
| 1150 | type = TOK_COMMENT; |
| 1151 | while (*p) |
| 1152 | p++; |
| 1153 | } else { |
| 1154 | /* |
| 1155 | * Anything else is an operator of some kind. We check |
| 1156 | * for all the double-character operators (>>, <<, //, |
| 1157 | * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1158 | * else is a single-character operator. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1159 | */ |
| 1160 | type = TOK_OTHER; |
| 1161 | if ((p[0] == '>' && p[1] == '>') || |
| 1162 | (p[0] == '<' && p[1] == '<') || |
| 1163 | (p[0] == '/' && p[1] == '/') || |
| 1164 | (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++; |
| 1173 | } |
| 1174 | p++; |
| 1175 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1176 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1177 | /* Handling unterminated string by UNV */ |
| 1178 | /*if (type == -1) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1179 | { |
| 1180 | *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1); |
| 1181 | t->text[p-line] = *line; |
| 1182 | tail = &t->next; |
| 1183 | } |
| 1184 | else */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1185 | if (type != TOK_COMMENT) { |
| 1186 | *tail = t = new_Token(NULL, type, line, p - line); |
| 1187 | tail = &t->next; |
| 1188 | } |
| 1189 | line = p; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1190 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1191 | return list; |
| 1192 | } |
| 1193 | |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1194 | /* |
| 1195 | * this function allocates a new managed block of memory and |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1196 | * returns a pointer to the block. The managed blocks are |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1197 | * deleted only all at once by the delete_Blocks function. |
| 1198 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1199 | static void *new_Block(size_t size) |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1200 | { |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1201 | Blocks *b = &blocks; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1202 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1203 | /* first, get to the end of the linked list */ |
| 1204 | while (b->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1205 | b = b->next; |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1206 | /* now allocate the requested chunk */ |
| 1207 | b->chunk = nasm_malloc(size); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1208 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1209 | /* now allocate a new block for the next request */ |
Cyrill Gorcunov | c31767c | 2014-06-28 02:22:17 +0400 | [diff] [blame] | 1210 | b->next = nasm_zalloc(sizeof(Blocks)); |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1211 | return b->chunk; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | /* |
| 1215 | * this function deletes all managed blocks of memory |
| 1216 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1217 | static void delete_Blocks(void) |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1218 | { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1219 | Blocks *a, *b = &blocks; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1220 | |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1221 | /* |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1222 | * keep in mind that the first block, pointed to by blocks |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1223 | * is a static and not dynamically allocated, so we don't |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1224 | * free it. |
| 1225 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1226 | while (b) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1227 | if (b->chunk) |
| 1228 | nasm_free(b->chunk); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1229 | a = b; |
| 1230 | b = b->next; |
| 1231 | if (a != &blocks) |
| 1232 | nasm_free(a); |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1233 | } |
Cyrill Gorcunov | dae24d7 | 2014-06-28 10:17:39 +0400 | [diff] [blame] | 1234 | memset(&blocks, 0, sizeof(blocks)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1235 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1236 | |
| 1237 | /* |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1238 | * 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] | 1239 | * 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] | 1240 | */ |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1241 | 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] | 1242 | const char *text, size_t txtlen) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1243 | { |
| 1244 | Token *t; |
| 1245 | int i; |
| 1246 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1247 | if (!freeTokens) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1248 | freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token)); |
| 1249 | for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++) |
| 1250 | freeTokens[i].next = &freeTokens[i + 1]; |
| 1251 | freeTokens[i].next = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1252 | } |
| 1253 | t = freeTokens; |
| 1254 | freeTokens = t->next; |
| 1255 | t->next = next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1256 | t->type = type; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1257 | if (type == TOK_WHITESPACE || !text) { |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 1258 | t->len = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1259 | t->text = NULL; |
| 1260 | } else { |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 1261 | if (txtlen == 0 && text[0]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1262 | txtlen = strlen(text); |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 1263 | t->len = txtlen; |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1264 | t->text = nasm_malloc(txtlen+1); |
| 1265 | memcpy(t->text, text, txtlen); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1266 | t->text[txtlen] = '\0'; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1267 | } |
| 1268 | return t; |
| 1269 | } |
| 1270 | |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 1271 | static Token *dup_Token(Token *next, const Token *src) |
| 1272 | { |
| 1273 | return new_Token(next, src->type, src->text, src->len); |
| 1274 | } |
| 1275 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1276 | static Token *delete_Token(Token * t) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1277 | { |
| 1278 | Token *next = t->next; |
| 1279 | nasm_free(t->text); |
H. Peter Anvin | 788e6c1 | 2002-04-30 21:02:01 +0000 | [diff] [blame] | 1280 | t->next = freeTokens; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1281 | freeTokens = t; |
| 1282 | return next; |
| 1283 | } |
| 1284 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1285 | /* |
| 1286 | * Convert a line of tokens back into text. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1287 | * If expand_locals is not zero, identifiers of the form "%$*xxx" |
| 1288 | * will be transformed into ..@ctxnum.xxx |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1289 | */ |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 1290 | static char *detoken(Token * tlist, bool expand_locals) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1291 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1292 | Token *t; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1293 | char *line, *p; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1294 | int len = 0; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1295 | |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1296 | list_for_each(t, tlist) { |
Cyrill Gorcunov | 9b7ee09 | 2017-10-22 21:42:59 +0300 | [diff] [blame] | 1297 | if (t->type == TOK_PREPROC_ID && t->text && |
| 1298 | t->text[0] && t->text[1] == '!') { |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1299 | char *v; |
| 1300 | char *q = t->text; |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1301 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1302 | v = t->text + 2; |
H. Peter Anvin | 53e2e4c | 2018-11-28 15:01:40 -0800 | [diff] [blame] | 1303 | if (nasm_isquote(*v)) { |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1304 | size_t len = nasm_unquote(v, NULL); |
| 1305 | size_t clen = strlen(v); |
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 | if (len != clen) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1308 | nasm_nonfatalf(ERR_PASS1, "NUL character in %%! string"); |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1309 | v = NULL; |
| 1310 | } |
| 1311 | } |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1312 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1313 | if (v) { |
| 1314 | char *p = getenv(v); |
| 1315 | if (!p) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1316 | nasm_nonfatalf(ERR_PASS1, "nonexistent environment variable `%s'", v); |
Cyrill Gorcunov | bbb7a1a | 2016-06-19 12:15:24 +0300 | [diff] [blame] | 1317 | /* |
| 1318 | * FIXME We better should investigate if accessing |
| 1319 | * ->text[1] without ->text[0] is safe enough. |
| 1320 | */ |
| 1321 | t->text = nasm_zalloc(2); |
| 1322 | } else |
| 1323 | t->text = nasm_strdup(p); |
Cyrill Gorcunov | 7500487 | 2017-07-26 01:21:16 +0300 | [diff] [blame] | 1324 | nasm_free(q); |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1325 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1326 | } |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1327 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1328 | /* Expand local macros here and not during preprocessing */ |
| 1329 | if (expand_locals && |
| 1330 | t->type == TOK_PREPROC_ID && t->text && |
| 1331 | t->text[0] == '%' && t->text[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1332 | const char *q; |
| 1333 | char *p; |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1334 | Context *ctx = get_ctx(t->text, &q); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1335 | if (ctx) { |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 1336 | p = nasm_asprintf("..@%"PRIu64".%s", ctx->number, q); |
| 1337 | t->len = nasm_last_string_len(); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1338 | nasm_free(t->text); |
| 1339 | t->text = p; |
| 1340 | } |
| 1341 | } |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1342 | if (t->type == TOK_WHITESPACE) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1343 | len++; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1344 | else if (t->text) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1345 | len += strlen(t->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1346 | } |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1347 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1348 | p = line = nasm_malloc(len + 1); |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1349 | |
| 1350 | list_for_each(t, tlist) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1351 | if (t->type == TOK_WHITESPACE) { |
H. Peter Anvin | b4daadc | 2008-01-21 16:31:57 -0800 | [diff] [blame] | 1352 | *p++ = ' '; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1353 | } else if (t->text) { |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 1354 | memcpy(p, t->text, t->len); |
| 1355 | p += t->len; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1356 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1357 | } |
| 1358 | *p = '\0'; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1359 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1360 | return line; |
| 1361 | } |
| 1362 | |
| 1363 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1364 | * A scanner, suitable for use by the expression evaluator, which |
| 1365 | * operates on a line of Tokens. Expects a pointer to a pointer to |
| 1366 | * the first token in the line to be passed in as its private_data |
| 1367 | * field. |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1368 | * |
| 1369 | * FIX: This really needs to be unified with stdscan. |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1370 | */ |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 1371 | struct ppscan { |
| 1372 | Token *tptr; |
| 1373 | int ntokens; |
| 1374 | }; |
| 1375 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1376 | static int ppscan(void *private_data, struct tokenval *tokval) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1377 | { |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 1378 | struct ppscan *pps = private_data; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1379 | Token *tline; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1380 | char ourcopy[MAX_KEYWORD+1], *p, *r, *s; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1381 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1382 | do { |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 1383 | if (pps->ntokens && (tline = pps->tptr)) { |
| 1384 | pps->ntokens--; |
| 1385 | pps->tptr = tline->next; |
| 1386 | } else { |
| 1387 | pps->tptr = NULL; |
| 1388 | pps->ntokens = 0; |
| 1389 | return tokval->t_type = TOKEN_EOS; |
| 1390 | } |
| 1391 | } while (tline->type == TOK_WHITESPACE || tline->type == TOK_COMMENT); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1392 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1393 | tokval->t_charptr = tline->text; |
| 1394 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1395 | if (tline->text[0] == '$' && !tline->text[1]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1396 | return tokval->t_type = TOKEN_HERE; |
H. Peter Anvin | 7cf897e | 2002-05-30 21:30:33 +0000 | [diff] [blame] | 1397 | if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1398 | return tokval->t_type = TOKEN_BASE; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1399 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1400 | if (tline->type == TOK_ID) { |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1401 | p = tokval->t_charptr = tline->text; |
| 1402 | if (p[0] == '$') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1403 | tokval->t_charptr++; |
| 1404 | return tokval->t_type = TOKEN_ID; |
| 1405 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1406 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1407 | for (r = p, s = ourcopy; *r; r++) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1408 | if (r >= p+MAX_KEYWORD) |
| 1409 | return tokval->t_type = TOKEN_ID; /* Not a keyword */ |
H. Peter Anvin | ac8f8fc | 2008-06-11 15:49:41 -0700 | [diff] [blame] | 1410 | *s++ = nasm_tolower(*r); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1411 | } |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1412 | *s = '\0'; |
| 1413 | /* right, so we have an identifier sitting in temp storage. now, |
| 1414 | * is it actually a register or instruction name, or what? */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1415 | return nasm_token_hash(ourcopy, tokval); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1416 | } |
| 1417 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1418 | if (tline->type == TOK_NUMBER) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1419 | bool rn_error; |
| 1420 | tokval->t_integer = readnum(tline->text, &rn_error); |
| 1421 | tokval->t_charptr = tline->text; |
| 1422 | if (rn_error) |
| 1423 | return tokval->t_type = TOKEN_ERRNUM; |
| 1424 | else |
| 1425 | return tokval->t_type = TOKEN_NUM; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1426 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1427 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1428 | if (tline->type == TOK_FLOAT) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1429 | return tokval->t_type = TOKEN_FLOAT; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1430 | } |
| 1431 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1432 | if (tline->type == TOK_STRING) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1433 | char bq, *ep; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1434 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1435 | bq = tline->text[0]; |
H. Peter Anvin | 1162704 | 2008-06-09 20:45:19 -0700 | [diff] [blame] | 1436 | tokval->t_charptr = tline->text; |
| 1437 | tokval->t_inttwo = nasm_unquote(tline->text, &ep); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 1438 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1439 | if (ep[0] != bq || ep[1] != '\0') |
| 1440 | return tokval->t_type = TOKEN_ERRSTR; |
| 1441 | else |
| 1442 | return tokval->t_type = TOKEN_STR; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1443 | } |
| 1444 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1445 | if (tline->type == TOK_OTHER) { |
| 1446 | if (!strcmp(tline->text, "<<")) |
| 1447 | return tokval->t_type = TOKEN_SHL; |
| 1448 | if (!strcmp(tline->text, ">>")) |
| 1449 | return tokval->t_type = TOKEN_SHR; |
| 1450 | if (!strcmp(tline->text, "//")) |
| 1451 | return tokval->t_type = TOKEN_SDIV; |
| 1452 | if (!strcmp(tline->text, "%%")) |
| 1453 | return tokval->t_type = TOKEN_SMOD; |
| 1454 | if (!strcmp(tline->text, "==")) |
| 1455 | return tokval->t_type = TOKEN_EQ; |
| 1456 | if (!strcmp(tline->text, "<>")) |
| 1457 | return tokval->t_type = TOKEN_NE; |
| 1458 | if (!strcmp(tline->text, "!=")) |
| 1459 | return tokval->t_type = TOKEN_NE; |
| 1460 | if (!strcmp(tline->text, "<=")) |
| 1461 | return tokval->t_type = TOKEN_LE; |
| 1462 | if (!strcmp(tline->text, ">=")) |
| 1463 | return tokval->t_type = TOKEN_GE; |
| 1464 | if (!strcmp(tline->text, "&&")) |
| 1465 | return tokval->t_type = TOKEN_DBL_AND; |
| 1466 | if (!strcmp(tline->text, "^^")) |
| 1467 | return tokval->t_type = TOKEN_DBL_XOR; |
| 1468 | if (!strcmp(tline->text, "||")) |
| 1469 | return tokval->t_type = TOKEN_DBL_OR; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1470 | } |
| 1471 | |
| 1472 | /* |
| 1473 | * We have no other options: just return the first character of |
| 1474 | * the token text. |
| 1475 | */ |
| 1476 | return tokval->t_type = tline->text[0]; |
| 1477 | } |
| 1478 | |
| 1479 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1480 | * Compare a string to the name of an existing macro; this is a |
| 1481 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 1482 | * depending on the value of the `casesense' parameter. |
| 1483 | */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 1484 | static int mstrcmp(const char *p, const char *q, bool casesense) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1485 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1486 | return casesense ? strcmp(p, q) : nasm_stricmp(p, q); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1487 | } |
| 1488 | |
| 1489 | /* |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1490 | * Compare a string to the name of an existing macro; this is a |
| 1491 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 1492 | * depending on the value of the `casesense' parameter. |
| 1493 | */ |
| 1494 | static int mmemcmp(const char *p, const char *q, size_t l, bool casesense) |
| 1495 | { |
| 1496 | return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l); |
| 1497 | } |
| 1498 | |
| 1499 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1500 | * Return the Context structure associated with a %$ token. Return |
| 1501 | * NULL, having _already_ reported an error condition, if the |
| 1502 | * context stack isn't deep enough for the supplied number of $ |
| 1503 | * signs. |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1504 | * |
| 1505 | * If "namep" is non-NULL, set it to the pointer to the macro name |
| 1506 | * tail, i.e. the part beyond %$... |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1507 | */ |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1508 | static Context *get_ctx(const char *name, const char **namep) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1509 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1510 | Context *ctx; |
| 1511 | int i; |
| 1512 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1513 | if (namep) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1514 | *namep = name; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1515 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1516 | if (!name || name[0] != '%' || name[1] != '$') |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1517 | return NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1518 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1519 | if (!cstk) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1520 | nasm_nonfatal("`%s': context stack is empty", name); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1521 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1524 | name += 2; |
| 1525 | ctx = cstk; |
| 1526 | i = 0; |
| 1527 | while (ctx && *name == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1528 | name++; |
| 1529 | i++; |
| 1530 | ctx = ctx->next; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1531 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1532 | if (!ctx) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1533 | nasm_nonfatal("`%s': context stack is only" |
| 1534 | " %d level%s deep", name, i, (i == 1 ? "" : "s")); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1535 | return NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1536 | } |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1537 | |
| 1538 | if (namep) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1539 | *namep = name; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1540 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1541 | return ctx; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
| 1544 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1545 | * Open an include file. This routine must always return a valid |
| 1546 | * file pointer if it returns - it's responsible for throwing an |
| 1547 | * ERR_FATAL and bombing out completely if not. It should also try |
| 1548 | * the include path one by one until it finds the file or reaches |
| 1549 | * the end of the path. |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1550 | * |
| 1551 | * Note: for INC_PROBE the function returns NULL at all times; |
| 1552 | * instead look for the |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1553 | */ |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1554 | enum incopen_mode { |
| 1555 | INC_NEEDED, /* File must exist */ |
| 1556 | INC_OPTIONAL, /* Missing is OK */ |
| 1557 | INC_PROBE /* Only an existence probe */ |
| 1558 | }; |
| 1559 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1560 | /* This is conducts a full pathname search */ |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1561 | static FILE *inc_fopen_search(const char *file, char **slpath, |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1562 | enum incopen_mode omode, enum file_flags fmode) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1563 | { |
H. Peter Anvin (Intel) | 6447109 | 2018-12-11 13:06:14 -0800 | [diff] [blame] | 1564 | const struct strlist_entry *ip = strlist_head(ipath_list); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1565 | FILE *fp; |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1566 | const char *prefix = ""; |
night199uk | fdb1a1b | 2018-10-18 23:19:47 +0200 | [diff] [blame] | 1567 | char *sp; |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1568 | bool found; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1569 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1570 | while (1) { |
night199uk | fdb1a1b | 2018-10-18 23:19:47 +0200 | [diff] [blame] | 1571 | sp = nasm_catfile(prefix, file); |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1572 | if (omode == INC_PROBE) { |
| 1573 | fp = NULL; |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1574 | found = nasm_file_exists(sp); |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1575 | } else { |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1576 | fp = nasm_open_read(sp, fmode); |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1577 | found = (fp != NULL); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1578 | } |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1579 | if (found) { |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1580 | *slpath = sp; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1581 | return fp; |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1582 | } |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 1583 | |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1584 | nasm_free(sp); |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 1585 | |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1586 | if (!ip) { |
| 1587 | *slpath = NULL; |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1588 | return NULL; |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1589 | } |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1590 | |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1591 | prefix = ip->str; |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1592 | ip = ip->next; |
| 1593 | } |
| 1594 | } |
| 1595 | |
| 1596 | /* |
| 1597 | * Open a file, or test for the presence of one (depending on omode), |
| 1598 | * considering the include path. |
| 1599 | */ |
| 1600 | static FILE *inc_fopen(const char *file, |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 1601 | struct strlist *dhead, |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 1602 | const char **found_path, |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1603 | enum incopen_mode omode, |
| 1604 | enum file_flags fmode) |
| 1605 | { |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1606 | struct hash_insert hi; |
| 1607 | void **hp; |
| 1608 | char *path; |
| 1609 | FILE *fp = NULL; |
| 1610 | |
| 1611 | hp = hash_find(&FileHash, file, &hi); |
| 1612 | if (hp) { |
| 1613 | path = *hp; |
Martin Storsjö | f283c8f | 2017-08-13 17:28:46 +0300 | [diff] [blame] | 1614 | if (path || omode != INC_NEEDED) { |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 1615 | strlist_add(dhead, path ? path : file); |
Martin Storsjö | f283c8f | 2017-08-13 17:28:46 +0300 | [diff] [blame] | 1616 | } |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1617 | } else { |
| 1618 | /* Need to do the actual path search */ |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1619 | fp = inc_fopen_search(file, &path, omode, fmode); |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1620 | |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1621 | /* Positive or negative result */ |
| 1622 | hash_add(&hi, nasm_strdup(file), path); |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1623 | |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 1624 | /* |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1625 | * Add file to dependency path. |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 1626 | */ |
| 1627 | if (path || omode != INC_NEEDED) |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 1628 | strlist_add(dhead, file); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1629 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1630 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1631 | if (!path) { |
| 1632 | if (omode == INC_NEEDED) |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 1633 | nasm_fatal("unable to open include file `%s'", file); |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1634 | } else { |
| 1635 | if (!fp && omode != INC_PROBE) |
| 1636 | fp = nasm_open_read(path, fmode); |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1637 | } |
| 1638 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1639 | if (found_path) |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 1640 | *found_path = path; |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1641 | |
| 1642 | return fp; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1643 | } |
| 1644 | |
| 1645 | /* |
Fabian Giesen | 0bbc38d | 2016-04-28 13:48:14 -0700 | [diff] [blame] | 1646 | * Opens an include or input file. Public version, for use by modules |
| 1647 | * that get a file:lineno pair and need to look at the file again |
| 1648 | * (e.g. the CodeView debug backend). Returns NULL on failure. |
| 1649 | */ |
H. Peter Anvin | 3e83cec | 2016-05-25 04:28:46 -0700 | [diff] [blame] | 1650 | FILE *pp_input_fopen(const char *filename, enum file_flags mode) |
Fabian Giesen | 0bbc38d | 2016-04-28 13:48:14 -0700 | [diff] [blame] | 1651 | { |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 1652 | return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode); |
Fabian Giesen | 0bbc38d | 2016-04-28 13:48:14 -0700 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1656 | * 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] | 1657 | * 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] | 1658 | * return true if _any_ single-line macro of that name is defined. |
| 1659 | * Otherwise, will return true if a single-line macro with either |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1660 | * `nparam' or no parameters is defined. |
| 1661 | * |
| 1662 | * If a macro with precisely the right number of parameters is |
H. Peter Anvin | ef7468f | 2002-04-30 20:57:59 +0000 | [diff] [blame] | 1663 | * defined, or nparam is -1, the address of the definition structure |
| 1664 | * 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] | 1665 | * is NULL, no action will be taken regarding its contents, and no |
| 1666 | * error will occur. |
| 1667 | * |
| 1668 | * Note that this is also called with nparam zero to resolve |
| 1669 | * `ifdef'. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1670 | * |
| 1671 | * If you already know which context macro belongs to, you can pass |
| 1672 | * the context pointer as first parameter; if you won't but name begins |
| 1673 | * with %$ the context will be automatically computed. If all_contexts |
| 1674 | * is true, macro will be searched in outer contexts as well. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1675 | */ |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1676 | static bool |
H. Peter Anvin | b2a5fda | 2008-06-19 21:42:42 -0700 | [diff] [blame] | 1677 | smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn, |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1678 | bool nocase) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1679 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1680 | struct hash_table *smtbl; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1681 | SMacro *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1682 | |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1683 | if (ctx) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1684 | smtbl = &ctx->localmac; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1685 | } else if (name[0] == '%' && name[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1686 | if (cstk) |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1687 | ctx = get_ctx(name, &name); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1688 | if (!ctx) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1689 | return false; /* got to return _something_ */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1690 | smtbl = &ctx->localmac; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1691 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1692 | smtbl = &smacros; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1693 | } |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1694 | m = (SMacro *) hash_findix(smtbl, name); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1695 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1696 | while (m) { |
| 1697 | if (!mstrcmp(m->name, name, m->casesense && nocase) && |
Charles Crayne | 192d5b5 | 2007-10-18 19:02:42 -0700 | [diff] [blame] | 1698 | (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1699 | if (defn) { |
Charles Crayne | 192d5b5 | 2007-10-18 19:02:42 -0700 | [diff] [blame] | 1700 | if (nparam == (int) m->nparam || nparam == -1) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1701 | *defn = m; |
| 1702 | else |
| 1703 | *defn = NULL; |
| 1704 | } |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1705 | return true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1706 | } |
| 1707 | m = m->next; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1708 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1709 | |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1710 | return false; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1711 | } |
| 1712 | |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 1713 | /* param should be a natural number [0; INT_MAX] */ |
| 1714 | static int read_param_count(const char *str) |
| 1715 | { |
| 1716 | int result; |
| 1717 | bool err; |
| 1718 | |
| 1719 | result = readnum(str, &err); |
| 1720 | if (result < 0 || result > INT_MAX) { |
| 1721 | result = 0; |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1722 | nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]", |
| 1723 | str, 0, INT_MAX); |
| 1724 | } else if (err) |
| 1725 | nasm_nonfatal("unable to parse parameter count `%s'", str); |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 1726 | return result; |
| 1727 | } |
| 1728 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1729 | /* |
| 1730 | * Count and mark off the parameters in a multi-line macro call. |
| 1731 | * This is called both from within the multi-line macro expansion |
| 1732 | * code, and also to mark off the default parameters when provided |
| 1733 | * in a %macro definition line. |
| 1734 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1735 | static void count_mmac_params(Token * t, int *nparam, Token *** params) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1736 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1737 | int paramsize, brace; |
| 1738 | |
| 1739 | *nparam = paramsize = 0; |
| 1740 | *params = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1741 | while (t) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1742 | /* +1: we need space for the final NULL */ |
H. Peter Anvin | 91fb6f1 | 2008-09-01 10:56:33 -0700 | [diff] [blame] | 1743 | if (*nparam+1 >= paramsize) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1744 | paramsize += PARAM_DELTA; |
| 1745 | *params = nasm_realloc(*params, sizeof(**params) * paramsize); |
| 1746 | } |
| 1747 | skip_white_(t); |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1748 | brace = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1749 | if (tok_is_(t, "{")) |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1750 | brace++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1751 | (*params)[(*nparam)++] = t; |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1752 | if (brace) { |
| 1753 | while (brace && (t = t->next) != NULL) { |
| 1754 | if (tok_is_(t, "{")) |
| 1755 | brace++; |
| 1756 | else if (tok_is_(t, "}")) |
| 1757 | brace--; |
| 1758 | } |
| 1759 | |
| 1760 | if (t) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1761 | /* |
| 1762 | * Now we've found the closing brace, look further |
| 1763 | * for the comma. |
| 1764 | */ |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1765 | t = t->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1766 | skip_white_(t); |
| 1767 | if (tok_isnt_(t, ",")) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1768 | nasm_nonfatal("braces do not enclose all of macro parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1769 | while (tok_isnt_(t, ",")) |
| 1770 | t = t->next; |
| 1771 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1772 | } |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1773 | } else { |
| 1774 | while (tok_isnt_(t, ",")) |
| 1775 | t = t->next; |
| 1776 | } |
| 1777 | if (t) { /* got a comma/brace */ |
| 1778 | t = t->next; /* eat the comma */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1779 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1780 | } |
| 1781 | } |
| 1782 | |
| 1783 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1784 | * Determine whether one of the various `if' conditions is true or |
| 1785 | * not. |
| 1786 | * |
| 1787 | * We must free the tline we get passed. |
| 1788 | */ |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1789 | static bool if_condition(Token * tline, enum preproc_token ct) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1790 | { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1791 | enum pp_conditional i = PP_COND(ct); |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1792 | bool j; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 1793 | Token *t, *tt, *origline; |
| 1794 | struct ppscan pps; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1795 | struct tokenval tokval; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1796 | expr *evalresult; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1797 | enum pp_token_type needtype; |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1798 | char *p; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1799 | |
| 1800 | origline = tline; |
| 1801 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1802 | switch (i) { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1803 | case PPC_IFCTX: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1804 | j = false; /* have we matched yet? */ |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1805 | while (true) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1806 | skip_white_(tline); |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1807 | if (!tline) |
| 1808 | break; |
| 1809 | if (tline->type != TOK_ID) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1810 | nasm_nonfatal("`%s' expects context identifiers", |
| 1811 | pp_directives[ct]); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1812 | free_tlist(origline); |
| 1813 | return -1; |
| 1814 | } |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1815 | if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1816 | j = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1817 | tline = tline->next; |
| 1818 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1819 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1820 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1821 | case PPC_IFDEF: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1822 | j = false; /* have we matched yet? */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1823 | while (tline) { |
| 1824 | skip_white_(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1825 | if (!tline || (tline->type != TOK_ID && |
| 1826 | (tline->type != TOK_PREPROC_ID || |
| 1827 | tline->text[1] != '$'))) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1828 | nasm_nonfatal("`%s' expects macro identifiers", |
| 1829 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1830 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1831 | } |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1832 | if (smacro_defined(NULL, tline->text, 0, NULL, true)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1833 | j = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1834 | tline = tline->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1835 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1836 | break; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1837 | |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1838 | case PPC_IFENV: |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1839 | tline = expand_smacro(tline); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1840 | j = false; /* have we matched yet? */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1841 | while (tline) { |
| 1842 | skip_white_(tline); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1843 | if (!tline || (tline->type != TOK_ID && |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1844 | tline->type != TOK_STRING && |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1845 | (tline->type != TOK_PREPROC_ID || |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1846 | tline->text[1] != '!'))) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1847 | nasm_nonfatal("`%s' expects environment variable names", |
| 1848 | pp_directives[ct]); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1849 | goto fail; |
| 1850 | } |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1851 | p = tline->text; |
| 1852 | if (tline->type == TOK_PREPROC_ID) |
| 1853 | p += 2; /* Skip leading %! */ |
H. Peter Anvin | 53e2e4c | 2018-11-28 15:01:40 -0800 | [diff] [blame] | 1854 | if (nasm_isquote(*p)) |
H. Peter Anvin | bb42d30 | 2019-04-22 14:29:29 -0700 | [diff] [blame] | 1855 | nasm_unquote_cstr(p, NULL); |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1856 | if (getenv(p)) |
| 1857 | j = true; |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1858 | tline = tline->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1859 | } |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1860 | break; |
| 1861 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1862 | case PPC_IFIDN: |
| 1863 | case PPC_IFIDNI: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1864 | tline = expand_smacro(tline); |
| 1865 | t = tt = tline; |
| 1866 | while (tok_isnt_(tt, ",")) |
| 1867 | tt = tt->next; |
| 1868 | if (!tt) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1869 | nasm_nonfatal("`%s' expects two comma-separated arguments", |
| 1870 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1871 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1872 | } |
| 1873 | tt = tt->next; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1874 | j = true; /* assume equality unless proved not */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1875 | while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) { |
| 1876 | if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1877 | nasm_nonfatal("`%s': more than one comma on line", |
| 1878 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1879 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1880 | } |
| 1881 | if (t->type == TOK_WHITESPACE) { |
| 1882 | t = t->next; |
| 1883 | continue; |
| 1884 | } |
| 1885 | if (tt->type == TOK_WHITESPACE) { |
| 1886 | tt = tt->next; |
| 1887 | continue; |
| 1888 | } |
| 1889 | if (tt->type != t->type) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1890 | j = false; /* found mismatching tokens */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1891 | break; |
| 1892 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1893 | /* When comparing strings, need to unquote them first */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1894 | if (t->type == TOK_STRING) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1895 | size_t l1 = nasm_unquote(t->text, NULL); |
| 1896 | size_t l2 = nasm_unquote(tt->text, NULL); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 1897 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1898 | if (l1 != l2) { |
| 1899 | j = false; |
| 1900 | break; |
| 1901 | } |
| 1902 | if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) { |
| 1903 | j = false; |
| 1904 | break; |
| 1905 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1906 | } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1907 | j = false; /* found mismatching tokens */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1908 | break; |
| 1909 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1910 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1911 | t = t->next; |
| 1912 | tt = tt->next; |
| 1913 | } |
| 1914 | if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1915 | j = false; /* trailing gunk on one end or other */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1916 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1917 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1918 | case PPC_IFMACRO: |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1919 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1920 | bool found = false; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1921 | MMacro searching, *mmac; |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 1922 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1923 | skip_white_(tline); |
| 1924 | tline = expand_id(tline); |
| 1925 | if (!tok_type_(tline, TOK_ID)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1926 | nasm_nonfatal("`%s' expects a macro name", pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1927 | goto fail; |
| 1928 | } |
| 1929 | searching.name = nasm_strdup(tline->text); |
| 1930 | searching.casesense = true; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1931 | searching.plus = false; |
| 1932 | searching.nolist = false; |
| 1933 | searching.in_progress = 0; |
| 1934 | searching.max_depth = 0; |
| 1935 | searching.rep_nest = NULL; |
| 1936 | searching.nparam_min = 0; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1937 | searching.nparam_max = INT_MAX; |
| 1938 | tline = expand_smacro(tline->next); |
| 1939 | skip_white_(tline); |
| 1940 | if (!tline) { |
| 1941 | } else if (!tok_type_(tline, TOK_NUMBER)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1942 | nasm_nonfatal("`%s' expects a parameter count or nothing", |
| 1943 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1944 | } else { |
| 1945 | searching.nparam_min = searching.nparam_max = |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 1946 | read_param_count(tline->text); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1947 | } |
| 1948 | if (tline && tok_is_(tline->next, "-")) { |
| 1949 | tline = tline->next->next; |
| 1950 | if (tok_is_(tline, "*")) |
| 1951 | searching.nparam_max = INT_MAX; |
| 1952 | else if (!tok_type_(tline, TOK_NUMBER)) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1953 | nasm_nonfatal("`%s' expects a parameter count after `-'", |
| 1954 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1955 | else { |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 1956 | searching.nparam_max = read_param_count(tline->text); |
Cyrill Gorcunov | c9244ea | 2017-10-22 15:25:48 +0300 | [diff] [blame] | 1957 | if (searching.nparam_min > searching.nparam_max) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1958 | nasm_nonfatal("minimum parameter count exceeds maximum"); |
Cyrill Gorcunov | c9244ea | 2017-10-22 15:25:48 +0300 | [diff] [blame] | 1959 | searching.nparam_max = searching.nparam_min; |
| 1960 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1961 | } |
| 1962 | } |
| 1963 | if (tline && tok_is_(tline->next, "+")) { |
| 1964 | tline = tline->next; |
| 1965 | searching.plus = true; |
| 1966 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1967 | mmac = (MMacro *) hash_findix(&mmacros, searching.name); |
| 1968 | while (mmac) { |
| 1969 | if (!strcmp(mmac->name, searching.name) && |
| 1970 | (mmac->nparam_min <= searching.nparam_max |
| 1971 | || searching.plus) |
| 1972 | && (searching.nparam_min <= mmac->nparam_max |
| 1973 | || mmac->plus)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1974 | found = true; |
| 1975 | break; |
| 1976 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1977 | mmac = mmac->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1978 | } |
| 1979 | if (tline && tline->next) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 1980 | nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1981 | nasm_free(searching.name); |
| 1982 | j = found; |
| 1983 | break; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1984 | } |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 1985 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1986 | case PPC_IFID: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1987 | needtype = TOK_ID; |
| 1988 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1989 | case PPC_IFNUM: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1990 | needtype = TOK_NUMBER; |
| 1991 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1992 | case PPC_IFSTR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1993 | needtype = TOK_STRING; |
| 1994 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1995 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1996 | iftype: |
| 1997 | t = tline = expand_smacro(tline); |
H. Peter Anvin | d85d250 | 2008-05-04 17:53:31 -0700 | [diff] [blame] | 1998 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1999 | while (tok_type_(t, TOK_WHITESPACE) || |
| 2000 | (needtype == TOK_NUMBER && |
| 2001 | tok_type_(t, TOK_OTHER) && |
| 2002 | (t->text[0] == '-' || t->text[0] == '+') && |
| 2003 | !t->text[1])) |
| 2004 | t = t->next; |
H. Peter Anvin | d85d250 | 2008-05-04 17:53:31 -0700 | [diff] [blame] | 2005 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2006 | j = tok_type_(t, needtype); |
| 2007 | break; |
H. Peter Anvin | cbf768d | 2008-02-16 16:41:25 -0800 | [diff] [blame] | 2008 | |
| 2009 | case PPC_IFTOKEN: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2010 | t = tline = expand_smacro(tline); |
| 2011 | while (tok_type_(t, TOK_WHITESPACE)) |
| 2012 | t = t->next; |
H. Peter Anvin | cbf768d | 2008-02-16 16:41:25 -0800 | [diff] [blame] | 2013 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2014 | j = false; |
| 2015 | if (t) { |
| 2016 | t = t->next; /* Skip the actual token */ |
| 2017 | while (tok_type_(t, TOK_WHITESPACE)) |
| 2018 | t = t->next; |
| 2019 | j = !t; /* Should be nothing left */ |
| 2020 | } |
| 2021 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2022 | |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 2023 | case PPC_IFEMPTY: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2024 | t = tline = expand_smacro(tline); |
| 2025 | while (tok_type_(t, TOK_WHITESPACE)) |
| 2026 | t = t->next; |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 2027 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2028 | j = !t; /* Should be empty */ |
| 2029 | break; |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 2030 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2031 | case PPC_IF: |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2032 | pps.tptr = tline = expand_smacro(tline); |
| 2033 | pps.ntokens = -1; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2034 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2035 | evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2036 | if (!evalresult) |
| 2037 | return -1; |
| 2038 | if (tokval.t_type) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2039 | nasm_warn(WARN_OTHER, "trailing garbage after expression ignored"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2040 | if (!is_simple(evalresult)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2041 | nasm_nonfatal("non-constant value given to `%s'", |
| 2042 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2043 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2044 | } |
Chuck Crayne | 60ae75d | 2007-05-02 01:59:16 +0000 | [diff] [blame] | 2045 | j = reloc_value(evalresult) != 0; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2046 | break; |
H. Peter Anvin | 95e2882 | 2007-09-12 04:20:08 +0000 | [diff] [blame] | 2047 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2048 | default: |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2049 | nasm_fatal("preprocessor directive `%s' not yet implemented", |
| 2050 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2051 | goto fail; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2052 | } |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2053 | |
| 2054 | free_tlist(origline); |
| 2055 | return j ^ PP_NEGATIVE(ct); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2056 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2057 | fail: |
| 2058 | free_tlist(origline); |
| 2059 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2060 | } |
| 2061 | |
| 2062 | /* |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 2063 | * Default smacro expansion routine: just returns a copy of the |
| 2064 | * expansion list. |
| 2065 | */ |
| 2066 | static Token * |
| 2067 | smacro_expand_default(const SMacro *s, Token **params, unsigned int nparams) |
| 2068 | { |
| 2069 | (void)params; |
| 2070 | (void)nparams; |
| 2071 | |
| 2072 | return dup_tlist(s->expansion, NULL); |
| 2073 | } |
| 2074 | |
| 2075 | /* |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 2076 | * Emit a macro defintion or undef to the listing file, if |
| 2077 | * desired. This is similar to detoken(), but it handles the reverse |
| 2078 | * expansion list, does not expand %! or local variable tokens, and |
| 2079 | * does some special handling for macro parameters. |
| 2080 | */ |
| 2081 | static void |
| 2082 | list_smacro_def(enum preproc_token op, const Context *ctx, const SMacro *m) |
| 2083 | { |
| 2084 | static const Token unused_arg_name = { NULL, "", 0, TOK_OTHER }; |
| 2085 | const Token **param_names; |
| 2086 | Token *junk_names = NULL; |
| 2087 | Token *t; |
| 2088 | size_t namelen, size; |
| 2089 | unsigned int i; |
| 2090 | char *def, *p; |
| 2091 | unsigned int context_depth = 0; |
| 2092 | |
| 2093 | nasm_newn(param_names, m->nparam); |
| 2094 | |
| 2095 | namelen = strlen(m->name); |
| 2096 | size = namelen + 2; /* Include room for space after name + NUL */ |
| 2097 | |
| 2098 | if (ctx) { |
| 2099 | context_depth = cstk->depth - ctx->depth + 1; |
| 2100 | size += context_depth + 1; /* % + some number of $ */ |
| 2101 | } |
| 2102 | |
| 2103 | list_for_each(t, m->expansion) { |
| 2104 | if (!t->text) { |
| 2105 | size++; /* Whitespace, presumably */ |
| 2106 | } else { |
| 2107 | size += t->len; |
| 2108 | if (is_smac_param(t->type)) |
| 2109 | param_names[smac_nparam(t->type)] = t; |
| 2110 | } |
| 2111 | } |
| 2112 | |
| 2113 | if (m->nparam) { |
| 2114 | /* |
| 2115 | * Space for ( and either , or ) around each |
| 2116 | * parameter, plus an optional =. |
| 2117 | */ |
| 2118 | size += 1 + 2 * m->nparam; |
| 2119 | for (i = 0; i < m->nparam; i++) { |
| 2120 | if (!param_names[i]) |
| 2121 | param_names[i] = &unused_arg_name; |
| 2122 | size += param_names[i]->len; |
| 2123 | } |
| 2124 | } |
| 2125 | |
| 2126 | def = nasm_malloc(size); |
| 2127 | p = def+size; |
| 2128 | *--p = '\0'; |
| 2129 | |
| 2130 | list_for_each(t, m->expansion) { |
| 2131 | if (!t->text) { |
| 2132 | *--p = ' '; |
| 2133 | } else { |
| 2134 | p -= t->len; |
| 2135 | memcpy(p, t->text, t->len); |
| 2136 | } |
| 2137 | } |
| 2138 | |
| 2139 | *--p = ' '; |
| 2140 | |
| 2141 | if (m->nparam) { |
| 2142 | *--p = ')'; |
| 2143 | for (i = m->nparam; i--;) { |
| 2144 | p -= param_names[i]->len; |
| 2145 | memcpy(p, param_names[i]->text, param_names[i]->len); |
| 2146 | if (m->eval_param && m->eval_param[i]) |
| 2147 | *--p = '='; |
| 2148 | *--p = ','; |
| 2149 | } |
| 2150 | *p = '('; /* First parameter starts with ( not , */ |
| 2151 | |
| 2152 | free_tlist(junk_names); |
| 2153 | } |
| 2154 | |
| 2155 | p -= namelen; |
| 2156 | memcpy(p, m->name, namelen); |
| 2157 | |
| 2158 | if (context_depth) { |
| 2159 | for (i = 0; i < context_depth; i++) |
| 2160 | *--p = '$'; |
| 2161 | *--p = '%'; |
| 2162 | } |
| 2163 | |
| 2164 | nasm_listmsg("%s %s", pp_directives[op], p); |
| 2165 | |
| 2166 | nasm_free(def); |
| 2167 | } |
| 2168 | |
| 2169 | /* |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2170 | * Common code for defining an smacro |
| 2171 | */ |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2172 | static SMacro *define_smacro(Context *ctx, const char *mname, |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 2173 | bool casesense, int nparam, |
| 2174 | bool *eval_param, Token *expansion) |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2175 | { |
| 2176 | SMacro *smac, **smhead; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2177 | struct hash_table *smtbl; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2178 | |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2179 | if (smacro_defined(ctx, mname, nparam, &smac, casesense)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2180 | if (!smac) { |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2181 | nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and" |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2182 | " without parameters", mname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2183 | /* |
| 2184 | * Some instances of the old code considered this a failure, |
| 2185 | * some others didn't. What is the right thing to do here? |
| 2186 | */ |
| 2187 | free_tlist(expansion); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2188 | return NULL; /* Failure */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2189 | } else { |
| 2190 | /* |
| 2191 | * We're redefining, so we have to take over an |
| 2192 | * existing SMacro structure. This means freeing |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2193 | * what was already in it, but not the structure itself. |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2194 | */ |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 2195 | clear_smacro(smac); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2196 | } |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2197 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2198 | smtbl = ctx ? &ctx->localmac : &smacros; |
| 2199 | smhead = (SMacro **) hash_findi_add(smtbl, mname); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2200 | nasm_new(smac); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2201 | smac->next = *smhead; |
| 2202 | *smhead = smac; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2203 | } |
| 2204 | smac->name = nasm_strdup(mname); |
| 2205 | smac->casesense = casesense; |
| 2206 | smac->nparam = nparam; |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 2207 | smac->eval_param = eval_param; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2208 | smac->expansion = expansion; |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 2209 | smac->expand = smacro_expand_default; |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 2210 | |
| 2211 | if (list_option('m')) |
| 2212 | list_smacro_def(casesense ? PP_DEFINE : PP_IDEFINE, ctx, smac); |
| 2213 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2214 | return smac; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2215 | } |
| 2216 | |
| 2217 | /* |
| 2218 | * Undefine an smacro |
| 2219 | */ |
| 2220 | static void undef_smacro(Context *ctx, const char *mname) |
| 2221 | { |
| 2222 | SMacro **smhead, *s, **sp; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2223 | struct hash_table *smtbl; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2224 | |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2225 | smtbl = ctx ? &ctx->localmac : &smacros; |
| 2226 | smhead = (SMacro **)hash_findi(smtbl, mname, NULL); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2227 | |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2228 | if (smhead) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2229 | /* |
| 2230 | * We now have a macro name... go hunt for it. |
| 2231 | */ |
| 2232 | sp = smhead; |
| 2233 | while ((s = *sp) != NULL) { |
| 2234 | if (!mstrcmp(s->name, mname, s->casesense)) { |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 2235 | if (list_option('m')) |
| 2236 | list_smacro_def(PP_UNDEF, ctx, s); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2237 | *sp = s->next; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 2238 | free_smacro(s); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2239 | } else { |
| 2240 | sp = &s->next; |
| 2241 | } |
| 2242 | } |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2243 | } |
| 2244 | } |
| 2245 | |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2246 | /* |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2247 | * Parse a mmacro specification. |
| 2248 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2249 | 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] | 2250 | { |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2251 | tline = tline->next; |
| 2252 | skip_white_(tline); |
| 2253 | tline = expand_id(tline); |
| 2254 | if (!tok_type_(tline, TOK_ID)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2255 | nasm_nonfatal("`%s' expects a macro name", directive); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2256 | return false; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2257 | } |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2258 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2259 | def->prev = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2260 | def->name = nasm_strdup(tline->text); |
| 2261 | def->plus = false; |
| 2262 | def->nolist = false; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2263 | def->in_progress = 0; |
| 2264 | def->rep_nest = NULL; |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2265 | def->nparam_min = 0; |
| 2266 | def->nparam_max = 0; |
| 2267 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2268 | tline = expand_smacro(tline->next); |
| 2269 | skip_white_(tline); |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2270 | if (!tok_type_(tline, TOK_NUMBER)) |
| 2271 | nasm_nonfatal("`%s' expects a parameter count", directive); |
| 2272 | else |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 2273 | def->nparam_min = def->nparam_max = read_param_count(tline->text); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2274 | if (tline && tok_is_(tline->next, "-")) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2275 | tline = tline->next->next; |
| 2276 | if (tok_is_(tline, "*")) { |
| 2277 | def->nparam_max = INT_MAX; |
| 2278 | } else if (!tok_type_(tline, TOK_NUMBER)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2279 | nasm_nonfatal("`%s' expects a parameter count after `-'", directive); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2280 | } else { |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 2281 | def->nparam_max = read_param_count(tline->text); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2282 | if (def->nparam_min > def->nparam_max) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2283 | nasm_nonfatal("minimum parameter count exceeds maximum"); |
Cyrill Gorcunov | c9244ea | 2017-10-22 15:25:48 +0300 | [diff] [blame] | 2284 | def->nparam_max = def->nparam_min; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2285 | } |
| 2286 | } |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2287 | } |
| 2288 | if (tline && tok_is_(tline->next, "+")) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2289 | tline = tline->next; |
| 2290 | def->plus = true; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2291 | } |
| 2292 | if (tline && tok_type_(tline->next, TOK_ID) && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2293 | !nasm_stricmp(tline->next->text, ".nolist")) { |
| 2294 | tline = tline->next; |
| 2295 | def->nolist = true; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2296 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2297 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2298 | /* |
| 2299 | * Handle default parameters. |
| 2300 | */ |
| 2301 | if (tline && tline->next) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2302 | def->dlist = tline->next; |
| 2303 | tline->next = NULL; |
| 2304 | count_mmac_params(def->dlist, &def->ndefs, &def->defaults); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2305 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2306 | def->dlist = NULL; |
| 2307 | def->defaults = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2308 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2309 | def->expansion = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2310 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2311 | 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] | 2312 | !def->plus) { |
| 2313 | /* |
| 2314 | *!macro-defaults [on] macros with more default than optional parameters |
| 2315 | *! warns when a macro has more default parameters than optional parameters. |
| 2316 | *! See \k{mlmacdef} for why might want to disable this warning. |
| 2317 | */ |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2318 | nasm_warn(WARN_MACRO_DEFAULTS, |
H. Peter Anvin (Intel) | 723ab48 | 2018-12-13 21:53:31 -0800 | [diff] [blame] | 2319 | "too many default macro parameters in macro `%s'", def->name); |
| 2320 | } |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2321 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2322 | return true; |
| 2323 | } |
| 2324 | |
| 2325 | |
| 2326 | /* |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2327 | * Decode a size directive |
| 2328 | */ |
| 2329 | static int parse_size(const char *str) { |
| 2330 | static const char *size_names[] = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2331 | { "byte", "dword", "oword", "qword", "tword", "word", "yword" }; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2332 | static const int sizes[] = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2333 | { 0, 1, 4, 16, 8, 10, 2, 32 }; |
Cyrill Gorcunov | c713b5f | 2018-09-29 14:30:14 +0300 | [diff] [blame] | 2334 | 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] | 2335 | } |
| 2336 | |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2337 | /* |
| 2338 | * Process a preprocessor %pragma directive. Currently there are none. |
| 2339 | * Gets passed the token list starting with the "preproc" token from |
| 2340 | * "%pragma preproc". |
| 2341 | */ |
| 2342 | static void do_pragma_preproc(Token *tline) |
| 2343 | { |
| 2344 | /* Skip to the real stuff */ |
| 2345 | tline = tline->next; |
| 2346 | skip_white_(tline); |
| 2347 | if (!tline) |
| 2348 | return; |
| 2349 | |
| 2350 | (void)tline; /* Nothing else to do at present */ |
| 2351 | } |
| 2352 | |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2353 | /** |
| 2354 | * find and process preprocessor directive in passed line |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2355 | * Find out if a line contains a preprocessor directive, and deal |
| 2356 | * with it if so. |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2357 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2358 | * If a directive _is_ found, it is the responsibility of this routine |
| 2359 | * (and not the caller) to free_tlist() the line. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2360 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2361 | * @param tline a pointer to the current tokeninzed line linked list |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2362 | * @param output if this directive generated output |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2363 | * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2364 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2365 | */ |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 2366 | static int do_directive(Token *tline, Token **output) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2367 | { |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2368 | enum preproc_token i; |
| 2369 | int j; |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 2370 | bool err; |
| 2371 | int nparam; |
| 2372 | bool nolist; |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 2373 | bool casesense; |
H. Peter Anvin | 8cfdb9d | 2007-09-14 18:36:01 -0700 | [diff] [blame] | 2374 | int k, m; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2375 | int offset; |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 2376 | char *p, *pp; |
| 2377 | const char *found_path; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 2378 | const char *mname; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2379 | struct ppscan pps; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2380 | Include *inc; |
| 2381 | Context *ctx; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2382 | Cond *cond; |
| 2383 | MMacro *mmac, **mmhead; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2384 | Token *t = NULL, *tt, *param_start, *macro_start, *last, *origline; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2385 | Line *l; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2386 | struct tokenval tokval; |
| 2387 | expr *evalresult; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2388 | MMacro *tmp_defining; /* Used when manipulating rep_nest */ |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 2389 | int64_t count; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 2390 | size_t len; |
H. Peter Anvin (Intel) | 6bde2ed | 2018-12-13 19:39:41 -0800 | [diff] [blame] | 2391 | errflags severity; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2392 | const char *dname; /* Name of directive, for messages */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2393 | |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2394 | *output = NULL; /* No output generated */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2395 | origline = tline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2396 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2397 | skip_white_(tline); |
H. Peter Anvin | f2936d7 | 2008-06-04 15:11:23 -0700 | [diff] [blame] | 2398 | if (!tline || !tok_type_(tline, TOK_PREPROC_ID) || |
Cyrill Gorcunov | 4b5b737 | 2018-10-29 22:54:08 +0300 | [diff] [blame] | 2399 | (tline->text[0] && (tline->text[1] == '%' || |
| 2400 | tline->text[1] == '$' || |
| 2401 | tline->text[1] == '!'))) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2402 | return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2403 | |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2404 | i = pp_token_hash(tline->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2405 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2406 | /* |
| 2407 | * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO |
| 2408 | * since they are known to be buggy at moment, we need to fix them |
| 2409 | * in future release (2.09-2.10) |
| 2410 | */ |
Philipp Kloke | b432f57 | 2013-03-31 12:01:23 +0200 | [diff] [blame] | 2411 | if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2412 | nasm_nonfatal("unknown preprocessor directive `%s'", tline->text); |
| 2413 | return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2414 | } |
| 2415 | |
| 2416 | /* |
| 2417 | * If we're in a non-emitting branch of a condition construct, |
| 2418 | * or walking to the end of an already terminated %rep block, |
| 2419 | * we should ignore all directives except for condition |
| 2420 | * directives. |
| 2421 | */ |
| 2422 | if (((istk->conds && !emitting(istk->conds->state)) || |
| 2423 | (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) { |
| 2424 | return NO_DIRECTIVE_FOUND; |
| 2425 | } |
| 2426 | |
| 2427 | /* |
| 2428 | * If we're defining a macro or reading a %rep block, we should |
| 2429 | * ignore all directives except for %macro/%imacro (which nest), |
| 2430 | * %endm/%endmacro, and (only if we're in a %rep block) %endrep. |
| 2431 | * If we're in a %rep block, another %rep nests, so should be let through. |
| 2432 | */ |
| 2433 | if (defining && i != PP_MACRO && i != PP_IMACRO && |
| 2434 | i != PP_RMACRO && i != PP_IRMACRO && |
| 2435 | i != PP_ENDMACRO && i != PP_ENDM && |
| 2436 | (defining->name || (i != PP_ENDREP && i != PP_REP))) { |
| 2437 | return NO_DIRECTIVE_FOUND; |
| 2438 | } |
| 2439 | |
| 2440 | if (defining) { |
| 2441 | if (i == PP_MACRO || i == PP_IMACRO || |
| 2442 | i == PP_RMACRO || i == PP_IRMACRO) { |
| 2443 | nested_mac_count++; |
| 2444 | return NO_DIRECTIVE_FOUND; |
| 2445 | } else if (nested_mac_count > 0) { |
| 2446 | if (i == PP_ENDMACRO) { |
| 2447 | nested_mac_count--; |
| 2448 | return NO_DIRECTIVE_FOUND; |
| 2449 | } |
| 2450 | } |
| 2451 | if (!defining->name) { |
| 2452 | if (i == PP_REP) { |
| 2453 | nested_rep_count++; |
| 2454 | return NO_DIRECTIVE_FOUND; |
| 2455 | } else if (nested_rep_count > 0) { |
| 2456 | if (i == PP_ENDREP) { |
| 2457 | nested_rep_count--; |
| 2458 | return NO_DIRECTIVE_FOUND; |
| 2459 | } |
| 2460 | } |
| 2461 | } |
| 2462 | } |
| 2463 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2464 | dname = pp_directives[i]; /* Directive name, for error messages */ |
| 2465 | casesense = true; /* Default to case sensitive */ |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2466 | switch (i) { |
| 2467 | case PP_INVALID: |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2468 | nasm_nonfatal("unknown preprocessor directive `%s'", tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2469 | return NO_DIRECTIVE_FOUND; /* didn't get it */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2470 | |
H. Peter Anvin | 3f87a2a | 2016-10-04 14:07:19 -0700 | [diff] [blame] | 2471 | case PP_PRAGMA: |
| 2472 | /* |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2473 | * %pragma namespace options... |
| 2474 | * |
| 2475 | * The namespace "preproc" is reserved for the preprocessor; |
| 2476 | * all other namespaces generate a [pragma] assembly directive. |
| 2477 | * |
| 2478 | * Invalid %pragmas are ignored and may have different |
| 2479 | * meaning in future versions of NASM. |
H. Peter Anvin | 3f87a2a | 2016-10-04 14:07:19 -0700 | [diff] [blame] | 2480 | */ |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2481 | tline = tline->next; |
| 2482 | skip_white_(tline); |
| 2483 | tline = expand_smacro(tline); |
| 2484 | if (tok_type_(tline, TOK_ID)) { |
| 2485 | if (!nasm_stricmp(tline->text, "preproc")) { |
| 2486 | /* Preprocessor pragma */ |
| 2487 | do_pragma_preproc(tline); |
| 2488 | } else { |
| 2489 | /* Build the assembler directive */ |
| 2490 | t = new_Token(NULL, TOK_OTHER, "[", 1); |
| 2491 | t->next = new_Token(NULL, TOK_ID, "pragma", 6); |
| 2492 | t->next->next = new_Token(tline, TOK_WHITESPACE, NULL, 0); |
| 2493 | tline = t; |
| 2494 | for (t = tline; t->next; t = t->next) |
| 2495 | ; |
| 2496 | t->next = new_Token(NULL, TOK_OTHER, "]", 1); |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 2497 | *output = tline; |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2498 | } |
| 2499 | } |
H. Peter Anvin | 3f87a2a | 2016-10-04 14:07:19 -0700 | [diff] [blame] | 2500 | free_tlist(origline); |
| 2501 | return DIRECTIVE_FOUND; |
| 2502 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2503 | case PP_STACKSIZE: |
| 2504 | /* Directive to tell NASM what the default stack size is. The |
| 2505 | * default is for a 16-bit stack, and this can be overriden with |
| 2506 | * %stacksize large. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2507 | */ |
| 2508 | tline = tline->next; |
| 2509 | if (tline && tline->type == TOK_WHITESPACE) |
| 2510 | tline = tline->next; |
| 2511 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2512 | nasm_nonfatal("`%s' missing size parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2513 | free_tlist(origline); |
| 2514 | return DIRECTIVE_FOUND; |
| 2515 | } |
| 2516 | if (nasm_stricmp(tline->text, "flat") == 0) { |
| 2517 | /* All subsequent ARG directives are for a 32-bit stack */ |
| 2518 | StackSize = 4; |
| 2519 | StackPointer = "ebp"; |
| 2520 | ArgOffset = 8; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2521 | LocalOffset = 0; |
Charles Crayne | 7eaf919 | 2007-11-08 22:11:14 -0800 | [diff] [blame] | 2522 | } else if (nasm_stricmp(tline->text, "flat64") == 0) { |
| 2523 | /* All subsequent ARG directives are for a 64-bit stack */ |
| 2524 | StackSize = 8; |
| 2525 | StackPointer = "rbp"; |
Per Jessen | 53252e0 | 2010-02-11 00:16:59 +0300 | [diff] [blame] | 2526 | ArgOffset = 16; |
Charles Crayne | 7eaf919 | 2007-11-08 22:11:14 -0800 | [diff] [blame] | 2527 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2528 | } else if (nasm_stricmp(tline->text, "large") == 0) { |
| 2529 | /* All subsequent ARG directives are for a 16-bit stack, |
| 2530 | * far function call. |
| 2531 | */ |
| 2532 | StackSize = 2; |
| 2533 | StackPointer = "bp"; |
| 2534 | ArgOffset = 4; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2535 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2536 | } else if (nasm_stricmp(tline->text, "small") == 0) { |
| 2537 | /* All subsequent ARG directives are for a 16-bit stack, |
| 2538 | * far function call. We don't support near functions. |
| 2539 | */ |
| 2540 | StackSize = 2; |
| 2541 | StackPointer = "bp"; |
| 2542 | ArgOffset = 6; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2543 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2544 | } else { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2545 | nasm_nonfatal("`%s' invalid size type", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2546 | free_tlist(origline); |
| 2547 | return DIRECTIVE_FOUND; |
| 2548 | } |
| 2549 | free_tlist(origline); |
| 2550 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2551 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2552 | case PP_ARG: |
| 2553 | /* TASM like ARG directive to define arguments to functions, in |
| 2554 | * the following form: |
| 2555 | * |
| 2556 | * ARG arg1:WORD, arg2:DWORD, arg4:QWORD |
| 2557 | */ |
| 2558 | offset = ArgOffset; |
| 2559 | do { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 2560 | char *arg, directive[256]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2561 | int size = StackSize; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2562 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2563 | /* Find the argument name */ |
| 2564 | tline = tline->next; |
| 2565 | if (tline && tline->type == TOK_WHITESPACE) |
| 2566 | tline = tline->next; |
| 2567 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2568 | nasm_nonfatal("`%s' missing argument parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2569 | free_tlist(origline); |
| 2570 | return DIRECTIVE_FOUND; |
| 2571 | } |
| 2572 | arg = tline->text; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2573 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2574 | /* Find the argument size type */ |
| 2575 | tline = tline->next; |
| 2576 | if (!tline || tline->type != TOK_OTHER |
| 2577 | || tline->text[0] != ':') { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2578 | nasm_nonfatal("syntax error processing `%s' directive", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2579 | free_tlist(origline); |
| 2580 | return DIRECTIVE_FOUND; |
| 2581 | } |
| 2582 | tline = tline->next; |
| 2583 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2584 | nasm_nonfatal("`%s' missing size type parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2585 | free_tlist(origline); |
| 2586 | return DIRECTIVE_FOUND; |
| 2587 | } |
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 | /* Allow macro expansion of type parameter */ |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2590 | tt = tokenize(tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2591 | tt = expand_smacro(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2592 | size = parse_size(tt->text); |
| 2593 | if (!size) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2594 | nasm_nonfatal("invalid size type for `%s' missing directive", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2595 | free_tlist(tt); |
| 2596 | free_tlist(origline); |
| 2597 | return DIRECTIVE_FOUND; |
| 2598 | } |
| 2599 | free_tlist(tt); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2600 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2601 | /* Round up to even stack slots */ |
| 2602 | size = ALIGN(size, StackSize); |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2603 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2604 | /* Now define the macro for the argument */ |
| 2605 | snprintf(directive, sizeof(directive), "%%define %s (%s+%d)", |
| 2606 | arg, StackPointer, offset); |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2607 | do_directive(tokenize(directive), output); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2608 | offset += size; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2609 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2610 | /* Move to the next argument in the list */ |
| 2611 | tline = tline->next; |
| 2612 | if (tline && tline->type == TOK_WHITESPACE) |
| 2613 | tline = tline->next; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2614 | } while (tline && tline->type == TOK_OTHER && tline->text[0] == ','); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2615 | ArgOffset = offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2616 | free_tlist(origline); |
| 2617 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2618 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2619 | case PP_LOCAL: |
| 2620 | /* TASM like LOCAL directive to define local variables for a |
| 2621 | * function, in the following form: |
| 2622 | * |
| 2623 | * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize |
| 2624 | * |
| 2625 | * The '= LocalSize' at the end is ignored by NASM, but is |
| 2626 | * required by TASM to define the local parameter size (and used |
| 2627 | * by the TASM macro package). |
| 2628 | */ |
| 2629 | offset = LocalOffset; |
| 2630 | do { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 2631 | char *local, directive[256]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2632 | int size = StackSize; |
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 | /* Find the argument name */ |
| 2635 | tline = tline->next; |
| 2636 | if (tline && tline->type == TOK_WHITESPACE) |
| 2637 | tline = tline->next; |
| 2638 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2639 | nasm_nonfatal("`%s' missing argument parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2640 | free_tlist(origline); |
| 2641 | return DIRECTIVE_FOUND; |
| 2642 | } |
| 2643 | local = tline->text; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2644 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2645 | /* Find the argument size type */ |
| 2646 | tline = tline->next; |
| 2647 | if (!tline || tline->type != TOK_OTHER |
| 2648 | || tline->text[0] != ':') { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2649 | nasm_nonfatal("syntax error processing `%s' directive", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2650 | free_tlist(origline); |
| 2651 | return DIRECTIVE_FOUND; |
| 2652 | } |
| 2653 | tline = tline->next; |
| 2654 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2655 | nasm_nonfatal("`%s' missing size type parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2656 | free_tlist(origline); |
| 2657 | return DIRECTIVE_FOUND; |
| 2658 | } |
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 | /* Allow macro expansion of type parameter */ |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2661 | tt = tokenize(tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2662 | tt = expand_smacro(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2663 | size = parse_size(tt->text); |
| 2664 | if (!size) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2665 | nasm_nonfatal("invalid size type for `%s' missing directive", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2666 | free_tlist(tt); |
| 2667 | free_tlist(origline); |
| 2668 | return DIRECTIVE_FOUND; |
| 2669 | } |
| 2670 | free_tlist(tt); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2671 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2672 | /* Round up to even stack slots */ |
| 2673 | size = ALIGN(size, StackSize); |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2674 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2675 | offset += size; /* Negative offset, increment before */ |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2676 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2677 | /* Now define the macro for the argument */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2678 | snprintf(directive, sizeof(directive), "%%define %s (%s-%d)", |
| 2679 | local, StackPointer, offset); |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2680 | do_directive(tokenize(directive), output); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2681 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2682 | /* Now define the assign to setup the enter_c macro correctly */ |
| 2683 | snprintf(directive, sizeof(directive), |
| 2684 | "%%assign %%$localsize %%$localsize+%d", size); |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2685 | do_directive(tokenize(directive), output); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2686 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2687 | /* Move to the next argument in the list */ |
| 2688 | tline = tline->next; |
| 2689 | if (tline && tline->type == TOK_WHITESPACE) |
| 2690 | tline = tline->next; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2691 | } while (tline && tline->type == TOK_OTHER && tline->text[0] == ','); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2692 | LocalOffset = offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2693 | free_tlist(origline); |
| 2694 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2695 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2696 | case PP_CLEAR: |
| 2697 | if (tline->next) |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2698 | nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2699 | free_macros(); |
| 2700 | init_macros(); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2701 | free_tlist(origline); |
| 2702 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2703 | |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2704 | case PP_DEPEND: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2705 | t = tline->next = expand_smacro(tline->next); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2706 | skip_white_(t); |
| 2707 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2708 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2709 | nasm_nonfatal("`%s' expects a file name", dname); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2710 | free_tlist(origline); |
| 2711 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2712 | } |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2713 | if (t->next) |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2714 | nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2715 | p = t->text; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2716 | if (t->type != TOK_INTERNAL_STRING) |
H. Peter Anvin | bb42d30 | 2019-04-22 14:29:29 -0700 | [diff] [blame] | 2717 | nasm_unquote_cstr(p, NULL); |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 2718 | strlist_add(deplist, p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2719 | free_tlist(origline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2720 | return DIRECTIVE_FOUND; |
| 2721 | |
| 2722 | case PP_INCLUDE: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2723 | t = tline->next = expand_smacro(tline->next); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2724 | skip_white_(t); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2725 | |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2726 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2727 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2728 | nasm_nonfatal("`%s' expects a file name", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2729 | free_tlist(origline); |
| 2730 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2731 | } |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2732 | if (t->next) |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2733 | nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2734 | p = t->text; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2735 | if (t->type != TOK_INTERNAL_STRING) |
H. Peter Anvin | bb42d30 | 2019-04-22 14:29:29 -0700 | [diff] [blame] | 2736 | nasm_unquote_cstr(p, NULL); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2737 | inc = nasm_malloc(sizeof(Include)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2738 | inc->next = istk; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2739 | inc->conds = NULL; |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 2740 | found_path = NULL; |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 2741 | inc->fp = inc_fopen(p, deplist, &found_path, |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 2742 | (pp_mode == PP_DEPS) |
| 2743 | ? INC_OPTIONAL : INC_NEEDED, NF_TEXT); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2744 | if (!inc->fp) { |
| 2745 | /* -MG given but file not found */ |
| 2746 | nasm_free(inc); |
| 2747 | } else { |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 2748 | inc->fname = src_set_fname(found_path ? found_path : p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2749 | inc->lineno = src_set_linnum(0); |
| 2750 | inc->lineinc = 1; |
| 2751 | inc->expansion = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2752 | inc->mstk = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2753 | istk = inc; |
H. Peter Anvin | 0d4d431 | 2019-08-07 00:46:27 -0700 | [diff] [blame] | 2754 | lfmt->uplevel(LIST_INCLUDE, 0); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2755 | } |
| 2756 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2757 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2758 | |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2759 | case PP_USE: |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2760 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2761 | static macros_t *use_pkg; |
| 2762 | const char *pkg_macro = NULL; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2763 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2764 | tline = tline->next; |
| 2765 | skip_white_(tline); |
| 2766 | tline = expand_id(tline); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2767 | |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2768 | if (!tline || (tline->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2769 | tline->type != TOK_INTERNAL_STRING && |
| 2770 | tline->type != TOK_ID)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2771 | nasm_nonfatal("`%s' expects a package name", dname); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2772 | free_tlist(origline); |
| 2773 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2774 | } |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2775 | if (tline->next) |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2776 | nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2777 | if (tline->type == TOK_STRING) |
H. Peter Anvin | bb42d30 | 2019-04-22 14:29:29 -0700 | [diff] [blame] | 2778 | nasm_unquote_cstr(tline->text, NULL); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2779 | use_pkg = nasm_stdmac_find_package(tline->text); |
| 2780 | if (!use_pkg) |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2781 | nasm_nonfatal("unknown `%s' package: %s", dname, tline->text); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2782 | else |
| 2783 | 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] | 2784 | if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2785 | /* Not already included, go ahead and include it */ |
| 2786 | stdmacpos = use_pkg; |
| 2787 | } |
| 2788 | free_tlist(origline); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2789 | return DIRECTIVE_FOUND; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2790 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2791 | case PP_PUSH: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2792 | case PP_REPL: |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2793 | case PP_POP: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2794 | tline = tline->next; |
| 2795 | skip_white_(tline); |
| 2796 | tline = expand_id(tline); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2797 | if (tline) { |
| 2798 | if (!tok_type_(tline, TOK_ID)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2799 | nasm_nonfatal("`%s' expects a context identifier", |
| 2800 | pp_directives[i]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2801 | free_tlist(origline); |
| 2802 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2803 | } |
| 2804 | if (tline->next) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2805 | nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2806 | pp_directives[i]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2807 | p = nasm_strdup(tline->text); |
| 2808 | } else { |
| 2809 | p = NULL; /* Anonymous */ |
| 2810 | } |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2811 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2812 | if (i == PP_PUSH) { |
H. Peter Anvin (Intel) | ebb05a0 | 2018-12-11 12:30:25 -0800 | [diff] [blame] | 2813 | nasm_new(ctx); |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 2814 | ctx->depth = cstk ? cstk->depth + 1 : 1; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2815 | ctx->next = cstk; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2816 | ctx->name = p; |
| 2817 | ctx->number = unique++; |
| 2818 | cstk = ctx; |
| 2819 | } else { |
| 2820 | /* %pop or %repl */ |
| 2821 | if (!cstk) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2822 | nasm_nonfatal("`%s': context stack is empty", |
| 2823 | pp_directives[i]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2824 | } else if (i == PP_POP) { |
| 2825 | if (p && (!cstk->name || nasm_stricmp(p, cstk->name))) |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2826 | nasm_nonfatal("`%s' in wrong context: %s, " |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2827 | "expected %s", |
| 2828 | dname, cstk->name ? cstk->name : "anonymous", p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2829 | else |
| 2830 | ctx_pop(); |
| 2831 | } else { |
| 2832 | /* i == PP_REPL */ |
| 2833 | nasm_free(cstk->name); |
| 2834 | cstk->name = p; |
| 2835 | p = NULL; |
| 2836 | } |
| 2837 | nasm_free(p); |
| 2838 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2839 | free_tlist(origline); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2840 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2841 | case PP_FATAL: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2842 | severity = ERR_FATAL; |
| 2843 | goto issue_error; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2844 | case PP_ERROR: |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2845 | severity = ERR_NONFATAL|ERR_PASS2; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2846 | goto issue_error; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2847 | case PP_WARNING: |
H. Peter Anvin (Intel) | 723ab48 | 2018-12-13 21:53:31 -0800 | [diff] [blame] | 2848 | /*! |
| 2849 | *!user [on] %warning directives |
| 2850 | *! controls output of \c{%warning} directives (see \k{pperror}). |
| 2851 | */ |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2852 | severity = ERR_WARNING|WARN_USER|ERR_PASS2; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2853 | goto issue_error; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2854 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2855 | issue_error: |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2856 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2857 | /* Only error out if this is the final pass */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2858 | tline->next = expand_smacro(tline->next); |
| 2859 | tline = tline->next; |
| 2860 | skip_white_(tline); |
| 2861 | t = tline ? tline->next : NULL; |
| 2862 | skip_white_(t); |
| 2863 | if (tok_type_(tline, TOK_STRING) && !t) { |
| 2864 | /* The line contains only a quoted string */ |
| 2865 | p = tline->text; |
| 2866 | nasm_unquote(p, NULL); /* Ignore NUL character truncation */ |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2867 | nasm_error(severity, "%s", p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2868 | } else { |
| 2869 | /* Not a quoted string, or more than a quoted string */ |
| 2870 | p = detoken(tline, false); |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2871 | nasm_error(severity, "%s", p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2872 | nasm_free(p); |
| 2873 | } |
| 2874 | free_tlist(origline); |
| 2875 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2876 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2877 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2878 | CASE_PP_IF: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2879 | if (istk->conds && !emitting(istk->conds->state)) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2880 | j = COND_NEVER; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2881 | else { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2882 | j = if_condition(tline->next, i); |
| 2883 | tline->next = NULL; /* it got freed */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2884 | j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2885 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2886 | cond = nasm_malloc(sizeof(Cond)); |
| 2887 | cond->next = istk->conds; |
| 2888 | cond->state = j; |
| 2889 | istk->conds = cond; |
| 2890 | if(istk->mstk) |
| 2891 | istk->mstk->condcnt ++; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2892 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2893 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2894 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2895 | CASE_PP_ELIF: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2896 | if (!istk->conds) |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2897 | nasm_fatal("`%s': no matching `%%if'", dname); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2898 | switch(istk->conds->state) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2899 | case COND_IF_TRUE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2900 | istk->conds->state = COND_DONE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2901 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2902 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2903 | case COND_DONE: |
| 2904 | case COND_NEVER: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2905 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2906 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2907 | case COND_ELSE_TRUE: |
| 2908 | case COND_ELSE_FALSE: |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2909 | nasm_warn(WARN_OTHER|ERR_PP_PRECOND, |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2910 | "`%%elif' after `%%else' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2911 | istk->conds->state = COND_NEVER; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2912 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2913 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2914 | case COND_IF_FALSE: |
| 2915 | /* |
| 2916 | * IMPORTANT: In the case of %if, we will already have |
| 2917 | * called expand_mmac_params(); however, if we're |
| 2918 | * processing an %elif we must have been in a |
| 2919 | * non-emitting mode, which would have inhibited |
| 2920 | * the normal invocation of expand_mmac_params(). |
| 2921 | * Therefore, we have to do it explicitly here. |
| 2922 | */ |
| 2923 | j = if_condition(expand_mmac_params(tline->next), i); |
| 2924 | tline->next = NULL; /* it got freed */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2925 | istk->conds->state = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2926 | j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; |
| 2927 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2928 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2929 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2930 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2931 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2932 | case PP_ELSE: |
| 2933 | if (tline->next) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2934 | nasm_warn(WARN_OTHER|ERR_PP_PRECOND, |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2935 | "trailing garbage after `%%else' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2936 | if (!istk->conds) |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 2937 | nasm_fatal("`%%else: no matching `%%if'"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2938 | switch(istk->conds->state) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2939 | case COND_IF_TRUE: |
| 2940 | case COND_DONE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2941 | istk->conds->state = COND_ELSE_FALSE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2942 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2943 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2944 | case COND_NEVER: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2945 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2946 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2947 | case COND_IF_FALSE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2948 | istk->conds->state = COND_ELSE_TRUE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2949 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2950 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2951 | case COND_ELSE_TRUE: |
| 2952 | case COND_ELSE_FALSE: |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2953 | nasm_warn(WARN_OTHER|ERR_PP_PRECOND, |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2954 | "`%%else' after `%%else' ignored."); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2955 | istk->conds->state = COND_NEVER; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2956 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2957 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2958 | free_tlist(origline); |
| 2959 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2960 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2961 | case PP_ENDIF: |
| 2962 | if (tline->next) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2963 | nasm_warn(WARN_OTHER|ERR_PP_PRECOND, |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2964 | "trailing garbage after `%%endif' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2965 | if (!istk->conds) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2966 | nasm_fatal("`%%endif': no matching `%%if'"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2967 | cond = istk->conds; |
| 2968 | istk->conds = cond->next; |
| 2969 | nasm_free(cond); |
| 2970 | if(istk->mstk) |
| 2971 | istk->mstk->condcnt --; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2972 | free_tlist(origline); |
| 2973 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2974 | |
H. Peter Anvin | db8f96e | 2009-07-15 09:07:29 -0400 | [diff] [blame] | 2975 | case PP_IRMACRO: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2976 | case PP_IMACRO: |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2977 | casesense = false; |
| 2978 | /* fall through */ |
| 2979 | case PP_RMACRO: |
| 2980 | case PP_MACRO: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2981 | if (defining) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2982 | nasm_fatal("`%s': already defining a macro", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2983 | return DIRECTIVE_FOUND; |
| 2984 | } |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 2985 | defining = nasm_zalloc(sizeof(MMacro)); |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 2986 | defining->max_depth = ((i == PP_RMACRO) || (i == PP_IRMACRO)) |
| 2987 | ? nasm_limit[LIMIT_MACROS] : 0; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2988 | defining->casesense = casesense; |
| 2989 | if (!parse_mmacro_spec(tline, defining, dname)) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2990 | nasm_free(defining); |
| 2991 | defining = NULL; |
| 2992 | return DIRECTIVE_FOUND; |
| 2993 | } |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2994 | |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 2995 | src_get(&defining->xline, &defining->fname); |
| 2996 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2997 | mmac = (MMacro *) hash_findix(&mmacros, defining->name); |
| 2998 | while (mmac) { |
| 2999 | if (!strcmp(mmac->name, defining->name) && |
| 3000 | (mmac->nparam_min <= defining->nparam_max |
| 3001 | || defining->plus) |
| 3002 | && (defining->nparam_min <= mmac->nparam_max |
| 3003 | || mmac->plus)) { |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 3004 | nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'", |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3005 | defining->name); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3006 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3007 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3008 | mmac = mmac->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3009 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3010 | free_tlist(origline); |
| 3011 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3012 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3013 | case PP_ENDM: |
| 3014 | case PP_ENDMACRO: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3015 | if (! (defining && defining->name)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3016 | nasm_nonfatal("`%s': not defining a macro", tline->text); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3017 | return DIRECTIVE_FOUND; |
| 3018 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3019 | mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name); |
| 3020 | defining->next = *mmhead; |
| 3021 | *mmhead = defining; |
| 3022 | defining = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3023 | free_tlist(origline); |
| 3024 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3025 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 3026 | case PP_EXITMACRO: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3027 | /* |
| 3028 | * We must search along istk->expansion until we hit a |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3029 | * macro-end marker for a macro with a name. Then we |
| 3030 | * bypass all lines between exitmacro and endmacro. |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3031 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3032 | list_for_each(l, istk->expansion) |
| 3033 | if (l->finishes && l->finishes->name) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3034 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3035 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3036 | if (l) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3037 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3038 | * Remove all conditional entries relative to this |
| 3039 | * macro invocation. (safe to do in this context) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3040 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3041 | for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) { |
| 3042 | cond = istk->conds; |
| 3043 | istk->conds = cond->next; |
| 3044 | nasm_free(cond); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3045 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3046 | istk->expansion = l; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3047 | } else { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3048 | nasm_nonfatal("`%%exitmacro' not within `%%macro' block"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3049 | } |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 3050 | free_tlist(origline); |
| 3051 | return DIRECTIVE_FOUND; |
| 3052 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 3053 | case PP_UNIMACRO: |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3054 | casesense = false; |
| 3055 | /* fall through */ |
| 3056 | case PP_UNMACRO: |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 3057 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3058 | MMacro **mmac_p; |
| 3059 | MMacro spec; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 3060 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3061 | spec.casesense = casesense; |
| 3062 | if (!parse_mmacro_spec(tline, &spec, dname)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3063 | return DIRECTIVE_FOUND; |
| 3064 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3065 | mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL); |
| 3066 | while (mmac_p && *mmac_p) { |
| 3067 | mmac = *mmac_p; |
| 3068 | if (mmac->casesense == spec.casesense && |
| 3069 | !mstrcmp(mmac->name, spec.name, spec.casesense) && |
| 3070 | mmac->nparam_min == spec.nparam_min && |
| 3071 | mmac->nparam_max == spec.nparam_max && |
| 3072 | mmac->plus == spec.plus) { |
| 3073 | *mmac_p = mmac->next; |
| 3074 | free_mmacro(mmac); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3075 | } else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3076 | mmac_p = &mmac->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3077 | } |
| 3078 | } |
| 3079 | free_tlist(origline); |
| 3080 | free_tlist(spec.dlist); |
| 3081 | return DIRECTIVE_FOUND; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 3082 | } |
| 3083 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3084 | case PP_ROTATE: |
| 3085 | if (tline->next && tline->next->type == TOK_WHITESPACE) |
| 3086 | tline = tline->next; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 3087 | if (!tline->next) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3088 | free_tlist(origline); |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3089 | nasm_nonfatal("`%%rotate' missing rotate count"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3090 | return DIRECTIVE_FOUND; |
| 3091 | } |
| 3092 | t = expand_smacro(tline->next); |
| 3093 | tline->next = NULL; |
| 3094 | free_tlist(origline); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3095 | pps.tptr = tline = t; |
| 3096 | pps.ntokens = -1; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3097 | tokval.t_type = TOKEN_INVALID; |
| 3098 | evalresult = |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3099 | evaluate(ppscan, &pps, &tokval, NULL, true, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3100 | free_tlist(tline); |
| 3101 | if (!evalresult) |
| 3102 | return DIRECTIVE_FOUND; |
| 3103 | if (tokval.t_type) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 3104 | nasm_warn(WARN_OTHER, "trailing garbage after expression ignored"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3105 | if (!is_simple(evalresult)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3106 | nasm_nonfatal("non-constant value given to `%%rotate'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3107 | return DIRECTIVE_FOUND; |
| 3108 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3109 | mmac = istk->mstk; |
| 3110 | while (mmac && !mmac->name) /* avoid mistaking %reps for macros */ |
| 3111 | mmac = mmac->next_active; |
| 3112 | if (!mmac) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3113 | nasm_nonfatal("`%%rotate' invoked outside a macro call"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3114 | } else if (mmac->nparam == 0) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3115 | nasm_nonfatal("`%%rotate' invoked within macro without parameters"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3116 | } else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3117 | int rotate = mmac->rotate + reloc_value(evalresult); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3118 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3119 | rotate %= (int)mmac->nparam; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3120 | if (rotate < 0) |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3121 | rotate += mmac->nparam; |
| 3122 | |
| 3123 | mmac->rotate = rotate; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3124 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3125 | return DIRECTIVE_FOUND; |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 3126 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3127 | case PP_REP: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 3128 | nolist = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3129 | do { |
| 3130 | tline = tline->next; |
| 3131 | } while (tok_type_(tline, TOK_WHITESPACE)); |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 3132 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3133 | if (tok_type_(tline, TOK_ID) && |
| 3134 | nasm_stricmp(tline->text, ".nolist") == 0) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 3135 | nolist = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3136 | do { |
| 3137 | tline = tline->next; |
| 3138 | } while (tok_type_(tline, TOK_WHITESPACE)); |
| 3139 | } |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 3140 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3141 | if (tline) { |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3142 | pps.tptr = expand_smacro(tline); |
| 3143 | pps.ntokens = -1; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3144 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3145 | /* XXX: really critical?! */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3146 | evalresult = |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3147 | evaluate(ppscan, &pps, &tokval, NULL, true, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3148 | if (!evalresult) { |
| 3149 | free_tlist(origline); |
| 3150 | return DIRECTIVE_FOUND; |
| 3151 | } |
| 3152 | if (tokval.t_type) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 3153 | nasm_warn(WARN_OTHER, "trailing garbage after expression ignored"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3154 | if (!is_simple(evalresult)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3155 | nasm_nonfatal("non-constant value given to `%%rep'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3156 | return DIRECTIVE_FOUND; |
| 3157 | } |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 3158 | count = reloc_value(evalresult); |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 3159 | if (count > nasm_limit[LIMIT_REP]) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3160 | nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")", |
| 3161 | count, nasm_limit[LIMIT_REP]); |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 3162 | count = 0; |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 3163 | } else if (count < 0) { |
H. Peter Anvin (Intel) | 723ab48 | 2018-12-13 21:53:31 -0800 | [diff] [blame] | 3164 | /*! |
| 3165 | *!negative-rep [on] regative %rep count |
| 3166 | *! warns about negative counts given to the \c{%rep} |
| 3167 | *! preprocessor directive. |
| 3168 | */ |
H. Peter Anvin (Intel) | 80c4f23 | 2018-12-14 13:33:24 -0800 | [diff] [blame] | 3169 | nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP, |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 3170 | "negative `%%rep' count: %"PRId64, count); |
| 3171 | count = 0; |
| 3172 | } else { |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 3173 | count++; |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 3174 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3175 | } else { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3176 | nasm_nonfatal("`%%rep' expects a repeat count"); |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 3177 | count = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3178 | } |
| 3179 | free_tlist(origline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3180 | |
| 3181 | tmp_defining = defining; |
| 3182 | defining = nasm_malloc(sizeof(MMacro)); |
| 3183 | defining->prev = NULL; |
| 3184 | defining->name = NULL; /* flags this macro as a %rep block */ |
| 3185 | defining->casesense = false; |
| 3186 | defining->plus = false; |
| 3187 | defining->nolist = nolist; |
| 3188 | defining->in_progress = count; |
| 3189 | defining->max_depth = 0; |
| 3190 | defining->nparam_min = defining->nparam_max = 0; |
| 3191 | defining->defaults = NULL; |
| 3192 | defining->dlist = NULL; |
| 3193 | defining->expansion = NULL; |
| 3194 | defining->next_active = istk->mstk; |
| 3195 | defining->rep_nest = tmp_defining; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3196 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3197 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3198 | case PP_ENDREP: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3199 | if (!defining || defining->name) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3200 | nasm_nonfatal("`%%endrep': no matching `%%rep'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3201 | return DIRECTIVE_FOUND; |
| 3202 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3203 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3204 | /* |
| 3205 | * Now we have a "macro" defined - although it has no name |
| 3206 | * and we won't be entering it in the hash tables - we must |
| 3207 | * push a macro-end marker for it on to istk->expansion. |
| 3208 | * After that, it will take care of propagating itself (a |
| 3209 | * macro-end marker line for a macro which is really a %rep |
| 3210 | * block will cause the macro to be re-expanded, complete |
| 3211 | * with another macro-end marker to ensure the process |
| 3212 | * continues) until the whole expansion is forcibly removed |
| 3213 | * from istk->expansion by a %exitrep. |
| 3214 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3215 | l = nasm_malloc(sizeof(Line)); |
| 3216 | l->next = istk->expansion; |
| 3217 | l->finishes = defining; |
| 3218 | l->first = NULL; |
| 3219 | istk->expansion = l; |
| 3220 | |
| 3221 | istk->mstk = defining; |
| 3222 | |
H. Peter Anvin | 0d4d431 | 2019-08-07 00:46:27 -0700 | [diff] [blame] | 3223 | lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3224 | tmp_defining = defining; |
| 3225 | defining = defining->rep_nest; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3226 | free_tlist(origline); |
| 3227 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3228 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3229 | case PP_EXITREP: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3230 | /* |
| 3231 | * We must search along istk->expansion until we hit a |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3232 | * macro-end marker for a macro with no name. Then we set |
| 3233 | * its `in_progress' flag to 0. |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3234 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3235 | list_for_each(l, istk->expansion) |
| 3236 | if (l->finishes && !l->finishes->name) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3237 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3238 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3239 | if (l) |
| 3240 | l->finishes->in_progress = 1; |
| 3241 | else |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3242 | nasm_nonfatal("`%%exitrep' not within `%%rep' block"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3243 | free_tlist(origline); |
| 3244 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3245 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3246 | case PP_IDEFINE: |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3247 | case PP_IXDEFINE: |
| 3248 | casesense = false; |
| 3249 | /* fall through */ |
| 3250 | case PP_DEFINE: |
| 3251 | case PP_XDEFINE: |
| 3252 | { |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3253 | bool have_eval_params = false; |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 3254 | bool *eval_params = NULL; |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3255 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3256 | tline = tline->next; |
| 3257 | skip_white_(tline); |
| 3258 | tline = expand_id(tline); |
| 3259 | if (!tline || (tline->type != TOK_ID && |
| 3260 | (tline->type != TOK_PREPROC_ID || |
| 3261 | tline->text[1] != '$'))) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3262 | nasm_nonfatal("`%s' expects a macro identifier", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3263 | free_tlist(origline); |
| 3264 | return DIRECTIVE_FOUND; |
| 3265 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3266 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3267 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3268 | last = tline; |
| 3269 | param_start = tline = tline->next; |
| 3270 | nparam = 0; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3271 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3272 | if (tok_is_(tline, "(")) { |
| 3273 | /* |
| 3274 | * This macro has parameters. |
| 3275 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3276 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3277 | tline = tline->next; |
| 3278 | while (1) { |
| 3279 | skip_white_(tline); |
| 3280 | if (!tline) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3281 | nasm_nonfatal("parameter identifier expected"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3282 | free_tlist(origline); |
| 3283 | return DIRECTIVE_FOUND; |
| 3284 | } |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3285 | if (tok_is_(tline, "=")) { |
| 3286 | have_eval_params = true; |
| 3287 | tline = tline->next; |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 3288 | skip_white_(tline); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3289 | } |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 3290 | |
| 3291 | /* |
| 3292 | * "*" means an unnamed, ignored argument; it can never |
| 3293 | * match anything because "*" is not TOK_ID, and so |
| 3294 | * none of the expansion entries will be converted |
| 3295 | * to parameters. |
| 3296 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3297 | if (tline->type != TOK_ID) { |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 3298 | if (tok_is_(tline, ",") || tok_is_(tline, ")")) { |
| 3299 | /* |
| 3300 | * Empty name; duplicate the termination |
| 3301 | * token and use the first copy as a placeholder. |
| 3302 | * It will never match anything, since the |
| 3303 | * associated text doesn't correspond to a TOK_ID. |
| 3304 | */ |
| 3305 | tline = dup_Token(tline, tline); |
| 3306 | } else { |
| 3307 | nasm_nonfatal("`%s': parameter identifier expected", |
| 3308 | tline->text); |
| 3309 | free_tlist(origline); |
| 3310 | return DIRECTIVE_FOUND; |
| 3311 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3312 | } |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3313 | tline->type = tok_smac_param(nparam++); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3314 | tline = tline->next; |
| 3315 | skip_white_(tline); |
| 3316 | if (tok_is_(tline, ",")) { |
| 3317 | tline = tline->next; |
H. Peter Anvin | ca348b6 | 2008-07-23 10:49:26 -0400 | [diff] [blame] | 3318 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3319 | if (!tok_is_(tline, ")")) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3320 | nasm_nonfatal("`)' expected to terminate macro template"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3321 | free_tlist(origline); |
| 3322 | return DIRECTIVE_FOUND; |
| 3323 | } |
| 3324 | break; |
| 3325 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3326 | } |
| 3327 | last = tline; |
| 3328 | tline = tline->next; |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 3329 | |
| 3330 | if (have_eval_params) { |
| 3331 | /* Create evaluated parameters table */ |
| 3332 | bool is_eval = false; |
| 3333 | |
| 3334 | nasm_newn(eval_params, nparam); |
| 3335 | list_for_each(tt, param_start) { |
| 3336 | if (is_smac_param(tt->type)) |
| 3337 | eval_params[smac_nparam(tt->type)] = is_eval; |
| 3338 | is_eval = tok_is_(tt, "="); |
| 3339 | } |
| 3340 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3341 | } |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 3342 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3343 | if (tok_type_(tline, TOK_WHITESPACE)) |
| 3344 | last = tline, tline = tline->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3345 | last->next = NULL; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3346 | |
| 3347 | /* Expand the macro definition now for %xdefine and %ixdefine */ |
| 3348 | if ((i == PP_XDEFINE) || (i == PP_IXDEFINE)) |
| 3349 | tline = expand_smacro(tline); |
| 3350 | |
| 3351 | macro_start = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3352 | t = tline; |
| 3353 | while (t) { |
| 3354 | if (t->type == TOK_ID) { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3355 | list_for_each(tt, param_start) |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 3356 | if (is_smac_param(tt->type) && |
| 3357 | tt->len == t->len && |
| 3358 | !memcmp(tt->text, t->text, tt->len)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3359 | t->type = tt->type; |
| 3360 | } |
| 3361 | tt = t->next; |
| 3362 | t->next = macro_start; |
| 3363 | macro_start = t; |
| 3364 | t = tt; |
| 3365 | } |
| 3366 | /* |
| 3367 | * Good. We now have a macro name, a parameter count, and a |
| 3368 | * token list (in reverse order) for an expansion. We ought |
| 3369 | * to be OK just to create an SMacro, store it, and let |
| 3370 | * free_tlist have the rest of the line (which we have |
| 3371 | * carefully re-terminated after chopping off the expansion |
| 3372 | * from the end). |
| 3373 | */ |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 3374 | define_smacro(ctx, mname, casesense, nparam, eval_params, macro_start); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3375 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3376 | free_tlist(origline); |
| 3377 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3378 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3379 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3380 | case PP_UNDEF: |
| 3381 | tline = tline->next; |
| 3382 | skip_white_(tline); |
| 3383 | tline = expand_id(tline); |
| 3384 | if (!tline || (tline->type != TOK_ID && |
| 3385 | (tline->type != TOK_PREPROC_ID || |
| 3386 | tline->text[1] != '$'))) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3387 | nasm_nonfatal("`%%undef' expects a macro identifier"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3388 | free_tlist(origline); |
| 3389 | return DIRECTIVE_FOUND; |
| 3390 | } |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3391 | if (tline->next) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 3392 | nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored"); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3393 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3394 | /* Find the context that symbol belongs to */ |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3395 | ctx = get_ctx(tline->text, &mname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3396 | undef_smacro(ctx, mname); |
| 3397 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3398 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3399 | |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3400 | case PP_IDEFSTR: |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3401 | casesense = false; |
| 3402 | /* fall through */ |
| 3403 | case PP_DEFSTR: |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3404 | tline = tline->next; |
| 3405 | skip_white_(tline); |
| 3406 | tline = expand_id(tline); |
| 3407 | if (!tline || (tline->type != TOK_ID && |
| 3408 | (tline->type != TOK_PREPROC_ID || |
| 3409 | tline->text[1] != '$'))) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3410 | nasm_nonfatal("`%s' expects a macro identifier", dname); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3411 | free_tlist(origline); |
| 3412 | return DIRECTIVE_FOUND; |
| 3413 | } |
| 3414 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3415 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3416 | last = tline; |
| 3417 | tline = expand_smacro(tline->next); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3418 | last->next = NULL; |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3419 | |
| 3420 | while (tok_type_(tline, TOK_WHITESPACE)) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3421 | tline = delete_Token(tline); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3422 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3423 | p = detoken(tline, false); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3424 | macro_start = make_tok_qstr(p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3425 | nasm_free(p); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3426 | |
| 3427 | /* |
| 3428 | * We now have a macro name, an implicit parameter count of |
| 3429 | * zero, and a string token to use as an expansion. Create |
| 3430 | * and store an SMacro. |
| 3431 | */ |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 3432 | define_smacro(ctx, mname, casesense, 0, NULL, macro_start); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3433 | free_tlist(origline); |
| 3434 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3435 | |
H. Peter Anvin | 2f55bda | 2009-07-14 15:04:04 -0400 | [diff] [blame] | 3436 | case PP_IDEFTOK: |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3437 | casesense = false; |
| 3438 | /* fall through */ |
| 3439 | case PP_DEFTOK: |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3440 | tline = tline->next; |
| 3441 | skip_white_(tline); |
| 3442 | tline = expand_id(tline); |
| 3443 | if (!tline || (tline->type != TOK_ID && |
| 3444 | (tline->type != TOK_PREPROC_ID || |
| 3445 | tline->text[1] != '$'))) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3446 | nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3447 | free_tlist(origline); |
| 3448 | return DIRECTIVE_FOUND; |
| 3449 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3450 | ctx = get_ctx(tline->text, &mname); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3451 | last = tline; |
| 3452 | tline = expand_smacro(tline->next); |
| 3453 | last->next = NULL; |
| 3454 | |
| 3455 | t = tline; |
| 3456 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3457 | t = t->next; |
| 3458 | /* t should now point to the string */ |
Cyrill Gorcunov | 6908e58 | 2010-09-06 19:36:15 +0400 | [diff] [blame] | 3459 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3460 | nasm_nonfatal("`%s` requires string as second parameter", dname); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3461 | free_tlist(tline); |
| 3462 | free_tlist(origline); |
| 3463 | return DIRECTIVE_FOUND; |
| 3464 | } |
| 3465 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 3466 | /* |
| 3467 | * Convert the string to a token stream. Note that smacros |
| 3468 | * are stored with the token stream reversed, so we have to |
| 3469 | * reverse the output of tokenize(). |
| 3470 | */ |
H. Peter Anvin | bb42d30 | 2019-04-22 14:29:29 -0700 | [diff] [blame] | 3471 | nasm_unquote_cstr(t->text, NULL); |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 3472 | macro_start = reverse_tokens(tokenize(t->text)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3473 | |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3474 | /* |
| 3475 | * We now have a macro name, an implicit parameter count of |
| 3476 | * zero, and a numeric token to use as an expansion. Create |
| 3477 | * and store an SMacro. |
| 3478 | */ |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 3479 | define_smacro(ctx, mname, casesense, 0, NULL, macro_start); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3480 | free_tlist(tline); |
| 3481 | free_tlist(origline); |
| 3482 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3483 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3484 | case PP_IPATHSEARCH: |
| 3485 | casesense = false; |
| 3486 | /* fall through */ |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3487 | case PP_PATHSEARCH: |
| 3488 | { |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 3489 | const char *found_path; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3490 | |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3491 | tline = tline->next; |
| 3492 | skip_white_(tline); |
| 3493 | tline = expand_id(tline); |
| 3494 | if (!tline || (tline->type != TOK_ID && |
| 3495 | (tline->type != TOK_PREPROC_ID || |
| 3496 | tline->text[1] != '$'))) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3497 | nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3498 | free_tlist(origline); |
| 3499 | return DIRECTIVE_FOUND; |
| 3500 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3501 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3502 | last = tline; |
| 3503 | tline = expand_smacro(tline->next); |
| 3504 | last->next = NULL; |
| 3505 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3506 | t = tline; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3507 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3508 | t = t->next; |
| 3509 | |
| 3510 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3511 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3512 | nasm_nonfatal("`%s' expects a file name", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3513 | free_tlist(tline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3514 | free_tlist(origline); |
| 3515 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 3516 | } |
| 3517 | if (t->next) |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3518 | nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3519 | p = t->text; |
H. Peter Anvin | 427cc91 | 2008-06-01 21:43:03 -0700 | [diff] [blame] | 3520 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3521 | nasm_unquote(p, NULL); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3522 | |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 3523 | inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY); |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 3524 | if (!found_path) |
| 3525 | found_path = p; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3526 | macro_start = make_tok_qstr(found_path); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3527 | |
| 3528 | /* |
| 3529 | * We now have a macro name, an implicit parameter count of |
| 3530 | * zero, and a string token to use as an expansion. Create |
| 3531 | * and store an SMacro. |
| 3532 | */ |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 3533 | define_smacro(ctx, mname, casesense, 0, NULL, macro_start); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3534 | free_tlist(tline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3535 | free_tlist(origline); |
| 3536 | return DIRECTIVE_FOUND; |
| 3537 | } |
| 3538 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3539 | case PP_ISTRLEN: |
| 3540 | casesense = false; |
| 3541 | /* fall through */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3542 | case PP_STRLEN: |
| 3543 | tline = tline->next; |
| 3544 | skip_white_(tline); |
| 3545 | tline = expand_id(tline); |
| 3546 | if (!tline || (tline->type != TOK_ID && |
| 3547 | (tline->type != TOK_PREPROC_ID || |
| 3548 | tline->text[1] != '$'))) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3549 | nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3550 | free_tlist(origline); |
| 3551 | return DIRECTIVE_FOUND; |
| 3552 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3553 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3554 | last = tline; |
| 3555 | tline = expand_smacro(tline->next); |
| 3556 | last->next = NULL; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3557 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3558 | t = tline; |
| 3559 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3560 | t = t->next; |
| 3561 | /* t should now point to the string */ |
Cyrill Gorcunov | 4e1d5ab | 2010-07-23 18:51:51 +0400 | [diff] [blame] | 3562 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3563 | nasm_nonfatal("`%s' requires string as second parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3564 | free_tlist(tline); |
| 3565 | free_tlist(origline); |
| 3566 | return DIRECTIVE_FOUND; |
| 3567 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3568 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3569 | macro_start = make_tok_num(nasm_unquote(t->text, NULL)); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3570 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3571 | /* |
| 3572 | * We now have a macro name, an implicit parameter count of |
| 3573 | * zero, and a numeric token to use as an expansion. Create |
| 3574 | * and store an SMacro. |
| 3575 | */ |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 3576 | define_smacro(ctx, mname, casesense, 0, NULL, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3577 | free_tlist(tline); |
| 3578 | free_tlist(origline); |
| 3579 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3580 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3581 | case PP_ISTRCAT: |
| 3582 | casesense = false; |
| 3583 | /* fall through */ |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3584 | case PP_STRCAT: |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3585 | tline = tline->next; |
| 3586 | skip_white_(tline); |
| 3587 | tline = expand_id(tline); |
| 3588 | if (!tline || (tline->type != TOK_ID && |
| 3589 | (tline->type != TOK_PREPROC_ID || |
| 3590 | tline->text[1] != '$'))) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3591 | nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3592 | free_tlist(origline); |
| 3593 | return DIRECTIVE_FOUND; |
| 3594 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3595 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3596 | last = tline; |
| 3597 | tline = expand_smacro(tline->next); |
| 3598 | last->next = NULL; |
| 3599 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3600 | len = 0; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3601 | list_for_each(t, tline) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3602 | switch (t->type) { |
| 3603 | case TOK_WHITESPACE: |
| 3604 | break; |
| 3605 | case TOK_STRING: |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 3606 | len += t->len = nasm_unquote(t->text, NULL); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3607 | break; |
| 3608 | case TOK_OTHER: |
| 3609 | if (!strcmp(t->text, ",")) /* permit comma separators */ |
| 3610 | break; |
| 3611 | /* else fall through */ |
| 3612 | default: |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3613 | nasm_nonfatal("non-string passed to `%s': %s", dname, t->text); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3614 | free_tlist(tline); |
| 3615 | free_tlist(origline); |
| 3616 | return DIRECTIVE_FOUND; |
| 3617 | } |
| 3618 | } |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3619 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3620 | p = pp = nasm_malloc(len); |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3621 | list_for_each(t, tline) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3622 | if (t->type == TOK_STRING) { |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 3623 | memcpy(p, t->text, t->len); |
| 3624 | p += t->len; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3625 | } |
| 3626 | } |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3627 | |
| 3628 | /* |
| 3629 | * We now have a macro name, an implicit parameter count of |
| 3630 | * zero, and a numeric token to use as an expansion. Create |
| 3631 | * and store an SMacro. |
| 3632 | */ |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3633 | macro_start = make_tok_qstr(pp); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3634 | nasm_free(pp); |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 3635 | define_smacro(ctx, mname, casesense, 0, NULL, macro_start); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3636 | free_tlist(tline); |
| 3637 | free_tlist(origline); |
| 3638 | return DIRECTIVE_FOUND; |
| 3639 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3640 | case PP_ISUBSTR: |
| 3641 | casesense = false; |
| 3642 | /* fall through */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3643 | case PP_SUBSTR: |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3644 | { |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3645 | int64_t start, count; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3646 | size_t len; |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 3647 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3648 | tline = tline->next; |
| 3649 | skip_white_(tline); |
| 3650 | tline = expand_id(tline); |
| 3651 | if (!tline || (tline->type != TOK_ID && |
| 3652 | (tline->type != TOK_PREPROC_ID || |
| 3653 | tline->text[1] != '$'))) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3654 | nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3655 | free_tlist(origline); |
| 3656 | return DIRECTIVE_FOUND; |
| 3657 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3658 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3659 | last = tline; |
| 3660 | tline = expand_smacro(tline->next); |
| 3661 | last->next = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3662 | |
Cyrill Gorcunov | 35519d6 | 2010-09-06 23:49:52 +0400 | [diff] [blame] | 3663 | if (tline) /* skip expanded id */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3664 | t = tline->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3665 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3666 | t = t->next; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3667 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3668 | /* t should now point to the string */ |
Cyrill Gorcunov | 35519d6 | 2010-09-06 23:49:52 +0400 | [diff] [blame] | 3669 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3670 | nasm_nonfatal("`%s' requires string as second parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3671 | free_tlist(tline); |
| 3672 | free_tlist(origline); |
| 3673 | return DIRECTIVE_FOUND; |
| 3674 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3675 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3676 | pps.tptr = t->next; |
| 3677 | pps.ntokens = -1; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3678 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3679 | evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3680 | if (!evalresult) { |
| 3681 | free_tlist(tline); |
| 3682 | free_tlist(origline); |
| 3683 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3684 | } else if (!is_simple(evalresult)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3685 | nasm_nonfatal("non-constant value given to `%s'", 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 | } |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3690 | start = evalresult->value - 1; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3691 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3692 | while (tok_type_(pps.tptr, TOK_WHITESPACE)) |
| 3693 | pps.tptr = pps.tptr->next; |
| 3694 | if (!pps.tptr) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3695 | count = 1; /* Backwards compatibility: one character */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3696 | } else { |
| 3697 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3698 | evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3699 | if (!evalresult) { |
| 3700 | free_tlist(tline); |
| 3701 | free_tlist(origline); |
| 3702 | return DIRECTIVE_FOUND; |
| 3703 | } else if (!is_simple(evalresult)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3704 | nasm_nonfatal("non-constant value given to `%s'", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3705 | free_tlist(tline); |
| 3706 | free_tlist(origline); |
| 3707 | return DIRECTIVE_FOUND; |
| 3708 | } |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3709 | count = evalresult->value; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3710 | } |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3711 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3712 | len = nasm_unquote(t->text, NULL); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3713 | |
Cyrill Gorcunov | cff031e | 2010-09-07 20:31:11 +0400 | [diff] [blame] | 3714 | /* make start and count being in range */ |
| 3715 | if (start < 0) |
| 3716 | start = 0; |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3717 | if (count < 0) |
| 3718 | count = len + count + 1 - start; |
| 3719 | if (start + count > (int64_t)len) |
Cyrill Gorcunov | cff031e | 2010-09-07 20:31:11 +0400 | [diff] [blame] | 3720 | count = len - start; |
| 3721 | if (!len || count < 0 || start >=(int64_t)len) |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3722 | start = -1, count = 0; /* empty string */ |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3723 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3724 | macro_start = new_Token(NULL, TOK_STRING, NULL, 0); |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 3725 | macro_start->len = count; |
| 3726 | macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, |
| 3727 | ¯o_start->len); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3728 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3729 | /* |
| 3730 | * We now have a macro name, an implicit parameter count of |
| 3731 | * zero, and a numeric token to use as an expansion. Create |
| 3732 | * and store an SMacro. |
| 3733 | */ |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 3734 | define_smacro(ctx, mname, casesense, 0, NULL, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3735 | free_tlist(tline); |
| 3736 | free_tlist(origline); |
| 3737 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3738 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3739 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3740 | case PP_IASSIGN: |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3741 | casesense = false; |
| 3742 | /* fall through */ |
| 3743 | case PP_ASSIGN: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3744 | tline = tline->next; |
| 3745 | skip_white_(tline); |
| 3746 | tline = expand_id(tline); |
| 3747 | if (!tline || (tline->type != TOK_ID && |
| 3748 | (tline->type != TOK_PREPROC_ID || |
| 3749 | tline->text[1] != '$'))) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3750 | nasm_nonfatal("`%s' expects a macro identifier", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3751 | free_tlist(origline); |
| 3752 | return DIRECTIVE_FOUND; |
| 3753 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3754 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3755 | last = tline; |
| 3756 | tline = expand_smacro(tline->next); |
| 3757 | last->next = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3758 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3759 | pps.tptr = tline; |
| 3760 | pps.ntokens = -1; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3761 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3762 | evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3763 | free_tlist(tline); |
| 3764 | if (!evalresult) { |
| 3765 | free_tlist(origline); |
| 3766 | return DIRECTIVE_FOUND; |
| 3767 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3768 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3769 | if (tokval.t_type) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 3770 | nasm_warn(WARN_OTHER, "trailing garbage after expression ignored"); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3771 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3772 | if (!is_simple(evalresult)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3773 | nasm_nonfatal("non-constant value given to `%s'", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3774 | free_tlist(origline); |
| 3775 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3776 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3777 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3778 | macro_start = make_tok_num(reloc_value(evalresult)); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3779 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3780 | /* |
| 3781 | * We now have a macro name, an implicit parameter count of |
| 3782 | * zero, and a numeric token to use as an expansion. Create |
| 3783 | * and store an SMacro. |
| 3784 | */ |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 3785 | define_smacro(ctx, mname, casesense, 0, NULL, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3786 | free_tlist(origline); |
| 3787 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3788 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3789 | case PP_LINE: |
| 3790 | /* |
| 3791 | * Syntax is `%line nnn[+mmm] [filename]' |
| 3792 | */ |
H. Peter Anvin (Intel) | 800c168 | 2018-12-14 12:22:11 -0800 | [diff] [blame] | 3793 | if (unlikely(pp_noline)) { |
| 3794 | free_tlist(origline); |
| 3795 | return DIRECTIVE_FOUND; |
| 3796 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3797 | tline = tline->next; |
| 3798 | skip_white_(tline); |
| 3799 | if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3800 | nasm_nonfatal("`%s' expects line number", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3801 | free_tlist(origline); |
| 3802 | return DIRECTIVE_FOUND; |
| 3803 | } |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 3804 | k = readnum(tline->text, &err); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3805 | m = 1; |
| 3806 | tline = tline->next; |
| 3807 | if (tok_is_(tline, "+")) { |
| 3808 | tline = tline->next; |
| 3809 | if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3810 | nasm_nonfatal("`%s' expects line increment", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3811 | free_tlist(origline); |
| 3812 | return DIRECTIVE_FOUND; |
| 3813 | } |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 3814 | m = readnum(tline->text, &err); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3815 | tline = tline->next; |
| 3816 | } |
| 3817 | skip_white_(tline); |
| 3818 | src_set_linnum(k); |
| 3819 | istk->lineinc = m; |
| 3820 | if (tline) { |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 3821 | char *fname = detoken(tline, false); |
| 3822 | src_set_fname(fname); |
| 3823 | nasm_free(fname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3824 | } |
| 3825 | free_tlist(origline); |
| 3826 | return DIRECTIVE_FOUND; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3827 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3828 | default: |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3829 | nasm_nonfatal("preprocessor directive `%s' not yet implemented", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3830 | return DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3831 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3832 | } |
| 3833 | |
| 3834 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3835 | * Ensure that a macro parameter contains a condition code and |
| 3836 | * nothing else. Return the condition code index if so, or -1 |
| 3837 | * otherwise. |
| 3838 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3839 | static int find_cc(Token * t) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3840 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3841 | Token *tt; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3842 | |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3843 | if (!t) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3844 | return -1; /* Probably a %+ without a space */ |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3845 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3846 | skip_white_(t); |
Cyrill Gorcunov | 7524cfd | 2017-10-22 19:01:16 +0300 | [diff] [blame] | 3847 | if (!t) |
| 3848 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3849 | if (t->type != TOK_ID) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3850 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3851 | tt = t->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3852 | skip_white_(tt); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3853 | if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ","))) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3854 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3855 | |
Cyrill Gorcunov | 1945639 | 2012-05-02 00:18:56 +0400 | [diff] [blame] | 3856 | return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3857 | } |
| 3858 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3859 | /* |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 3860 | * This routines walks over tokens strem and handles tokens |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3861 | * pasting, if @handle_explicit passed then explicit pasting |
| 3862 | * term is handled, otherwise -- implicit pastings only. |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 3863 | * The @m array can contain a series of token types which are |
| 3864 | * executed as separate passes. |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3865 | */ |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 3866 | static bool paste_tokens(Token **head, const struct tokseq_match *m, |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3867 | size_t mnum, bool handle_explicit) |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3868 | { |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3869 | Token *tok, *next, **prev_next, **prev_nonspace; |
| 3870 | bool pasted = false; |
| 3871 | char *buf, *p; |
| 3872 | size_t len, i; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3873 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3874 | /* |
| 3875 | * The last token before pasting. We need it |
| 3876 | * to be able to connect new handled tokens. |
| 3877 | * In other words if there were a tokens stream |
| 3878 | * |
| 3879 | * A -> B -> C -> D |
| 3880 | * |
| 3881 | * and we've joined tokens B and C, the resulting |
| 3882 | * stream should be |
| 3883 | * |
| 3884 | * A -> BC -> D |
| 3885 | */ |
| 3886 | tok = *head; |
| 3887 | prev_next = NULL; |
| 3888 | |
| 3889 | if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE)) |
| 3890 | prev_nonspace = head; |
| 3891 | else |
| 3892 | prev_nonspace = NULL; |
| 3893 | |
| 3894 | while (tok && (next = tok->next)) { |
| 3895 | |
| 3896 | switch (tok->type) { |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3897 | case TOK_WHITESPACE: |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3898 | /* Zap redundant whitespaces */ |
| 3899 | while (tok_type_(next, TOK_WHITESPACE)) |
| 3900 | next = delete_Token(next); |
| 3901 | tok->next = next; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3902 | break; |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3903 | |
| 3904 | case TOK_PASTE: |
| 3905 | /* Explicit pasting */ |
| 3906 | if (!handle_explicit) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3907 | break; |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3908 | next = delete_Token(tok); |
| 3909 | |
| 3910 | while (tok_type_(next, TOK_WHITESPACE)) |
| 3911 | next = delete_Token(next); |
| 3912 | |
| 3913 | if (!pasted) |
| 3914 | pasted = true; |
| 3915 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3916 | /* Left pasting token is start of line */ |
| 3917 | if (!prev_nonspace) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3918 | nasm_fatal("No lvalue found on pasting"); |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3919 | |
Cyrill Gorcunov | 8b5c9fb | 2013-02-04 01:24:54 +0400 | [diff] [blame] | 3920 | /* |
| 3921 | * No ending token, this might happen in two |
| 3922 | * cases |
| 3923 | * |
| 3924 | * 1) There indeed no right token at all |
| 3925 | * 2) There is a bare "%define ID" statement, |
| 3926 | * and @ID does expand to whitespace. |
| 3927 | * |
| 3928 | * So technically we need to do a grammar analysis |
| 3929 | * in another stage of parsing, but for now lets don't |
| 3930 | * change the behaviour people used to. Simply allow |
| 3931 | * whitespace after paste token. |
| 3932 | */ |
| 3933 | if (!next) { |
| 3934 | /* |
| 3935 | * Zap ending space tokens and that's all. |
| 3936 | */ |
| 3937 | tok = (*prev_nonspace)->next; |
| 3938 | while (tok_type_(tok, TOK_WHITESPACE)) |
| 3939 | tok = delete_Token(tok); |
| 3940 | tok = *prev_nonspace; |
| 3941 | tok->next = NULL; |
| 3942 | break; |
| 3943 | } |
| 3944 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3945 | tok = *prev_nonspace; |
| 3946 | while (tok_type_(tok, TOK_WHITESPACE)) |
| 3947 | tok = delete_Token(tok); |
| 3948 | len = strlen(tok->text); |
| 3949 | len += strlen(next->text); |
| 3950 | |
| 3951 | p = buf = nasm_malloc(len + 1); |
| 3952 | strcpy(p, tok->text); |
| 3953 | p = strchr(p, '\0'); |
| 3954 | strcpy(p, next->text); |
| 3955 | |
| 3956 | delete_Token(tok); |
| 3957 | |
| 3958 | tok = tokenize(buf); |
| 3959 | nasm_free(buf); |
| 3960 | |
| 3961 | *prev_nonspace = tok; |
| 3962 | while (tok && tok->next) |
| 3963 | tok = tok->next; |
| 3964 | |
| 3965 | tok->next = delete_Token(next); |
| 3966 | |
| 3967 | /* Restart from pasted tokens head */ |
| 3968 | tok = *prev_nonspace; |
| 3969 | break; |
| 3970 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3971 | default: |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3972 | /* implicit pasting */ |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 3973 | for (i = 0; i < mnum; i++) { |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3974 | if (!(PP_CONCAT_MATCH(tok, m[i].mask_head))) |
| 3975 | continue; |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 3976 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3977 | len = 0; |
| 3978 | while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) { |
| 3979 | len += strlen(next->text); |
| 3980 | next = next->next; |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 3981 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3982 | |
Cyrill Gorcunov | 6f8109e | 2017-10-22 21:26:36 +0300 | [diff] [blame] | 3983 | /* No match or no text to process */ |
| 3984 | if (tok == next || len == 0) |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3985 | break; |
| 3986 | |
| 3987 | len += strlen(tok->text); |
| 3988 | p = buf = nasm_malloc(len + 1); |
| 3989 | |
Adam Majer | 1a06943 | 2017-07-25 11:12:35 +0200 | [diff] [blame] | 3990 | strcpy(p, tok->text); |
| 3991 | p = strchr(p, '\0'); |
| 3992 | tok = delete_Token(tok); |
| 3993 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3994 | while (tok != next) { |
Adam Majer | 1a06943 | 2017-07-25 11:12:35 +0200 | [diff] [blame] | 3995 | if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) { |
| 3996 | strcpy(p, tok->text); |
| 3997 | p = strchr(p, '\0'); |
| 3998 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3999 | tok = delete_Token(tok); |
| 4000 | } |
| 4001 | |
| 4002 | tok = tokenize(buf); |
| 4003 | nasm_free(buf); |
| 4004 | |
| 4005 | if (prev_next) |
| 4006 | *prev_next = tok; |
| 4007 | else |
| 4008 | *head = tok; |
| 4009 | |
| 4010 | /* |
| 4011 | * Connect pasted into original stream, |
| 4012 | * ie A -> new-tokens -> B |
| 4013 | */ |
| 4014 | while (tok && tok->next) |
| 4015 | tok = tok->next; |
| 4016 | tok->next = next; |
| 4017 | |
| 4018 | if (!pasted) |
| 4019 | pasted = true; |
| 4020 | |
| 4021 | /* Restart from pasted tokens head */ |
| 4022 | tok = prev_next ? *prev_next : *head; |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4023 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 4024 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4025 | break; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 4026 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 4027 | |
| 4028 | prev_next = &tok->next; |
| 4029 | |
| 4030 | if (tok->next && |
| 4031 | !tok_type_(tok->next, TOK_WHITESPACE) && |
| 4032 | !tok_type_(tok->next, TOK_PASTE)) |
| 4033 | prev_nonspace = prev_next; |
| 4034 | |
| 4035 | tok = tok->next; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 4036 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 4037 | |
| 4038 | return pasted; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 4039 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4040 | |
| 4041 | /* |
| 4042 | * expands to a list of tokens from %{x:y} |
| 4043 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4044 | static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4045 | { |
| 4046 | Token *t = tline, **tt, *tm, *head; |
| 4047 | char *pos; |
| 4048 | int fst, lst, j, i; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4049 | |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4050 | pos = strchr(tline->text, ':'); |
| 4051 | nasm_assert(pos); |
| 4052 | |
| 4053 | lst = atoi(pos + 1); |
| 4054 | fst = atoi(tline->text + 1); |
| 4055 | |
| 4056 | /* |
| 4057 | * only macros params are accounted so |
| 4058 | * if someone passes %0 -- we reject such |
| 4059 | * value(s) |
| 4060 | */ |
| 4061 | if (lst == 0 || fst == 0) |
| 4062 | goto err; |
| 4063 | |
| 4064 | /* the values should be sane */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4065 | if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) || |
| 4066 | (lst > (int)mac->nparam || lst < (-(int)mac->nparam))) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4067 | goto err; |
| 4068 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4069 | fst = fst < 0 ? fst + (int)mac->nparam + 1: fst; |
| 4070 | lst = lst < 0 ? lst + (int)mac->nparam + 1: lst; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4071 | |
| 4072 | /* counted from zero */ |
| 4073 | fst--, lst--; |
| 4074 | |
| 4075 | /* |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 4076 | * It will be at least one token. Note we |
| 4077 | * need to scan params until separator, otherwise |
| 4078 | * only first token will be passed. |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4079 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4080 | tm = mac->params[(fst + mac->rotate) % mac->nparam]; |
Cyrill Gorcunov | 67f2ca2 | 2018-10-13 19:41:01 +0300 | [diff] [blame] | 4081 | if (!tm) |
| 4082 | goto err; |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4083 | head = dup_Token(NULL, tm); |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 4084 | tt = &head->next, tm = tm->next; |
| 4085 | while (tok_isnt_(tm, ",")) { |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4086 | t = dup_Token(NULL, tm); |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 4087 | *tt = t, tt = &t->next, tm = tm->next; |
| 4088 | } |
| 4089 | |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4090 | if (fst < lst) { |
| 4091 | for (i = fst + 1; i <= lst; i++) { |
| 4092 | t = new_Token(NULL, TOK_OTHER, ",", 0); |
| 4093 | *tt = t, tt = &t->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4094 | j = (i + mac->rotate) % mac->nparam; |
| 4095 | tm = mac->params[j]; |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 4096 | while (tok_isnt_(tm, ",")) { |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4097 | t = dup_Token(NULL, tm); |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 4098 | *tt = t, tt = &t->next, tm = tm->next; |
| 4099 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4100 | } |
| 4101 | } else { |
| 4102 | for (i = fst - 1; i >= lst; i--) { |
| 4103 | t = new_Token(NULL, TOK_OTHER, ",", 0); |
| 4104 | *tt = t, tt = &t->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4105 | j = (i + mac->rotate) % mac->nparam; |
| 4106 | tm = mac->params[j]; |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 4107 | while (tok_isnt_(tm, ",")) { |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4108 | t = dup_Token(NULL, tm); |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 4109 | *tt = t, tt = &t->next, tm = tm->next; |
| 4110 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4111 | } |
| 4112 | } |
| 4113 | |
| 4114 | *last = tt; |
| 4115 | return head; |
| 4116 | |
| 4117 | err: |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4118 | nasm_nonfatal("`%%{%s}': macro parameters out of range", |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4119 | &tline->text[1]); |
| 4120 | return tline; |
| 4121 | } |
| 4122 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4123 | /* |
| 4124 | * Expand MMacro-local things: parameter references (%0, %n, %+n, |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 4125 | * %-n) and MMacro-local identifiers (%%foo) as well as |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4126 | * macro indirection (%[...]) and range (%{..:..}). |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4127 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4128 | static Token *expand_mmac_params(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4129 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4130 | Token *t, *tt, **tail, *thead; |
H. Peter Anvin | 6125b62 | 2009-04-08 14:02:25 -0700 | [diff] [blame] | 4131 | bool changed = false; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4132 | char *pos; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4133 | |
| 4134 | tail = &thead; |
| 4135 | thead = NULL; |
| 4136 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4137 | while (tline) { |
Cyrill Gorcunov | 661f723 | 2018-10-28 20:39:34 +0300 | [diff] [blame] | 4138 | if (tline->type == TOK_PREPROC_ID && tline->text && tline->text[0] && |
Cyrill Gorcunov | ca61119 | 2010-06-04 09:22:12 +0400 | [diff] [blame] | 4139 | (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) || |
| 4140 | (tline->text[1] >= '0' && tline->text[1] <= '9') || |
| 4141 | tline->text[1] == '%')) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4142 | char *text = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4143 | int type = 0, cc; /* type = 0 to placate optimisers */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4144 | char tmpbuf[30]; |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 4145 | unsigned int n; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4146 | int i; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4147 | MMacro *mac; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4148 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4149 | t = tline; |
| 4150 | tline = tline->next; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4151 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4152 | mac = istk->mstk; |
| 4153 | while (mac && !mac->name) /* avoid mistaking %reps for macros */ |
| 4154 | mac = mac->next_active; |
| 4155 | if (!mac) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4156 | nasm_nonfatal("`%s': not in a macro call", t->text); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4157 | } else { |
| 4158 | pos = strchr(t->text, ':'); |
| 4159 | if (!pos) { |
| 4160 | switch (t->text[1]) { |
| 4161 | /* |
| 4162 | * We have to make a substitution of one of the |
| 4163 | * forms %1, %-1, %+1, %%foo, %0. |
| 4164 | */ |
| 4165 | case '0': |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4166 | type = TOK_NUMBER; |
| 4167 | snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam); |
| 4168 | text = nasm_strdup(tmpbuf); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4169 | break; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4170 | case '%': |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4171 | type = TOK_ID; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4172 | snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".", |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4173 | mac->unique); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4174 | text = nasm_strcat(tmpbuf, t->text + 2); |
| 4175 | break; |
| 4176 | case '-': |
| 4177 | n = atoi(t->text + 2) - 1; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4178 | if (n >= mac->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4179 | tt = NULL; |
| 4180 | else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4181 | if (mac->nparam > 1) |
| 4182 | n = (n + mac->rotate) % mac->nparam; |
| 4183 | tt = mac->params[n]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4184 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4185 | cc = find_cc(tt); |
| 4186 | if (cc == -1) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4187 | nasm_nonfatal("macro parameter %d is not a condition code", |
| 4188 | n + 1); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4189 | text = NULL; |
| 4190 | } else { |
| 4191 | type = TOK_ID; |
| 4192 | if (inverse_ccs[cc] == -1) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4193 | nasm_nonfatal("condition code `%s' is not invertible", |
| 4194 | conditions[cc]); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4195 | text = NULL; |
| 4196 | } else |
| 4197 | text = nasm_strdup(conditions[inverse_ccs[cc]]); |
| 4198 | } |
| 4199 | break; |
| 4200 | case '+': |
| 4201 | n = atoi(t->text + 2) - 1; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4202 | if (n >= mac->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4203 | tt = NULL; |
| 4204 | else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4205 | if (mac->nparam > 1) |
| 4206 | n = (n + mac->rotate) % mac->nparam; |
| 4207 | tt = mac->params[n]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4208 | } |
| 4209 | cc = find_cc(tt); |
| 4210 | if (cc == -1) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4211 | nasm_nonfatal("macro parameter %d is not a condition code", |
| 4212 | n + 1); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4213 | text = NULL; |
| 4214 | } else { |
| 4215 | type = TOK_ID; |
| 4216 | text = nasm_strdup(conditions[cc]); |
| 4217 | } |
| 4218 | break; |
| 4219 | default: |
| 4220 | n = atoi(t->text + 1) - 1; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4221 | if (n >= mac->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4222 | tt = NULL; |
| 4223 | else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4224 | if (mac->nparam > 1) |
| 4225 | n = (n + mac->rotate) % mac->nparam; |
| 4226 | tt = mac->params[n]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4227 | } |
| 4228 | if (tt) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4229 | for (i = 0; i < mac->paramlen[n]; i++) { |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4230 | *tail = dup_Token(NULL, tt); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4231 | tail = &(*tail)->next; |
| 4232 | tt = tt->next; |
| 4233 | } |
| 4234 | } |
| 4235 | text = NULL; /* we've done it here */ |
| 4236 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4237 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4238 | } else { |
| 4239 | /* |
| 4240 | * seems we have a parameters range here |
| 4241 | */ |
| 4242 | Token *head, **last; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4243 | head = expand_mmac_params_range(mac, t, &last); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4244 | if (head != t) { |
| 4245 | *tail = head; |
| 4246 | *last = tline; |
| 4247 | tline = head; |
| 4248 | text = NULL; |
| 4249 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4250 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4251 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4252 | if (!text) { |
| 4253 | delete_Token(t); |
| 4254 | } else { |
| 4255 | *tail = t; |
| 4256 | tail = &t->next; |
| 4257 | t->type = type; |
| 4258 | nasm_free(t->text); |
| 4259 | t->text = text; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4260 | t->len = strlen(text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4261 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4262 | changed = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4263 | continue; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4264 | } else if (tline->type == TOK_INDIRECT) { |
| 4265 | t = tline; |
| 4266 | tline = tline->next; |
| 4267 | tt = tokenize(t->text); |
| 4268 | tt = expand_mmac_params(tt); |
| 4269 | tt = expand_smacro(tt); |
| 4270 | *tail = tt; |
| 4271 | while (tt) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4272 | tail = &tt->next; |
| 4273 | tt = tt->next; |
| 4274 | } |
| 4275 | delete_Token(t); |
| 4276 | changed = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4277 | } else { |
| 4278 | t = *tail = tline; |
| 4279 | tline = tline->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4280 | tail = &t->next; |
| 4281 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4282 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4283 | *tail = NULL; |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 4284 | |
Cyrill Gorcunov | c6a742c | 2011-06-27 01:23:09 +0400 | [diff] [blame] | 4285 | if (changed) { |
| 4286 | const struct tokseq_match t[] = { |
| 4287 | { |
| 4288 | PP_CONCAT_MASK(TOK_ID) | |
| 4289 | PP_CONCAT_MASK(TOK_FLOAT), /* head */ |
| 4290 | PP_CONCAT_MASK(TOK_ID) | |
| 4291 | PP_CONCAT_MASK(TOK_NUMBER) | |
| 4292 | PP_CONCAT_MASK(TOK_FLOAT) | |
| 4293 | PP_CONCAT_MASK(TOK_OTHER) /* tail */ |
| 4294 | }, |
| 4295 | { |
| 4296 | PP_CONCAT_MASK(TOK_NUMBER), /* head */ |
| 4297 | PP_CONCAT_MASK(TOK_NUMBER) /* tail */ |
| 4298 | } |
| 4299 | }; |
| 4300 | paste_tokens(&thead, t, ARRAY_SIZE(t), false); |
| 4301 | } |
H. Peter Anvin | 6125b62 | 2009-04-08 14:02:25 -0700 | [diff] [blame] | 4302 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4303 | return thead; |
| 4304 | } |
| 4305 | |
| 4306 | /* |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4307 | * 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] | 4308 | * a macro at all, it is simply copied to the output and the pointer |
| 4309 | * advanced. tpp should be a pointer to a pointer (usually the next |
| 4310 | * pointer of the previous token) to the first token. **tpp is updated |
| 4311 | * to point to the last token of the expansion, and *tpp updated to |
| 4312 | * 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] | 4313 | * |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4314 | * If the expansion is empty, *tpp will be unchanged but **tpp will |
| 4315 | * be advanced past the macro call. |
| 4316 | * |
| 4317 | * The return value equals **tpp. |
| 4318 | * |
| 4319 | * 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] | 4320 | * |
| 4321 | */ |
| 4322 | static Token *expand_smacro_noreset(Token * tline); |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4323 | static int64_t smacro_deadman; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4324 | |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4325 | static bool expand_one_smacro(Token ***tpp) |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4326 | { |
| 4327 | Token **params = NULL; |
| 4328 | const char *mname; |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4329 | Token *tline = **tpp; |
| 4330 | Token *mstart = **tpp; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4331 | SMacro *head, *m; |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4332 | unsigned int i; |
| 4333 | Token *t, *tup, *ttail; |
| 4334 | unsigned int nparam = 0; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4335 | |
| 4336 | if (!tline) |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4337 | return false; /* Empty line, nothing to do */ |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4338 | |
| 4339 | mname = tline->text; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4340 | |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4341 | if (--smacro_deadman <= 0) { |
| 4342 | if (smacro_deadman == 0) |
| 4343 | nasm_nonfatal("interminable macro recursion"); |
| 4344 | goto not_a_macro; |
| 4345 | } else if (tline->type == TOK_ID) { |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4346 | head = (SMacro *)hash_findix(&smacros, mname); |
| 4347 | } else if (tline->type == TOK_PREPROC_ID) { |
| 4348 | Context *ctx = get_ctx(mname, &mname); |
| 4349 | head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL; |
| 4350 | } else { |
| 4351 | goto not_a_macro; |
| 4352 | } |
| 4353 | |
| 4354 | /* |
| 4355 | * We've hit an identifier of some sort. First check whether the |
| 4356 | * identifier is a single-line macro at all, then think about |
| 4357 | * checking for parameters if necessary. |
| 4358 | */ |
| 4359 | list_for_each(m, head) { |
| 4360 | if (!mstrcmp(m->name, mname, m->casesense)) |
| 4361 | break; |
| 4362 | } |
| 4363 | |
| 4364 | if (!m) { |
| 4365 | goto not_a_macro; |
| 4366 | } |
| 4367 | |
| 4368 | /* Parse parameters, if applicable */ |
| 4369 | |
| 4370 | params = NULL; |
| 4371 | nparam = 0; |
| 4372 | |
| 4373 | if (m->nparam == 0) { |
| 4374 | /* |
| 4375 | * Simple case: the macro is parameterless. |
| 4376 | * Nothing to parse; the expansion code will |
| 4377 | * drop the macro name token. |
| 4378 | */ |
| 4379 | } else { |
| 4380 | /* |
| 4381 | * Complicated case: at least one macro with this name |
| 4382 | * exists and takes parameters. We must find the |
| 4383 | * parameters in the call, count them, find the SMacro |
| 4384 | * that corresponds to that form of the macro call, and |
| 4385 | * substitute for the parameters when we expand. What a |
| 4386 | * pain. |
| 4387 | */ |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4388 | Token **phead, **pep; |
| 4389 | int paren = 1; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4390 | int white = 0; |
| 4391 | int brackets = 0; |
| 4392 | bool bracketed = false; |
| 4393 | bool bad_bracket = false; |
| 4394 | unsigned int sparam = PARAM_DELTA; |
| 4395 | |
| 4396 | tline = tline->next; |
| 4397 | |
| 4398 | while (tok_type_(tline, TOK_WHITESPACE)) { |
| 4399 | tline = tline->next; |
| 4400 | } |
| 4401 | if (!tok_is_(tline, "(")) { |
| 4402 | /* |
| 4403 | * This macro wasn't called with parameters: ignore |
| 4404 | * the call. (Behaviour borrowed from gnu cpp.) |
| 4405 | */ |
| 4406 | goto not_a_macro; |
| 4407 | } |
| 4408 | |
| 4409 | paren = 1; |
| 4410 | nparam = 0; |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4411 | nasm_newn(params, sparam); |
| 4412 | phead = pep = ¶ms[0]; |
| 4413 | *pep = NULL; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4414 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4415 | while (paren) { |
| 4416 | bool skip; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4417 | char ch; |
| 4418 | |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4419 | tline = tline->next; |
| 4420 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4421 | if (!tline) { |
| 4422 | nasm_nonfatal("macro call expects terminating `)'"); |
| 4423 | goto not_a_macro; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4424 | } |
| 4425 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4426 | ch = 0; |
| 4427 | skip = false; |
| 4428 | |
| 4429 | switch (tline->type) { |
| 4430 | case TOK_OTHER: |
| 4431 | if (!tline->text[1]) |
| 4432 | ch = tline->text[0]; |
| 4433 | break; |
| 4434 | |
| 4435 | case TOK_WHITESPACE: |
| 4436 | if (brackets || *phead) |
| 4437 | white++; /* Keep interior whitespace */ |
| 4438 | skip = true; |
| 4439 | break; |
| 4440 | |
| 4441 | default: |
| 4442 | break; |
| 4443 | } |
| 4444 | |
| 4445 | switch (ch) { |
| 4446 | case ',': |
| 4447 | if (!brackets) { |
| 4448 | if (++nparam >= sparam) { |
| 4449 | sparam += PARAM_DELTA; |
| 4450 | params = nasm_realloc(params, sparam * sizeof *params); |
| 4451 | } |
| 4452 | pep = ¶ms[nparam]; |
| 4453 | *pep = NULL; |
| 4454 | bracketed = false; |
| 4455 | skip = true; |
| 4456 | } |
| 4457 | break; |
| 4458 | |
| 4459 | case '{': |
| 4460 | if (!bracketed) { |
| 4461 | bracketed = !*phead; |
| 4462 | skip = true; |
| 4463 | } |
| 4464 | brackets++; |
| 4465 | break; |
| 4466 | |
| 4467 | case '}': |
| 4468 | if (brackets > 0) { |
| 4469 | if (!--brackets) |
| 4470 | skip = bracketed; |
| 4471 | } |
| 4472 | break; |
| 4473 | |
| 4474 | case '(': |
| 4475 | if (!brackets) |
| 4476 | paren++; |
| 4477 | break; |
| 4478 | |
| 4479 | case ')': |
| 4480 | if (!brackets) { |
| 4481 | paren--; |
| 4482 | if (!paren) { |
| 4483 | skip = true; |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 4484 | /* Count the final argument */ |
| 4485 | nparam++; |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4486 | } |
| 4487 | } |
| 4488 | break; |
| 4489 | |
| 4490 | default: |
| 4491 | break; /* Normal token */ |
| 4492 | } |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4493 | |
| 4494 | if (!skip) { |
| 4495 | Token *t; |
| 4496 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4497 | bad_bracket |= bracketed && !brackets; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4498 | |
| 4499 | if (white) { |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4500 | *pep = t = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4501 | pep = &t->next; |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4502 | white = 0; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4503 | } |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4504 | *pep = t = dup_Token(NULL, tline); |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4505 | pep = &t->next; |
| 4506 | white = 0; |
| 4507 | } |
| 4508 | } |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4509 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4510 | /* |
| 4511 | * Look for a macro matching in both name and parameter count. |
| 4512 | * We already know any matches cannot be anywhere before the |
| 4513 | * current position of "m", so there is no reason to |
| 4514 | * backtrack. |
| 4515 | */ |
| 4516 | while (1) { |
| 4517 | if (!m) { |
| 4518 | /*! |
| 4519 | *!macro-params-single [on] single-line macro calls with wrong parameter count |
| 4520 | *! warns about \i{single-line macros} being invoked |
| 4521 | *! with the wrong number of parameters. |
| 4522 | */ |
| 4523 | nasm_warn(WARN_MACRO_PARAMS_SINGLE, |
| 4524 | "single-line macro `%s' exists, " |
| 4525 | "but not taking %d parameter%s", |
| 4526 | mname, nparam, (nparam == 1) ? "" : "s"); |
| 4527 | goto not_a_macro; |
| 4528 | } |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4529 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4530 | if (m->nparam == nparam && !mstrcmp(m->name, mname, m->casesense)) |
| 4531 | break; /* It's good */ |
| 4532 | |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4533 | m = m->next; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4534 | } |
| 4535 | } |
| 4536 | |
| 4537 | if (m->in_progress) |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4538 | goto not_a_macro; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4539 | |
| 4540 | /* Expand each parameter */ |
| 4541 | m->in_progress = true; |
| 4542 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4543 | if (m->eval_param) { |
| 4544 | for (i = 0; i < nparam; i++) { |
| 4545 | if (m->eval_param[i]) { |
| 4546 | /* Evaluate this parameter as a number */ |
| 4547 | struct ppscan pps; |
| 4548 | struct tokenval tokval; |
| 4549 | expr *evalresult; |
| 4550 | Token *eval_param; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4551 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4552 | pps.tptr = eval_param = expand_smacro_noreset(params[i]); |
| 4553 | pps.ntokens = -1; |
| 4554 | tokval.t_type = TOKEN_INVALID; |
| 4555 | evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL); |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4556 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4557 | free_tlist(eval_param); |
| 4558 | params[i] = NULL; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4559 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4560 | if (!evalresult) { |
| 4561 | /* Nothing meaningful to do */ |
| 4562 | } else if (tokval.t_type) { |
| 4563 | nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name); |
| 4564 | } else if (!is_simple(evalresult)) { |
| 4565 | nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name); |
| 4566 | } else { |
| 4567 | params[i] = make_tok_num(reloc_value(evalresult)); |
| 4568 | } |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4569 | } |
| 4570 | } |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4571 | } |
| 4572 | |
| 4573 | t = tline; |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4574 | tline = tline->next; /* Remove the macro call from the input */ |
| 4575 | t->next = NULL; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4576 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4577 | /* Note: we own the expansion this returns. */ |
| 4578 | t = m->expand(m, params, nparam); |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4579 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4580 | tup = NULL; |
| 4581 | ttail = NULL; /* Pointer to the last token of the expansion */ |
| 4582 | while (t) { |
| 4583 | enum pp_token_type type = t->type; |
| 4584 | Token *tnext = t->next; |
| 4585 | Token **tp; |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4586 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4587 | switch (type) { |
| 4588 | case TOK_PREPROC_Q: |
| 4589 | case TOK_PREPROC_QQ: |
| 4590 | t->type = TOK_ID; |
| 4591 | nasm_free(t->text); |
| 4592 | t->text = nasm_strdup(type == TOK_PREPROC_QQ ? m->name : mname); |
| 4593 | t->len = nasm_last_string_len(); |
| 4594 | t->next = tline; |
| 4595 | break; |
| 4596 | |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4597 | case TOK_ID: |
| 4598 | case TOK_PREPROC_ID: |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4599 | /* |
| 4600 | * Chain this into the target line *before* expanding, |
| 4601 | * that way we pick up any arguments to the new macro call, |
| 4602 | * if applicable. |
| 4603 | */ |
| 4604 | t->next = tline; |
| 4605 | tp = &t; |
| 4606 | expand_one_smacro(&tp); |
| 4607 | if (t == tline) |
| 4608 | t = NULL; /* Null expansion */ |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4609 | break; |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4610 | |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4611 | default: |
| 4612 | if (is_smac_param(t->type)) { |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4613 | unsigned int param = smac_nparam(t->type); |
| 4614 | nasm_assert(!tup && param < nparam); |
| 4615 | delete_Token(t); |
| 4616 | t = NULL; |
| 4617 | tup = tnext; |
| 4618 | tnext = dup_tlist_reverse(params[param], NULL); |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4619 | } else { |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4620 | t->next = tline; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4621 | } |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4622 | } |
| 4623 | |
| 4624 | if (t) { |
| 4625 | tline = t; |
| 4626 | if (!ttail) |
| 4627 | ttail = t; |
| 4628 | } |
| 4629 | |
| 4630 | if (tnext) { |
| 4631 | t = tnext; |
| 4632 | } else { |
| 4633 | t = tup; |
| 4634 | tup = NULL; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4635 | } |
| 4636 | } |
| 4637 | |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4638 | **tpp = tline; |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4639 | if (ttail) |
| 4640 | *tpp = &ttail->next; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4641 | |
| 4642 | m->in_progress = false; |
| 4643 | |
| 4644 | /* 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] | 4645 | free_tlist(mstart); |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4646 | free_tlist_array(params, nparam); |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4647 | return true; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4648 | |
| 4649 | /* |
| 4650 | * No macro expansion needed; roll back to mstart (if necessary) |
| 4651 | * and then advance to the next input token. |
| 4652 | */ |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4653 | not_a_macro: |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4654 | *tpp = &mstart->next; |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4655 | free_tlist_array(params, nparam); |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4656 | return false; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4657 | } |
| 4658 | |
| 4659 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4660 | * Expand all single-line macro calls made in the given line. |
| 4661 | * Return the expanded version of the line. The original is deemed |
| 4662 | * to be destroyed in the process. (In reality we'll just move |
| 4663 | * Tokens from input to output a lot of the time, rather than |
| 4664 | * actually bothering to destroy and replicate.) |
| 4665 | */ |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4666 | static Token *expand_smacro(Token *tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4667 | { |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4668 | smacro_deadman = nasm_limit[LIMIT_MACROS]; |
| 4669 | return expand_smacro_noreset(tline); |
| 4670 | } |
| 4671 | |
| 4672 | static Token *expand_smacro_noreset(Token * tline) |
| 4673 | { |
| 4674 | Token *t, **tail, *thead; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4675 | Token *org_tline = tline; |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4676 | bool expanded; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4677 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4678 | /* |
| 4679 | * Trick: we should avoid changing the start token pointer since it can |
| 4680 | * be contained in "next" field of other token. Because of this |
| 4681 | * we allocate a copy of first token and work with it; at the end of |
| 4682 | * routine we copy it back |
| 4683 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4684 | if (org_tline) { |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4685 | tline = new_Token(org_tline->next, org_tline->type, |
| 4686 | org_tline->text, 0); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4687 | nasm_free(org_tline->text); |
| 4688 | org_tline->text = NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4689 | } |
| 4690 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4691 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4692 | /* |
| 4693 | * Pretend that we always end up doing expansion on the first pass; |
| 4694 | * that way %+ get processed. However, if we process %+ before the |
| 4695 | * first pass, we end up with things like MACRO %+ TAIL trying to |
| 4696 | * look up the macro "MACROTAIL", which we don't want. |
| 4697 | */ |
| 4698 | expanded = true; |
| 4699 | thead = tline; |
| 4700 | while (true) { |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4701 | static const struct tokseq_match tmatch[] = { |
Cyrill Gorcunov | c6a742c | 2011-06-27 01:23:09 +0400 | [diff] [blame] | 4702 | { |
| 4703 | PP_CONCAT_MASK(TOK_ID) | |
| 4704 | PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */ |
| 4705 | PP_CONCAT_MASK(TOK_ID) | |
| 4706 | PP_CONCAT_MASK(TOK_PREPROC_ID) | |
| 4707 | PP_CONCAT_MASK(TOK_NUMBER) /* tail */ |
| 4708 | } |
| 4709 | }; |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4710 | |
| 4711 | tail = &thead; |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4712 | while ((t = *tail)) { /* main token loop */ |
| 4713 | |
| 4714 | expanded |= expand_one_smacro(&tail); |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4715 | } |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4716 | |
| 4717 | if (!expanded) { |
| 4718 | tline = thead; |
| 4719 | break; /* Done! */ |
| 4720 | } |
| 4721 | |
| 4722 | /* |
| 4723 | * Now scan the entire line and look for successive TOK_IDs |
| 4724 | * that resulted after expansion (they can't be produced by |
| 4725 | * tokenize()). The successive TOK_IDs should be concatenated. |
| 4726 | * Also we look for %+ tokens and concatenate the tokens |
| 4727 | * before and after them (without white spaces in between). |
| 4728 | */ |
| 4729 | paste_tokens(&thead, tmatch, ARRAY_SIZE(tmatch), true); |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4730 | |
| 4731 | expanded = false; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4732 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4733 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4734 | if (org_tline) { |
| 4735 | if (thead) { |
| 4736 | *org_tline = *thead; |
| 4737 | /* since we just gave text to org_line, don't free it */ |
| 4738 | thead->text = NULL; |
| 4739 | delete_Token(thead); |
| 4740 | } else { |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4741 | /* |
| 4742 | * The expression expanded to empty line; |
| 4743 | * we can't return NULL because of the "trick" above. |
| 4744 | * Just set the line to a single WHITESPACE token. |
| 4745 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4746 | memset(org_tline, 0, sizeof(*org_tline)); |
| 4747 | org_tline->text = NULL; |
| 4748 | org_tline->type = TOK_WHITESPACE; |
| 4749 | } |
| 4750 | thead = org_tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4751 | } |
| 4752 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4753 | return thead; |
| 4754 | } |
| 4755 | |
| 4756 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4757 | * Similar to expand_smacro but used exclusively with macro identifiers |
| 4758 | * right before they are fetched in. The reason is that there can be |
| 4759 | * identifiers consisting of several subparts. We consider that if there |
| 4760 | * are more than one element forming the name, user wants a expansion, |
| 4761 | * otherwise it will be left as-is. Example: |
| 4762 | * |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4763 | * %define %$abc cde |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4764 | * |
| 4765 | * the identifier %$abc will be left as-is so that the handler for %define |
| 4766 | * will suck it and define the corresponding value. Other case: |
| 4767 | * |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4768 | * %define _%$abc cde |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4769 | * |
| 4770 | * In this case user wants name to be expanded *before* %define starts |
| 4771 | * working, so we'll expand %$abc into something (if it has a value; |
| 4772 | * otherwise it will be left as-is) then concatenate all successive |
| 4773 | * PP_IDs into one. |
| 4774 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4775 | static Token *expand_id(Token * tline) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4776 | { |
| 4777 | Token *cur, *oldnext = NULL; |
| 4778 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4779 | if (!tline || !tline->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4780 | return tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4781 | |
| 4782 | cur = tline; |
| 4783 | while (cur->next && |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4784 | (cur->next->type == TOK_ID || |
| 4785 | cur->next->type == TOK_PREPROC_ID |
| 4786 | || cur->next->type == TOK_NUMBER)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4787 | cur = cur->next; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4788 | |
| 4789 | /* If identifier consists of just one token, don't expand */ |
| 4790 | if (cur == tline) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4791 | return tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4792 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4793 | if (cur) { |
| 4794 | oldnext = cur->next; /* Detach the tail past identifier */ |
| 4795 | cur->next = NULL; /* so that expand_smacro stops here */ |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4796 | } |
| 4797 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4798 | tline = expand_smacro(tline); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4799 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4800 | if (cur) { |
| 4801 | /* expand_smacro possibly changhed tline; re-scan for EOL */ |
| 4802 | cur = tline; |
| 4803 | while (cur && cur->next) |
| 4804 | cur = cur->next; |
| 4805 | if (cur) |
| 4806 | cur->next = oldnext; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4807 | } |
| 4808 | |
| 4809 | return tline; |
| 4810 | } |
| 4811 | |
| 4812 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4813 | * Determine whether the given line constitutes a multi-line macro |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4814 | * 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] | 4815 | * to check for an initial label - that's taken care of in |
| 4816 | * expand_mmacro - but must check numbers of parameters. Guaranteed |
| 4817 | * to be called with tline->type == TOK_ID, so the putative macro |
| 4818 | * name is easy to find. |
| 4819 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4820 | static MMacro *is_mmacro(Token * tline, Token *** params_array) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4821 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4822 | MMacro *head, *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4823 | Token **params; |
| 4824 | int nparam; |
| 4825 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4826 | head = (MMacro *) hash_findix(&mmacros, tline->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4827 | |
| 4828 | /* |
| 4829 | * Efficiency: first we see if any macro exists with the given |
| 4830 | * name. If not, we can return NULL immediately. _Then_ we |
| 4831 | * count the parameters, and then we look further along the |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4832 | * list if necessary to find the proper MMacro. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4833 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4834 | list_for_each(m, head) |
| 4835 | if (!mstrcmp(m->name, tline->text, m->casesense)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4836 | break; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4837 | if (!m) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4838 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4839 | |
| 4840 | /* |
| 4841 | * OK, we have a potential macro. Count and demarcate the |
| 4842 | * parameters. |
| 4843 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4844 | count_mmac_params(tline->next, &nparam, ¶ms); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4845 | |
| 4846 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4847 | * 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] | 4848 | * structure that handles this number. |
| 4849 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4850 | while (m) { |
| 4851 | if (m->nparam_min <= nparam |
| 4852 | && (m->plus || nparam <= m->nparam_max)) { |
| 4853 | /* |
| 4854 | * This one is right. Just check if cycle removal |
| 4855 | * prohibits us using it before we actually celebrate... |
| 4856 | */ |
| 4857 | if (m->in_progress > m->max_depth) { |
| 4858 | if (m->max_depth > 0) { |
H. Peter Anvin (Intel) | 80c4f23 | 2018-12-14 13:33:24 -0800 | [diff] [blame] | 4859 | nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i", |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4860 | m->max_depth); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4861 | } |
| 4862 | nasm_free(params); |
| 4863 | return NULL; |
| 4864 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4865 | /* |
| 4866 | * It's right, and we can use it. Add its default |
| 4867 | * parameters to the end of our list if necessary. |
| 4868 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4869 | if (m->defaults && nparam < m->nparam_min + m->ndefs) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4870 | params = |
| 4871 | nasm_realloc(params, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4872 | ((m->nparam_min + m->ndefs + |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4873 | 1) * sizeof(*params))); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4874 | while (nparam < m->nparam_min + m->ndefs) { |
| 4875 | params[nparam] = m->defaults[nparam - m->nparam_min]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4876 | nparam++; |
| 4877 | } |
| 4878 | } |
| 4879 | /* |
| 4880 | * If we've gone over the maximum parameter count (and |
| 4881 | * we're in Plus mode), ignore parameters beyond |
| 4882 | * nparam_max. |
| 4883 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4884 | if (m->plus && nparam > m->nparam_max) |
| 4885 | nparam = m->nparam_max; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4886 | /* |
| 4887 | * Then terminate the parameter list, and leave. |
| 4888 | */ |
| 4889 | if (!params) { /* need this special case */ |
| 4890 | params = nasm_malloc(sizeof(*params)); |
| 4891 | nparam = 0; |
| 4892 | } |
| 4893 | params[nparam] = NULL; |
| 4894 | *params_array = params; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4895 | return m; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4896 | } |
| 4897 | /* |
| 4898 | * This one wasn't right: look for the next one with the |
| 4899 | * same name. |
| 4900 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4901 | list_for_each(m, m->next) |
| 4902 | if (!mstrcmp(m->name, tline->text, m->casesense)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4903 | break; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4904 | } |
| 4905 | |
| 4906 | /* |
| 4907 | * After all that, we didn't find one with the right number of |
| 4908 | * parameters. Issue a warning, and fail to expand the macro. |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4909 | *! |
| 4910 | *!macro-params-multi [on] multi-line macro calls with wrong parameter count |
| 4911 | *! warns about \i{multi-line macros} being invoked |
| 4912 | *! with the wrong number of parameters. See \k{mlmacover} for an |
| 4913 | *! example of why you might want to disable this warning. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4914 | */ |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4915 | nasm_warn(WARN_MACRO_PARAMS_MULTI, |
| 4916 | "multi-line macro `%s' exists, but not taking %d parameter%s", |
| 4917 | tline->text, nparam, (nparam == 1) ? "" : "s"); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4918 | nasm_free(params); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4919 | return NULL; |
| 4920 | } |
| 4921 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4922 | |
| 4923 | /* |
| 4924 | * Save MMacro invocation specific fields in |
| 4925 | * preparation for a recursive macro expansion |
| 4926 | */ |
| 4927 | static void push_mmacro(MMacro *m) |
| 4928 | { |
| 4929 | MMacroInvocation *i; |
| 4930 | |
| 4931 | i = nasm_malloc(sizeof(MMacroInvocation)); |
| 4932 | i->prev = m->prev; |
| 4933 | i->params = m->params; |
| 4934 | i->iline = m->iline; |
| 4935 | i->nparam = m->nparam; |
| 4936 | i->rotate = m->rotate; |
| 4937 | i->paramlen = m->paramlen; |
| 4938 | i->unique = m->unique; |
| 4939 | i->condcnt = m->condcnt; |
| 4940 | m->prev = i; |
| 4941 | } |
| 4942 | |
| 4943 | |
| 4944 | /* |
| 4945 | * Restore MMacro invocation specific fields that were |
| 4946 | * saved during a previous recursive macro expansion |
| 4947 | */ |
| 4948 | static void pop_mmacro(MMacro *m) |
| 4949 | { |
| 4950 | MMacroInvocation *i; |
| 4951 | |
| 4952 | if (m->prev) { |
| 4953 | i = m->prev; |
| 4954 | m->prev = i->prev; |
| 4955 | m->params = i->params; |
| 4956 | m->iline = i->iline; |
| 4957 | m->nparam = i->nparam; |
| 4958 | m->rotate = i->rotate; |
| 4959 | m->paramlen = i->paramlen; |
| 4960 | m->unique = i->unique; |
| 4961 | m->condcnt = i->condcnt; |
| 4962 | nasm_free(i); |
| 4963 | } |
| 4964 | } |
| 4965 | |
| 4966 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4967 | /* |
| 4968 | * Expand the multi-line macro call made by the given line, if |
| 4969 | * 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] | 4970 | * istk->expansion and return 1. Otherwise return 0. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4971 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4972 | static int expand_mmacro(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4973 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4974 | Token *startline = tline; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4975 | Token *label = NULL; |
| 4976 | int dont_prepend = 0; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4977 | Token **params, *t, *tt; |
| 4978 | MMacro *m; |
| 4979 | Line *l, *ll; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4980 | int i, nparam, *paramlen; |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 4981 | const char *mname; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4982 | |
| 4983 | t = tline; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4984 | skip_white_(t); |
H. Peter Anvin | ce2233b | 2008-05-25 21:57:00 -0700 | [diff] [blame] | 4985 | /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */ |
H. Peter Anvin | dce1e2f | 2002-04-30 21:06:37 +0000 | [diff] [blame] | 4986 | 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] | 4987 | return 0; |
| 4988 | m = is_mmacro(t, ¶ms); |
| 4989 | if (m) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4990 | mname = t->text; |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 4991 | } else { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4992 | Token *last; |
| 4993 | /* |
| 4994 | * We have an id which isn't a macro call. We'll assume |
| 4995 | * it might be a label; we'll also check to see if a |
| 4996 | * colon follows it. Then, if there's another id after |
| 4997 | * that lot, we'll check it again for macro-hood. |
| 4998 | */ |
| 4999 | label = last = t; |
| 5000 | t = t->next; |
| 5001 | if (tok_type_(t, TOK_WHITESPACE)) |
| 5002 | last = t, t = t->next; |
| 5003 | if (tok_is_(t, ":")) { |
| 5004 | dont_prepend = 1; |
| 5005 | last = t, t = t->next; |
| 5006 | if (tok_type_(t, TOK_WHITESPACE)) |
| 5007 | last = t, t = t->next; |
| 5008 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5009 | if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, ¶ms))) |
| 5010 | return 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5011 | last->next = NULL; |
Keith Kanios | 891775e | 2009-07-11 06:08:54 -0500 | [diff] [blame] | 5012 | mname = t->text; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5013 | tline = t; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5014 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5015 | |
| 5016 | /* |
| 5017 | * Fix up the parameters: this involves stripping leading and |
| 5018 | * trailing whitespace, then stripping braces if they are |
| 5019 | * present. |
| 5020 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5021 | for (nparam = 0; params[nparam]; nparam++) ; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5022 | paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5023 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5024 | for (i = 0; params[i]; i++) { |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 5025 | int brace = 0; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5026 | int comma = (!m->plus || i < nparam - 1); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5027 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5028 | t = params[i]; |
| 5029 | skip_white_(t); |
| 5030 | if (tok_is_(t, "{")) |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 5031 | t = t->next, brace++, comma = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5032 | params[i] = t; |
| 5033 | paramlen[i] = 0; |
| 5034 | while (t) { |
| 5035 | if (comma && t->type == TOK_OTHER && !strcmp(t->text, ",")) |
| 5036 | break; /* ... because we have hit a comma */ |
| 5037 | if (comma && t->type == TOK_WHITESPACE |
| 5038 | && tok_is_(t->next, ",")) |
| 5039 | break; /* ... or a space then a comma */ |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 5040 | if (brace && t->type == TOK_OTHER) { |
| 5041 | if (t->text[0] == '{') |
| 5042 | brace++; /* ... or a nested opening brace */ |
| 5043 | else if (t->text[0] == '}') |
| 5044 | if (!--brace) |
| 5045 | break; /* ... or a brace */ |
| 5046 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5047 | t = t->next; |
| 5048 | paramlen[i]++; |
| 5049 | } |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 5050 | if (brace) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 5051 | nasm_nonfatal("macro params should be enclosed in braces"); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5052 | } |
| 5053 | |
| 5054 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5055 | * OK, we have a MMacro structure together with a set of |
| 5056 | * parameters. We must now go through the expansion and push |
| 5057 | * copies of each Line on to istk->expansion. Substitution of |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 5058 | * parameter tokens and macro-local tokens doesn't get done |
| 5059 | * until the single-line macro substitution process; this is |
| 5060 | * because delaying them allows us to change the semantics |
| 5061 | * later through %rotate. |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5062 | * |
| 5063 | * First, push an end marker on to istk->expansion, mark this |
| 5064 | * macro as in progress, and set up its invocation-specific |
| 5065 | * variables. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5066 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5067 | ll = nasm_malloc(sizeof(Line)); |
| 5068 | ll->next = istk->expansion; |
| 5069 | ll->finishes = m; |
| 5070 | ll->first = NULL; |
| 5071 | istk->expansion = ll; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5072 | |
| 5073 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5074 | * Save the previous MMacro expansion in the case of |
| 5075 | * macro recursion |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5076 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5077 | if (m->max_depth && m->in_progress) |
| 5078 | push_mmacro(m); |
| 5079 | |
| 5080 | m->in_progress ++; |
| 5081 | m->params = params; |
| 5082 | m->iline = tline; |
| 5083 | m->nparam = nparam; |
| 5084 | m->rotate = 0; |
| 5085 | m->paramlen = paramlen; |
| 5086 | m->unique = unique++; |
| 5087 | m->lineno = 0; |
| 5088 | m->condcnt = 0; |
| 5089 | |
| 5090 | m->next_active = istk->mstk; |
| 5091 | istk->mstk = m; |
| 5092 | |
| 5093 | list_for_each(l, m->expansion) { |
| 5094 | Token **tail; |
| 5095 | |
| 5096 | ll = nasm_malloc(sizeof(Line)); |
| 5097 | ll->finishes = NULL; |
| 5098 | ll->next = istk->expansion; |
| 5099 | istk->expansion = ll; |
| 5100 | tail = &ll->first; |
| 5101 | |
| 5102 | list_for_each(t, l->first) { |
| 5103 | Token *x = t; |
| 5104 | switch (t->type) { |
| 5105 | case TOK_PREPROC_Q: |
| 5106 | tt = *tail = new_Token(NULL, TOK_ID, mname, 0); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5107 | break; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5108 | case TOK_PREPROC_QQ: |
| 5109 | tt = *tail = new_Token(NULL, TOK_ID, m->name, 0); |
| 5110 | break; |
| 5111 | case TOK_PREPROC_ID: |
| 5112 | if (t->text[1] == '0' && t->text[2] == '0') { |
| 5113 | dont_prepend = -1; |
| 5114 | x = label; |
| 5115 | if (!x) |
| 5116 | continue; |
| 5117 | } |
| 5118 | /* fall through */ |
| 5119 | default: |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5120 | tt = *tail = dup_Token(NULL, x); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5121 | break; |
| 5122 | } |
| 5123 | tail = &tt->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5124 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5125 | *tail = NULL; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5126 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5127 | |
| 5128 | /* |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5129 | * If we had a label, push it on as the first line of |
| 5130 | * the macro expansion. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5131 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5132 | if (label) { |
| 5133 | if (dont_prepend < 0) |
| 5134 | free_tlist(startline); |
| 5135 | else { |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5136 | nasm_new(ll); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5137 | ll->finishes = NULL; |
| 5138 | ll->next = istk->expansion; |
| 5139 | istk->expansion = ll; |
| 5140 | ll->first = startline; |
| 5141 | if (!dont_prepend) { |
| 5142 | while (label->next) |
| 5143 | label = label->next; |
| 5144 | label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5145 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5146 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 5147 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5148 | |
H. Peter Anvin | 0d4d431 | 2019-08-07 00:46:27 -0700 | [diff] [blame] | 5149 | lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5150 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5151 | return 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5152 | } |
| 5153 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5154 | /* |
| 5155 | * This function adds macro names to error messages, and suppresses |
| 5156 | * them if necessary. |
| 5157 | */ |
H. Peter Anvin (Intel) | 6bde2ed | 2018-12-13 19:39:41 -0800 | [diff] [blame] | 5158 | 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] | 5159 | { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5160 | char buff[BUFSIZ]; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5161 | MMacro *mmac = NULL; |
| 5162 | int delta = 0; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 5163 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5164 | /* |
| 5165 | * If we're in a dead branch of IF or something like it, ignore the error. |
| 5166 | * However, because %else etc are evaluated in the state context |
| 5167 | * of the previous branch, errors might get lost: |
| 5168 | * %if 0 ... %else trailing garbage ... %endif |
| 5169 | * So %else etc should set the ERR_PP_PRECOND flag. |
| 5170 | */ |
| 5171 | if ((severity & ERR_MASK) < ERR_FATAL && |
| 5172 | istk && istk->conds && |
| 5173 | ((severity & ERR_PP_PRECOND) ? |
| 5174 | istk->conds->state == COND_NEVER : |
H. Peter Anvin | eb6653f | 2016-04-05 13:03:10 -0700 | [diff] [blame] | 5175 | !emitting(istk->conds->state))) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5176 | return; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 5177 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5178 | /* get %macro name */ |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5179 | if (!(severity & ERR_NOFILE) && istk && istk->mstk) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5180 | mmac = istk->mstk; |
| 5181 | /* but %rep blocks should be skipped */ |
| 5182 | while (mmac && !mmac->name) |
| 5183 | mmac = mmac->next_active, delta++; |
Cyrill Gorcunov | 9900c6b | 2011-10-09 18:58:46 +0400 | [diff] [blame] | 5184 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5185 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5186 | if (mmac) { |
| 5187 | vsnprintf(buff, sizeof(buff), fmt, arg); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 5188 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5189 | nasm_set_verror(real_verror); |
| 5190 | nasm_error(severity, "(%s:%d) %s", |
| 5191 | mmac->name, mmac->lineno - delta, buff); |
| 5192 | nasm_set_verror(pp_verror); |
| 5193 | } else { |
| 5194 | real_verror(severity, fmt, arg); |
| 5195 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 5196 | } |
| 5197 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 5198 | static Token * |
| 5199 | stdmac_file(const SMacro *s, Token **params, unsigned int nparams) |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5200 | { |
| 5201 | (void)s; |
| 5202 | (void)params; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 5203 | (void)nparams; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5204 | |
| 5205 | return make_tok_qstr(src_get_fname()); |
| 5206 | } |
| 5207 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 5208 | static Token * |
| 5209 | stdmac_line(const SMacro *s, Token **params, unsigned int nparams) |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5210 | { |
| 5211 | (void)s; |
| 5212 | (void)params; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 5213 | (void)nparams; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5214 | |
| 5215 | return make_tok_num(src_get_linnum()); |
| 5216 | } |
| 5217 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 5218 | static Token * |
| 5219 | stdmac_bits(const SMacro *s, Token **params, unsigned int nparams) |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5220 | { |
| 5221 | (void)s; |
| 5222 | (void)params; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 5223 | (void)nparams; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5224 | |
| 5225 | return make_tok_num(globalbits); |
| 5226 | } |
| 5227 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 5228 | static Token * |
| 5229 | stdmac_ptr(const SMacro *s, Token **params, unsigned int nparams) |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5230 | { |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 5231 | const Token *t; |
Chang S. Bae | fea2269 | 2019-05-28 12:25:16 -0700 | [diff] [blame] | 5232 | static const Token ptr_word = { NULL, "word", 4, TOK_ID }; |
| 5233 | static const Token ptr_dword = { NULL, "dword", 5, TOK_ID }; |
| 5234 | static const Token ptr_qword = { NULL, "qword", 5, TOK_ID }; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5235 | |
| 5236 | (void)s; |
| 5237 | (void)params; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 5238 | (void)nparams; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5239 | |
| 5240 | switch (globalbits) { |
| 5241 | case 16: |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 5242 | t = &ptr_word; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5243 | break; |
| 5244 | case 32: |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 5245 | t = &ptr_dword; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5246 | break; |
| 5247 | case 64: |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 5248 | t = &ptr_qword; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5249 | break; |
| 5250 | default: |
| 5251 | panic(); |
| 5252 | } |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 5253 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 5254 | return dup_Token(NULL, t); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5255 | } |
| 5256 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5257 | /* Add magic standard macros */ |
| 5258 | struct magic_macros { |
| 5259 | const char *name; |
| 5260 | int nparams; |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 5261 | ExpandSMacro func; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5262 | }; |
| 5263 | static const struct magic_macros magic_macros[] = |
| 5264 | { |
| 5265 | { "__FILE__", 0, stdmac_file }, |
| 5266 | { "__LINE__", 0, stdmac_line }, |
| 5267 | { "__BITS__", 0, stdmac_bits }, |
| 5268 | { "__PTR__", 0, stdmac_ptr }, |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5269 | { NULL, 0, NULL } |
| 5270 | }; |
| 5271 | |
| 5272 | static void pp_add_magic_stdmac(void) |
| 5273 | { |
| 5274 | const struct magic_macros *m; |
| 5275 | SMacro *s; |
| 5276 | |
| 5277 | for (m = magic_macros; m->name; m++) { |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 5278 | 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] | 5279 | s->expand = m->func; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5280 | } |
| 5281 | } |
| 5282 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5283 | static void |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5284 | 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] | 5285 | { |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5286 | int apass; |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 5287 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5288 | cstk = NULL; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5289 | nasm_new(istk); |
H. Peter Anvin | 3e83cec | 2016-05-25 04:28:46 -0700 | [diff] [blame] | 5290 | istk->fp = nasm_open_read(file, NF_TEXT); |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 5291 | src_set(0, file); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5292 | istk->lineinc = 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5293 | if (!istk->fp) |
Cyrill Gorcunov | c3527dd | 2018-11-24 22:17:47 +0300 | [diff] [blame] | 5294 | nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5295 | defining = NULL; |
Charles Crayne | d4200be | 2008-07-12 16:42:33 -0700 | [diff] [blame] | 5296 | nested_mac_count = 0; |
| 5297 | nested_rep_count = 0; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 5298 | init_macros(); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5299 | unique = 0; |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 5300 | deplist = dep_list; |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5301 | pp_mode = mode; |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 5302 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5303 | pp_add_magic_stdmac(); |
| 5304 | |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 5305 | if (tasm_compatible_mode) |
| 5306 | pp_add_stdmac(nasm_stdmac_tasm); |
| 5307 | |
| 5308 | pp_add_stdmac(nasm_stdmac_nasm); |
| 5309 | pp_add_stdmac(nasm_stdmac_version); |
| 5310 | |
Cyrill Gorcunov | 15ce78f | 2017-01-06 20:21:28 +0300 | [diff] [blame] | 5311 | if (extrastdmac) |
| 5312 | pp_add_stdmac(extrastdmac); |
| 5313 | |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 5314 | stdmacpos = stdmacros[0]; |
| 5315 | stdmacnext = &stdmacros[1]; |
| 5316 | |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 5317 | do_predef = true; |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 5318 | |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 5319 | strlist_add(deplist, file); |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 5320 | |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 5321 | /* |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 5322 | * Define the __PASS__ macro. This is defined here unlike all the |
| 5323 | * other builtins, because it is special -- it varies between |
| 5324 | * passes -- but there is really no particular reason to make it |
| 5325 | * magic. |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5326 | * |
| 5327 | * 0 = dependencies only |
| 5328 | * 1 = preparatory passes |
| 5329 | * 2 = final pass |
| 5330 | * 3 = preproces only |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 5331 | */ |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5332 | switch (mode) { |
| 5333 | case PP_NORMAL: |
| 5334 | apass = pass_final() ? 2 : 1; |
| 5335 | break; |
| 5336 | case PP_DEPS: |
| 5337 | apass = 0; |
| 5338 | break; |
| 5339 | case PP_PREPROC: |
| 5340 | apass = 3; |
| 5341 | break; |
| 5342 | default: |
| 5343 | panic(); |
| 5344 | } |
| 5345 | |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 5346 | define_smacro(NULL, "__PASS__", true, 0, NULL, make_tok_num(apass)); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5347 | } |
| 5348 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 5349 | static void pp_init(void) |
| 5350 | { |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 5351 | } |
| 5352 | |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5353 | /* |
| 5354 | * Get a line of tokens. If we popped the macro expansion/include stack, |
| 5355 | * we return a pointer to the dummy token tok_pop; at that point if |
| 5356 | * istk is NULL then we have reached end of input; |
| 5357 | */ |
| 5358 | static Token tok_pop; /* Dummy token placeholder */ |
| 5359 | |
| 5360 | static Token *pp_tokline(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5361 | { |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5362 | Token *tline, *dtline; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5363 | |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5364 | while (true) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5365 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5366 | * Fetch a tokenized line, either from the macro-expansion |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5367 | * buffer or from the input file. |
| 5368 | */ |
| 5369 | tline = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5370 | while (istk->expansion && istk->expansion->finishes) { |
| 5371 | Line *l = istk->expansion; |
| 5372 | if (!l->finishes->name && l->finishes->in_progress > 1) { |
| 5373 | Line *ll; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5374 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5375 | /* |
| 5376 | * This is a macro-end marker for a macro with no |
| 5377 | * name, which means it's not really a macro at all |
| 5378 | * but a %rep block, and the `in_progress' field is |
| 5379 | * more than 1, meaning that we still need to |
| 5380 | * repeat. (1 means the natural last repetition; 0 |
| 5381 | * means termination by %exitrep.) We have |
| 5382 | * therefore expanded up to the %endrep, and must |
| 5383 | * push the whole block on to the expansion buffer |
| 5384 | * again. We don't bother to remove the macro-end |
| 5385 | * marker: we'd only have to generate another one |
| 5386 | * if we did. |
| 5387 | */ |
| 5388 | l->finishes->in_progress--; |
| 5389 | list_for_each(l, l->finishes->expansion) { |
| 5390 | Token *t, *tt, **tail; |
| 5391 | |
| 5392 | ll = nasm_malloc(sizeof(Line)); |
| 5393 | ll->next = istk->expansion; |
| 5394 | ll->finishes = NULL; |
| 5395 | ll->first = NULL; |
| 5396 | tail = &ll->first; |
| 5397 | |
| 5398 | list_for_each(t, l->first) { |
| 5399 | if (t->text || t->type == TOK_WHITESPACE) { |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 5400 | tt = *tail = dup_Token(NULL, t); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5401 | tail = &tt->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5402 | } |
| 5403 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5404 | istk->expansion = ll; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5405 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5406 | } else { |
| 5407 | /* |
| 5408 | * Check whether a `%rep' was started and not ended |
| 5409 | * within this macro expansion. This can happen and |
| 5410 | * should be detected. It's a fatal error because |
| 5411 | * I'm too confused to work out how to recover |
| 5412 | * sensibly from it. |
| 5413 | */ |
| 5414 | if (defining) { |
| 5415 | if (defining->name) |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 5416 | nasm_panic("defining with name in expansion"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5417 | else if (istk->mstk->name) |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 5418 | nasm_fatal("`%%rep' without `%%endrep' within" |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5419 | " expansion of macro `%s'", |
| 5420 | istk->mstk->name); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5421 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5422 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5423 | /* |
| 5424 | * FIXME: investigate the relationship at this point between |
| 5425 | * istk->mstk and l->finishes |
| 5426 | */ |
| 5427 | { |
| 5428 | MMacro *m = istk->mstk; |
| 5429 | istk->mstk = m->next_active; |
| 5430 | if (m->name) { |
| 5431 | /* |
| 5432 | * This was a real macro call, not a %rep, and |
| 5433 | * therefore the parameter information needs to |
| 5434 | * be freed. |
| 5435 | */ |
| 5436 | if (m->prev) { |
| 5437 | pop_mmacro(m); |
| 5438 | l->finishes->in_progress --; |
| 5439 | } else { |
| 5440 | nasm_free(m->params); |
| 5441 | free_tlist(m->iline); |
| 5442 | nasm_free(m->paramlen); |
| 5443 | l->finishes->in_progress = 0; |
| 5444 | } |
Adam Majer | 91e7240 | 2017-07-25 10:42:01 +0200 | [diff] [blame] | 5445 | } |
| 5446 | |
| 5447 | /* |
| 5448 | * FIXME It is incorrect to always free_mmacro here. |
| 5449 | * It leads to usage-after-free. |
| 5450 | * |
| 5451 | * https://bugzilla.nasm.us/show_bug.cgi?id=3392414 |
| 5452 | */ |
| 5453 | #if 0 |
| 5454 | else |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5455 | free_mmacro(m); |
Adam Majer | 91e7240 | 2017-07-25 10:42:01 +0200 | [diff] [blame] | 5456 | #endif |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5457 | } |
| 5458 | istk->expansion = l->next; |
| 5459 | nasm_free(l); |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 5460 | lfmt->downlevel(LIST_MACRO); |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5461 | return &tok_pop; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5462 | } |
| 5463 | } |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5464 | do { /* until we get a line we can use */ |
| 5465 | char *line; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5466 | |
| 5467 | if (istk->expansion) { /* from a macro expansion */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5468 | Line *l = istk->expansion; |
| 5469 | if (istk->mstk) |
| 5470 | istk->mstk->lineno++; |
| 5471 | tline = l->first; |
| 5472 | istk->expansion = l->next; |
| 5473 | nasm_free(l); |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5474 | line = detoken(tline, false); |
| 5475 | lfmt->line(LIST_MACRO, line); |
| 5476 | nasm_free(line); |
| 5477 | } else if ((line = read_line())) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5478 | line = prepreproc(line); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5479 | tline = tokenize(line); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5480 | nasm_free(line); |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5481 | } else { |
| 5482 | /* |
| 5483 | * The current file has ended; work down the istk |
| 5484 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5485 | Include *i = istk; |
| 5486 | fclose(i->fp); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5487 | if (i->conds) { |
| 5488 | /* nasm_error can't be conditionally suppressed */ |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 5489 | nasm_fatal("expected `%%endif' before end of file"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5490 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5491 | /* 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] | 5492 | if (i->next) |
| 5493 | src_set(i->lineno, i->fname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5494 | istk = i->next; |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 5495 | lfmt->downlevel(LIST_INCLUDE); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5496 | nasm_free(i); |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5497 | return &tok_pop; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5498 | } |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5499 | } while (0); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5500 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5501 | /* |
| 5502 | * We must expand MMacro parameters and MMacro-local labels |
| 5503 | * _before_ we plunge into directive processing, to cope |
| 5504 | * with things like `%define something %1' such as STRUC |
| 5505 | * uses. Unless we're _defining_ a MMacro, in which case |
| 5506 | * those tokens should be left alone to go into the |
| 5507 | * definition; and unless we're in a non-emitting |
| 5508 | * condition, in which case we don't want to meddle with |
| 5509 | * anything. |
| 5510 | */ |
| 5511 | if (!defining && !(istk->conds && !emitting(istk->conds->state)) |
| 5512 | && !(istk->mstk && !istk->mstk->in_progress)) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5513 | tline = expand_mmac_params(tline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5514 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5515 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5516 | /* |
| 5517 | * Check the line to see if it's a preprocessor directive. |
| 5518 | */ |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5519 | if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) { |
| 5520 | if (dtline) |
| 5521 | return dtline; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5522 | } else if (defining) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5523 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5524 | * We're defining a multi-line macro. We emit nothing |
| 5525 | * at all, and just |
| 5526 | * shove the tokenized line on to the macro definition. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5527 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5528 | Line *l = nasm_malloc(sizeof(Line)); |
| 5529 | l->next = defining->expansion; |
| 5530 | l->first = tline; |
| 5531 | l->finishes = NULL; |
| 5532 | defining->expansion = l; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5533 | } else if (istk->conds && !emitting(istk->conds->state)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5534 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5535 | * We're in a non-emitting branch of a condition block. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5536 | * Emit nothing at all, not even a blank line: when we |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5537 | * emerge from the condition we'll give a line-number |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5538 | * directive so we keep our place correctly. |
| 5539 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5540 | free_tlist(tline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5541 | } else if (istk->mstk && !istk->mstk->in_progress) { |
| 5542 | /* |
| 5543 | * We're in a %rep block which has been terminated, so |
| 5544 | * we're walking through to the %endrep without |
| 5545 | * emitting anything. Emit nothing at all, not even a |
| 5546 | * blank line: when we emerge from the %rep block we'll |
| 5547 | * give a line-number directive so we keep our place |
| 5548 | * correctly. |
| 5549 | */ |
| 5550 | free_tlist(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5551 | } else { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5552 | tline = expand_smacro(tline); |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5553 | if (!expand_mmacro(tline)) |
| 5554 | return tline; |
| 5555 | } |
| 5556 | } |
| 5557 | } |
| 5558 | |
| 5559 | static char *pp_getline(void) |
| 5560 | { |
| 5561 | char *line = NULL; |
| 5562 | Token *tline; |
| 5563 | |
| 5564 | real_verror = nasm_set_verror(pp_verror); |
| 5565 | |
| 5566 | while (true) { |
| 5567 | tline = pp_tokline(); |
| 5568 | if (tline == &tok_pop) { |
| 5569 | /* |
| 5570 | * We popped the macro/include stack. If istk is empty, |
| 5571 | * we are at end of input, otherwise just loop back. |
| 5572 | */ |
| 5573 | if (!istk) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5574 | break; |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5575 | } else { |
| 5576 | /* |
| 5577 | * De-tokenize the line and emit it. |
| 5578 | */ |
| 5579 | line = detoken(tline, true); |
| 5580 | free_tlist(tline); |
| 5581 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5582 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5583 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5584 | |
H. Peter Anvin (Intel) | d6e8177 | 2019-08-09 08:06:39 -0700 | [diff] [blame] | 5585 | if (list_option('e') && line && line[0]) { |
| 5586 | char *buf = nasm_strcat(" ;;; ", line); |
| 5587 | lfmt->line(LIST_MACRO, buf); |
| 5588 | nasm_free(buf); |
| 5589 | } |
| 5590 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5591 | nasm_set_verror(real_verror); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5592 | return line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5593 | } |
| 5594 | |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5595 | static void pp_cleanup_pass(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5596 | { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5597 | real_verror = nasm_set_verror(pp_verror); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5598 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5599 | if (defining) { |
| 5600 | if (defining->name) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 5601 | nasm_nonfatal("end of file while still defining macro `%s'", |
| 5602 | defining->name); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5603 | } else { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 5604 | nasm_nonfatal("end of file while still in %%rep"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5605 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5606 | |
| 5607 | free_mmacro(defining); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5608 | defining = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5609 | } |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5610 | |
| 5611 | nasm_set_verror(real_verror); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5612 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5613 | while (cstk) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5614 | ctx_pop(); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 5615 | free_macros(); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5616 | while (istk) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5617 | Include *i = istk; |
| 5618 | istk = istk->next; |
| 5619 | fclose(i->fp); |
Cyrill Gorcunov | 8dcfd88 | 2011-03-03 09:18:56 +0300 | [diff] [blame] | 5620 | nasm_free(i); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5621 | } |
| 5622 | while (cstk) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5623 | ctx_pop(); |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 5624 | src_set_fname(NULL); |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5625 | } |
| 5626 | |
| 5627 | static void pp_cleanup_session(void) |
| 5628 | { |
| 5629 | free_llist(predef); |
| 5630 | predef = NULL; |
| 5631 | delete_Blocks(); |
| 5632 | freeTokens = NULL; |
| 5633 | ipath_list = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5634 | } |
| 5635 | |
Cyrill Gorcunov | 8c0666b | 2018-11-24 14:33:48 +0300 | [diff] [blame] | 5636 | static void pp_include_path(struct strlist *list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5637 | { |
Cyrill Gorcunov | 8c0666b | 2018-11-24 14:33:48 +0300 | [diff] [blame] | 5638 | ipath_list = list; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5639 | } |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5640 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5641 | static void pp_pre_include(char *fname) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5642 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5643 | Token *inc, *space, *name; |
| 5644 | Line *l; |
| 5645 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5646 | name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0); |
| 5647 | space = new_Token(name, TOK_WHITESPACE, NULL, 0); |
| 5648 | inc = new_Token(space, TOK_PREPROC_ID, "%include", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5649 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5650 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5651 | l->next = predef; |
| 5652 | l->first = inc; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5653 | l->finishes = NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5654 | predef = l; |
| 5655 | } |
| 5656 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5657 | static void pp_pre_define(char *definition) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5658 | { |
| 5659 | Token *def, *space; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5660 | Line *l; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5661 | char *equals; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5662 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5663 | real_verror = nasm_set_verror(pp_verror); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5664 | |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5665 | equals = strchr(definition, '='); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5666 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 5667 | def = new_Token(space, TOK_PREPROC_ID, "%define", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5668 | if (equals) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5669 | *equals = ' '; |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5670 | space->next = tokenize(definition); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5671 | if (equals) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5672 | *equals = '='; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5673 | |
Cyrill Gorcunov | 6d42e9b | 2015-02-08 11:07:17 +0300 | [diff] [blame] | 5674 | if (space->next->type != TOK_PREPROC_ID && |
| 5675 | space->next->type != TOK_ID) |
H. Peter Anvin (Intel) | 80c4f23 | 2018-12-14 13:33:24 -0800 | [diff] [blame] | 5676 | nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition); |
Cyrill Gorcunov | 6d42e9b | 2015-02-08 11:07:17 +0300 | [diff] [blame] | 5677 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5678 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5679 | l->next = predef; |
| 5680 | l->first = def; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5681 | l->finishes = NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5682 | predef = l; |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5683 | |
| 5684 | nasm_set_verror(real_verror); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5685 | } |
| 5686 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5687 | static void pp_pre_undefine(char *definition) |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5688 | { |
| 5689 | Token *def, *space; |
| 5690 | Line *l; |
| 5691 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5692 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 5693 | def = new_Token(space, TOK_PREPROC_ID, "%undef", 0); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5694 | space->next = tokenize(definition); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [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 | 620515a | 2002-04-30 20:57:38 +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 | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5700 | predef = l; |
| 5701 | } |
| 5702 | |
H. Peter Anvin | 0599034 | 2018-06-11 13:32:42 -0700 | [diff] [blame] | 5703 | /* Insert an early preprocessor command that doesn't need special handling */ |
| 5704 | static void pp_pre_command(const char *what, char *string) |
| 5705 | { |
| 5706 | char *cmd; |
| 5707 | Token *def, *space; |
| 5708 | Line *l; |
| 5709 | |
| 5710 | def = tokenize(string); |
| 5711 | if (what) { |
| 5712 | cmd = nasm_strcat(what[0] == '%' ? "" : "%", what); |
| 5713 | space = new_Token(def, TOK_WHITESPACE, NULL, 0); |
| 5714 | def = new_Token(space, TOK_PREPROC_ID, cmd, 0); |
| 5715 | } |
| 5716 | |
| 5717 | l = nasm_malloc(sizeof(Line)); |
| 5718 | l->next = predef; |
| 5719 | l->first = def; |
| 5720 | l->finishes = NULL; |
| 5721 | predef = l; |
| 5722 | } |
| 5723 | |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 5724 | static void pp_add_stdmac(macros_t *macros) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5725 | { |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 5726 | macros_t **mp; |
| 5727 | |
| 5728 | /* Find the end of the list and avoid duplicates */ |
| 5729 | for (mp = stdmacros; *mp; mp++) { |
| 5730 | if (*mp == macros) |
| 5731 | return; /* Nothing to do */ |
| 5732 | } |
| 5733 | |
| 5734 | nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]); |
| 5735 | |
| 5736 | *mp = macros; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 5737 | } |
| 5738 | |
Cyrill Gorcunov | 15ce78f | 2017-01-06 20:21:28 +0300 | [diff] [blame] | 5739 | static void pp_extra_stdmac(macros_t *macros) |
| 5740 | { |
| 5741 | extrastdmac = macros; |
| 5742 | } |
| 5743 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5744 | static Token *make_tok_num(int64_t val) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5745 | { |
Cyrill Gorcunov | ce65274 | 2013-05-06 23:43:43 +0400 | [diff] [blame] | 5746 | char numbuf[32]; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5747 | int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val); |
| 5748 | return new_Token(NULL, TOK_NUMBER, numbuf, len); |
| 5749 | } |
| 5750 | |
| 5751 | static Token *make_tok_qstr(const char *str) |
| 5752 | { |
| 5753 | Token *t = new_Token(NULL, TOK_STRING, NULL, 0); |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 5754 | t->text = nasm_quote_cstr(str, &t->len); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5755 | return t; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5756 | } |
| 5757 | |
H. Peter Anvin (Intel) | 6bde2ed | 2018-12-13 19:39:41 -0800 | [diff] [blame] | 5758 | static void pp_list_one_macro(MMacro *m, errflags severity) |
H. Peter Anvin | 3736895 | 2016-05-09 14:10:32 -0700 | [diff] [blame] | 5759 | { |
| 5760 | if (!m) |
| 5761 | return; |
| 5762 | |
| 5763 | /* We need to print the next_active list in reverse order */ |
| 5764 | pp_list_one_macro(m->next_active, severity); |
| 5765 | |
| 5766 | if (m->name && !m->nolist) { |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 5767 | src_set(m->xline + m->lineno, m->fname); |
H. Peter Anvin | ddb2906 | 2018-12-11 00:06:29 -0800 | [diff] [blame] | 5768 | nasm_error(severity, "... from macro `%s' defined", m->name); |
H. Peter Anvin | 3736895 | 2016-05-09 14:10:32 -0700 | [diff] [blame] | 5769 | } |
| 5770 | } |
| 5771 | |
H. Peter Anvin (Intel) | 6bde2ed | 2018-12-13 19:39:41 -0800 | [diff] [blame] | 5772 | static void pp_error_list_macros(errflags severity) |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5773 | { |
H. Peter Anvin | ddb2906 | 2018-12-11 00:06:29 -0800 | [diff] [blame] | 5774 | struct src_location saved; |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5775 | |
H. Peter Anvin | ddb2906 | 2018-12-11 00:06:29 -0800 | [diff] [blame] | 5776 | severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE; |
| 5777 | saved = src_where(); |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5778 | |
Cyrill Gorcunov | 771d04e | 2016-05-10 23:27:03 +0300 | [diff] [blame] | 5779 | if (istk) |
| 5780 | pp_list_one_macro(istk->mstk, severity); |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5781 | |
H. Peter Anvin | ddb2906 | 2018-12-11 00:06:29 -0800 | [diff] [blame] | 5782 | src_update(saved); |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5783 | } |
| 5784 | |
H. Peter Anvin | e746971 | 2016-02-18 02:20:59 -0800 | [diff] [blame] | 5785 | const struct preproc_ops nasmpp = { |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 5786 | pp_init, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5787 | pp_reset, |
| 5788 | pp_getline, |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5789 | pp_cleanup_pass, |
| 5790 | pp_cleanup_session, |
Cyrill Gorcunov | 15ce78f | 2017-01-06 20:21:28 +0300 | [diff] [blame] | 5791 | pp_extra_stdmac, |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5792 | pp_pre_define, |
| 5793 | pp_pre_undefine, |
| 5794 | pp_pre_include, |
H. Peter Anvin | 0599034 | 2018-06-11 13:32:42 -0700 | [diff] [blame] | 5795 | pp_pre_command, |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5796 | pp_include_path, |
| 5797 | pp_error_list_macros, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5798 | }; |