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; |
| 187 | uint32_t number; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 188 | }; |
| 189 | |
| 190 | /* |
| 191 | * This is the internal form which we break input lines up into. |
| 192 | * Typically stored in linked lists. |
| 193 | * |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 194 | * Note that `type' serves a double meaning: TOK_SMAC_START_PARAMS is |
| 195 | * not necessarily used as-is, but is also used to encode the number |
| 196 | * and expansion type of substituted parameter. So in the definition |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 197 | * |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 198 | * %define a(x,=y) ( (x) & ~(y) ) |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 199 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 200 | * the token representing `x' will have its type changed to |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 201 | * tok_smac_param(0) but the one representing `y' will be |
| 202 | * tok_smac_param(1); see the accessor functions below. |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 203 | * |
| 204 | * TOK_INTERNAL_STRING is a dirty hack: it's a single string token |
| 205 | * which doesn't need quotes around it. Used in the pre-include |
| 206 | * mechanism as an alternative to trying to find a sensible type of |
| 207 | * quote to use on the filename we were passed. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 208 | */ |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 209 | enum pp_token_type { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 210 | TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID, |
| 211 | TOK_PREPROC_ID, TOK_STRING, |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 212 | TOK_NUMBER, TOK_FLOAT, TOK_OTHER, |
H. Peter Anvin | 6c81f0a | 2008-05-25 21:46:17 -0700 | [diff] [blame] | 213 | TOK_INTERNAL_STRING, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 214 | TOK_PREPROC_Q, TOK_PREPROC_QQ, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 215 | TOK_PASTE, /* %+ */ |
| 216 | TOK_INDIRECT, /* %[...] */ |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 217 | TOK_SMAC_START_PARAMS, /* MUST BE LAST IN THE LIST!!! */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 218 | TOK_MAX = INT_MAX /* Keep compiler from reducing the range */ |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 219 | }; |
| 220 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 221 | static inline enum pp_token_type tok_smac_param(int param) |
| 222 | { |
| 223 | return TOK_SMAC_START_PARAMS + param; |
| 224 | } |
| 225 | static int smac_nparam(enum pp_token_type toktype) |
| 226 | { |
| 227 | return toktype - TOK_SMAC_START_PARAMS; |
| 228 | } |
| 229 | static bool is_smac_param(enum pp_token_type toktype) |
| 230 | { |
| 231 | return toktype >= TOK_SMAC_START_PARAMS; |
| 232 | } |
| 233 | |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 234 | #define PP_CONCAT_MASK(x) (1 << (x)) |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 235 | #define PP_CONCAT_MATCH(t, mask) (PP_CONCAT_MASK((t)->type) & mask) |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 236 | |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 237 | struct tokseq_match { |
| 238 | int mask_head; |
| 239 | int mask_tail; |
| 240 | }; |
| 241 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 242 | struct Token { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 243 | Token *next; |
| 244 | char *text; |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 245 | size_t len; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 246 | enum pp_token_type type; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 247 | }; |
| 248 | |
| 249 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 250 | * Multi-line macro definitions are stored as a linked list of |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 251 | * these, which is essentially a container to allow several linked |
| 252 | * lists of Tokens. |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 253 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 254 | * Note that in this module, linked lists are treated as stacks |
| 255 | * wherever possible. For this reason, Lines are _pushed_ on to the |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 256 | * `expansion' field in MMacro structures, so that the linked list, |
| 257 | * if walked, would give the macro lines in reverse order; this |
| 258 | * means that we can walk the list when expanding a macro, and thus |
| 259 | * push the lines on to the `expansion' field in _istk_ in reverse |
| 260 | * order (so that when popped back off they are in the right |
| 261 | * order). It may seem cockeyed, and it relies on my design having |
| 262 | * an even number of steps in, but it works... |
| 263 | * |
| 264 | * Some of these structures, rather than being actual lines, are |
| 265 | * markers delimiting the end of the expansion of a given macro. |
| 266 | * This is for use in the cycle-tracking and %rep-handling code. |
| 267 | * Such structures have `finishes' non-NULL, and `first' NULL. All |
| 268 | * others have `finishes' NULL, but `first' may still be NULL if |
| 269 | * the line is blank. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 270 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 271 | struct Line { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 272 | Line *next; |
| 273 | MMacro *finishes; |
| 274 | Token *first; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 275 | }; |
| 276 | |
| 277 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 278 | * To handle an arbitrary level of file inclusion, we maintain a |
| 279 | * stack (ie linked list) of these things. |
| 280 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 281 | struct Include { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 282 | Include *next; |
| 283 | FILE *fp; |
| 284 | Cond *conds; |
| 285 | Line *expansion; |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 286 | const char *fname; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 287 | int lineno, lineinc; |
| 288 | MMacro *mstk; /* stack of active macros/reps */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 289 | }; |
| 290 | |
| 291 | /* |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 292 | * File real name hash, so we don't have to re-search the include |
| 293 | * path for every pass (and potentially more than that if a file |
| 294 | * is used more than once.) |
| 295 | */ |
| 296 | struct hash_table FileHash; |
| 297 | |
| 298 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 299 | * Conditional assembly: we maintain a separate stack of these for |
| 300 | * each level of file inclusion. (The only reason we keep the |
| 301 | * stacks separate is to ensure that a stray `%endif' in a file |
| 302 | * included from within the true branch of a `%if' won't terminate |
| 303 | * it and cause confusion: instead, rightly, it'll cause an error.) |
| 304 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 305 | struct Cond { |
| 306 | Cond *next; |
| 307 | int state; |
| 308 | }; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 309 | enum { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 310 | /* |
| 311 | * These states are for use just after %if or %elif: IF_TRUE |
| 312 | * means the condition has evaluated to truth so we are |
| 313 | * currently emitting, whereas IF_FALSE means we are not |
| 314 | * currently emitting but will start doing so if a %else comes |
| 315 | * up. In these states, all directives are admissible: %elif, |
| 316 | * %else and %endif. (And of course %if.) |
| 317 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 318 | COND_IF_TRUE, COND_IF_FALSE, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 319 | /* |
| 320 | * These states come up after a %else: ELSE_TRUE means we're |
| 321 | * emitting, and ELSE_FALSE means we're not. In ELSE_* states, |
| 322 | * any %elif or %else will cause an error. |
| 323 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 324 | COND_ELSE_TRUE, COND_ELSE_FALSE, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 325 | /* |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 326 | * These states mean that we're not emitting now, and also that |
| 327 | * nothing until %endif will be emitted at all. COND_DONE is |
| 328 | * used when we've had our moment of emission |
| 329 | * and have now started seeing %elifs. COND_NEVER is used when |
| 330 | * the condition construct in question is contained within a |
| 331 | * non-emitting branch of a larger condition construct, |
| 332 | * or if there is an error. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 333 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 334 | COND_DONE, COND_NEVER |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 335 | }; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 336 | #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE ) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 337 | |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 338 | /* |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 339 | * These defines are used as the possible return values for do_directive |
| 340 | */ |
| 341 | #define NO_DIRECTIVE_FOUND 0 |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 342 | #define DIRECTIVE_FOUND 1 |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 343 | |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 344 | /* max reps */ |
| 345 | #define REP_LIMIT ((INT64_C(1) << 62)) |
| 346 | |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 347 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 348 | * Condition codes. Note that we use c_ prefix not C_ because C_ is |
| 349 | * used in nasm.h for the "real" condition codes. At _this_ level, |
| 350 | * we treat CXZ and ECXZ as condition codes, albeit non-invertible |
| 351 | * ones, so we need a different enum... |
| 352 | */ |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 353 | static const char * const conditions[] = { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 354 | "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le", |
| 355 | "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no", |
H. Peter Anvin | ce9be34 | 2007-09-12 00:22:29 +0000 | [diff] [blame] | 356 | "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z" |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 357 | }; |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 358 | enum pp_conds { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 359 | c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE, |
| 360 | 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] | 361 | c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z, |
| 362 | c_none = -1 |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 363 | }; |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 364 | static const enum pp_conds inverse_ccs[] = { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 365 | c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE, |
| 366 | 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] | 367 | 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] | 368 | }; |
| 369 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 370 | /* |
| 371 | * Directive names. |
| 372 | */ |
| 373 | /* If this is a an IF, ELIF, ELSE or ENDIF keyword */ |
| 374 | static int is_condition(enum preproc_token arg) |
| 375 | { |
| 376 | return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF); |
| 377 | } |
| 378 | |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 379 | /* For TASM compatibility we need to be able to recognise TASM compatible |
| 380 | * conditional compilation directives. Using the NASM pre-processor does |
| 381 | * not work, so we look for them specifically from the following list and |
| 382 | * then jam in the equivalent NASM directive into the input stream. |
| 383 | */ |
| 384 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 385 | enum { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 386 | TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI, |
| 387 | TM_IFNDEF, TM_INCLUDE, TM_LOCAL |
| 388 | }; |
| 389 | |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 390 | static const char * const tasm_directives[] = { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 391 | "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi", |
| 392 | "ifndef", "include", "local" |
| 393 | }; |
| 394 | |
| 395 | static int StackSize = 4; |
H. Peter Anvin | 6c8b2be | 2016-05-24 23:46:50 -0700 | [diff] [blame] | 396 | static const char *StackPointer = "ebp"; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 397 | static int ArgOffset = 8; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 398 | static int LocalOffset = 0; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 399 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 400 | static Context *cstk; |
| 401 | static Include *istk; |
Cyrill Gorcunov | 8c0666b | 2018-11-24 14:33:48 +0300 | [diff] [blame] | 402 | static const struct strlist *ipath_list; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 403 | |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 404 | static struct strlist *deplist; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 405 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 406 | static uint64_t unique; /* unique identifier numbers */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 407 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 408 | static Line *predef = NULL; |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 409 | static bool do_predef; |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 410 | static enum preproc_mode pp_mode; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 411 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 412 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 413 | * The current set of multi-line macros we have defined. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 414 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 415 | static struct hash_table mmacros; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 416 | |
| 417 | /* |
| 418 | * The current set of single-line macros we have defined. |
| 419 | */ |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 420 | static struct hash_table smacros; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 421 | |
| 422 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 423 | * The multi-line macro we are currently defining, or the %rep |
| 424 | * block we are currently reading, if any. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 425 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 426 | static MMacro *defining; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 427 | |
Charles Crayne | d4200be | 2008-07-12 16:42:33 -0700 | [diff] [blame] | 428 | static uint64_t nested_mac_count; |
| 429 | static uint64_t nested_rep_count; |
| 430 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 431 | /* |
| 432 | * The number of macro parameters to allocate space for at a time. |
| 433 | */ |
| 434 | #define PARAM_DELTA 16 |
| 435 | |
| 436 | /* |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 437 | * The standard macro set: defined in macros.c in a set of arrays. |
| 438 | * This gives our position in any macro set, while we are processing it. |
| 439 | * The stdmacset is an array of such macro sets. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 440 | */ |
H. Peter Anvin | a70547f | 2008-07-19 21:44:26 -0700 | [diff] [blame] | 441 | static macros_t *stdmacpos; |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 442 | static macros_t **stdmacnext; |
| 443 | static macros_t *stdmacros[8]; |
Cyrill Gorcunov | 15ce78f | 2017-01-06 20:21:28 +0300 | [diff] [blame] | 444 | static macros_t *extrastdmac; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 445 | |
| 446 | /* |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 447 | * Tokens are allocated in blocks to improve speed |
| 448 | */ |
| 449 | #define TOKEN_BLOCKSIZE 4096 |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 450 | static Token *freeTokens = NULL; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 451 | struct Blocks { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 452 | Blocks *next; |
| 453 | void *chunk; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 454 | }; |
| 455 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 456 | static Blocks blocks = { NULL, NULL }; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 457 | |
| 458 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 459 | * Forward declarations. |
| 460 | */ |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 461 | static void pp_add_stdmac(macros_t *macros); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 462 | static Token *expand_mmac_params(Token * tline); |
| 463 | static Token *expand_smacro(Token * tline); |
| 464 | static Token *expand_id(Token * tline); |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 465 | static Context *get_ctx(const char *name, const char **namep); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 466 | static Token *make_tok_num(int64_t val); |
| 467 | static Token *make_tok_qstr(const char *str); |
H. Peter Anvin (Intel) | 6bde2ed | 2018-12-13 19:39:41 -0800 | [diff] [blame] | 468 | 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] | 469 | static vefunc real_verror; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 470 | static void *new_Block(size_t size); |
| 471 | static void delete_Blocks(void); |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 472 | 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] | 473 | const char *text, size_t txtlen); |
| 474 | static Token *dup_Token(Token *next, const Token *src); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 475 | static Token *delete_Token(Token * t); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 476 | |
| 477 | /* |
| 478 | * Macros for safe checking of token pointers, avoid *(NULL) |
| 479 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 480 | #define tok_type_(x,t) ((x) && (x)->type == (t)) |
| 481 | #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next |
| 482 | #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v))) |
| 483 | #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] | 484 | |
Cyrill Gorcunov | 194ba89 | 2011-06-30 01:16:35 +0400 | [diff] [blame] | 485 | /* |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 486 | * In-place reverse a list of tokens. |
| 487 | */ |
| 488 | static Token *reverse_tokens(Token *t) |
| 489 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 490 | Token *prev = NULL; |
| 491 | Token *next; |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 492 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 493 | while (t) { |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 494 | next = t->next; |
| 495 | t->next = prev; |
| 496 | prev = t; |
| 497 | t = next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 498 | } |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 499 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 500 | return prev; |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | /* |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 504 | * Handle TASM specific directives, which do not contain a % in |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 505 | * front of them. We do it here because I could not find any other |
| 506 | * place to do it for the moment, and it is a hack (ideally it would |
| 507 | * be nice to be able to use the NASM pre-processor to do it). |
| 508 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 509 | static char *check_tasm_directive(char *line) |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 510 | { |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 511 | int32_t i, j, k, m, len; |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 512 | char *p, *q, *oldline, oldchar; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 513 | |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 514 | p = nasm_skip_spaces(line); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 515 | |
| 516 | /* Binary search for the directive name */ |
| 517 | i = -1; |
Cyrill Gorcunov | a731924 | 2010-06-03 22:04:36 +0400 | [diff] [blame] | 518 | j = ARRAY_SIZE(tasm_directives); |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 519 | q = nasm_skip_word(p); |
| 520 | len = q - p; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 521 | if (len) { |
| 522 | oldchar = p[len]; |
| 523 | p[len] = 0; |
| 524 | while (j - i > 1) { |
| 525 | k = (j + i) / 2; |
| 526 | m = nasm_stricmp(p, tasm_directives[k]); |
| 527 | if (m == 0) { |
| 528 | /* We have found a directive, so jam a % in front of it |
| 529 | * so that NASM will then recognise it as one if it's own. |
| 530 | */ |
| 531 | p[len] = oldchar; |
| 532 | len = strlen(p); |
| 533 | oldline = line; |
| 534 | line = nasm_malloc(len + 2); |
| 535 | line[0] = '%'; |
| 536 | if (k == TM_IFDIFI) { |
H. Peter Anvin | 18f4879 | 2009-06-27 15:56:27 -0700 | [diff] [blame] | 537 | /* |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 538 | * NASM does not recognise IFDIFI, so we convert |
| 539 | * it to %if 0. This is not used in NASM |
| 540 | * compatible code, but does need to parse for the |
| 541 | * TASM macro package. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 542 | */ |
H. Peter Anvin | 18f4879 | 2009-06-27 15:56:27 -0700 | [diff] [blame] | 543 | strcpy(line + 1, "if 0"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 544 | } else { |
| 545 | memcpy(line + 1, p, len + 1); |
| 546 | } |
| 547 | nasm_free(oldline); |
| 548 | return line; |
| 549 | } else if (m < 0) { |
| 550 | j = k; |
| 551 | } else |
| 552 | i = k; |
| 553 | } |
| 554 | p[len] = oldchar; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 555 | } |
| 556 | return line; |
| 557 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 558 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 559 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 560 | * The pre-preprocessing stage... This function translates line |
| 561 | * number indications as they emerge from GNU cpp (`# lineno "file" |
| 562 | * flags') into NASM preprocessor line number indications (`%line |
| 563 | * lineno file'). |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 564 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 565 | static char *prepreproc(char *line) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 566 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 567 | int lineno, fnlen; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 568 | char *fname, *oldline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 569 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 570 | if (line[0] == '#' && line[1] == ' ') { |
| 571 | oldline = line; |
| 572 | fname = oldline + 2; |
| 573 | lineno = atoi(fname); |
| 574 | fname += strspn(fname, "0123456789 "); |
| 575 | if (*fname == '"') |
| 576 | fname++; |
| 577 | fnlen = strcspn(fname, "\""); |
| 578 | line = nasm_malloc(20 + fnlen); |
| 579 | snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname); |
| 580 | nasm_free(oldline); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 581 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 582 | if (tasm_compatible_mode) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 583 | return check_tasm_directive(line); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 584 | return line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 588 | * Free a linked list of tokens. |
| 589 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 590 | static void free_tlist(Token * list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 591 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 592 | while (list) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 593 | list = delete_Token(list); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | /* |
| 597 | * Free a linked list of lines. |
| 598 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 599 | static void free_llist(Line * list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 600 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 601 | Line *l, *tmp; |
| 602 | list_for_each_safe(l, tmp, list) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 603 | free_tlist(l->first); |
| 604 | nasm_free(l); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | |
| 608 | /* |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 609 | * Free an array of linked lists of tokens |
| 610 | */ |
| 611 | static void free_tlist_array(Token **array, size_t nlists) |
| 612 | { |
| 613 | Token **listp = array; |
| 614 | |
| 615 | while (nlists--) |
| 616 | free_tlist(*listp++); |
| 617 | |
| 618 | nasm_free(array); |
| 619 | } |
| 620 | |
| 621 | /* |
| 622 | * Duplicate a linked list of tokens. |
| 623 | */ |
| 624 | static Token *dup_tlist(const Token *list, Token ***tailp) |
| 625 | { |
| 626 | Token *newlist = NULL; |
| 627 | Token **tailpp = &newlist; |
| 628 | const Token *t; |
| 629 | |
| 630 | list_for_each(t, list) { |
| 631 | Token *nt; |
| 632 | *tailpp = nt = dup_Token(NULL, t); |
| 633 | tailpp = &nt->next; |
| 634 | } |
| 635 | |
| 636 | if (tailp) |
| 637 | *tailp = tailpp; |
| 638 | |
| 639 | return newlist; |
| 640 | } |
| 641 | |
| 642 | /* |
| 643 | * Duplicate a linked list of tokens in reverse order |
| 644 | */ |
| 645 | static Token *dup_tlist_reverse(const Token *list, Token *tail) |
| 646 | { |
| 647 | const Token *t; |
| 648 | |
| 649 | list_for_each(t, list) |
| 650 | tail = dup_Token(tail, t); |
| 651 | |
| 652 | return tail; |
| 653 | } |
| 654 | |
| 655 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 656 | * Free an MMacro |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 657 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 658 | static void free_mmacro(MMacro * m) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 659 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 660 | nasm_free(m->name); |
| 661 | free_tlist(m->dlist); |
| 662 | nasm_free(m->defaults); |
| 663 | free_llist(m->expansion); |
| 664 | nasm_free(m); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | /* |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 668 | * Clear or free an SMacro |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 669 | */ |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 670 | static void free_smacro_members(SMacro *s) |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 671 | { |
| 672 | nasm_free(s->name); |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 673 | free_tlist(s->expansion); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 674 | nasm_free(s->eval_param); |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | static void clear_smacro(SMacro *s) |
| 678 | { |
| 679 | free_smacro_members(s); |
| 680 | /* Wipe everything except the next pointer */ |
| 681 | memset(&s->next + 1, 0, sizeof *s - sizeof s->next); |
| 682 | } |
| 683 | |
| 684 | /* |
| 685 | * Free an SMacro |
| 686 | */ |
| 687 | static void free_smacro(SMacro *s) |
| 688 | { |
| 689 | free_smacro_members(s); |
| 690 | nasm_free(s); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | /* |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 694 | * Free all currently defined macros, and free the hash tables |
| 695 | */ |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 696 | static void free_smacro_table(struct hash_table *smt) |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 697 | { |
H. Peter Anvin (Intel) | ebb05a0 | 2018-12-11 12:30:25 -0800 | [diff] [blame] | 698 | struct hash_iterator it; |
| 699 | const struct hash_node *np; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 700 | |
H. Peter Anvin (Intel) | ebb05a0 | 2018-12-11 12:30:25 -0800 | [diff] [blame] | 701 | hash_for_each(smt, it, np) { |
| 702 | SMacro *tmp; |
| 703 | SMacro *s = np->data; |
| 704 | nasm_free((void *)np->key); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 705 | list_for_each_safe(s, tmp, s) |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 706 | free_smacro(s); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 707 | } |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 708 | hash_free(smt); |
| 709 | } |
| 710 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 711 | static void free_mmacro_table(struct hash_table *mmt) |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 712 | { |
H. Peter Anvin (Intel) | ebb05a0 | 2018-12-11 12:30:25 -0800 | [diff] [blame] | 713 | struct hash_iterator it; |
| 714 | const struct hash_node *np; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 715 | |
H. Peter Anvin (Intel) | ebb05a0 | 2018-12-11 12:30:25 -0800 | [diff] [blame] | 716 | hash_for_each(mmt, it, np) { |
| 717 | MMacro *tmp; |
| 718 | MMacro *m = np->data; |
| 719 | nasm_free((void *)np->key); |
| 720 | list_for_each_safe(m, tmp, m) |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 721 | free_mmacro(m); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 722 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 723 | hash_free(mmt); |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | static void free_macros(void) |
| 727 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 728 | free_smacro_table(&smacros); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 729 | free_mmacro_table(&mmacros); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | /* |
| 733 | * Initialize the hash tables |
| 734 | */ |
| 735 | static void init_macros(void) |
| 736 | { |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 740 | * Pop the context stack. |
| 741 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 742 | static void ctx_pop(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 743 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 744 | Context *c = cstk; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 745 | |
| 746 | cstk = cstk->next; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 747 | free_smacro_table(&c->localmac); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 748 | nasm_free(c->name); |
| 749 | nasm_free(c); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 750 | } |
| 751 | |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 752 | /* |
| 753 | * Search for a key in the hash index; adding it if necessary |
| 754 | * (in which case we initialize the data pointer to NULL.) |
| 755 | */ |
| 756 | static void ** |
| 757 | hash_findi_add(struct hash_table *hash, const char *str) |
| 758 | { |
| 759 | struct hash_insert hi; |
| 760 | void **r; |
| 761 | char *strx; |
H. Peter Anvin (Intel) | ebb05a0 | 2018-12-11 12:30:25 -0800 | [diff] [blame] | 762 | size_t l = strlen(str) + 1; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 763 | |
H. Peter Anvin (Intel) | ebb05a0 | 2018-12-11 12:30:25 -0800 | [diff] [blame] | 764 | r = hash_findib(hash, str, l, &hi); |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 765 | if (r) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 766 | return r; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 767 | |
H. Peter Anvin (Intel) | ebb05a0 | 2018-12-11 12:30:25 -0800 | [diff] [blame] | 768 | strx = nasm_malloc(l); /* Use a more efficient allocator here? */ |
| 769 | memcpy(strx, str, l); |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 770 | return hash_add(&hi, strx, NULL); |
| 771 | } |
| 772 | |
| 773 | /* |
| 774 | * Like hash_findi, but returns the data element rather than a pointer |
| 775 | * to it. Used only when not adding a new element, hence no third |
| 776 | * argument. |
| 777 | */ |
| 778 | static void * |
| 779 | hash_findix(struct hash_table *hash, const char *str) |
| 780 | { |
| 781 | void **p; |
| 782 | |
| 783 | p = hash_findi(hash, str, NULL); |
| 784 | return p ? *p : NULL; |
| 785 | } |
| 786 | |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 787 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 788 | * read line from standart macros set, |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 789 | * if there no more left -- return NULL |
| 790 | */ |
| 791 | static char *line_from_stdmac(void) |
| 792 | { |
| 793 | unsigned char c; |
| 794 | const unsigned char *p = stdmacpos; |
| 795 | char *line, *q; |
| 796 | size_t len = 0; |
| 797 | |
| 798 | if (!stdmacpos) |
| 799 | return NULL; |
| 800 | |
| 801 | while ((c = *p++)) { |
| 802 | if (c >= 0x80) |
| 803 | len += pp_directives_len[c - 0x80] + 1; |
| 804 | else |
| 805 | len++; |
| 806 | } |
| 807 | |
| 808 | line = nasm_malloc(len + 1); |
| 809 | q = line; |
| 810 | while ((c = *stdmacpos++)) { |
| 811 | if (c >= 0x80) { |
| 812 | memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]); |
| 813 | q += pp_directives_len[c - 0x80]; |
| 814 | *q++ = ' '; |
| 815 | } else { |
| 816 | *q++ = c; |
| 817 | } |
| 818 | } |
| 819 | stdmacpos = p; |
| 820 | *q = '\0'; |
| 821 | |
| 822 | if (!*stdmacpos) { |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 823 | /* This was the last of this particular macro set */ |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 824 | stdmacpos = NULL; |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 825 | if (*stdmacnext) { |
| 826 | stdmacpos = *stdmacnext++; |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 827 | } else if (do_predef) { |
| 828 | Line *pd, *l; |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 829 | |
| 830 | /* |
| 831 | * Nasty hack: here we push the contents of |
| 832 | * `predef' on to the top-level expansion stack, |
| 833 | * since this is the most convenient way to |
| 834 | * implement the pre-include and pre-define |
| 835 | * features. |
| 836 | */ |
| 837 | list_for_each(pd, predef) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 838 | l = nasm_malloc(sizeof(Line)); |
| 839 | l->next = istk->expansion; |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 840 | l->first = dup_tlist(pd->first, NULL); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 841 | l->finishes = NULL; |
| 842 | |
| 843 | istk->expansion = l; |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 844 | } |
| 845 | do_predef = false; |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | return line; |
| 850 | } |
| 851 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 852 | static char *read_line(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 853 | { |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 854 | int c; |
| 855 | unsigned int size, next; |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 856 | const unsigned int delta = 512; |
| 857 | const unsigned int pad = 8; |
| 858 | unsigned int nr_cont = 0; |
| 859 | bool cont = false; |
| 860 | char *buffer, *p; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 861 | |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 862 | /* Standart macros set (predefined) goes first */ |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 863 | p = line_from_stdmac(); |
| 864 | if (p) |
| 865 | return p; |
H. Peter Anvin | 72edbb8 | 2008-06-19 16:00:04 -0700 | [diff] [blame] | 866 | |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 867 | size = delta; |
| 868 | p = buffer = nasm_malloc(size); |
| 869 | |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 870 | do { |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 871 | c = fgetc(istk->fp); |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 872 | |
| 873 | switch (c) { |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 874 | case EOF: |
| 875 | if (p == buffer) { |
| 876 | nasm_free(buffer); |
| 877 | return NULL; |
| 878 | } |
| 879 | c = 0; |
| 880 | break; |
| 881 | |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 882 | case '\r': |
| 883 | next = fgetc(istk->fp); |
| 884 | if (next != '\n') |
| 885 | ungetc(next, istk->fp); |
| 886 | if (cont) { |
| 887 | cont = false; |
| 888 | continue; |
| 889 | } |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 890 | c = 0; |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 891 | break; |
| 892 | |
| 893 | case '\n': |
| 894 | if (cont) { |
| 895 | cont = false; |
| 896 | continue; |
| 897 | } |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 898 | c = 0; |
| 899 | break; |
| 900 | |
| 901 | case 032: /* ^Z = legacy MS-DOS end of file mark */ |
| 902 | c = 0; |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 903 | break; |
| 904 | |
| 905 | case '\\': |
| 906 | next = fgetc(istk->fp); |
| 907 | ungetc(next, istk->fp); |
Cyrill Gorcunov | 490f85e | 2012-12-27 20:02:17 +0400 | [diff] [blame] | 908 | if (next == '\r' || next == '\n') { |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 909 | cont = true; |
| 910 | nr_cont++; |
| 911 | continue; |
| 912 | } |
| 913 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 914 | } |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 915 | |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 916 | if (p >= (buffer + size - pad)) { |
| 917 | buffer = nasm_realloc(buffer, size + delta); |
| 918 | p = buffer + size - pad; |
| 919 | size += delta; |
| 920 | } |
| 921 | |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 922 | *p++ = c; |
| 923 | } while (c); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 924 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 925 | src_set_linnum(src_get_linnum() + istk->lineinc + |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 926 | (nr_cont * istk->lineinc)); |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 927 | lfmt->line(LIST_READ, buffer); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 928 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 929 | return buffer; |
| 930 | } |
| 931 | |
| 932 | /* |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 933 | * 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] | 934 | * don't need to parse the value out of e.g. numeric tokens: we |
| 935 | * simply split one string into many. |
| 936 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 937 | static Token *tokenize(char *line) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 938 | { |
H. Peter Anvin | ca544db | 2008-10-19 19:30:11 -0700 | [diff] [blame] | 939 | char c, *p = line; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 940 | enum pp_token_type type; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 941 | Token *list = NULL; |
| 942 | Token *t, **tail = &list; |
| 943 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 944 | while (*line) { |
| 945 | p = line; |
| 946 | if (*p == '%') { |
| 947 | p++; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 948 | if (*p == '+' && !nasm_isdigit(p[1])) { |
| 949 | p++; |
| 950 | type = TOK_PASTE; |
| 951 | } else if (nasm_isdigit(*p) || |
| 952 | ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 953 | do { |
| 954 | p++; |
| 955 | } |
H. Peter Anvin | bda7a6e | 2008-06-21 10:23:17 -0700 | [diff] [blame] | 956 | while (nasm_isdigit(*p)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 957 | type = TOK_PREPROC_ID; |
| 958 | } else if (*p == '{') { |
| 959 | p++; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 960 | while (*p) { |
| 961 | if (*p == '}') |
| 962 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 963 | p[-1] = *p; |
| 964 | p++; |
| 965 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 966 | if (*p != '}') |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 967 | nasm_warn(WARN_OTHER, "unterminated %%{ construct"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 968 | p[-1] = '\0'; |
| 969 | if (*p) |
| 970 | p++; |
| 971 | type = TOK_PREPROC_ID; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 972 | } else if (*p == '[') { |
| 973 | int lvl = 1; |
| 974 | line += 2; /* Skip the leading %[ */ |
| 975 | p++; |
| 976 | while (lvl && (c = *p++)) { |
| 977 | switch (c) { |
| 978 | case ']': |
| 979 | lvl--; |
| 980 | break; |
| 981 | case '%': |
| 982 | if (*p == '[') |
| 983 | lvl++; |
| 984 | break; |
| 985 | case '\'': |
| 986 | case '\"': |
| 987 | case '`': |
Cyrill Gorcunov | 3144e84 | 2017-10-22 10:50:55 +0300 | [diff] [blame] | 988 | p = nasm_skip_string(p - 1); |
| 989 | if (*p) |
| 990 | p++; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 991 | break; |
| 992 | default: |
| 993 | break; |
| 994 | } |
| 995 | } |
| 996 | p--; |
| 997 | if (*p) |
| 998 | *p++ = '\0'; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 999 | if (lvl) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1000 | nasm_nonfatalf(ERR_PASS1, "unterminated %%[ construct"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1001 | type = TOK_INDIRECT; |
| 1002 | } else if (*p == '?') { |
| 1003 | type = TOK_PREPROC_Q; /* %? */ |
| 1004 | p++; |
| 1005 | if (*p == '?') { |
| 1006 | type = TOK_PREPROC_QQ; /* %?? */ |
| 1007 | p++; |
| 1008 | } |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1009 | } else if (*p == '!') { |
| 1010 | type = TOK_PREPROC_ID; |
| 1011 | p++; |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 1012 | if (nasm_isidchar(*p)) { |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1013 | do { |
| 1014 | p++; |
| 1015 | } |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 1016 | while (nasm_isidchar(*p)); |
H. Peter Anvin | 53e2e4c | 2018-11-28 15:01:40 -0800 | [diff] [blame] | 1017 | } else if (nasm_isquote(*p)) { |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1018 | p = nasm_skip_string(p); |
| 1019 | if (*p) |
| 1020 | p++; |
| 1021 | else |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1022 | nasm_nonfatalf(ERR_PASS1, "unterminated %%! string"); |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1023 | } else { |
| 1024 | /* %! without string or identifier */ |
| 1025 | type = TOK_OTHER; /* Legacy behavior... */ |
| 1026 | } |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 1027 | } else if (nasm_isidchar(*p) || |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1028 | ((*p == '!' || *p == '%' || *p == '$') && |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 1029 | nasm_isidchar(p[1]))) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1030 | do { |
| 1031 | p++; |
| 1032 | } |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 1033 | while (nasm_isidchar(*p)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1034 | type = TOK_PREPROC_ID; |
| 1035 | } else { |
| 1036 | type = TOK_OTHER; |
| 1037 | if (*p == '%') |
| 1038 | p++; |
| 1039 | } |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 1040 | } else if (nasm_isidstart(*p) || (*p == '$' && nasm_isidstart(p[1]))) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1041 | type = TOK_ID; |
| 1042 | p++; |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 1043 | while (*p && nasm_isidchar(*p)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1044 | p++; |
H. Peter Anvin | 53e2e4c | 2018-11-28 15:01:40 -0800 | [diff] [blame] | 1045 | } else if (nasm_isquote(*p)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1046 | /* |
| 1047 | * A string token. |
| 1048 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1049 | type = TOK_STRING; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1050 | p = nasm_skip_string(p); |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1051 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1052 | if (*p) { |
| 1053 | p++; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1054 | } else { |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 1055 | nasm_warn(WARN_OTHER, "unterminated string"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1056 | /* Handling unterminated strings by UNV */ |
| 1057 | /* type = -1; */ |
| 1058 | } |
Victor van den Elzen | fb5f251 | 2009-04-17 16:17:59 +0200 | [diff] [blame] | 1059 | } else if (p[0] == '$' && p[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1060 | type = TOK_OTHER; /* TOKEN_BASE */ |
Victor van den Elzen | fb5f251 | 2009-04-17 16:17:59 +0200 | [diff] [blame] | 1061 | p += 2; |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 1062 | } else if (nasm_isnumstart(*p)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1063 | bool is_hex = false; |
| 1064 | bool is_float = false; |
| 1065 | bool has_e = false; |
| 1066 | char c, *r; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1067 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1068 | /* |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1069 | * A numeric token. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1070 | */ |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1071 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1072 | if (*p == '$') { |
| 1073 | p++; |
| 1074 | is_hex = true; |
| 1075 | } |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1076 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1077 | for (;;) { |
| 1078 | c = *p++; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1079 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1080 | if (!is_hex && (c == 'e' || c == 'E')) { |
| 1081 | has_e = true; |
| 1082 | if (*p == '+' || *p == '-') { |
| 1083 | /* |
| 1084 | * e can only be followed by +/- if it is either a |
| 1085 | * prefixed hex number or a floating-point number |
| 1086 | */ |
| 1087 | p++; |
| 1088 | is_float = true; |
| 1089 | } |
| 1090 | } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') { |
| 1091 | is_hex = true; |
| 1092 | } else if (c == 'P' || c == 'p') { |
| 1093 | is_float = true; |
| 1094 | if (*p == '+' || *p == '-') |
| 1095 | p++; |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 1096 | } else if (nasm_isnumchar(c)) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1097 | ; /* just advance */ |
| 1098 | else if (c == '.') { |
| 1099 | /* |
| 1100 | * we need to deal with consequences of the legacy |
| 1101 | * parser, like "1.nolist" being two tokens |
| 1102 | * (TOK_NUMBER, TOK_ID) here; at least give it |
| 1103 | * a shot for now. In the future, we probably need |
| 1104 | * a flex-based scanner with proper pattern matching |
| 1105 | * to do it as well as it can be done. Nothing in |
| 1106 | * the world is going to help the person who wants |
| 1107 | * 0x123.p16 interpreted as two tokens, though. |
| 1108 | */ |
| 1109 | r = p; |
| 1110 | while (*r == '_') |
| 1111 | r++; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1112 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1113 | if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) || |
| 1114 | (!is_hex && (*r == 'e' || *r == 'E')) || |
| 1115 | (*r == 'p' || *r == 'P')) { |
| 1116 | p = r; |
| 1117 | is_float = true; |
| 1118 | } else |
| 1119 | break; /* Terminate the token */ |
| 1120 | } else |
| 1121 | break; |
| 1122 | } |
| 1123 | p--; /* Point to first character beyond number */ |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1124 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1125 | if (p == line+1 && *line == '$') { |
| 1126 | type = TOK_OTHER; /* TOKEN_HERE */ |
| 1127 | } else { |
| 1128 | if (has_e && !is_hex) { |
| 1129 | /* 1e13 is floating-point, but 1e13h is not */ |
| 1130 | is_float = true; |
| 1131 | } |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 1132 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1133 | type = is_float ? TOK_FLOAT : TOK_NUMBER; |
| 1134 | } |
H. Peter Anvin | bda7a6e | 2008-06-21 10:23:17 -0700 | [diff] [blame] | 1135 | } else if (nasm_isspace(*p)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1136 | type = TOK_WHITESPACE; |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 1137 | p = nasm_skip_spaces(p); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1138 | /* |
| 1139 | * Whitespace just before end-of-line is discarded by |
| 1140 | * pretending it's a comment; whitespace just before a |
| 1141 | * comment gets lumped into the comment. |
| 1142 | */ |
| 1143 | if (!*p || *p == ';') { |
| 1144 | type = TOK_COMMENT; |
| 1145 | while (*p) |
| 1146 | p++; |
| 1147 | } |
| 1148 | } else if (*p == ';') { |
| 1149 | type = TOK_COMMENT; |
| 1150 | while (*p) |
| 1151 | p++; |
| 1152 | } else { |
| 1153 | /* |
| 1154 | * Anything else is an operator of some kind. We check |
| 1155 | * for all the double-character operators (>>, <<, //, |
| 1156 | * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1157 | * else is a single-character operator. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1158 | */ |
| 1159 | type = TOK_OTHER; |
| 1160 | if ((p[0] == '>' && p[1] == '>') || |
| 1161 | (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++; |
| 1172 | } |
| 1173 | p++; |
| 1174 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1175 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1176 | /* Handling unterminated string by UNV */ |
| 1177 | /*if (type == -1) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1178 | { |
| 1179 | *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1); |
| 1180 | t->text[p-line] = *line; |
| 1181 | tail = &t->next; |
| 1182 | } |
| 1183 | else */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1184 | if (type != TOK_COMMENT) { |
| 1185 | *tail = t = new_Token(NULL, type, line, p - line); |
| 1186 | tail = &t->next; |
| 1187 | } |
| 1188 | line = p; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1189 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1190 | return list; |
| 1191 | } |
| 1192 | |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1193 | /* |
| 1194 | * this function allocates a new managed block of memory and |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1195 | * returns a pointer to the block. The managed blocks are |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1196 | * deleted only all at once by the delete_Blocks function. |
| 1197 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1198 | static void *new_Block(size_t size) |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1199 | { |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1200 | Blocks *b = &blocks; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1201 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1202 | /* first, get to the end of the linked list */ |
| 1203 | while (b->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1204 | b = b->next; |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1205 | /* now allocate the requested chunk */ |
| 1206 | b->chunk = nasm_malloc(size); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1207 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1208 | /* now allocate a new block for the next request */ |
Cyrill Gorcunov | c31767c | 2014-06-28 02:22:17 +0400 | [diff] [blame] | 1209 | b->next = nasm_zalloc(sizeof(Blocks)); |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1210 | return b->chunk; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | /* |
| 1214 | * this function deletes all managed blocks of memory |
| 1215 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1216 | static void delete_Blocks(void) |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1217 | { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1218 | Blocks *a, *b = &blocks; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1219 | |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1220 | /* |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1221 | * keep in mind that the first block, pointed to by blocks |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1222 | * is a static and not dynamically allocated, so we don't |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1223 | * free it. |
| 1224 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1225 | while (b) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1226 | if (b->chunk) |
| 1227 | nasm_free(b->chunk); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1228 | a = b; |
| 1229 | b = b->next; |
| 1230 | if (a != &blocks) |
| 1231 | nasm_free(a); |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1232 | } |
Cyrill Gorcunov | dae24d7 | 2014-06-28 10:17:39 +0400 | [diff] [blame] | 1233 | memset(&blocks, 0, sizeof(blocks)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1234 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1235 | |
| 1236 | /* |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1237 | * 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] | 1238 | * 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] | 1239 | */ |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1240 | 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] | 1241 | const char *text, size_t txtlen) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1242 | { |
| 1243 | Token *t; |
| 1244 | int i; |
| 1245 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1246 | if (!freeTokens) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1247 | freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token)); |
| 1248 | for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++) |
| 1249 | freeTokens[i].next = &freeTokens[i + 1]; |
| 1250 | freeTokens[i].next = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1251 | } |
| 1252 | t = freeTokens; |
| 1253 | freeTokens = t->next; |
| 1254 | t->next = next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1255 | t->type = type; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1256 | if (type == TOK_WHITESPACE || !text) { |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 1257 | t->len = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1258 | t->text = NULL; |
| 1259 | } else { |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 1260 | if (txtlen == 0 && text[0]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1261 | txtlen = strlen(text); |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 1262 | t->len = txtlen; |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1263 | t->text = nasm_malloc(txtlen+1); |
| 1264 | memcpy(t->text, text, txtlen); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1265 | t->text[txtlen] = '\0'; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1266 | } |
| 1267 | return t; |
| 1268 | } |
| 1269 | |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 1270 | static Token *dup_Token(Token *next, const Token *src) |
| 1271 | { |
| 1272 | return new_Token(next, src->type, src->text, src->len); |
| 1273 | } |
| 1274 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1275 | static Token *delete_Token(Token * t) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1276 | { |
| 1277 | Token *next = t->next; |
| 1278 | nasm_free(t->text); |
H. Peter Anvin | 788e6c1 | 2002-04-30 21:02:01 +0000 | [diff] [blame] | 1279 | t->next = freeTokens; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1280 | freeTokens = t; |
| 1281 | return next; |
| 1282 | } |
| 1283 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1284 | /* |
| 1285 | * Convert a line of tokens back into text. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1286 | * If expand_locals is not zero, identifiers of the form "%$*xxx" |
| 1287 | * will be transformed into ..@ctxnum.xxx |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1288 | */ |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 1289 | static char *detoken(Token * tlist, bool expand_locals) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1290 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1291 | Token *t; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1292 | char *line, *p; |
H. Peter Anvin | b4daadc | 2008-01-21 16:31:57 -0800 | [diff] [blame] | 1293 | const char *q; |
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) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1336 | char buffer[40]; |
Keith Kanios | 93f2e9a | 2007-04-14 00:10:59 +0000 | [diff] [blame] | 1337 | snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1338 | p = nasm_strcat(buffer, q); |
| 1339 | nasm_free(t->text); |
| 1340 | t->text = p; |
| 1341 | } |
| 1342 | } |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1343 | if (t->type == TOK_WHITESPACE) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1344 | len++; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1345 | else if (t->text) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1346 | len += strlen(t->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1347 | } |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1348 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1349 | p = line = nasm_malloc(len + 1); |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1350 | |
| 1351 | list_for_each(t, tlist) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1352 | if (t->type == TOK_WHITESPACE) { |
H. Peter Anvin | b4daadc | 2008-01-21 16:31:57 -0800 | [diff] [blame] | 1353 | *p++ = ' '; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1354 | } else if (t->text) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1355 | q = t->text; |
| 1356 | while (*q) |
| 1357 | *p++ = *q++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1358 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1359 | } |
| 1360 | *p = '\0'; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1361 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1362 | return line; |
| 1363 | } |
| 1364 | |
| 1365 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1366 | * A scanner, suitable for use by the expression evaluator, which |
| 1367 | * operates on a line of Tokens. Expects a pointer to a pointer to |
| 1368 | * the first token in the line to be passed in as its private_data |
| 1369 | * field. |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1370 | * |
| 1371 | * FIX: This really needs to be unified with stdscan. |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1372 | */ |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 1373 | struct ppscan { |
| 1374 | Token *tptr; |
| 1375 | int ntokens; |
| 1376 | }; |
| 1377 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1378 | static int ppscan(void *private_data, struct tokenval *tokval) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1379 | { |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 1380 | struct ppscan *pps = private_data; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1381 | Token *tline; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1382 | char ourcopy[MAX_KEYWORD+1], *p, *r, *s; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1383 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1384 | do { |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 1385 | if (pps->ntokens && (tline = pps->tptr)) { |
| 1386 | pps->ntokens--; |
| 1387 | pps->tptr = tline->next; |
| 1388 | } else { |
| 1389 | pps->tptr = NULL; |
| 1390 | pps->ntokens = 0; |
| 1391 | return tokval->t_type = TOKEN_EOS; |
| 1392 | } |
| 1393 | } while (tline->type == TOK_WHITESPACE || tline->type == TOK_COMMENT); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1394 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1395 | tokval->t_charptr = tline->text; |
| 1396 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1397 | if (tline->text[0] == '$' && !tline->text[1]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1398 | return tokval->t_type = TOKEN_HERE; |
H. Peter Anvin | 7cf897e | 2002-05-30 21:30:33 +0000 | [diff] [blame] | 1399 | if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1400 | return tokval->t_type = TOKEN_BASE; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1401 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1402 | if (tline->type == TOK_ID) { |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1403 | p = tokval->t_charptr = tline->text; |
| 1404 | if (p[0] == '$') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1405 | tokval->t_charptr++; |
| 1406 | return tokval->t_type = TOKEN_ID; |
| 1407 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1408 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1409 | for (r = p, s = ourcopy; *r; r++) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1410 | if (r >= p+MAX_KEYWORD) |
| 1411 | return tokval->t_type = TOKEN_ID; /* Not a keyword */ |
H. Peter Anvin | ac8f8fc | 2008-06-11 15:49:41 -0700 | [diff] [blame] | 1412 | *s++ = nasm_tolower(*r); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1413 | } |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1414 | *s = '\0'; |
| 1415 | /* right, so we have an identifier sitting in temp storage. now, |
| 1416 | * is it actually a register or instruction name, or what? */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1417 | return nasm_token_hash(ourcopy, tokval); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1418 | } |
| 1419 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1420 | if (tline->type == TOK_NUMBER) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1421 | bool rn_error; |
| 1422 | tokval->t_integer = readnum(tline->text, &rn_error); |
| 1423 | tokval->t_charptr = tline->text; |
| 1424 | if (rn_error) |
| 1425 | return tokval->t_type = TOKEN_ERRNUM; |
| 1426 | else |
| 1427 | return tokval->t_type = TOKEN_NUM; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1428 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1429 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1430 | if (tline->type == TOK_FLOAT) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1431 | return tokval->t_type = TOKEN_FLOAT; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1432 | } |
| 1433 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1434 | if (tline->type == TOK_STRING) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1435 | char bq, *ep; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1436 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1437 | bq = tline->text[0]; |
H. Peter Anvin | 1162704 | 2008-06-09 20:45:19 -0700 | [diff] [blame] | 1438 | tokval->t_charptr = tline->text; |
| 1439 | tokval->t_inttwo = nasm_unquote(tline->text, &ep); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 1440 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1441 | if (ep[0] != bq || ep[1] != '\0') |
| 1442 | return tokval->t_type = TOKEN_ERRSTR; |
| 1443 | else |
| 1444 | return tokval->t_type = TOKEN_STR; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1447 | if (tline->type == TOK_OTHER) { |
| 1448 | if (!strcmp(tline->text, "<<")) |
| 1449 | return tokval->t_type = TOKEN_SHL; |
| 1450 | if (!strcmp(tline->text, ">>")) |
| 1451 | return tokval->t_type = TOKEN_SHR; |
| 1452 | if (!strcmp(tline->text, "//")) |
| 1453 | return tokval->t_type = TOKEN_SDIV; |
| 1454 | if (!strcmp(tline->text, "%%")) |
| 1455 | return tokval->t_type = TOKEN_SMOD; |
| 1456 | if (!strcmp(tline->text, "==")) |
| 1457 | return tokval->t_type = TOKEN_EQ; |
| 1458 | if (!strcmp(tline->text, "<>")) |
| 1459 | return tokval->t_type = TOKEN_NE; |
| 1460 | if (!strcmp(tline->text, "!=")) |
| 1461 | return tokval->t_type = TOKEN_NE; |
| 1462 | if (!strcmp(tline->text, "<=")) |
| 1463 | return tokval->t_type = TOKEN_LE; |
| 1464 | if (!strcmp(tline->text, ">=")) |
| 1465 | return tokval->t_type = TOKEN_GE; |
| 1466 | if (!strcmp(tline->text, "&&")) |
| 1467 | return tokval->t_type = TOKEN_DBL_AND; |
| 1468 | if (!strcmp(tline->text, "^^")) |
| 1469 | return tokval->t_type = TOKEN_DBL_XOR; |
| 1470 | if (!strcmp(tline->text, "||")) |
| 1471 | return tokval->t_type = TOKEN_DBL_OR; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
| 1474 | /* |
| 1475 | * We have no other options: just return the first character of |
| 1476 | * the token text. |
| 1477 | */ |
| 1478 | return tokval->t_type = tline->text[0]; |
| 1479 | } |
| 1480 | |
| 1481 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1482 | * Compare a string to the name of an existing macro; this is a |
| 1483 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 1484 | * depending on the value of the `casesense' parameter. |
| 1485 | */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 1486 | static int mstrcmp(const char *p, const char *q, bool casesense) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1487 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1488 | return casesense ? strcmp(p, q) : nasm_stricmp(p, q); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1489 | } |
| 1490 | |
| 1491 | /* |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1492 | * Compare a string to the name of an existing macro; this is a |
| 1493 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 1494 | * depending on the value of the `casesense' parameter. |
| 1495 | */ |
| 1496 | static int mmemcmp(const char *p, const char *q, size_t l, bool casesense) |
| 1497 | { |
| 1498 | return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l); |
| 1499 | } |
| 1500 | |
| 1501 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1502 | * Return the Context structure associated with a %$ token. Return |
| 1503 | * NULL, having _already_ reported an error condition, if the |
| 1504 | * context stack isn't deep enough for the supplied number of $ |
| 1505 | * signs. |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1506 | * |
| 1507 | * If "namep" is non-NULL, set it to the pointer to the macro name |
| 1508 | * tail, i.e. the part beyond %$... |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1509 | */ |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1510 | static Context *get_ctx(const char *name, const char **namep) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1511 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1512 | Context *ctx; |
| 1513 | int i; |
| 1514 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1515 | if (namep) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1516 | *namep = name; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1517 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1518 | if (!name || name[0] != '%' || name[1] != '$') |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1519 | return NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1520 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1521 | if (!cstk) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1522 | nasm_nonfatal("`%s': context stack is empty", name); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1523 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1524 | } |
| 1525 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1526 | name += 2; |
| 1527 | ctx = cstk; |
| 1528 | i = 0; |
| 1529 | while (ctx && *name == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1530 | name++; |
| 1531 | i++; |
| 1532 | ctx = ctx->next; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1533 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1534 | if (!ctx) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1535 | nasm_nonfatal("`%s': context stack is only" |
| 1536 | " %d level%s deep", name, i, (i == 1 ? "" : "s")); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1537 | return NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1538 | } |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1539 | |
| 1540 | if (namep) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1541 | *namep = name; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1542 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1543 | return ctx; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1544 | } |
| 1545 | |
| 1546 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1547 | * Open an include file. This routine must always return a valid |
| 1548 | * file pointer if it returns - it's responsible for throwing an |
| 1549 | * ERR_FATAL and bombing out completely if not. It should also try |
| 1550 | * the include path one by one until it finds the file or reaches |
| 1551 | * the end of the path. |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1552 | * |
| 1553 | * Note: for INC_PROBE the function returns NULL at all times; |
| 1554 | * instead look for the |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1555 | */ |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1556 | enum incopen_mode { |
| 1557 | INC_NEEDED, /* File must exist */ |
| 1558 | INC_OPTIONAL, /* Missing is OK */ |
| 1559 | INC_PROBE /* Only an existence probe */ |
| 1560 | }; |
| 1561 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1562 | /* This is conducts a full pathname search */ |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1563 | static FILE *inc_fopen_search(const char *file, char **slpath, |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1564 | enum incopen_mode omode, enum file_flags fmode) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1565 | { |
H. Peter Anvin (Intel) | 6447109 | 2018-12-11 13:06:14 -0800 | [diff] [blame] | 1566 | const struct strlist_entry *ip = strlist_head(ipath_list); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1567 | FILE *fp; |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1568 | const char *prefix = ""; |
night199uk | fdb1a1b | 2018-10-18 23:19:47 +0200 | [diff] [blame] | 1569 | char *sp; |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1570 | bool found; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1571 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1572 | while (1) { |
night199uk | fdb1a1b | 2018-10-18 23:19:47 +0200 | [diff] [blame] | 1573 | sp = nasm_catfile(prefix, file); |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1574 | if (omode == INC_PROBE) { |
| 1575 | fp = NULL; |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1576 | found = nasm_file_exists(sp); |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1577 | } else { |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1578 | fp = nasm_open_read(sp, fmode); |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1579 | found = (fp != NULL); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1580 | } |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1581 | if (found) { |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1582 | *slpath = sp; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1583 | return fp; |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1584 | } |
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 | nasm_free(sp); |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 1587 | |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1588 | if (!ip) { |
| 1589 | *slpath = NULL; |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1590 | return NULL; |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1591 | } |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1592 | |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1593 | prefix = ip->str; |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1594 | ip = ip->next; |
| 1595 | } |
| 1596 | } |
| 1597 | |
| 1598 | /* |
| 1599 | * Open a file, or test for the presence of one (depending on omode), |
| 1600 | * considering the include path. |
| 1601 | */ |
| 1602 | static FILE *inc_fopen(const char *file, |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 1603 | struct strlist *dhead, |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 1604 | const char **found_path, |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1605 | enum incopen_mode omode, |
| 1606 | enum file_flags fmode) |
| 1607 | { |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1608 | struct hash_insert hi; |
| 1609 | void **hp; |
| 1610 | char *path; |
| 1611 | FILE *fp = NULL; |
| 1612 | |
| 1613 | hp = hash_find(&FileHash, file, &hi); |
| 1614 | if (hp) { |
| 1615 | path = *hp; |
Martin Storsjö | f283c8f | 2017-08-13 17:28:46 +0300 | [diff] [blame] | 1616 | if (path || omode != INC_NEEDED) { |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 1617 | strlist_add(dhead, path ? path : file); |
Martin Storsjö | f283c8f | 2017-08-13 17:28:46 +0300 | [diff] [blame] | 1618 | } |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1619 | } else { |
| 1620 | /* Need to do the actual path search */ |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1621 | fp = inc_fopen_search(file, &path, omode, fmode); |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1622 | |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1623 | /* Positive or negative result */ |
| 1624 | hash_add(&hi, nasm_strdup(file), path); |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1625 | |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 1626 | /* |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1627 | * Add file to dependency path. |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 1628 | */ |
| 1629 | if (path || omode != INC_NEEDED) |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 1630 | strlist_add(dhead, file); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1631 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1632 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1633 | if (!path) { |
| 1634 | if (omode == INC_NEEDED) |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 1635 | nasm_fatal("unable to open include file `%s'", file); |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1636 | } else { |
| 1637 | if (!fp && omode != INC_PROBE) |
| 1638 | fp = nasm_open_read(path, fmode); |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1639 | } |
| 1640 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1641 | if (found_path) |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 1642 | *found_path = path; |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1643 | |
| 1644 | return fp; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1645 | } |
| 1646 | |
| 1647 | /* |
Fabian Giesen | 0bbc38d | 2016-04-28 13:48:14 -0700 | [diff] [blame] | 1648 | * Opens an include or input file. Public version, for use by modules |
| 1649 | * that get a file:lineno pair and need to look at the file again |
| 1650 | * (e.g. the CodeView debug backend). Returns NULL on failure. |
| 1651 | */ |
H. Peter Anvin | 3e83cec | 2016-05-25 04:28:46 -0700 | [diff] [blame] | 1652 | FILE *pp_input_fopen(const char *filename, enum file_flags mode) |
Fabian Giesen | 0bbc38d | 2016-04-28 13:48:14 -0700 | [diff] [blame] | 1653 | { |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 1654 | return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode); |
Fabian Giesen | 0bbc38d | 2016-04-28 13:48:14 -0700 | [diff] [blame] | 1655 | } |
| 1656 | |
| 1657 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1658 | * 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] | 1659 | * 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] | 1660 | * return true if _any_ single-line macro of that name is defined. |
| 1661 | * Otherwise, will return true if a single-line macro with either |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1662 | * `nparam' or no parameters is defined. |
| 1663 | * |
| 1664 | * If a macro with precisely the right number of parameters is |
H. Peter Anvin | ef7468f | 2002-04-30 20:57:59 +0000 | [diff] [blame] | 1665 | * defined, or nparam is -1, the address of the definition structure |
| 1666 | * 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] | 1667 | * is NULL, no action will be taken regarding its contents, and no |
| 1668 | * error will occur. |
| 1669 | * |
| 1670 | * Note that this is also called with nparam zero to resolve |
| 1671 | * `ifdef'. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1672 | * |
| 1673 | * If you already know which context macro belongs to, you can pass |
| 1674 | * the context pointer as first parameter; if you won't but name begins |
| 1675 | * with %$ the context will be automatically computed. If all_contexts |
| 1676 | * is true, macro will be searched in outer contexts as well. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1677 | */ |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1678 | static bool |
H. Peter Anvin | b2a5fda | 2008-06-19 21:42:42 -0700 | [diff] [blame] | 1679 | smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn, |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1680 | bool nocase) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1681 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1682 | struct hash_table *smtbl; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1683 | SMacro *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1684 | |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1685 | if (ctx) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1686 | smtbl = &ctx->localmac; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1687 | } else if (name[0] == '%' && name[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1688 | if (cstk) |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1689 | ctx = get_ctx(name, &name); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1690 | if (!ctx) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1691 | return false; /* got to return _something_ */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1692 | smtbl = &ctx->localmac; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1693 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1694 | smtbl = &smacros; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1695 | } |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1696 | m = (SMacro *) hash_findix(smtbl, name); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1697 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1698 | while (m) { |
| 1699 | if (!mstrcmp(m->name, name, m->casesense && nocase) && |
Charles Crayne | 192d5b5 | 2007-10-18 19:02:42 -0700 | [diff] [blame] | 1700 | (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1701 | if (defn) { |
Charles Crayne | 192d5b5 | 2007-10-18 19:02:42 -0700 | [diff] [blame] | 1702 | if (nparam == (int) m->nparam || nparam == -1) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1703 | *defn = m; |
| 1704 | else |
| 1705 | *defn = NULL; |
| 1706 | } |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1707 | return true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1708 | } |
| 1709 | m = m->next; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1710 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1711 | |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1712 | return false; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1713 | } |
| 1714 | |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 1715 | /* param should be a natural number [0; INT_MAX] */ |
| 1716 | static int read_param_count(const char *str) |
| 1717 | { |
| 1718 | int result; |
| 1719 | bool err; |
| 1720 | |
| 1721 | result = readnum(str, &err); |
| 1722 | if (result < 0 || result > INT_MAX) { |
| 1723 | result = 0; |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1724 | nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]", |
| 1725 | str, 0, INT_MAX); |
| 1726 | } else if (err) |
| 1727 | nasm_nonfatal("unable to parse parameter count `%s'", str); |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 1728 | return result; |
| 1729 | } |
| 1730 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1731 | /* |
| 1732 | * Count and mark off the parameters in a multi-line macro call. |
| 1733 | * This is called both from within the multi-line macro expansion |
| 1734 | * code, and also to mark off the default parameters when provided |
| 1735 | * in a %macro definition line. |
| 1736 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1737 | static void count_mmac_params(Token * t, int *nparam, Token *** params) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1738 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1739 | int paramsize, brace; |
| 1740 | |
| 1741 | *nparam = paramsize = 0; |
| 1742 | *params = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1743 | while (t) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1744 | /* +1: we need space for the final NULL */ |
H. Peter Anvin | 91fb6f1 | 2008-09-01 10:56:33 -0700 | [diff] [blame] | 1745 | if (*nparam+1 >= paramsize) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1746 | paramsize += PARAM_DELTA; |
| 1747 | *params = nasm_realloc(*params, sizeof(**params) * paramsize); |
| 1748 | } |
| 1749 | skip_white_(t); |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1750 | brace = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1751 | if (tok_is_(t, "{")) |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1752 | brace++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1753 | (*params)[(*nparam)++] = t; |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1754 | if (brace) { |
| 1755 | while (brace && (t = t->next) != NULL) { |
| 1756 | if (tok_is_(t, "{")) |
| 1757 | brace++; |
| 1758 | else if (tok_is_(t, "}")) |
| 1759 | brace--; |
| 1760 | } |
| 1761 | |
| 1762 | if (t) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1763 | /* |
| 1764 | * Now we've found the closing brace, look further |
| 1765 | * for the comma. |
| 1766 | */ |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1767 | t = t->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1768 | skip_white_(t); |
| 1769 | if (tok_isnt_(t, ",")) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1770 | nasm_nonfatal("braces do not enclose all of macro parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1771 | while (tok_isnt_(t, ",")) |
| 1772 | t = t->next; |
| 1773 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1774 | } |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1775 | } else { |
| 1776 | while (tok_isnt_(t, ",")) |
| 1777 | t = t->next; |
| 1778 | } |
| 1779 | if (t) { /* got a comma/brace */ |
| 1780 | t = t->next; /* eat the comma */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1781 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1782 | } |
| 1783 | } |
| 1784 | |
| 1785 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1786 | * Determine whether one of the various `if' conditions is true or |
| 1787 | * not. |
| 1788 | * |
| 1789 | * We must free the tline we get passed. |
| 1790 | */ |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1791 | static bool if_condition(Token * tline, enum preproc_token ct) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1792 | { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1793 | enum pp_conditional i = PP_COND(ct); |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1794 | bool j; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 1795 | Token *t, *tt, *origline; |
| 1796 | struct ppscan pps; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1797 | struct tokenval tokval; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1798 | expr *evalresult; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1799 | enum pp_token_type needtype; |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1800 | char *p; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1801 | |
| 1802 | origline = tline; |
| 1803 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1804 | switch (i) { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1805 | case PPC_IFCTX: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1806 | j = false; /* have we matched yet? */ |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1807 | while (true) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1808 | skip_white_(tline); |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1809 | if (!tline) |
| 1810 | break; |
| 1811 | if (tline->type != TOK_ID) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1812 | nasm_nonfatal("`%s' expects context identifiers", |
| 1813 | pp_directives[ct]); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1814 | free_tlist(origline); |
| 1815 | return -1; |
| 1816 | } |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1817 | if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1818 | j = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1819 | tline = tline->next; |
| 1820 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1821 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1822 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1823 | case PPC_IFDEF: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1824 | j = false; /* have we matched yet? */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1825 | while (tline) { |
| 1826 | skip_white_(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1827 | if (!tline || (tline->type != TOK_ID && |
| 1828 | (tline->type != TOK_PREPROC_ID || |
| 1829 | tline->text[1] != '$'))) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1830 | nasm_nonfatal("`%s' expects macro identifiers", |
| 1831 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1832 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1833 | } |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1834 | if (smacro_defined(NULL, tline->text, 0, NULL, true)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1835 | j = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1836 | tline = tline->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1837 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1838 | break; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1839 | |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1840 | case PPC_IFENV: |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1841 | tline = expand_smacro(tline); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1842 | j = false; /* have we matched yet? */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1843 | while (tline) { |
| 1844 | skip_white_(tline); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1845 | if (!tline || (tline->type != TOK_ID && |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1846 | tline->type != TOK_STRING && |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1847 | (tline->type != TOK_PREPROC_ID || |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1848 | tline->text[1] != '!'))) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1849 | nasm_nonfatal("`%s' expects environment variable names", |
| 1850 | pp_directives[ct]); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1851 | goto fail; |
| 1852 | } |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1853 | p = tline->text; |
| 1854 | if (tline->type == TOK_PREPROC_ID) |
| 1855 | p += 2; /* Skip leading %! */ |
H. Peter Anvin | 53e2e4c | 2018-11-28 15:01:40 -0800 | [diff] [blame] | 1856 | if (nasm_isquote(*p)) |
H. Peter Anvin | bb42d30 | 2019-04-22 14:29:29 -0700 | [diff] [blame] | 1857 | nasm_unquote_cstr(p, NULL); |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1858 | if (getenv(p)) |
| 1859 | j = true; |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1860 | tline = tline->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1861 | } |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1862 | break; |
| 1863 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1864 | case PPC_IFIDN: |
| 1865 | case PPC_IFIDNI: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1866 | tline = expand_smacro(tline); |
| 1867 | t = tt = tline; |
| 1868 | while (tok_isnt_(tt, ",")) |
| 1869 | tt = tt->next; |
| 1870 | if (!tt) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1871 | nasm_nonfatal("`%s' expects two comma-separated arguments", |
| 1872 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1873 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1874 | } |
| 1875 | tt = tt->next; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1876 | j = true; /* assume equality unless proved not */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1877 | while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) { |
| 1878 | if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1879 | nasm_nonfatal("`%s': more than one comma on line", |
| 1880 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1881 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1882 | } |
| 1883 | if (t->type == TOK_WHITESPACE) { |
| 1884 | t = t->next; |
| 1885 | continue; |
| 1886 | } |
| 1887 | if (tt->type == TOK_WHITESPACE) { |
| 1888 | tt = tt->next; |
| 1889 | continue; |
| 1890 | } |
| 1891 | if (tt->type != t->type) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1892 | j = false; /* found mismatching tokens */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1893 | break; |
| 1894 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1895 | /* When comparing strings, need to unquote them first */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1896 | if (t->type == TOK_STRING) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1897 | size_t l1 = nasm_unquote(t->text, NULL); |
| 1898 | size_t l2 = nasm_unquote(tt->text, NULL); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 1899 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1900 | if (l1 != l2) { |
| 1901 | j = false; |
| 1902 | break; |
| 1903 | } |
| 1904 | if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) { |
| 1905 | j = false; |
| 1906 | break; |
| 1907 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1908 | } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1909 | j = false; /* found mismatching tokens */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1910 | break; |
| 1911 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1912 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1913 | t = t->next; |
| 1914 | tt = tt->next; |
| 1915 | } |
| 1916 | if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1917 | j = false; /* trailing gunk on one end or other */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1918 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1919 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1920 | case PPC_IFMACRO: |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1921 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1922 | bool found = false; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1923 | MMacro searching, *mmac; |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 1924 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1925 | skip_white_(tline); |
| 1926 | tline = expand_id(tline); |
| 1927 | if (!tok_type_(tline, TOK_ID)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1928 | nasm_nonfatal("`%s' expects a macro name", pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1929 | goto fail; |
| 1930 | } |
| 1931 | searching.name = nasm_strdup(tline->text); |
| 1932 | searching.casesense = true; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1933 | searching.plus = false; |
| 1934 | searching.nolist = false; |
| 1935 | searching.in_progress = 0; |
| 1936 | searching.max_depth = 0; |
| 1937 | searching.rep_nest = NULL; |
| 1938 | searching.nparam_min = 0; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1939 | searching.nparam_max = INT_MAX; |
| 1940 | tline = expand_smacro(tline->next); |
| 1941 | skip_white_(tline); |
| 1942 | if (!tline) { |
| 1943 | } else if (!tok_type_(tline, TOK_NUMBER)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1944 | nasm_nonfatal("`%s' expects a parameter count or nothing", |
| 1945 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1946 | } else { |
| 1947 | searching.nparam_min = searching.nparam_max = |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 1948 | read_param_count(tline->text); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1949 | } |
| 1950 | if (tline && tok_is_(tline->next, "-")) { |
| 1951 | tline = tline->next->next; |
| 1952 | if (tok_is_(tline, "*")) |
| 1953 | searching.nparam_max = INT_MAX; |
| 1954 | else if (!tok_type_(tline, TOK_NUMBER)) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1955 | nasm_nonfatal("`%s' expects a parameter count after `-'", |
| 1956 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1957 | else { |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 1958 | searching.nparam_max = read_param_count(tline->text); |
Cyrill Gorcunov | c9244ea | 2017-10-22 15:25:48 +0300 | [diff] [blame] | 1959 | if (searching.nparam_min > searching.nparam_max) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1960 | nasm_nonfatal("minimum parameter count exceeds maximum"); |
Cyrill Gorcunov | c9244ea | 2017-10-22 15:25:48 +0300 | [diff] [blame] | 1961 | searching.nparam_max = searching.nparam_min; |
| 1962 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1963 | } |
| 1964 | } |
| 1965 | if (tline && tok_is_(tline->next, "+")) { |
| 1966 | tline = tline->next; |
| 1967 | searching.plus = true; |
| 1968 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1969 | mmac = (MMacro *) hash_findix(&mmacros, searching.name); |
| 1970 | while (mmac) { |
| 1971 | if (!strcmp(mmac->name, searching.name) && |
| 1972 | (mmac->nparam_min <= searching.nparam_max |
| 1973 | || searching.plus) |
| 1974 | && (searching.nparam_min <= mmac->nparam_max |
| 1975 | || mmac->plus)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1976 | found = true; |
| 1977 | break; |
| 1978 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1979 | mmac = mmac->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1980 | } |
| 1981 | if (tline && tline->next) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 1982 | nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1983 | nasm_free(searching.name); |
| 1984 | j = found; |
| 1985 | break; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1986 | } |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 1987 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1988 | case PPC_IFID: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1989 | needtype = TOK_ID; |
| 1990 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1991 | case PPC_IFNUM: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1992 | needtype = TOK_NUMBER; |
| 1993 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1994 | case PPC_IFSTR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1995 | needtype = TOK_STRING; |
| 1996 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1997 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1998 | iftype: |
| 1999 | t = tline = expand_smacro(tline); |
H. Peter Anvin | d85d250 | 2008-05-04 17:53:31 -0700 | [diff] [blame] | 2000 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2001 | while (tok_type_(t, TOK_WHITESPACE) || |
| 2002 | (needtype == TOK_NUMBER && |
| 2003 | tok_type_(t, TOK_OTHER) && |
| 2004 | (t->text[0] == '-' || t->text[0] == '+') && |
| 2005 | !t->text[1])) |
| 2006 | t = t->next; |
H. Peter Anvin | d85d250 | 2008-05-04 17:53:31 -0700 | [diff] [blame] | 2007 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2008 | j = tok_type_(t, needtype); |
| 2009 | break; |
H. Peter Anvin | cbf768d | 2008-02-16 16:41:25 -0800 | [diff] [blame] | 2010 | |
| 2011 | case PPC_IFTOKEN: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2012 | t = tline = expand_smacro(tline); |
| 2013 | while (tok_type_(t, TOK_WHITESPACE)) |
| 2014 | t = t->next; |
H. Peter Anvin | cbf768d | 2008-02-16 16:41:25 -0800 | [diff] [blame] | 2015 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2016 | j = false; |
| 2017 | if (t) { |
| 2018 | t = t->next; /* Skip the actual token */ |
| 2019 | while (tok_type_(t, TOK_WHITESPACE)) |
| 2020 | t = t->next; |
| 2021 | j = !t; /* Should be nothing left */ |
| 2022 | } |
| 2023 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2024 | |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 2025 | case PPC_IFEMPTY: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2026 | t = tline = expand_smacro(tline); |
| 2027 | while (tok_type_(t, TOK_WHITESPACE)) |
| 2028 | t = t->next; |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 2029 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2030 | j = !t; /* Should be empty */ |
| 2031 | break; |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 2032 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2033 | case PPC_IF: |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2034 | pps.tptr = tline = expand_smacro(tline); |
| 2035 | pps.ntokens = -1; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2036 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2037 | evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2038 | if (!evalresult) |
| 2039 | return -1; |
| 2040 | if (tokval.t_type) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2041 | nasm_warn(WARN_OTHER, "trailing garbage after expression ignored"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2042 | if (!is_simple(evalresult)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2043 | nasm_nonfatal("non-constant value given to `%s'", |
| 2044 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2045 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2046 | } |
Chuck Crayne | 60ae75d | 2007-05-02 01:59:16 +0000 | [diff] [blame] | 2047 | j = reloc_value(evalresult) != 0; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2048 | break; |
H. Peter Anvin | 95e2882 | 2007-09-12 04:20:08 +0000 | [diff] [blame] | 2049 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2050 | default: |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2051 | nasm_fatal("preprocessor directive `%s' not yet implemented", |
| 2052 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2053 | goto fail; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2054 | } |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2055 | |
| 2056 | free_tlist(origline); |
| 2057 | return j ^ PP_NEGATIVE(ct); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2058 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2059 | fail: |
| 2060 | free_tlist(origline); |
| 2061 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2062 | } |
| 2063 | |
| 2064 | /* |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 2065 | * Default smacro expansion routine: just returns a copy of the |
| 2066 | * expansion list. |
| 2067 | */ |
| 2068 | static Token * |
| 2069 | smacro_expand_default(const SMacro *s, Token **params, unsigned int nparams) |
| 2070 | { |
| 2071 | (void)params; |
| 2072 | (void)nparams; |
| 2073 | |
| 2074 | return dup_tlist(s->expansion, NULL); |
| 2075 | } |
| 2076 | |
| 2077 | /* |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2078 | * Common code for defining an smacro |
| 2079 | */ |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2080 | static SMacro *define_smacro(Context *ctx, const char *mname, |
| 2081 | bool casesense, int nparam, Token *expansion) |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2082 | { |
| 2083 | SMacro *smac, **smhead; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2084 | struct hash_table *smtbl; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2085 | |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2086 | if (smacro_defined(ctx, mname, nparam, &smac, casesense)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2087 | if (!smac) { |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2088 | nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and" |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2089 | " without parameters", mname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2090 | /* |
| 2091 | * Some instances of the old code considered this a failure, |
| 2092 | * some others didn't. What is the right thing to do here? |
| 2093 | */ |
| 2094 | free_tlist(expansion); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2095 | return NULL; /* Failure */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2096 | } else { |
| 2097 | /* |
| 2098 | * We're redefining, so we have to take over an |
| 2099 | * existing SMacro structure. This means freeing |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2100 | * what was already in it, but not the structure itself. |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2101 | */ |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 2102 | clear_smacro(smac); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2103 | } |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2104 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2105 | smtbl = ctx ? &ctx->localmac : &smacros; |
| 2106 | smhead = (SMacro **) hash_findi_add(smtbl, mname); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2107 | nasm_new(smac); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2108 | smac->next = *smhead; |
| 2109 | *smhead = smac; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2110 | } |
| 2111 | smac->name = nasm_strdup(mname); |
| 2112 | smac->casesense = casesense; |
| 2113 | smac->nparam = nparam; |
| 2114 | smac->expansion = expansion; |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 2115 | smac->expand = smacro_expand_default; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2116 | return smac; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2117 | } |
| 2118 | |
| 2119 | /* |
| 2120 | * Undefine an smacro |
| 2121 | */ |
| 2122 | static void undef_smacro(Context *ctx, const char *mname) |
| 2123 | { |
| 2124 | SMacro **smhead, *s, **sp; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2125 | struct hash_table *smtbl; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2126 | |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2127 | smtbl = ctx ? &ctx->localmac : &smacros; |
| 2128 | smhead = (SMacro **)hash_findi(smtbl, mname, NULL); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2129 | |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2130 | if (smhead) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2131 | /* |
| 2132 | * We now have a macro name... go hunt for it. |
| 2133 | */ |
| 2134 | sp = smhead; |
| 2135 | while ((s = *sp) != NULL) { |
| 2136 | if (!mstrcmp(s->name, mname, s->casesense)) { |
| 2137 | *sp = s->next; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 2138 | free_smacro(s); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2139 | } else { |
| 2140 | sp = &s->next; |
| 2141 | } |
| 2142 | } |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2143 | } |
| 2144 | } |
| 2145 | |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2146 | /* |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2147 | * Parse a mmacro specification. |
| 2148 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2149 | 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] | 2150 | { |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2151 | tline = tline->next; |
| 2152 | skip_white_(tline); |
| 2153 | tline = expand_id(tline); |
| 2154 | if (!tok_type_(tline, TOK_ID)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2155 | nasm_nonfatal("`%s' expects a macro name", directive); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2156 | return false; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2157 | } |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2158 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2159 | def->prev = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2160 | def->name = nasm_strdup(tline->text); |
| 2161 | def->plus = false; |
| 2162 | def->nolist = false; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2163 | def->in_progress = 0; |
| 2164 | def->rep_nest = NULL; |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2165 | def->nparam_min = 0; |
| 2166 | def->nparam_max = 0; |
| 2167 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2168 | tline = expand_smacro(tline->next); |
| 2169 | skip_white_(tline); |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2170 | if (!tok_type_(tline, TOK_NUMBER)) |
| 2171 | nasm_nonfatal("`%s' expects a parameter count", directive); |
| 2172 | else |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 2173 | def->nparam_min = def->nparam_max = read_param_count(tline->text); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2174 | if (tline && tok_is_(tline->next, "-")) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2175 | tline = tline->next->next; |
| 2176 | if (tok_is_(tline, "*")) { |
| 2177 | def->nparam_max = INT_MAX; |
| 2178 | } else if (!tok_type_(tline, TOK_NUMBER)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2179 | nasm_nonfatal("`%s' expects a parameter count after `-'", directive); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2180 | } else { |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 2181 | def->nparam_max = read_param_count(tline->text); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2182 | if (def->nparam_min > def->nparam_max) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2183 | nasm_nonfatal("minimum parameter count exceeds maximum"); |
Cyrill Gorcunov | c9244ea | 2017-10-22 15:25:48 +0300 | [diff] [blame] | 2184 | def->nparam_max = def->nparam_min; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2185 | } |
| 2186 | } |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2187 | } |
| 2188 | if (tline && tok_is_(tline->next, "+")) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2189 | tline = tline->next; |
| 2190 | def->plus = true; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2191 | } |
| 2192 | if (tline && tok_type_(tline->next, TOK_ID) && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2193 | !nasm_stricmp(tline->next->text, ".nolist")) { |
| 2194 | tline = tline->next; |
| 2195 | def->nolist = true; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2196 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2197 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2198 | /* |
| 2199 | * Handle default parameters. |
| 2200 | */ |
| 2201 | if (tline && tline->next) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2202 | def->dlist = tline->next; |
| 2203 | tline->next = NULL; |
| 2204 | count_mmac_params(def->dlist, &def->ndefs, &def->defaults); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2205 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2206 | def->dlist = NULL; |
| 2207 | def->defaults = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2208 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2209 | def->expansion = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2210 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2211 | 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] | 2212 | !def->plus) { |
| 2213 | /* |
| 2214 | *!macro-defaults [on] macros with more default than optional parameters |
| 2215 | *! warns when a macro has more default parameters than optional parameters. |
| 2216 | *! See \k{mlmacdef} for why might want to disable this warning. |
| 2217 | */ |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2218 | nasm_warn(WARN_MACRO_DEFAULTS, |
H. Peter Anvin (Intel) | 723ab48 | 2018-12-13 21:53:31 -0800 | [diff] [blame] | 2219 | "too many default macro parameters in macro `%s'", def->name); |
| 2220 | } |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2221 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2222 | return true; |
| 2223 | } |
| 2224 | |
| 2225 | |
| 2226 | /* |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2227 | * Decode a size directive |
| 2228 | */ |
| 2229 | static int parse_size(const char *str) { |
| 2230 | static const char *size_names[] = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2231 | { "byte", "dword", "oword", "qword", "tword", "word", "yword" }; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2232 | static const int sizes[] = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2233 | { 0, 1, 4, 16, 8, 10, 2, 32 }; |
Cyrill Gorcunov | c713b5f | 2018-09-29 14:30:14 +0300 | [diff] [blame] | 2234 | 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] | 2235 | } |
| 2236 | |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2237 | /* |
| 2238 | * Process a preprocessor %pragma directive. Currently there are none. |
| 2239 | * Gets passed the token list starting with the "preproc" token from |
| 2240 | * "%pragma preproc". |
| 2241 | */ |
| 2242 | static void do_pragma_preproc(Token *tline) |
| 2243 | { |
| 2244 | /* Skip to the real stuff */ |
| 2245 | tline = tline->next; |
| 2246 | skip_white_(tline); |
| 2247 | if (!tline) |
| 2248 | return; |
| 2249 | |
| 2250 | (void)tline; /* Nothing else to do at present */ |
| 2251 | } |
| 2252 | |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2253 | /** |
| 2254 | * find and process preprocessor directive in passed line |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2255 | * Find out if a line contains a preprocessor directive, and deal |
| 2256 | * with it if so. |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2257 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2258 | * If a directive _is_ found, it is the responsibility of this routine |
| 2259 | * (and not the caller) to free_tlist() the line. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2260 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2261 | * @param tline a pointer to the current tokeninzed line linked list |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2262 | * @param output if this directive generated output |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2263 | * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2264 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2265 | */ |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 2266 | static int do_directive(Token *tline, Token **output) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2267 | { |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2268 | enum preproc_token i; |
| 2269 | int j; |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 2270 | bool err; |
| 2271 | int nparam; |
| 2272 | bool nolist; |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 2273 | bool casesense; |
H. Peter Anvin | 8cfdb9d | 2007-09-14 18:36:01 -0700 | [diff] [blame] | 2274 | int k, m; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2275 | int offset; |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 2276 | char *p, *pp; |
| 2277 | const char *found_path; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 2278 | const char *mname; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2279 | struct ppscan pps; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2280 | Include *inc; |
| 2281 | Context *ctx; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2282 | Cond *cond; |
| 2283 | MMacro *mmac, **mmhead; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2284 | Token *t = NULL, *tt, *param_start, *macro_start, *last, *origline; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2285 | Line *l; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2286 | struct tokenval tokval; |
| 2287 | expr *evalresult; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2288 | MMacro *tmp_defining; /* Used when manipulating rep_nest */ |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 2289 | int64_t count; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 2290 | size_t len; |
H. Peter Anvin (Intel) | 6bde2ed | 2018-12-13 19:39:41 -0800 | [diff] [blame] | 2291 | errflags severity; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2292 | const char *dname; /* Name of directive, for messages */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2293 | |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2294 | *output = NULL; /* No output generated */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2295 | origline = tline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2296 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2297 | skip_white_(tline); |
H. Peter Anvin | f2936d7 | 2008-06-04 15:11:23 -0700 | [diff] [blame] | 2298 | if (!tline || !tok_type_(tline, TOK_PREPROC_ID) || |
Cyrill Gorcunov | 4b5b737 | 2018-10-29 22:54:08 +0300 | [diff] [blame] | 2299 | (tline->text[0] && (tline->text[1] == '%' || |
| 2300 | tline->text[1] == '$' || |
| 2301 | tline->text[1] == '!'))) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2302 | return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2303 | |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2304 | i = pp_token_hash(tline->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2305 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2306 | /* |
| 2307 | * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO |
| 2308 | * since they are known to be buggy at moment, we need to fix them |
| 2309 | * in future release (2.09-2.10) |
| 2310 | */ |
Philipp Kloke | b432f57 | 2013-03-31 12:01:23 +0200 | [diff] [blame] | 2311 | if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2312 | nasm_nonfatal("unknown preprocessor directive `%s'", tline->text); |
| 2313 | return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2314 | } |
| 2315 | |
| 2316 | /* |
| 2317 | * If we're in a non-emitting branch of a condition construct, |
| 2318 | * or walking to the end of an already terminated %rep block, |
| 2319 | * we should ignore all directives except for condition |
| 2320 | * directives. |
| 2321 | */ |
| 2322 | if (((istk->conds && !emitting(istk->conds->state)) || |
| 2323 | (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) { |
| 2324 | return NO_DIRECTIVE_FOUND; |
| 2325 | } |
| 2326 | |
| 2327 | /* |
| 2328 | * If we're defining a macro or reading a %rep block, we should |
| 2329 | * ignore all directives except for %macro/%imacro (which nest), |
| 2330 | * %endm/%endmacro, and (only if we're in a %rep block) %endrep. |
| 2331 | * If we're in a %rep block, another %rep nests, so should be let through. |
| 2332 | */ |
| 2333 | if (defining && i != PP_MACRO && i != PP_IMACRO && |
| 2334 | i != PP_RMACRO && i != PP_IRMACRO && |
| 2335 | i != PP_ENDMACRO && i != PP_ENDM && |
| 2336 | (defining->name || (i != PP_ENDREP && i != PP_REP))) { |
| 2337 | return NO_DIRECTIVE_FOUND; |
| 2338 | } |
| 2339 | |
| 2340 | if (defining) { |
| 2341 | if (i == PP_MACRO || i == PP_IMACRO || |
| 2342 | i == PP_RMACRO || i == PP_IRMACRO) { |
| 2343 | nested_mac_count++; |
| 2344 | return NO_DIRECTIVE_FOUND; |
| 2345 | } else if (nested_mac_count > 0) { |
| 2346 | if (i == PP_ENDMACRO) { |
| 2347 | nested_mac_count--; |
| 2348 | return NO_DIRECTIVE_FOUND; |
| 2349 | } |
| 2350 | } |
| 2351 | if (!defining->name) { |
| 2352 | if (i == PP_REP) { |
| 2353 | nested_rep_count++; |
| 2354 | return NO_DIRECTIVE_FOUND; |
| 2355 | } else if (nested_rep_count > 0) { |
| 2356 | if (i == PP_ENDREP) { |
| 2357 | nested_rep_count--; |
| 2358 | return NO_DIRECTIVE_FOUND; |
| 2359 | } |
| 2360 | } |
| 2361 | } |
| 2362 | } |
| 2363 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2364 | dname = pp_directives[i]; /* Directive name, for error messages */ |
| 2365 | casesense = true; /* Default to case sensitive */ |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2366 | switch (i) { |
| 2367 | case PP_INVALID: |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2368 | nasm_nonfatal("unknown preprocessor directive `%s'", tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2369 | return NO_DIRECTIVE_FOUND; /* didn't get it */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2370 | |
H. Peter Anvin | 3f87a2a | 2016-10-04 14:07:19 -0700 | [diff] [blame] | 2371 | case PP_PRAGMA: |
| 2372 | /* |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2373 | * %pragma namespace options... |
| 2374 | * |
| 2375 | * The namespace "preproc" is reserved for the preprocessor; |
| 2376 | * all other namespaces generate a [pragma] assembly directive. |
| 2377 | * |
| 2378 | * Invalid %pragmas are ignored and may have different |
| 2379 | * meaning in future versions of NASM. |
H. Peter Anvin | 3f87a2a | 2016-10-04 14:07:19 -0700 | [diff] [blame] | 2380 | */ |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2381 | tline = tline->next; |
| 2382 | skip_white_(tline); |
| 2383 | tline = expand_smacro(tline); |
| 2384 | if (tok_type_(tline, TOK_ID)) { |
| 2385 | if (!nasm_stricmp(tline->text, "preproc")) { |
| 2386 | /* Preprocessor pragma */ |
| 2387 | do_pragma_preproc(tline); |
| 2388 | } else { |
| 2389 | /* Build the assembler directive */ |
| 2390 | t = new_Token(NULL, TOK_OTHER, "[", 1); |
| 2391 | t->next = new_Token(NULL, TOK_ID, "pragma", 6); |
| 2392 | t->next->next = new_Token(tline, TOK_WHITESPACE, NULL, 0); |
| 2393 | tline = t; |
| 2394 | for (t = tline; t->next; t = t->next) |
| 2395 | ; |
| 2396 | t->next = new_Token(NULL, TOK_OTHER, "]", 1); |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 2397 | *output = tline; |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2398 | } |
| 2399 | } |
H. Peter Anvin | 3f87a2a | 2016-10-04 14:07:19 -0700 | [diff] [blame] | 2400 | free_tlist(origline); |
| 2401 | return DIRECTIVE_FOUND; |
| 2402 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2403 | case PP_STACKSIZE: |
| 2404 | /* Directive to tell NASM what the default stack size is. The |
| 2405 | * default is for a 16-bit stack, and this can be overriden with |
| 2406 | * %stacksize large. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2407 | */ |
| 2408 | tline = tline->next; |
| 2409 | if (tline && tline->type == TOK_WHITESPACE) |
| 2410 | tline = tline->next; |
| 2411 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2412 | nasm_nonfatal("`%s' missing size parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2413 | free_tlist(origline); |
| 2414 | return DIRECTIVE_FOUND; |
| 2415 | } |
| 2416 | if (nasm_stricmp(tline->text, "flat") == 0) { |
| 2417 | /* All subsequent ARG directives are for a 32-bit stack */ |
| 2418 | StackSize = 4; |
| 2419 | StackPointer = "ebp"; |
| 2420 | ArgOffset = 8; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2421 | LocalOffset = 0; |
Charles Crayne | 7eaf919 | 2007-11-08 22:11:14 -0800 | [diff] [blame] | 2422 | } else if (nasm_stricmp(tline->text, "flat64") == 0) { |
| 2423 | /* All subsequent ARG directives are for a 64-bit stack */ |
| 2424 | StackSize = 8; |
| 2425 | StackPointer = "rbp"; |
Per Jessen | 53252e0 | 2010-02-11 00:16:59 +0300 | [diff] [blame] | 2426 | ArgOffset = 16; |
Charles Crayne | 7eaf919 | 2007-11-08 22:11:14 -0800 | [diff] [blame] | 2427 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2428 | } else if (nasm_stricmp(tline->text, "large") == 0) { |
| 2429 | /* All subsequent ARG directives are for a 16-bit stack, |
| 2430 | * far function call. |
| 2431 | */ |
| 2432 | StackSize = 2; |
| 2433 | StackPointer = "bp"; |
| 2434 | ArgOffset = 4; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2435 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2436 | } else if (nasm_stricmp(tline->text, "small") == 0) { |
| 2437 | /* All subsequent ARG directives are for a 16-bit stack, |
| 2438 | * far function call. We don't support near functions. |
| 2439 | */ |
| 2440 | StackSize = 2; |
| 2441 | StackPointer = "bp"; |
| 2442 | ArgOffset = 6; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2443 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2444 | } else { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2445 | nasm_nonfatal("`%s' invalid size type", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2446 | free_tlist(origline); |
| 2447 | return DIRECTIVE_FOUND; |
| 2448 | } |
| 2449 | free_tlist(origline); |
| 2450 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2451 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2452 | case PP_ARG: |
| 2453 | /* TASM like ARG directive to define arguments to functions, in |
| 2454 | * the following form: |
| 2455 | * |
| 2456 | * ARG arg1:WORD, arg2:DWORD, arg4:QWORD |
| 2457 | */ |
| 2458 | offset = ArgOffset; |
| 2459 | do { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 2460 | char *arg, directive[256]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2461 | int size = StackSize; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2462 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2463 | /* Find the argument name */ |
| 2464 | tline = tline->next; |
| 2465 | if (tline && tline->type == TOK_WHITESPACE) |
| 2466 | tline = tline->next; |
| 2467 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2468 | nasm_nonfatal("`%s' missing argument parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2469 | free_tlist(origline); |
| 2470 | return DIRECTIVE_FOUND; |
| 2471 | } |
| 2472 | arg = tline->text; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2473 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2474 | /* Find the argument size type */ |
| 2475 | tline = tline->next; |
| 2476 | if (!tline || tline->type != TOK_OTHER |
| 2477 | || tline->text[0] != ':') { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2478 | nasm_nonfatal("syntax error processing `%s' directive", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2479 | free_tlist(origline); |
| 2480 | return DIRECTIVE_FOUND; |
| 2481 | } |
| 2482 | tline = tline->next; |
| 2483 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2484 | nasm_nonfatal("`%s' missing size type parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2485 | free_tlist(origline); |
| 2486 | return DIRECTIVE_FOUND; |
| 2487 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2488 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2489 | /* Allow macro expansion of type parameter */ |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2490 | tt = tokenize(tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2491 | tt = expand_smacro(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2492 | size = parse_size(tt->text); |
| 2493 | if (!size) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2494 | nasm_nonfatal("invalid size type for `%s' missing directive", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2495 | free_tlist(tt); |
| 2496 | free_tlist(origline); |
| 2497 | return DIRECTIVE_FOUND; |
| 2498 | } |
| 2499 | free_tlist(tt); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2500 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2501 | /* Round up to even stack slots */ |
| 2502 | size = ALIGN(size, StackSize); |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2503 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2504 | /* Now define the macro for the argument */ |
| 2505 | snprintf(directive, sizeof(directive), "%%define %s (%s+%d)", |
| 2506 | arg, StackPointer, offset); |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2507 | do_directive(tokenize(directive), output); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2508 | offset += size; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2509 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2510 | /* Move to the next argument in the list */ |
| 2511 | tline = tline->next; |
| 2512 | if (tline && tline->type == TOK_WHITESPACE) |
| 2513 | tline = tline->next; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2514 | } while (tline && tline->type == TOK_OTHER && tline->text[0] == ','); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2515 | ArgOffset = offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2516 | free_tlist(origline); |
| 2517 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2518 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2519 | case PP_LOCAL: |
| 2520 | /* TASM like LOCAL directive to define local variables for a |
| 2521 | * function, in the following form: |
| 2522 | * |
| 2523 | * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize |
| 2524 | * |
| 2525 | * The '= LocalSize' at the end is ignored by NASM, but is |
| 2526 | * required by TASM to define the local parameter size (and used |
| 2527 | * by the TASM macro package). |
| 2528 | */ |
| 2529 | offset = LocalOffset; |
| 2530 | do { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 2531 | char *local, directive[256]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2532 | int size = StackSize; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2533 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2534 | /* Find the argument name */ |
| 2535 | tline = tline->next; |
| 2536 | if (tline && tline->type == TOK_WHITESPACE) |
| 2537 | tline = tline->next; |
| 2538 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2539 | nasm_nonfatal("`%s' missing argument parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2540 | free_tlist(origline); |
| 2541 | return DIRECTIVE_FOUND; |
| 2542 | } |
| 2543 | local = tline->text; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2544 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2545 | /* Find the argument size type */ |
| 2546 | tline = tline->next; |
| 2547 | if (!tline || tline->type != TOK_OTHER |
| 2548 | || tline->text[0] != ':') { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2549 | nasm_nonfatal("syntax error processing `%s' directive", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2550 | free_tlist(origline); |
| 2551 | return DIRECTIVE_FOUND; |
| 2552 | } |
| 2553 | tline = tline->next; |
| 2554 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2555 | nasm_nonfatal("`%s' missing size type parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2556 | free_tlist(origline); |
| 2557 | return DIRECTIVE_FOUND; |
| 2558 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2559 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2560 | /* Allow macro expansion of type parameter */ |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2561 | tt = tokenize(tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2562 | tt = expand_smacro(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2563 | size = parse_size(tt->text); |
| 2564 | if (!size) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2565 | nasm_nonfatal("invalid size type for `%s' missing directive", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2566 | free_tlist(tt); |
| 2567 | free_tlist(origline); |
| 2568 | return DIRECTIVE_FOUND; |
| 2569 | } |
| 2570 | free_tlist(tt); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2571 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2572 | /* Round up to even stack slots */ |
| 2573 | size = ALIGN(size, StackSize); |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2574 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2575 | offset += size; /* Negative offset, increment before */ |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2576 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2577 | /* Now define the macro for the argument */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2578 | snprintf(directive, sizeof(directive), "%%define %s (%s-%d)", |
| 2579 | local, StackPointer, offset); |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2580 | do_directive(tokenize(directive), output); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2581 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2582 | /* Now define the assign to setup the enter_c macro correctly */ |
| 2583 | snprintf(directive, sizeof(directive), |
| 2584 | "%%assign %%$localsize %%$localsize+%d", size); |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2585 | do_directive(tokenize(directive), output); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2586 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2587 | /* Move to the next argument in the list */ |
| 2588 | tline = tline->next; |
| 2589 | if (tline && tline->type == TOK_WHITESPACE) |
| 2590 | tline = tline->next; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2591 | } while (tline && tline->type == TOK_OTHER && tline->text[0] == ','); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2592 | LocalOffset = offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2593 | free_tlist(origline); |
| 2594 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2595 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2596 | case PP_CLEAR: |
| 2597 | if (tline->next) |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2598 | nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2599 | free_macros(); |
| 2600 | init_macros(); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2601 | free_tlist(origline); |
| 2602 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2603 | |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2604 | case PP_DEPEND: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2605 | t = tline->next = expand_smacro(tline->next); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2606 | skip_white_(t); |
| 2607 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2608 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2609 | nasm_nonfatal("`%s' expects a file name", dname); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2610 | free_tlist(origline); |
| 2611 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2612 | } |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2613 | if (t->next) |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2614 | nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2615 | p = t->text; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2616 | if (t->type != TOK_INTERNAL_STRING) |
H. Peter Anvin | bb42d30 | 2019-04-22 14:29:29 -0700 | [diff] [blame] | 2617 | nasm_unquote_cstr(p, NULL); |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 2618 | strlist_add(deplist, p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2619 | free_tlist(origline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2620 | return DIRECTIVE_FOUND; |
| 2621 | |
| 2622 | case PP_INCLUDE: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2623 | t = tline->next = expand_smacro(tline->next); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2624 | skip_white_(t); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2625 | |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2626 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2627 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2628 | nasm_nonfatal("`%s' expects a file name", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2629 | free_tlist(origline); |
| 2630 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2631 | } |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2632 | if (t->next) |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2633 | nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2634 | p = t->text; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2635 | if (t->type != TOK_INTERNAL_STRING) |
H. Peter Anvin | bb42d30 | 2019-04-22 14:29:29 -0700 | [diff] [blame] | 2636 | nasm_unquote_cstr(p, NULL); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2637 | inc = nasm_malloc(sizeof(Include)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2638 | inc->next = istk; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2639 | inc->conds = NULL; |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 2640 | found_path = NULL; |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 2641 | inc->fp = inc_fopen(p, deplist, &found_path, |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 2642 | (pp_mode == PP_DEPS) |
| 2643 | ? INC_OPTIONAL : INC_NEEDED, NF_TEXT); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2644 | if (!inc->fp) { |
| 2645 | /* -MG given but file not found */ |
| 2646 | nasm_free(inc); |
| 2647 | } else { |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 2648 | inc->fname = src_set_fname(found_path ? found_path : p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2649 | inc->lineno = src_set_linnum(0); |
| 2650 | inc->lineinc = 1; |
| 2651 | inc->expansion = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2652 | inc->mstk = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2653 | istk = inc; |
H. Peter Anvin | 0d4d431 | 2019-08-07 00:46:27 -0700 | [diff] [blame] | 2654 | lfmt->uplevel(LIST_INCLUDE, 0); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2655 | } |
| 2656 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2657 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2658 | |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2659 | case PP_USE: |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2660 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2661 | static macros_t *use_pkg; |
| 2662 | const char *pkg_macro = NULL; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2663 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2664 | tline = tline->next; |
| 2665 | skip_white_(tline); |
| 2666 | tline = expand_id(tline); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2667 | |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2668 | if (!tline || (tline->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2669 | tline->type != TOK_INTERNAL_STRING && |
| 2670 | tline->type != TOK_ID)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2671 | nasm_nonfatal("`%s' expects a package name", dname); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2672 | free_tlist(origline); |
| 2673 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2674 | } |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2675 | if (tline->next) |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2676 | nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2677 | if (tline->type == TOK_STRING) |
H. Peter Anvin | bb42d30 | 2019-04-22 14:29:29 -0700 | [diff] [blame] | 2678 | nasm_unquote_cstr(tline->text, NULL); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2679 | use_pkg = nasm_stdmac_find_package(tline->text); |
| 2680 | if (!use_pkg) |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2681 | nasm_nonfatal("unknown `%s' package: %s", dname, tline->text); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2682 | else |
| 2683 | 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] | 2684 | if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2685 | /* Not already included, go ahead and include it */ |
| 2686 | stdmacpos = use_pkg; |
| 2687 | } |
| 2688 | free_tlist(origline); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2689 | return DIRECTIVE_FOUND; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2690 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2691 | case PP_PUSH: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2692 | case PP_REPL: |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2693 | case PP_POP: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2694 | tline = tline->next; |
| 2695 | skip_white_(tline); |
| 2696 | tline = expand_id(tline); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2697 | if (tline) { |
| 2698 | if (!tok_type_(tline, TOK_ID)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2699 | nasm_nonfatal("`%s' expects a context identifier", |
| 2700 | pp_directives[i]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2701 | free_tlist(origline); |
| 2702 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2703 | } |
| 2704 | if (tline->next) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2705 | nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2706 | pp_directives[i]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2707 | p = nasm_strdup(tline->text); |
| 2708 | } else { |
| 2709 | p = NULL; /* Anonymous */ |
| 2710 | } |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2711 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2712 | if (i == PP_PUSH) { |
H. Peter Anvin (Intel) | ebb05a0 | 2018-12-11 12:30:25 -0800 | [diff] [blame] | 2713 | nasm_new(ctx); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2714 | ctx->next = cstk; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2715 | ctx->name = p; |
| 2716 | ctx->number = unique++; |
| 2717 | cstk = ctx; |
| 2718 | } else { |
| 2719 | /* %pop or %repl */ |
| 2720 | if (!cstk) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2721 | nasm_nonfatal("`%s': context stack is empty", |
| 2722 | pp_directives[i]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2723 | } else if (i == PP_POP) { |
| 2724 | if (p && (!cstk->name || nasm_stricmp(p, cstk->name))) |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2725 | nasm_nonfatal("`%s' in wrong context: %s, " |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2726 | "expected %s", |
| 2727 | dname, cstk->name ? cstk->name : "anonymous", p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2728 | else |
| 2729 | ctx_pop(); |
| 2730 | } else { |
| 2731 | /* i == PP_REPL */ |
| 2732 | nasm_free(cstk->name); |
| 2733 | cstk->name = p; |
| 2734 | p = NULL; |
| 2735 | } |
| 2736 | nasm_free(p); |
| 2737 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2738 | free_tlist(origline); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2739 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2740 | case PP_FATAL: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2741 | severity = ERR_FATAL; |
| 2742 | goto issue_error; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2743 | case PP_ERROR: |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2744 | severity = ERR_NONFATAL|ERR_PASS2; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2745 | goto issue_error; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2746 | case PP_WARNING: |
H. Peter Anvin (Intel) | 723ab48 | 2018-12-13 21:53:31 -0800 | [diff] [blame] | 2747 | /*! |
| 2748 | *!user [on] %warning directives |
| 2749 | *! controls output of \c{%warning} directives (see \k{pperror}). |
| 2750 | */ |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2751 | severity = ERR_WARNING|WARN_USER|ERR_PASS2; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2752 | goto issue_error; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2753 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2754 | issue_error: |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2755 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2756 | /* Only error out if this is the final pass */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2757 | tline->next = expand_smacro(tline->next); |
| 2758 | tline = tline->next; |
| 2759 | skip_white_(tline); |
| 2760 | t = tline ? tline->next : NULL; |
| 2761 | skip_white_(t); |
| 2762 | if (tok_type_(tline, TOK_STRING) && !t) { |
| 2763 | /* The line contains only a quoted string */ |
| 2764 | p = tline->text; |
| 2765 | nasm_unquote(p, NULL); /* Ignore NUL character truncation */ |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2766 | nasm_error(severity, "%s", p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2767 | } else { |
| 2768 | /* Not a quoted string, or more than a quoted string */ |
| 2769 | p = detoken(tline, false); |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2770 | nasm_error(severity, "%s", p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2771 | nasm_free(p); |
| 2772 | } |
| 2773 | free_tlist(origline); |
| 2774 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2775 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2776 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2777 | CASE_PP_IF: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2778 | if (istk->conds && !emitting(istk->conds->state)) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2779 | j = COND_NEVER; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2780 | else { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2781 | j = if_condition(tline->next, i); |
| 2782 | tline->next = NULL; /* it got freed */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2783 | j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2784 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2785 | cond = nasm_malloc(sizeof(Cond)); |
| 2786 | cond->next = istk->conds; |
| 2787 | cond->state = j; |
| 2788 | istk->conds = cond; |
| 2789 | if(istk->mstk) |
| 2790 | istk->mstk->condcnt ++; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2791 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2792 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2793 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2794 | CASE_PP_ELIF: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2795 | if (!istk->conds) |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2796 | nasm_fatal("`%s': no matching `%%if'", dname); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2797 | switch(istk->conds->state) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2798 | case COND_IF_TRUE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2799 | istk->conds->state = COND_DONE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2800 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2801 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2802 | case COND_DONE: |
| 2803 | case COND_NEVER: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2804 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2805 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2806 | case COND_ELSE_TRUE: |
| 2807 | case COND_ELSE_FALSE: |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2808 | nasm_warn(WARN_OTHER|ERR_PP_PRECOND, |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2809 | "`%%elif' after `%%else' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2810 | istk->conds->state = COND_NEVER; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2811 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2812 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2813 | case COND_IF_FALSE: |
| 2814 | /* |
| 2815 | * IMPORTANT: In the case of %if, we will already have |
| 2816 | * called expand_mmac_params(); however, if we're |
| 2817 | * processing an %elif we must have been in a |
| 2818 | * non-emitting mode, which would have inhibited |
| 2819 | * the normal invocation of expand_mmac_params(). |
| 2820 | * Therefore, we have to do it explicitly here. |
| 2821 | */ |
| 2822 | j = if_condition(expand_mmac_params(tline->next), i); |
| 2823 | tline->next = NULL; /* it got freed */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2824 | istk->conds->state = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2825 | j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; |
| 2826 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2827 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2828 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2829 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2830 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2831 | case PP_ELSE: |
| 2832 | if (tline->next) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2833 | nasm_warn(WARN_OTHER|ERR_PP_PRECOND, |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2834 | "trailing garbage after `%%else' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2835 | if (!istk->conds) |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 2836 | nasm_fatal("`%%else: no matching `%%if'"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2837 | switch(istk->conds->state) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2838 | case COND_IF_TRUE: |
| 2839 | case COND_DONE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2840 | istk->conds->state = COND_ELSE_FALSE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2841 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2842 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2843 | case COND_NEVER: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2844 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2845 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2846 | case COND_IF_FALSE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2847 | istk->conds->state = COND_ELSE_TRUE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2848 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2849 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2850 | case COND_ELSE_TRUE: |
| 2851 | case COND_ELSE_FALSE: |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2852 | nasm_warn(WARN_OTHER|ERR_PP_PRECOND, |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2853 | "`%%else' after `%%else' ignored."); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2854 | istk->conds->state = COND_NEVER; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2855 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2856 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2857 | free_tlist(origline); |
| 2858 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2859 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2860 | case PP_ENDIF: |
| 2861 | if (tline->next) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2862 | nasm_warn(WARN_OTHER|ERR_PP_PRECOND, |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2863 | "trailing garbage after `%%endif' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2864 | if (!istk->conds) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2865 | nasm_fatal("`%%endif': no matching `%%if'"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2866 | cond = istk->conds; |
| 2867 | istk->conds = cond->next; |
| 2868 | nasm_free(cond); |
| 2869 | if(istk->mstk) |
| 2870 | istk->mstk->condcnt --; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2871 | free_tlist(origline); |
| 2872 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2873 | |
H. Peter Anvin | db8f96e | 2009-07-15 09:07:29 -0400 | [diff] [blame] | 2874 | case PP_IRMACRO: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2875 | case PP_IMACRO: |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2876 | casesense = false; |
| 2877 | /* fall through */ |
| 2878 | case PP_RMACRO: |
| 2879 | case PP_MACRO: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2880 | if (defining) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2881 | nasm_fatal("`%s': already defining a macro", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2882 | return DIRECTIVE_FOUND; |
| 2883 | } |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 2884 | defining = nasm_zalloc(sizeof(MMacro)); |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 2885 | defining->max_depth = ((i == PP_RMACRO) || (i == PP_IRMACRO)) |
| 2886 | ? nasm_limit[LIMIT_MACROS] : 0; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2887 | defining->casesense = casesense; |
| 2888 | if (!parse_mmacro_spec(tline, defining, dname)) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2889 | nasm_free(defining); |
| 2890 | defining = NULL; |
| 2891 | return DIRECTIVE_FOUND; |
| 2892 | } |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2893 | |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 2894 | src_get(&defining->xline, &defining->fname); |
| 2895 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2896 | mmac = (MMacro *) hash_findix(&mmacros, defining->name); |
| 2897 | while (mmac) { |
| 2898 | if (!strcmp(mmac->name, defining->name) && |
| 2899 | (mmac->nparam_min <= defining->nparam_max |
| 2900 | || defining->plus) |
| 2901 | && (defining->nparam_min <= mmac->nparam_max |
| 2902 | || mmac->plus)) { |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2903 | nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'", |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2904 | defining->name); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2905 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2906 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2907 | mmac = mmac->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2908 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2909 | free_tlist(origline); |
| 2910 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2911 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2912 | case PP_ENDM: |
| 2913 | case PP_ENDMACRO: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2914 | if (! (defining && defining->name)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2915 | nasm_nonfatal("`%s': not defining a macro", tline->text); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2916 | return DIRECTIVE_FOUND; |
| 2917 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2918 | mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name); |
| 2919 | defining->next = *mmhead; |
| 2920 | *mmhead = defining; |
| 2921 | defining = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2922 | free_tlist(origline); |
| 2923 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2924 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2925 | case PP_EXITMACRO: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2926 | /* |
| 2927 | * We must search along istk->expansion until we hit a |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2928 | * macro-end marker for a macro with a name. Then we |
| 2929 | * bypass all lines between exitmacro and endmacro. |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2930 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2931 | list_for_each(l, istk->expansion) |
| 2932 | if (l->finishes && l->finishes->name) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2933 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2934 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2935 | if (l) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2936 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2937 | * Remove all conditional entries relative to this |
| 2938 | * macro invocation. (safe to do in this context) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2939 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2940 | for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) { |
| 2941 | cond = istk->conds; |
| 2942 | istk->conds = cond->next; |
| 2943 | nasm_free(cond); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2944 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2945 | istk->expansion = l; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2946 | } else { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2947 | nasm_nonfatal("`%%exitmacro' not within `%%macro' block"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2948 | } |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 2949 | free_tlist(origline); |
| 2950 | return DIRECTIVE_FOUND; |
| 2951 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2952 | case PP_UNIMACRO: |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2953 | casesense = false; |
| 2954 | /* fall through */ |
| 2955 | case PP_UNMACRO: |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2956 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2957 | MMacro **mmac_p; |
| 2958 | MMacro spec; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2959 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2960 | spec.casesense = casesense; |
| 2961 | if (!parse_mmacro_spec(tline, &spec, dname)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2962 | return DIRECTIVE_FOUND; |
| 2963 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2964 | mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL); |
| 2965 | while (mmac_p && *mmac_p) { |
| 2966 | mmac = *mmac_p; |
| 2967 | if (mmac->casesense == spec.casesense && |
| 2968 | !mstrcmp(mmac->name, spec.name, spec.casesense) && |
| 2969 | mmac->nparam_min == spec.nparam_min && |
| 2970 | mmac->nparam_max == spec.nparam_max && |
| 2971 | mmac->plus == spec.plus) { |
| 2972 | *mmac_p = mmac->next; |
| 2973 | free_mmacro(mmac); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2974 | } else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2975 | mmac_p = &mmac->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2976 | } |
| 2977 | } |
| 2978 | free_tlist(origline); |
| 2979 | free_tlist(spec.dlist); |
| 2980 | return DIRECTIVE_FOUND; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2981 | } |
| 2982 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2983 | case PP_ROTATE: |
| 2984 | if (tline->next && tline->next->type == TOK_WHITESPACE) |
| 2985 | tline = tline->next; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2986 | if (!tline->next) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2987 | free_tlist(origline); |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2988 | nasm_nonfatal("`%%rotate' missing rotate count"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2989 | return DIRECTIVE_FOUND; |
| 2990 | } |
| 2991 | t = expand_smacro(tline->next); |
| 2992 | tline->next = NULL; |
| 2993 | free_tlist(origline); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2994 | pps.tptr = tline = t; |
| 2995 | pps.ntokens = -1; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2996 | tokval.t_type = TOKEN_INVALID; |
| 2997 | evalresult = |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2998 | evaluate(ppscan, &pps, &tokval, NULL, true, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2999 | free_tlist(tline); |
| 3000 | if (!evalresult) |
| 3001 | return DIRECTIVE_FOUND; |
| 3002 | if (tokval.t_type) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 3003 | nasm_warn(WARN_OTHER, "trailing garbage after expression ignored"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3004 | if (!is_simple(evalresult)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3005 | nasm_nonfatal("non-constant value given to `%%rotate'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3006 | return DIRECTIVE_FOUND; |
| 3007 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3008 | mmac = istk->mstk; |
| 3009 | while (mmac && !mmac->name) /* avoid mistaking %reps for macros */ |
| 3010 | mmac = mmac->next_active; |
| 3011 | if (!mmac) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3012 | nasm_nonfatal("`%%rotate' invoked outside a macro call"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3013 | } else if (mmac->nparam == 0) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3014 | nasm_nonfatal("`%%rotate' invoked within macro without parameters"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3015 | } else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3016 | int rotate = mmac->rotate + reloc_value(evalresult); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3017 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3018 | rotate %= (int)mmac->nparam; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3019 | if (rotate < 0) |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3020 | rotate += mmac->nparam; |
| 3021 | |
| 3022 | mmac->rotate = rotate; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3023 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3024 | return DIRECTIVE_FOUND; |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 3025 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3026 | case PP_REP: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 3027 | nolist = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3028 | do { |
| 3029 | tline = tline->next; |
| 3030 | } while (tok_type_(tline, TOK_WHITESPACE)); |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 3031 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3032 | if (tok_type_(tline, TOK_ID) && |
| 3033 | nasm_stricmp(tline->text, ".nolist") == 0) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 3034 | nolist = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3035 | do { |
| 3036 | tline = tline->next; |
| 3037 | } while (tok_type_(tline, TOK_WHITESPACE)); |
| 3038 | } |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 3039 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3040 | if (tline) { |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3041 | pps.tptr = expand_smacro(tline); |
| 3042 | pps.ntokens = -1; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3043 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3044 | /* XXX: really critical?! */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3045 | evalresult = |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3046 | evaluate(ppscan, &pps, &tokval, NULL, true, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3047 | if (!evalresult) { |
| 3048 | free_tlist(origline); |
| 3049 | return DIRECTIVE_FOUND; |
| 3050 | } |
| 3051 | if (tokval.t_type) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 3052 | nasm_warn(WARN_OTHER, "trailing garbage after expression ignored"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3053 | if (!is_simple(evalresult)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3054 | nasm_nonfatal("non-constant value given to `%%rep'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3055 | return DIRECTIVE_FOUND; |
| 3056 | } |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 3057 | count = reloc_value(evalresult); |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 3058 | if (count > nasm_limit[LIMIT_REP]) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3059 | nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")", |
| 3060 | count, nasm_limit[LIMIT_REP]); |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 3061 | count = 0; |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 3062 | } else if (count < 0) { |
H. Peter Anvin (Intel) | 723ab48 | 2018-12-13 21:53:31 -0800 | [diff] [blame] | 3063 | /*! |
| 3064 | *!negative-rep [on] regative %rep count |
| 3065 | *! warns about negative counts given to the \c{%rep} |
| 3066 | *! preprocessor directive. |
| 3067 | */ |
H. Peter Anvin (Intel) | 80c4f23 | 2018-12-14 13:33:24 -0800 | [diff] [blame] | 3068 | nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP, |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 3069 | "negative `%%rep' count: %"PRId64, count); |
| 3070 | count = 0; |
| 3071 | } else { |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 3072 | count++; |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 3073 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3074 | } else { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3075 | nasm_nonfatal("`%%rep' expects a repeat count"); |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 3076 | count = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3077 | } |
| 3078 | free_tlist(origline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3079 | |
| 3080 | tmp_defining = defining; |
| 3081 | defining = nasm_malloc(sizeof(MMacro)); |
| 3082 | defining->prev = NULL; |
| 3083 | defining->name = NULL; /* flags this macro as a %rep block */ |
| 3084 | defining->casesense = false; |
| 3085 | defining->plus = false; |
| 3086 | defining->nolist = nolist; |
| 3087 | defining->in_progress = count; |
| 3088 | defining->max_depth = 0; |
| 3089 | defining->nparam_min = defining->nparam_max = 0; |
| 3090 | defining->defaults = NULL; |
| 3091 | defining->dlist = NULL; |
| 3092 | defining->expansion = NULL; |
| 3093 | defining->next_active = istk->mstk; |
| 3094 | defining->rep_nest = tmp_defining; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3095 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3096 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3097 | case PP_ENDREP: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3098 | if (!defining || defining->name) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3099 | nasm_nonfatal("`%%endrep': no matching `%%rep'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3100 | return DIRECTIVE_FOUND; |
| 3101 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3102 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3103 | /* |
| 3104 | * Now we have a "macro" defined - although it has no name |
| 3105 | * and we won't be entering it in the hash tables - we must |
| 3106 | * push a macro-end marker for it on to istk->expansion. |
| 3107 | * After that, it will take care of propagating itself (a |
| 3108 | * macro-end marker line for a macro which is really a %rep |
| 3109 | * block will cause the macro to be re-expanded, complete |
| 3110 | * with another macro-end marker to ensure the process |
| 3111 | * continues) until the whole expansion is forcibly removed |
| 3112 | * from istk->expansion by a %exitrep. |
| 3113 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3114 | l = nasm_malloc(sizeof(Line)); |
| 3115 | l->next = istk->expansion; |
| 3116 | l->finishes = defining; |
| 3117 | l->first = NULL; |
| 3118 | istk->expansion = l; |
| 3119 | |
| 3120 | istk->mstk = defining; |
| 3121 | |
H. Peter Anvin | 0d4d431 | 2019-08-07 00:46:27 -0700 | [diff] [blame] | 3122 | lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3123 | tmp_defining = defining; |
| 3124 | defining = defining->rep_nest; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3125 | free_tlist(origline); |
| 3126 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3127 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3128 | case PP_EXITREP: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3129 | /* |
| 3130 | * We must search along istk->expansion until we hit a |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3131 | * macro-end marker for a macro with no name. Then we set |
| 3132 | * its `in_progress' flag to 0. |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3133 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3134 | list_for_each(l, istk->expansion) |
| 3135 | if (l->finishes && !l->finishes->name) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3136 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3137 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3138 | if (l) |
| 3139 | l->finishes->in_progress = 1; |
| 3140 | else |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3141 | nasm_nonfatal("`%%exitrep' not within `%%rep' block"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3142 | free_tlist(origline); |
| 3143 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3144 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3145 | case PP_IDEFINE: |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3146 | case PP_IXDEFINE: |
| 3147 | casesense = false; |
| 3148 | /* fall through */ |
| 3149 | case PP_DEFINE: |
| 3150 | case PP_XDEFINE: |
| 3151 | { |
| 3152 | SMacro *s; |
| 3153 | bool have_eval_params = false; |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3154 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3155 | tline = tline->next; |
| 3156 | skip_white_(tline); |
| 3157 | tline = expand_id(tline); |
| 3158 | if (!tline || (tline->type != TOK_ID && |
| 3159 | (tline->type != TOK_PREPROC_ID || |
| 3160 | tline->text[1] != '$'))) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3161 | nasm_nonfatal("`%s' expects a macro identifier", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3162 | free_tlist(origline); |
| 3163 | return DIRECTIVE_FOUND; |
| 3164 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3165 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3166 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3167 | last = tline; |
| 3168 | param_start = tline = tline->next; |
| 3169 | nparam = 0; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3170 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3171 | if (tok_is_(tline, "(")) { |
| 3172 | /* |
| 3173 | * This macro has parameters. |
| 3174 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3175 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3176 | tline = tline->next; |
| 3177 | while (1) { |
| 3178 | skip_white_(tline); |
| 3179 | if (!tline) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3180 | nasm_nonfatal("parameter identifier expected"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3181 | free_tlist(origline); |
| 3182 | return DIRECTIVE_FOUND; |
| 3183 | } |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3184 | if (tok_is_(tline, "=")) { |
| 3185 | have_eval_params = true; |
| 3186 | tline = tline->next; |
| 3187 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3188 | if (tline->type != TOK_ID) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3189 | nasm_nonfatal("`%s': parameter identifier expected", |
| 3190 | tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3191 | free_tlist(origline); |
| 3192 | return DIRECTIVE_FOUND; |
| 3193 | } |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3194 | tline->type = tok_smac_param(nparam++); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3195 | tline = tline->next; |
| 3196 | skip_white_(tline); |
| 3197 | if (tok_is_(tline, ",")) { |
| 3198 | tline = tline->next; |
H. Peter Anvin | ca348b6 | 2008-07-23 10:49:26 -0400 | [diff] [blame] | 3199 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3200 | if (!tok_is_(tline, ")")) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3201 | nasm_nonfatal("`)' expected to terminate macro template"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3202 | free_tlist(origline); |
| 3203 | return DIRECTIVE_FOUND; |
| 3204 | } |
| 3205 | break; |
| 3206 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3207 | } |
| 3208 | last = tline; |
| 3209 | tline = tline->next; |
| 3210 | } |
| 3211 | if (tok_type_(tline, TOK_WHITESPACE)) |
| 3212 | last = tline, tline = tline->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3213 | last->next = NULL; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3214 | |
| 3215 | /* Expand the macro definition now for %xdefine and %ixdefine */ |
| 3216 | if ((i == PP_XDEFINE) || (i == PP_IXDEFINE)) |
| 3217 | tline = expand_smacro(tline); |
| 3218 | |
| 3219 | macro_start = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3220 | t = tline; |
| 3221 | while (t) { |
| 3222 | if (t->type == TOK_ID) { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3223 | list_for_each(tt, param_start) |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 3224 | if (is_smac_param(tt->type) && |
| 3225 | tt->len == t->len && |
| 3226 | !memcmp(tt->text, t->text, tt->len)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3227 | t->type = tt->type; |
| 3228 | } |
| 3229 | tt = t->next; |
| 3230 | t->next = macro_start; |
| 3231 | macro_start = t; |
| 3232 | t = tt; |
| 3233 | } |
| 3234 | /* |
| 3235 | * Good. We now have a macro name, a parameter count, and a |
| 3236 | * token list (in reverse order) for an expansion. We ought |
| 3237 | * to be OK just to create an SMacro, store it, and let |
| 3238 | * free_tlist have the rest of the line (which we have |
| 3239 | * carefully re-terminated after chopping off the expansion |
| 3240 | * from the end). |
| 3241 | */ |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3242 | s = define_smacro(ctx, mname, casesense, nparam, macro_start); |
| 3243 | |
| 3244 | if (have_eval_params) { |
| 3245 | /* Create evaluated parameters table */ |
| 3246 | bool is_eval = false; |
| 3247 | |
| 3248 | nasm_newn(s->eval_param, nparam); |
| 3249 | list_for_each(tt, param_start) { |
| 3250 | if (is_smac_param(tt->type)) |
| 3251 | s->eval_param[smac_nparam(tt->type)] = is_eval; |
| 3252 | is_eval = tok_is_(tt, "="); |
| 3253 | } |
| 3254 | } |
| 3255 | |
| 3256 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3257 | free_tlist(origline); |
| 3258 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3259 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3260 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3261 | case PP_UNDEF: |
| 3262 | tline = tline->next; |
| 3263 | skip_white_(tline); |
| 3264 | tline = expand_id(tline); |
| 3265 | if (!tline || (tline->type != TOK_ID && |
| 3266 | (tline->type != TOK_PREPROC_ID || |
| 3267 | tline->text[1] != '$'))) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3268 | nasm_nonfatal("`%%undef' expects a macro identifier"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3269 | free_tlist(origline); |
| 3270 | return DIRECTIVE_FOUND; |
| 3271 | } |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3272 | if (tline->next) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 3273 | nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored"); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3274 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3275 | /* Find the context that symbol belongs to */ |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3276 | ctx = get_ctx(tline->text, &mname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3277 | undef_smacro(ctx, mname); |
| 3278 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3279 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3280 | |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3281 | case PP_IDEFSTR: |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3282 | casesense = false; |
| 3283 | /* fall through */ |
| 3284 | case PP_DEFSTR: |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3285 | tline = tline->next; |
| 3286 | skip_white_(tline); |
| 3287 | tline = expand_id(tline); |
| 3288 | if (!tline || (tline->type != TOK_ID && |
| 3289 | (tline->type != TOK_PREPROC_ID || |
| 3290 | tline->text[1] != '$'))) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3291 | nasm_nonfatal("`%s' expects a macro identifier", dname); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3292 | free_tlist(origline); |
| 3293 | return DIRECTIVE_FOUND; |
| 3294 | } |
| 3295 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3296 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3297 | last = tline; |
| 3298 | tline = expand_smacro(tline->next); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3299 | last->next = NULL; |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3300 | |
| 3301 | while (tok_type_(tline, TOK_WHITESPACE)) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3302 | tline = delete_Token(tline); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3303 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3304 | p = detoken(tline, false); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3305 | macro_start = make_tok_qstr(p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3306 | nasm_free(p); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3307 | |
| 3308 | /* |
| 3309 | * We now have a macro name, an implicit parameter count of |
| 3310 | * zero, and a string token to use as an expansion. Create |
| 3311 | * and store an SMacro. |
| 3312 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3313 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3314 | free_tlist(origline); |
| 3315 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3316 | |
H. Peter Anvin | 2f55bda | 2009-07-14 15:04:04 -0400 | [diff] [blame] | 3317 | case PP_IDEFTOK: |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3318 | casesense = false; |
| 3319 | /* fall through */ |
| 3320 | case PP_DEFTOK: |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3321 | tline = tline->next; |
| 3322 | skip_white_(tline); |
| 3323 | tline = expand_id(tline); |
| 3324 | if (!tline || (tline->type != TOK_ID && |
| 3325 | (tline->type != TOK_PREPROC_ID || |
| 3326 | tline->text[1] != '$'))) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3327 | nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3328 | free_tlist(origline); |
| 3329 | return DIRECTIVE_FOUND; |
| 3330 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3331 | ctx = get_ctx(tline->text, &mname); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3332 | last = tline; |
| 3333 | tline = expand_smacro(tline->next); |
| 3334 | last->next = NULL; |
| 3335 | |
| 3336 | t = tline; |
| 3337 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3338 | t = t->next; |
| 3339 | /* t should now point to the string */ |
Cyrill Gorcunov | 6908e58 | 2010-09-06 19:36:15 +0400 | [diff] [blame] | 3340 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3341 | nasm_nonfatal("`%s` requires string as second parameter", dname); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3342 | free_tlist(tline); |
| 3343 | free_tlist(origline); |
| 3344 | return DIRECTIVE_FOUND; |
| 3345 | } |
| 3346 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 3347 | /* |
| 3348 | * Convert the string to a token stream. Note that smacros |
| 3349 | * are stored with the token stream reversed, so we have to |
| 3350 | * reverse the output of tokenize(). |
| 3351 | */ |
H. Peter Anvin | bb42d30 | 2019-04-22 14:29:29 -0700 | [diff] [blame] | 3352 | nasm_unquote_cstr(t->text, NULL); |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 3353 | macro_start = reverse_tokens(tokenize(t->text)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3354 | |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3355 | /* |
| 3356 | * We now have a macro name, an implicit parameter count of |
| 3357 | * zero, and a numeric token to use as an expansion. Create |
| 3358 | * and store an SMacro. |
| 3359 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3360 | define_smacro(ctx, mname, casesense, 0, macro_start); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3361 | free_tlist(tline); |
| 3362 | free_tlist(origline); |
| 3363 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3364 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3365 | case PP_IPATHSEARCH: |
| 3366 | casesense = false; |
| 3367 | /* fall through */ |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3368 | case PP_PATHSEARCH: |
| 3369 | { |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 3370 | const char *found_path; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3371 | |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3372 | tline = tline->next; |
| 3373 | skip_white_(tline); |
| 3374 | tline = expand_id(tline); |
| 3375 | if (!tline || (tline->type != TOK_ID && |
| 3376 | (tline->type != TOK_PREPROC_ID || |
| 3377 | tline->text[1] != '$'))) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3378 | nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3379 | free_tlist(origline); |
| 3380 | return DIRECTIVE_FOUND; |
| 3381 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3382 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3383 | last = tline; |
| 3384 | tline = expand_smacro(tline->next); |
| 3385 | last->next = NULL; |
| 3386 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3387 | t = tline; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3388 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3389 | t = t->next; |
| 3390 | |
| 3391 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3392 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3393 | nasm_nonfatal("`%s' expects a file name", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3394 | free_tlist(tline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3395 | free_tlist(origline); |
| 3396 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 3397 | } |
| 3398 | if (t->next) |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3399 | nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3400 | p = t->text; |
H. Peter Anvin | 427cc91 | 2008-06-01 21:43:03 -0700 | [diff] [blame] | 3401 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3402 | nasm_unquote(p, NULL); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3403 | |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 3404 | inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY); |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 3405 | if (!found_path) |
| 3406 | found_path = p; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3407 | macro_start = make_tok_qstr(found_path); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3408 | |
| 3409 | /* |
| 3410 | * We now have a macro name, an implicit parameter count of |
| 3411 | * zero, and a string token to use as an expansion. Create |
| 3412 | * and store an SMacro. |
| 3413 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3414 | define_smacro(ctx, mname, casesense, 0, macro_start); |
| 3415 | free_tlist(tline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3416 | free_tlist(origline); |
| 3417 | return DIRECTIVE_FOUND; |
| 3418 | } |
| 3419 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3420 | case PP_ISTRLEN: |
| 3421 | casesense = false; |
| 3422 | /* fall through */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3423 | case PP_STRLEN: |
| 3424 | tline = tline->next; |
| 3425 | skip_white_(tline); |
| 3426 | tline = expand_id(tline); |
| 3427 | if (!tline || (tline->type != TOK_ID && |
| 3428 | (tline->type != TOK_PREPROC_ID || |
| 3429 | tline->text[1] != '$'))) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3430 | nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3431 | free_tlist(origline); |
| 3432 | return DIRECTIVE_FOUND; |
| 3433 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3434 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3435 | last = tline; |
| 3436 | tline = expand_smacro(tline->next); |
| 3437 | last->next = NULL; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3438 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3439 | t = tline; |
| 3440 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3441 | t = t->next; |
| 3442 | /* t should now point to the string */ |
Cyrill Gorcunov | 4e1d5ab | 2010-07-23 18:51:51 +0400 | [diff] [blame] | 3443 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3444 | nasm_nonfatal("`%s' requires string as second parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3445 | free_tlist(tline); |
| 3446 | free_tlist(origline); |
| 3447 | return DIRECTIVE_FOUND; |
| 3448 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3449 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3450 | macro_start = make_tok_num(nasm_unquote(t->text, NULL)); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3451 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3452 | /* |
| 3453 | * We now have a macro name, an implicit parameter count of |
| 3454 | * zero, and a numeric token to use as an expansion. Create |
| 3455 | * and store an SMacro. |
| 3456 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3457 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3458 | free_tlist(tline); |
| 3459 | free_tlist(origline); |
| 3460 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3461 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3462 | case PP_ISTRCAT: |
| 3463 | casesense = false; |
| 3464 | /* fall through */ |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3465 | case PP_STRCAT: |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3466 | tline = tline->next; |
| 3467 | skip_white_(tline); |
| 3468 | tline = expand_id(tline); |
| 3469 | if (!tline || (tline->type != TOK_ID && |
| 3470 | (tline->type != TOK_PREPROC_ID || |
| 3471 | tline->text[1] != '$'))) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3472 | nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3473 | free_tlist(origline); |
| 3474 | return DIRECTIVE_FOUND; |
| 3475 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3476 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3477 | last = tline; |
| 3478 | tline = expand_smacro(tline->next); |
| 3479 | last->next = NULL; |
| 3480 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3481 | len = 0; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3482 | list_for_each(t, tline) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3483 | switch (t->type) { |
| 3484 | case TOK_WHITESPACE: |
| 3485 | break; |
| 3486 | case TOK_STRING: |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 3487 | len += t->len = nasm_unquote(t->text, NULL); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3488 | break; |
| 3489 | case TOK_OTHER: |
| 3490 | if (!strcmp(t->text, ",")) /* permit comma separators */ |
| 3491 | break; |
| 3492 | /* else fall through */ |
| 3493 | default: |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3494 | nasm_nonfatal("non-string passed to `%s': %s", dname, t->text); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3495 | free_tlist(tline); |
| 3496 | free_tlist(origline); |
| 3497 | return DIRECTIVE_FOUND; |
| 3498 | } |
| 3499 | } |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3500 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3501 | p = pp = nasm_malloc(len); |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3502 | list_for_each(t, tline) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3503 | if (t->type == TOK_STRING) { |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 3504 | memcpy(p, t->text, t->len); |
| 3505 | p += t->len; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3506 | } |
| 3507 | } |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3508 | |
| 3509 | /* |
| 3510 | * We now have a macro name, an implicit parameter count of |
| 3511 | * zero, and a numeric token to use as an expansion. Create |
| 3512 | * and store an SMacro. |
| 3513 | */ |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3514 | macro_start = make_tok_qstr(pp); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3515 | nasm_free(pp); |
| 3516 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3517 | free_tlist(tline); |
| 3518 | free_tlist(origline); |
| 3519 | return DIRECTIVE_FOUND; |
| 3520 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3521 | case PP_ISUBSTR: |
| 3522 | casesense = false; |
| 3523 | /* fall through */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3524 | case PP_SUBSTR: |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3525 | { |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3526 | int64_t start, count; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3527 | size_t len; |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 3528 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3529 | tline = tline->next; |
| 3530 | skip_white_(tline); |
| 3531 | tline = expand_id(tline); |
| 3532 | if (!tline || (tline->type != TOK_ID && |
| 3533 | (tline->type != TOK_PREPROC_ID || |
| 3534 | tline->text[1] != '$'))) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3535 | nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3536 | free_tlist(origline); |
| 3537 | return DIRECTIVE_FOUND; |
| 3538 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3539 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3540 | last = tline; |
| 3541 | tline = expand_smacro(tline->next); |
| 3542 | last->next = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3543 | |
Cyrill Gorcunov | 35519d6 | 2010-09-06 23:49:52 +0400 | [diff] [blame] | 3544 | if (tline) /* skip expanded id */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3545 | t = tline->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3546 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3547 | t = t->next; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3548 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3549 | /* t should now point to the string */ |
Cyrill Gorcunov | 35519d6 | 2010-09-06 23:49:52 +0400 | [diff] [blame] | 3550 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3551 | nasm_nonfatal("`%s' requires string as second parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3552 | free_tlist(tline); |
| 3553 | free_tlist(origline); |
| 3554 | return DIRECTIVE_FOUND; |
| 3555 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3556 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3557 | pps.tptr = t->next; |
| 3558 | pps.ntokens = -1; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3559 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3560 | evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3561 | if (!evalresult) { |
| 3562 | free_tlist(tline); |
| 3563 | free_tlist(origline); |
| 3564 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3565 | } else if (!is_simple(evalresult)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3566 | nasm_nonfatal("non-constant value given to `%s'", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3567 | free_tlist(tline); |
| 3568 | free_tlist(origline); |
| 3569 | return DIRECTIVE_FOUND; |
| 3570 | } |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3571 | start = evalresult->value - 1; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3572 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3573 | while (tok_type_(pps.tptr, TOK_WHITESPACE)) |
| 3574 | pps.tptr = pps.tptr->next; |
| 3575 | if (!pps.tptr) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3576 | count = 1; /* Backwards compatibility: one character */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3577 | } else { |
| 3578 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3579 | evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3580 | if (!evalresult) { |
| 3581 | free_tlist(tline); |
| 3582 | free_tlist(origline); |
| 3583 | return DIRECTIVE_FOUND; |
| 3584 | } else if (!is_simple(evalresult)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3585 | nasm_nonfatal("non-constant value given to `%s'", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3586 | free_tlist(tline); |
| 3587 | free_tlist(origline); |
| 3588 | return DIRECTIVE_FOUND; |
| 3589 | } |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3590 | count = evalresult->value; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3591 | } |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3592 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3593 | len = nasm_unquote(t->text, NULL); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3594 | |
Cyrill Gorcunov | cff031e | 2010-09-07 20:31:11 +0400 | [diff] [blame] | 3595 | /* make start and count being in range */ |
| 3596 | if (start < 0) |
| 3597 | start = 0; |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3598 | if (count < 0) |
| 3599 | count = len + count + 1 - start; |
| 3600 | if (start + count > (int64_t)len) |
Cyrill Gorcunov | cff031e | 2010-09-07 20:31:11 +0400 | [diff] [blame] | 3601 | count = len - start; |
| 3602 | if (!len || count < 0 || start >=(int64_t)len) |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3603 | start = -1, count = 0; /* empty string */ |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3604 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3605 | macro_start = new_Token(NULL, TOK_STRING, NULL, 0); |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 3606 | macro_start->len = count; |
| 3607 | macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, |
| 3608 | ¯o_start->len); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3609 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3610 | /* |
| 3611 | * We now have a macro name, an implicit parameter count of |
| 3612 | * zero, and a numeric token to use as an expansion. Create |
| 3613 | * and store an SMacro. |
| 3614 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3615 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3616 | free_tlist(tline); |
| 3617 | free_tlist(origline); |
| 3618 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3619 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3620 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3621 | case PP_IASSIGN: |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3622 | casesense = false; |
| 3623 | /* fall through */ |
| 3624 | case PP_ASSIGN: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3625 | tline = tline->next; |
| 3626 | skip_white_(tline); |
| 3627 | tline = expand_id(tline); |
| 3628 | if (!tline || (tline->type != TOK_ID && |
| 3629 | (tline->type != TOK_PREPROC_ID || |
| 3630 | tline->text[1] != '$'))) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3631 | nasm_nonfatal("`%s' expects a macro identifier", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3632 | free_tlist(origline); |
| 3633 | return DIRECTIVE_FOUND; |
| 3634 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3635 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3636 | last = tline; |
| 3637 | tline = expand_smacro(tline->next); |
| 3638 | last->next = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3639 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3640 | pps.tptr = tline; |
| 3641 | pps.ntokens = -1; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3642 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3643 | evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3644 | free_tlist(tline); |
| 3645 | if (!evalresult) { |
| 3646 | free_tlist(origline); |
| 3647 | return DIRECTIVE_FOUND; |
| 3648 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3649 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3650 | if (tokval.t_type) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 3651 | nasm_warn(WARN_OTHER, "trailing garbage after expression ignored"); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3652 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3653 | if (!is_simple(evalresult)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3654 | nasm_nonfatal("non-constant value given to `%s'", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3655 | free_tlist(origline); |
| 3656 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3657 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3658 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3659 | macro_start = make_tok_num(reloc_value(evalresult)); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3660 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3661 | /* |
| 3662 | * We now have a macro name, an implicit parameter count of |
| 3663 | * zero, and a numeric token to use as an expansion. Create |
| 3664 | * and store an SMacro. |
| 3665 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3666 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3667 | free_tlist(origline); |
| 3668 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3669 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3670 | case PP_LINE: |
| 3671 | /* |
| 3672 | * Syntax is `%line nnn[+mmm] [filename]' |
| 3673 | */ |
H. Peter Anvin (Intel) | 800c168 | 2018-12-14 12:22:11 -0800 | [diff] [blame] | 3674 | if (unlikely(pp_noline)) { |
| 3675 | free_tlist(origline); |
| 3676 | return DIRECTIVE_FOUND; |
| 3677 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3678 | tline = tline->next; |
| 3679 | skip_white_(tline); |
| 3680 | if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3681 | nasm_nonfatal("`%s' expects line number", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3682 | free_tlist(origline); |
| 3683 | return DIRECTIVE_FOUND; |
| 3684 | } |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 3685 | k = readnum(tline->text, &err); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3686 | m = 1; |
| 3687 | tline = tline->next; |
| 3688 | if (tok_is_(tline, "+")) { |
| 3689 | tline = tline->next; |
| 3690 | if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3691 | nasm_nonfatal("`%s' expects line increment", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3692 | free_tlist(origline); |
| 3693 | return DIRECTIVE_FOUND; |
| 3694 | } |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 3695 | m = readnum(tline->text, &err); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3696 | tline = tline->next; |
| 3697 | } |
| 3698 | skip_white_(tline); |
| 3699 | src_set_linnum(k); |
| 3700 | istk->lineinc = m; |
| 3701 | if (tline) { |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 3702 | char *fname = detoken(tline, false); |
| 3703 | src_set_fname(fname); |
| 3704 | nasm_free(fname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3705 | } |
| 3706 | free_tlist(origline); |
| 3707 | return DIRECTIVE_FOUND; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3708 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3709 | default: |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3710 | nasm_nonfatal("preprocessor directive `%s' not yet implemented", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3711 | return DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3712 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3713 | } |
| 3714 | |
| 3715 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3716 | * Ensure that a macro parameter contains a condition code and |
| 3717 | * nothing else. Return the condition code index if so, or -1 |
| 3718 | * otherwise. |
| 3719 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3720 | static int find_cc(Token * t) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3721 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3722 | Token *tt; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3723 | |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3724 | if (!t) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3725 | return -1; /* Probably a %+ without a space */ |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3726 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3727 | skip_white_(t); |
Cyrill Gorcunov | 7524cfd | 2017-10-22 19:01:16 +0300 | [diff] [blame] | 3728 | if (!t) |
| 3729 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3730 | if (t->type != TOK_ID) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3731 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3732 | tt = t->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3733 | skip_white_(tt); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3734 | if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ","))) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3735 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3736 | |
Cyrill Gorcunov | 1945639 | 2012-05-02 00:18:56 +0400 | [diff] [blame] | 3737 | return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3738 | } |
| 3739 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3740 | /* |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 3741 | * This routines walks over tokens strem and handles tokens |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3742 | * pasting, if @handle_explicit passed then explicit pasting |
| 3743 | * term is handled, otherwise -- implicit pastings only. |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 3744 | * The @m array can contain a series of token types which are |
| 3745 | * executed as separate passes. |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3746 | */ |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 3747 | static bool paste_tokens(Token **head, const struct tokseq_match *m, |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3748 | size_t mnum, bool handle_explicit) |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3749 | { |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3750 | Token *tok, *next, **prev_next, **prev_nonspace; |
| 3751 | bool pasted = false; |
| 3752 | char *buf, *p; |
| 3753 | size_t len, i; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3754 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3755 | /* |
| 3756 | * The last token before pasting. We need it |
| 3757 | * to be able to connect new handled tokens. |
| 3758 | * In other words if there were a tokens stream |
| 3759 | * |
| 3760 | * A -> B -> C -> D |
| 3761 | * |
| 3762 | * and we've joined tokens B and C, the resulting |
| 3763 | * stream should be |
| 3764 | * |
| 3765 | * A -> BC -> D |
| 3766 | */ |
| 3767 | tok = *head; |
| 3768 | prev_next = NULL; |
| 3769 | |
| 3770 | if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE)) |
| 3771 | prev_nonspace = head; |
| 3772 | else |
| 3773 | prev_nonspace = NULL; |
| 3774 | |
| 3775 | while (tok && (next = tok->next)) { |
| 3776 | |
| 3777 | switch (tok->type) { |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3778 | case TOK_WHITESPACE: |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3779 | /* Zap redundant whitespaces */ |
| 3780 | while (tok_type_(next, TOK_WHITESPACE)) |
| 3781 | next = delete_Token(next); |
| 3782 | tok->next = next; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3783 | break; |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3784 | |
| 3785 | case TOK_PASTE: |
| 3786 | /* Explicit pasting */ |
| 3787 | if (!handle_explicit) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3788 | break; |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3789 | next = delete_Token(tok); |
| 3790 | |
| 3791 | while (tok_type_(next, TOK_WHITESPACE)) |
| 3792 | next = delete_Token(next); |
| 3793 | |
| 3794 | if (!pasted) |
| 3795 | pasted = true; |
| 3796 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3797 | /* Left pasting token is start of line */ |
| 3798 | if (!prev_nonspace) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3799 | nasm_fatal("No lvalue found on pasting"); |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3800 | |
Cyrill Gorcunov | 8b5c9fb | 2013-02-04 01:24:54 +0400 | [diff] [blame] | 3801 | /* |
| 3802 | * No ending token, this might happen in two |
| 3803 | * cases |
| 3804 | * |
| 3805 | * 1) There indeed no right token at all |
| 3806 | * 2) There is a bare "%define ID" statement, |
| 3807 | * and @ID does expand to whitespace. |
| 3808 | * |
| 3809 | * So technically we need to do a grammar analysis |
| 3810 | * in another stage of parsing, but for now lets don't |
| 3811 | * change the behaviour people used to. Simply allow |
| 3812 | * whitespace after paste token. |
| 3813 | */ |
| 3814 | if (!next) { |
| 3815 | /* |
| 3816 | * Zap ending space tokens and that's all. |
| 3817 | */ |
| 3818 | tok = (*prev_nonspace)->next; |
| 3819 | while (tok_type_(tok, TOK_WHITESPACE)) |
| 3820 | tok = delete_Token(tok); |
| 3821 | tok = *prev_nonspace; |
| 3822 | tok->next = NULL; |
| 3823 | break; |
| 3824 | } |
| 3825 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3826 | tok = *prev_nonspace; |
| 3827 | while (tok_type_(tok, TOK_WHITESPACE)) |
| 3828 | tok = delete_Token(tok); |
| 3829 | len = strlen(tok->text); |
| 3830 | len += strlen(next->text); |
| 3831 | |
| 3832 | p = buf = nasm_malloc(len + 1); |
| 3833 | strcpy(p, tok->text); |
| 3834 | p = strchr(p, '\0'); |
| 3835 | strcpy(p, next->text); |
| 3836 | |
| 3837 | delete_Token(tok); |
| 3838 | |
| 3839 | tok = tokenize(buf); |
| 3840 | nasm_free(buf); |
| 3841 | |
| 3842 | *prev_nonspace = tok; |
| 3843 | while (tok && tok->next) |
| 3844 | tok = tok->next; |
| 3845 | |
| 3846 | tok->next = delete_Token(next); |
| 3847 | |
| 3848 | /* Restart from pasted tokens head */ |
| 3849 | tok = *prev_nonspace; |
| 3850 | break; |
| 3851 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3852 | default: |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3853 | /* implicit pasting */ |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 3854 | for (i = 0; i < mnum; i++) { |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3855 | if (!(PP_CONCAT_MATCH(tok, m[i].mask_head))) |
| 3856 | continue; |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 3857 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3858 | len = 0; |
| 3859 | while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) { |
| 3860 | len += strlen(next->text); |
| 3861 | next = next->next; |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 3862 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3863 | |
Cyrill Gorcunov | 6f8109e | 2017-10-22 21:26:36 +0300 | [diff] [blame] | 3864 | /* No match or no text to process */ |
| 3865 | if (tok == next || len == 0) |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3866 | break; |
| 3867 | |
| 3868 | len += strlen(tok->text); |
| 3869 | p = buf = nasm_malloc(len + 1); |
| 3870 | |
Adam Majer | 1a06943 | 2017-07-25 11:12:35 +0200 | [diff] [blame] | 3871 | strcpy(p, tok->text); |
| 3872 | p = strchr(p, '\0'); |
| 3873 | tok = delete_Token(tok); |
| 3874 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3875 | while (tok != next) { |
Adam Majer | 1a06943 | 2017-07-25 11:12:35 +0200 | [diff] [blame] | 3876 | if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) { |
| 3877 | strcpy(p, tok->text); |
| 3878 | p = strchr(p, '\0'); |
| 3879 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3880 | tok = delete_Token(tok); |
| 3881 | } |
| 3882 | |
| 3883 | tok = tokenize(buf); |
| 3884 | nasm_free(buf); |
| 3885 | |
| 3886 | if (prev_next) |
| 3887 | *prev_next = tok; |
| 3888 | else |
| 3889 | *head = tok; |
| 3890 | |
| 3891 | /* |
| 3892 | * Connect pasted into original stream, |
| 3893 | * ie A -> new-tokens -> B |
| 3894 | */ |
| 3895 | while (tok && tok->next) |
| 3896 | tok = tok->next; |
| 3897 | tok->next = next; |
| 3898 | |
| 3899 | if (!pasted) |
| 3900 | pasted = true; |
| 3901 | |
| 3902 | /* Restart from pasted tokens head */ |
| 3903 | tok = prev_next ? *prev_next : *head; |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 3904 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3905 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3906 | break; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3907 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3908 | |
| 3909 | prev_next = &tok->next; |
| 3910 | |
| 3911 | if (tok->next && |
| 3912 | !tok_type_(tok->next, TOK_WHITESPACE) && |
| 3913 | !tok_type_(tok->next, TOK_PASTE)) |
| 3914 | prev_nonspace = prev_next; |
| 3915 | |
| 3916 | tok = tok->next; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3917 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3918 | |
| 3919 | return pasted; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3920 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3921 | |
| 3922 | /* |
| 3923 | * expands to a list of tokens from %{x:y} |
| 3924 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3925 | static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3926 | { |
| 3927 | Token *t = tline, **tt, *tm, *head; |
| 3928 | char *pos; |
| 3929 | int fst, lst, j, i; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3930 | |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3931 | pos = strchr(tline->text, ':'); |
| 3932 | nasm_assert(pos); |
| 3933 | |
| 3934 | lst = atoi(pos + 1); |
| 3935 | fst = atoi(tline->text + 1); |
| 3936 | |
| 3937 | /* |
| 3938 | * only macros params are accounted so |
| 3939 | * if someone passes %0 -- we reject such |
| 3940 | * value(s) |
| 3941 | */ |
| 3942 | if (lst == 0 || fst == 0) |
| 3943 | goto err; |
| 3944 | |
| 3945 | /* the values should be sane */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3946 | if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) || |
| 3947 | (lst > (int)mac->nparam || lst < (-(int)mac->nparam))) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3948 | goto err; |
| 3949 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3950 | fst = fst < 0 ? fst + (int)mac->nparam + 1: fst; |
| 3951 | lst = lst < 0 ? lst + (int)mac->nparam + 1: lst; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3952 | |
| 3953 | /* counted from zero */ |
| 3954 | fst--, lst--; |
| 3955 | |
| 3956 | /* |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3957 | * It will be at least one token. Note we |
| 3958 | * need to scan params until separator, otherwise |
| 3959 | * only first token will be passed. |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3960 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3961 | tm = mac->params[(fst + mac->rotate) % mac->nparam]; |
Cyrill Gorcunov | 67f2ca2 | 2018-10-13 19:41:01 +0300 | [diff] [blame] | 3962 | if (!tm) |
| 3963 | goto err; |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 3964 | head = dup_Token(NULL, tm); |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3965 | tt = &head->next, tm = tm->next; |
| 3966 | while (tok_isnt_(tm, ",")) { |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 3967 | t = dup_Token(NULL, tm); |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3968 | *tt = t, tt = &t->next, tm = tm->next; |
| 3969 | } |
| 3970 | |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3971 | if (fst < lst) { |
| 3972 | for (i = fst + 1; i <= lst; i++) { |
| 3973 | t = new_Token(NULL, TOK_OTHER, ",", 0); |
| 3974 | *tt = t, tt = &t->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3975 | j = (i + mac->rotate) % mac->nparam; |
| 3976 | tm = mac->params[j]; |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3977 | while (tok_isnt_(tm, ",")) { |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 3978 | t = dup_Token(NULL, tm); |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3979 | *tt = t, tt = &t->next, tm = tm->next; |
| 3980 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3981 | } |
| 3982 | } else { |
| 3983 | for (i = fst - 1; i >= lst; i--) { |
| 3984 | t = new_Token(NULL, TOK_OTHER, ",", 0); |
| 3985 | *tt = t, tt = &t->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3986 | j = (i + mac->rotate) % mac->nparam; |
| 3987 | tm = mac->params[j]; |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3988 | while (tok_isnt_(tm, ",")) { |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 3989 | t = dup_Token(NULL, tm); |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3990 | *tt = t, tt = &t->next, tm = tm->next; |
| 3991 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3992 | } |
| 3993 | } |
| 3994 | |
| 3995 | *last = tt; |
| 3996 | return head; |
| 3997 | |
| 3998 | err: |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3999 | nasm_nonfatal("`%%{%s}': macro parameters out of range", |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4000 | &tline->text[1]); |
| 4001 | return tline; |
| 4002 | } |
| 4003 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4004 | /* |
| 4005 | * Expand MMacro-local things: parameter references (%0, %n, %+n, |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 4006 | * %-n) and MMacro-local identifiers (%%foo) as well as |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4007 | * macro indirection (%[...]) and range (%{..:..}). |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4008 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4009 | static Token *expand_mmac_params(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4010 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4011 | Token *t, *tt, **tail, *thead; |
H. Peter Anvin | 6125b62 | 2009-04-08 14:02:25 -0700 | [diff] [blame] | 4012 | bool changed = false; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4013 | char *pos; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4014 | |
| 4015 | tail = &thead; |
| 4016 | thead = NULL; |
| 4017 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4018 | while (tline) { |
Cyrill Gorcunov | 661f723 | 2018-10-28 20:39:34 +0300 | [diff] [blame] | 4019 | if (tline->type == TOK_PREPROC_ID && tline->text && tline->text[0] && |
Cyrill Gorcunov | ca61119 | 2010-06-04 09:22:12 +0400 | [diff] [blame] | 4020 | (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) || |
| 4021 | (tline->text[1] >= '0' && tline->text[1] <= '9') || |
| 4022 | tline->text[1] == '%')) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4023 | char *text = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4024 | int type = 0, cc; /* type = 0 to placate optimisers */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4025 | char tmpbuf[30]; |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 4026 | unsigned int n; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4027 | int i; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4028 | MMacro *mac; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4029 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4030 | t = tline; |
| 4031 | tline = tline->next; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4032 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4033 | mac = istk->mstk; |
| 4034 | while (mac && !mac->name) /* avoid mistaking %reps for macros */ |
| 4035 | mac = mac->next_active; |
| 4036 | if (!mac) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4037 | nasm_nonfatal("`%s': not in a macro call", t->text); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4038 | } else { |
| 4039 | pos = strchr(t->text, ':'); |
| 4040 | if (!pos) { |
| 4041 | switch (t->text[1]) { |
| 4042 | /* |
| 4043 | * We have to make a substitution of one of the |
| 4044 | * forms %1, %-1, %+1, %%foo, %0. |
| 4045 | */ |
| 4046 | case '0': |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4047 | type = TOK_NUMBER; |
| 4048 | snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam); |
| 4049 | text = nasm_strdup(tmpbuf); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4050 | break; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4051 | case '%': |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4052 | type = TOK_ID; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4053 | snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".", |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4054 | mac->unique); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4055 | text = nasm_strcat(tmpbuf, t->text + 2); |
| 4056 | break; |
| 4057 | case '-': |
| 4058 | n = atoi(t->text + 2) - 1; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4059 | if (n >= mac->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4060 | tt = NULL; |
| 4061 | else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4062 | if (mac->nparam > 1) |
| 4063 | n = (n + mac->rotate) % mac->nparam; |
| 4064 | tt = mac->params[n]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4065 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4066 | cc = find_cc(tt); |
| 4067 | if (cc == -1) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4068 | nasm_nonfatal("macro parameter %d is not a condition code", |
| 4069 | n + 1); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4070 | text = NULL; |
| 4071 | } else { |
| 4072 | type = TOK_ID; |
| 4073 | if (inverse_ccs[cc] == -1) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4074 | nasm_nonfatal("condition code `%s' is not invertible", |
| 4075 | conditions[cc]); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4076 | text = NULL; |
| 4077 | } else |
| 4078 | text = nasm_strdup(conditions[inverse_ccs[cc]]); |
| 4079 | } |
| 4080 | break; |
| 4081 | case '+': |
| 4082 | n = atoi(t->text + 2) - 1; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4083 | if (n >= mac->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4084 | tt = NULL; |
| 4085 | else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4086 | if (mac->nparam > 1) |
| 4087 | n = (n + mac->rotate) % mac->nparam; |
| 4088 | tt = mac->params[n]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4089 | } |
| 4090 | cc = find_cc(tt); |
| 4091 | if (cc == -1) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4092 | nasm_nonfatal("macro parameter %d is not a condition code", |
| 4093 | n + 1); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4094 | text = NULL; |
| 4095 | } else { |
| 4096 | type = TOK_ID; |
| 4097 | text = nasm_strdup(conditions[cc]); |
| 4098 | } |
| 4099 | break; |
| 4100 | default: |
| 4101 | n = atoi(t->text + 1) - 1; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4102 | if (n >= mac->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4103 | tt = NULL; |
| 4104 | else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4105 | if (mac->nparam > 1) |
| 4106 | n = (n + mac->rotate) % mac->nparam; |
| 4107 | tt = mac->params[n]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4108 | } |
| 4109 | if (tt) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4110 | for (i = 0; i < mac->paramlen[n]; i++) { |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4111 | *tail = dup_Token(NULL, tt); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4112 | tail = &(*tail)->next; |
| 4113 | tt = tt->next; |
| 4114 | } |
| 4115 | } |
| 4116 | text = NULL; /* we've done it here */ |
| 4117 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4118 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4119 | } else { |
| 4120 | /* |
| 4121 | * seems we have a parameters range here |
| 4122 | */ |
| 4123 | Token *head, **last; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4124 | head = expand_mmac_params_range(mac, t, &last); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4125 | if (head != t) { |
| 4126 | *tail = head; |
| 4127 | *last = tline; |
| 4128 | tline = head; |
| 4129 | text = NULL; |
| 4130 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4131 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4132 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4133 | if (!text) { |
| 4134 | delete_Token(t); |
| 4135 | } else { |
| 4136 | *tail = t; |
| 4137 | tail = &t->next; |
| 4138 | t->type = type; |
| 4139 | nasm_free(t->text); |
| 4140 | t->text = text; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4141 | t->len = strlen(text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4142 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4143 | changed = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4144 | continue; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4145 | } else if (tline->type == TOK_INDIRECT) { |
| 4146 | t = tline; |
| 4147 | tline = tline->next; |
| 4148 | tt = tokenize(t->text); |
| 4149 | tt = expand_mmac_params(tt); |
| 4150 | tt = expand_smacro(tt); |
| 4151 | *tail = tt; |
| 4152 | while (tt) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4153 | tail = &tt->next; |
| 4154 | tt = tt->next; |
| 4155 | } |
| 4156 | delete_Token(t); |
| 4157 | changed = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4158 | } else { |
| 4159 | t = *tail = tline; |
| 4160 | tline = tline->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4161 | tail = &t->next; |
| 4162 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4163 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4164 | *tail = NULL; |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 4165 | |
Cyrill Gorcunov | c6a742c | 2011-06-27 01:23:09 +0400 | [diff] [blame] | 4166 | if (changed) { |
| 4167 | const struct tokseq_match t[] = { |
| 4168 | { |
| 4169 | PP_CONCAT_MASK(TOK_ID) | |
| 4170 | PP_CONCAT_MASK(TOK_FLOAT), /* head */ |
| 4171 | PP_CONCAT_MASK(TOK_ID) | |
| 4172 | PP_CONCAT_MASK(TOK_NUMBER) | |
| 4173 | PP_CONCAT_MASK(TOK_FLOAT) | |
| 4174 | PP_CONCAT_MASK(TOK_OTHER) /* tail */ |
| 4175 | }, |
| 4176 | { |
| 4177 | PP_CONCAT_MASK(TOK_NUMBER), /* head */ |
| 4178 | PP_CONCAT_MASK(TOK_NUMBER) /* tail */ |
| 4179 | } |
| 4180 | }; |
| 4181 | paste_tokens(&thead, t, ARRAY_SIZE(t), false); |
| 4182 | } |
H. Peter Anvin | 6125b62 | 2009-04-08 14:02:25 -0700 | [diff] [blame] | 4183 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4184 | return thead; |
| 4185 | } |
| 4186 | |
| 4187 | /* |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4188 | * 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] | 4189 | * a macro at all, it is simply copied to the output and the pointer |
| 4190 | * advanced. tpp should be a pointer to a pointer (usually the next |
| 4191 | * pointer of the previous token) to the first token. **tpp is updated |
| 4192 | * to point to the last token of the expansion, and *tpp updated to |
| 4193 | * 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] | 4194 | * |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4195 | * If the expansion is empty, *tpp will be unchanged but **tpp will |
| 4196 | * be advanced past the macro call. |
| 4197 | * |
| 4198 | * The return value equals **tpp. |
| 4199 | * |
| 4200 | * 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] | 4201 | * |
| 4202 | */ |
| 4203 | static Token *expand_smacro_noreset(Token * tline); |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4204 | static int64_t smacro_deadman; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4205 | |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4206 | static bool expand_one_smacro(Token ***tpp) |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4207 | { |
| 4208 | Token **params = NULL; |
| 4209 | const char *mname; |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4210 | Token *tline = **tpp; |
| 4211 | Token *mstart = **tpp; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4212 | SMacro *head, *m; |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4213 | unsigned int i; |
| 4214 | Token *t, *tup, *ttail; |
| 4215 | unsigned int nparam = 0; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4216 | |
| 4217 | if (!tline) |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4218 | return false; /* Empty line, nothing to do */ |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4219 | |
| 4220 | mname = tline->text; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4221 | |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4222 | if (--smacro_deadman <= 0) { |
| 4223 | if (smacro_deadman == 0) |
| 4224 | nasm_nonfatal("interminable macro recursion"); |
| 4225 | goto not_a_macro; |
| 4226 | } else if (tline->type == TOK_ID) { |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4227 | head = (SMacro *)hash_findix(&smacros, mname); |
| 4228 | } else if (tline->type == TOK_PREPROC_ID) { |
| 4229 | Context *ctx = get_ctx(mname, &mname); |
| 4230 | head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL; |
| 4231 | } else { |
| 4232 | goto not_a_macro; |
| 4233 | } |
| 4234 | |
| 4235 | /* |
| 4236 | * We've hit an identifier of some sort. First check whether the |
| 4237 | * identifier is a single-line macro at all, then think about |
| 4238 | * checking for parameters if necessary. |
| 4239 | */ |
| 4240 | list_for_each(m, head) { |
| 4241 | if (!mstrcmp(m->name, mname, m->casesense)) |
| 4242 | break; |
| 4243 | } |
| 4244 | |
| 4245 | if (!m) { |
| 4246 | goto not_a_macro; |
| 4247 | } |
| 4248 | |
| 4249 | /* Parse parameters, if applicable */ |
| 4250 | |
| 4251 | params = NULL; |
| 4252 | nparam = 0; |
| 4253 | |
| 4254 | if (m->nparam == 0) { |
| 4255 | /* |
| 4256 | * Simple case: the macro is parameterless. |
| 4257 | * Nothing to parse; the expansion code will |
| 4258 | * drop the macro name token. |
| 4259 | */ |
| 4260 | } else { |
| 4261 | /* |
| 4262 | * Complicated case: at least one macro with this name |
| 4263 | * exists and takes parameters. We must find the |
| 4264 | * parameters in the call, count them, find the SMacro |
| 4265 | * that corresponds to that form of the macro call, and |
| 4266 | * substitute for the parameters when we expand. What a |
| 4267 | * pain. |
| 4268 | */ |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4269 | Token **phead, **pep; |
| 4270 | int paren = 1; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4271 | int white = 0; |
| 4272 | int brackets = 0; |
| 4273 | bool bracketed = false; |
| 4274 | bool bad_bracket = false; |
| 4275 | unsigned int sparam = PARAM_DELTA; |
| 4276 | |
| 4277 | tline = tline->next; |
| 4278 | |
| 4279 | while (tok_type_(tline, TOK_WHITESPACE)) { |
| 4280 | tline = tline->next; |
| 4281 | } |
| 4282 | if (!tok_is_(tline, "(")) { |
| 4283 | /* |
| 4284 | * This macro wasn't called with parameters: ignore |
| 4285 | * the call. (Behaviour borrowed from gnu cpp.) |
| 4286 | */ |
| 4287 | goto not_a_macro; |
| 4288 | } |
| 4289 | |
| 4290 | paren = 1; |
| 4291 | nparam = 0; |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4292 | nasm_newn(params, sparam); |
| 4293 | phead = pep = ¶ms[0]; |
| 4294 | *pep = NULL; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4295 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4296 | while (paren) { |
| 4297 | bool skip; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4298 | char ch; |
| 4299 | |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4300 | tline = tline->next; |
| 4301 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4302 | if (!tline) { |
| 4303 | nasm_nonfatal("macro call expects terminating `)'"); |
| 4304 | goto not_a_macro; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4305 | } |
| 4306 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4307 | ch = 0; |
| 4308 | skip = false; |
| 4309 | |
| 4310 | switch (tline->type) { |
| 4311 | case TOK_OTHER: |
| 4312 | if (!tline->text[1]) |
| 4313 | ch = tline->text[0]; |
| 4314 | break; |
| 4315 | |
| 4316 | case TOK_WHITESPACE: |
| 4317 | if (brackets || *phead) |
| 4318 | white++; /* Keep interior whitespace */ |
| 4319 | skip = true; |
| 4320 | break; |
| 4321 | |
| 4322 | default: |
| 4323 | break; |
| 4324 | } |
| 4325 | |
| 4326 | switch (ch) { |
| 4327 | case ',': |
| 4328 | if (!brackets) { |
| 4329 | if (++nparam >= sparam) { |
| 4330 | sparam += PARAM_DELTA; |
| 4331 | params = nasm_realloc(params, sparam * sizeof *params); |
| 4332 | } |
| 4333 | pep = ¶ms[nparam]; |
| 4334 | *pep = NULL; |
| 4335 | bracketed = false; |
| 4336 | skip = true; |
| 4337 | } |
| 4338 | break; |
| 4339 | |
| 4340 | case '{': |
| 4341 | if (!bracketed) { |
| 4342 | bracketed = !*phead; |
| 4343 | skip = true; |
| 4344 | } |
| 4345 | brackets++; |
| 4346 | break; |
| 4347 | |
| 4348 | case '}': |
| 4349 | if (brackets > 0) { |
| 4350 | if (!--brackets) |
| 4351 | skip = bracketed; |
| 4352 | } |
| 4353 | break; |
| 4354 | |
| 4355 | case '(': |
| 4356 | if (!brackets) |
| 4357 | paren++; |
| 4358 | break; |
| 4359 | |
| 4360 | case ')': |
| 4361 | if (!brackets) { |
| 4362 | paren--; |
| 4363 | if (!paren) { |
| 4364 | skip = true; |
| 4365 | if (nparam > 0 || *phead) { |
| 4366 | /* Count the final argument unless just () */ |
| 4367 | nparam++; |
| 4368 | } |
| 4369 | } |
| 4370 | } |
| 4371 | break; |
| 4372 | |
| 4373 | default: |
| 4374 | break; /* Normal token */ |
| 4375 | } |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4376 | |
| 4377 | if (!skip) { |
| 4378 | Token *t; |
| 4379 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4380 | bad_bracket |= bracketed && !brackets; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4381 | |
| 4382 | if (white) { |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4383 | *pep = t = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4384 | pep = &t->next; |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4385 | white = 0; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4386 | } |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4387 | *pep = t = dup_Token(NULL, tline); |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4388 | pep = &t->next; |
| 4389 | white = 0; |
| 4390 | } |
| 4391 | } |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4392 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4393 | /* |
| 4394 | * Look for a macro matching in both name and parameter count. |
| 4395 | * We already know any matches cannot be anywhere before the |
| 4396 | * current position of "m", so there is no reason to |
| 4397 | * backtrack. |
| 4398 | */ |
| 4399 | while (1) { |
| 4400 | if (!m) { |
| 4401 | /*! |
| 4402 | *!macro-params-single [on] single-line macro calls with wrong parameter count |
| 4403 | *! warns about \i{single-line macros} being invoked |
| 4404 | *! with the wrong number of parameters. |
| 4405 | */ |
| 4406 | nasm_warn(WARN_MACRO_PARAMS_SINGLE, |
| 4407 | "single-line macro `%s' exists, " |
| 4408 | "but not taking %d parameter%s", |
| 4409 | mname, nparam, (nparam == 1) ? "" : "s"); |
| 4410 | goto not_a_macro; |
| 4411 | } |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4412 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4413 | if (m->nparam == nparam && !mstrcmp(m->name, mname, m->casesense)) |
| 4414 | break; /* It's good */ |
| 4415 | |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4416 | m = m->next; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4417 | } |
| 4418 | } |
| 4419 | |
| 4420 | if (m->in_progress) |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4421 | goto not_a_macro; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4422 | |
| 4423 | /* Expand each parameter */ |
| 4424 | m->in_progress = true; |
| 4425 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4426 | if (m->eval_param) { |
| 4427 | for (i = 0; i < nparam; i++) { |
| 4428 | if (m->eval_param[i]) { |
| 4429 | /* Evaluate this parameter as a number */ |
| 4430 | struct ppscan pps; |
| 4431 | struct tokenval tokval; |
| 4432 | expr *evalresult; |
| 4433 | Token *eval_param; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4434 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4435 | pps.tptr = eval_param = expand_smacro_noreset(params[i]); |
| 4436 | pps.ntokens = -1; |
| 4437 | tokval.t_type = TOKEN_INVALID; |
| 4438 | evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL); |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4439 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4440 | free_tlist(eval_param); |
| 4441 | params[i] = NULL; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4442 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4443 | if (!evalresult) { |
| 4444 | /* Nothing meaningful to do */ |
| 4445 | } else if (tokval.t_type) { |
| 4446 | nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name); |
| 4447 | } else if (!is_simple(evalresult)) { |
| 4448 | nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name); |
| 4449 | } else { |
| 4450 | params[i] = make_tok_num(reloc_value(evalresult)); |
| 4451 | } |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4452 | } |
| 4453 | } |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4454 | } |
| 4455 | |
| 4456 | t = tline; |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4457 | tline = tline->next; /* Remove the macro call from the input */ |
| 4458 | t->next = NULL; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4459 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4460 | /* Note: we own the expansion this returns. */ |
| 4461 | t = m->expand(m, params, nparam); |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4462 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4463 | tup = NULL; |
| 4464 | ttail = NULL; /* Pointer to the last token of the expansion */ |
| 4465 | while (t) { |
| 4466 | enum pp_token_type type = t->type; |
| 4467 | Token *tnext = t->next; |
| 4468 | Token **tp; |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4469 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4470 | switch (type) { |
| 4471 | case TOK_PREPROC_Q: |
| 4472 | case TOK_PREPROC_QQ: |
| 4473 | t->type = TOK_ID; |
| 4474 | nasm_free(t->text); |
| 4475 | t->text = nasm_strdup(type == TOK_PREPROC_QQ ? m->name : mname); |
| 4476 | t->len = nasm_last_string_len(); |
| 4477 | t->next = tline; |
| 4478 | break; |
| 4479 | |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4480 | case TOK_ID: |
| 4481 | case TOK_PREPROC_ID: |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4482 | /* |
| 4483 | * Chain this into the target line *before* expanding, |
| 4484 | * that way we pick up any arguments to the new macro call, |
| 4485 | * if applicable. |
| 4486 | */ |
| 4487 | t->next = tline; |
| 4488 | tp = &t; |
| 4489 | expand_one_smacro(&tp); |
| 4490 | if (t == tline) |
| 4491 | t = NULL; /* Null expansion */ |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4492 | break; |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4493 | |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4494 | default: |
| 4495 | if (is_smac_param(t->type)) { |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4496 | unsigned int param = smac_nparam(t->type); |
| 4497 | nasm_assert(!tup && param < nparam); |
| 4498 | delete_Token(t); |
| 4499 | t = NULL; |
| 4500 | tup = tnext; |
| 4501 | tnext = dup_tlist_reverse(params[param], NULL); |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4502 | } else { |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4503 | t->next = tline; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4504 | } |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4505 | } |
| 4506 | |
| 4507 | if (t) { |
| 4508 | tline = t; |
| 4509 | if (!ttail) |
| 4510 | ttail = t; |
| 4511 | } |
| 4512 | |
| 4513 | if (tnext) { |
| 4514 | t = tnext; |
| 4515 | } else { |
| 4516 | t = tup; |
| 4517 | tup = NULL; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4518 | } |
| 4519 | } |
| 4520 | |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4521 | **tpp = tline; |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4522 | if (ttail) |
| 4523 | *tpp = &ttail->next; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4524 | |
| 4525 | m->in_progress = false; |
| 4526 | |
| 4527 | /* 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] | 4528 | free_tlist(mstart); |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4529 | free_tlist_array(params, nparam); |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4530 | return true; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4531 | |
| 4532 | /* |
| 4533 | * No macro expansion needed; roll back to mstart (if necessary) |
| 4534 | * and then advance to the next input token. |
| 4535 | */ |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4536 | not_a_macro: |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4537 | *tpp = &mstart->next; |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4538 | free_tlist_array(params, nparam); |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4539 | return false; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4540 | } |
| 4541 | |
| 4542 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4543 | * Expand all single-line macro calls made in the given line. |
| 4544 | * Return the expanded version of the line. The original is deemed |
| 4545 | * to be destroyed in the process. (In reality we'll just move |
| 4546 | * Tokens from input to output a lot of the time, rather than |
| 4547 | * actually bothering to destroy and replicate.) |
| 4548 | */ |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4549 | static Token *expand_smacro(Token *tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4550 | { |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4551 | smacro_deadman = nasm_limit[LIMIT_MACROS]; |
| 4552 | return expand_smacro_noreset(tline); |
| 4553 | } |
| 4554 | |
| 4555 | static Token *expand_smacro_noreset(Token * tline) |
| 4556 | { |
| 4557 | Token *t, **tail, *thead; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4558 | Token *org_tline = tline; |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4559 | bool expanded; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4560 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4561 | /* |
| 4562 | * Trick: we should avoid changing the start token pointer since it can |
| 4563 | * be contained in "next" field of other token. Because of this |
| 4564 | * we allocate a copy of first token and work with it; at the end of |
| 4565 | * routine we copy it back |
| 4566 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4567 | if (org_tline) { |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4568 | tline = new_Token(org_tline->next, org_tline->type, |
| 4569 | org_tline->text, 0); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4570 | nasm_free(org_tline->text); |
| 4571 | org_tline->text = NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4572 | } |
| 4573 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4574 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4575 | /* |
| 4576 | * Pretend that we always end up doing expansion on the first pass; |
| 4577 | * that way %+ get processed. However, if we process %+ before the |
| 4578 | * first pass, we end up with things like MACRO %+ TAIL trying to |
| 4579 | * look up the macro "MACROTAIL", which we don't want. |
| 4580 | */ |
| 4581 | expanded = true; |
| 4582 | thead = tline; |
| 4583 | while (true) { |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4584 | static const struct tokseq_match tmatch[] = { |
Cyrill Gorcunov | c6a742c | 2011-06-27 01:23:09 +0400 | [diff] [blame] | 4585 | { |
| 4586 | PP_CONCAT_MASK(TOK_ID) | |
| 4587 | PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */ |
| 4588 | PP_CONCAT_MASK(TOK_ID) | |
| 4589 | PP_CONCAT_MASK(TOK_PREPROC_ID) | |
| 4590 | PP_CONCAT_MASK(TOK_NUMBER) /* tail */ |
| 4591 | } |
| 4592 | }; |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4593 | |
| 4594 | tail = &thead; |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4595 | while ((t = *tail)) { /* main token loop */ |
| 4596 | |
| 4597 | expanded |= expand_one_smacro(&tail); |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4598 | } |
H. Peter Anvin (Intel) | 875eb24 | 2019-08-07 17:12:24 -0700 | [diff] [blame] | 4599 | |
| 4600 | if (!expanded) { |
| 4601 | tline = thead; |
| 4602 | break; /* Done! */ |
| 4603 | } |
| 4604 | |
| 4605 | /* |
| 4606 | * Now scan the entire line and look for successive TOK_IDs |
| 4607 | * that resulted after expansion (they can't be produced by |
| 4608 | * tokenize()). The successive TOK_IDs should be concatenated. |
| 4609 | * Also we look for %+ tokens and concatenate the tokens |
| 4610 | * before and after them (without white spaces in between). |
| 4611 | */ |
| 4612 | paste_tokens(&thead, tmatch, ARRAY_SIZE(tmatch), true); |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4613 | |
| 4614 | expanded = false; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4615 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4616 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4617 | if (org_tline) { |
| 4618 | if (thead) { |
| 4619 | *org_tline = *thead; |
| 4620 | /* since we just gave text to org_line, don't free it */ |
| 4621 | thead->text = NULL; |
| 4622 | delete_Token(thead); |
| 4623 | } else { |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4624 | /* |
| 4625 | * The expression expanded to empty line; |
| 4626 | * we can't return NULL because of the "trick" above. |
| 4627 | * Just set the line to a single WHITESPACE token. |
| 4628 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4629 | memset(org_tline, 0, sizeof(*org_tline)); |
| 4630 | org_tline->text = NULL; |
| 4631 | org_tline->type = TOK_WHITESPACE; |
| 4632 | } |
| 4633 | thead = org_tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4634 | } |
| 4635 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4636 | return thead; |
| 4637 | } |
| 4638 | |
| 4639 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4640 | * Similar to expand_smacro but used exclusively with macro identifiers |
| 4641 | * right before they are fetched in. The reason is that there can be |
| 4642 | * identifiers consisting of several subparts. We consider that if there |
| 4643 | * are more than one element forming the name, user wants a expansion, |
| 4644 | * otherwise it will be left as-is. Example: |
| 4645 | * |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4646 | * %define %$abc cde |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4647 | * |
| 4648 | * the identifier %$abc will be left as-is so that the handler for %define |
| 4649 | * will suck it and define the corresponding value. Other case: |
| 4650 | * |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4651 | * %define _%$abc cde |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4652 | * |
| 4653 | * In this case user wants name to be expanded *before* %define starts |
| 4654 | * working, so we'll expand %$abc into something (if it has a value; |
| 4655 | * otherwise it will be left as-is) then concatenate all successive |
| 4656 | * PP_IDs into one. |
| 4657 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4658 | static Token *expand_id(Token * tline) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4659 | { |
| 4660 | Token *cur, *oldnext = NULL; |
| 4661 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4662 | if (!tline || !tline->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4663 | return tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4664 | |
| 4665 | cur = tline; |
| 4666 | while (cur->next && |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4667 | (cur->next->type == TOK_ID || |
| 4668 | cur->next->type == TOK_PREPROC_ID |
| 4669 | || cur->next->type == TOK_NUMBER)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4670 | cur = cur->next; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4671 | |
| 4672 | /* If identifier consists of just one token, don't expand */ |
| 4673 | if (cur == tline) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4674 | return tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4675 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4676 | if (cur) { |
| 4677 | oldnext = cur->next; /* Detach the tail past identifier */ |
| 4678 | cur->next = NULL; /* so that expand_smacro stops here */ |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4679 | } |
| 4680 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4681 | tline = expand_smacro(tline); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4682 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4683 | if (cur) { |
| 4684 | /* expand_smacro possibly changhed tline; re-scan for EOL */ |
| 4685 | cur = tline; |
| 4686 | while (cur && cur->next) |
| 4687 | cur = cur->next; |
| 4688 | if (cur) |
| 4689 | cur->next = oldnext; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4690 | } |
| 4691 | |
| 4692 | return tline; |
| 4693 | } |
| 4694 | |
| 4695 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4696 | * Determine whether the given line constitutes a multi-line macro |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4697 | * 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] | 4698 | * to check for an initial label - that's taken care of in |
| 4699 | * expand_mmacro - but must check numbers of parameters. Guaranteed |
| 4700 | * to be called with tline->type == TOK_ID, so the putative macro |
| 4701 | * name is easy to find. |
| 4702 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4703 | static MMacro *is_mmacro(Token * tline, Token *** params_array) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4704 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4705 | MMacro *head, *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4706 | Token **params; |
| 4707 | int nparam; |
| 4708 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4709 | head = (MMacro *) hash_findix(&mmacros, tline->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4710 | |
| 4711 | /* |
| 4712 | * Efficiency: first we see if any macro exists with the given |
| 4713 | * name. If not, we can return NULL immediately. _Then_ we |
| 4714 | * count the parameters, and then we look further along the |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4715 | * list if necessary to find the proper MMacro. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4716 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4717 | list_for_each(m, head) |
| 4718 | if (!mstrcmp(m->name, tline->text, m->casesense)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4719 | break; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4720 | if (!m) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4721 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4722 | |
| 4723 | /* |
| 4724 | * OK, we have a potential macro. Count and demarcate the |
| 4725 | * parameters. |
| 4726 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4727 | count_mmac_params(tline->next, &nparam, ¶ms); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4728 | |
| 4729 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4730 | * 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] | 4731 | * structure that handles this number. |
| 4732 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4733 | while (m) { |
| 4734 | if (m->nparam_min <= nparam |
| 4735 | && (m->plus || nparam <= m->nparam_max)) { |
| 4736 | /* |
| 4737 | * This one is right. Just check if cycle removal |
| 4738 | * prohibits us using it before we actually celebrate... |
| 4739 | */ |
| 4740 | if (m->in_progress > m->max_depth) { |
| 4741 | if (m->max_depth > 0) { |
H. Peter Anvin (Intel) | 80c4f23 | 2018-12-14 13:33:24 -0800 | [diff] [blame] | 4742 | nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i", |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4743 | m->max_depth); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4744 | } |
| 4745 | nasm_free(params); |
| 4746 | return NULL; |
| 4747 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4748 | /* |
| 4749 | * It's right, and we can use it. Add its default |
| 4750 | * parameters to the end of our list if necessary. |
| 4751 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4752 | if (m->defaults && nparam < m->nparam_min + m->ndefs) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4753 | params = |
| 4754 | nasm_realloc(params, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4755 | ((m->nparam_min + m->ndefs + |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4756 | 1) * sizeof(*params))); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4757 | while (nparam < m->nparam_min + m->ndefs) { |
| 4758 | params[nparam] = m->defaults[nparam - m->nparam_min]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4759 | nparam++; |
| 4760 | } |
| 4761 | } |
| 4762 | /* |
| 4763 | * If we've gone over the maximum parameter count (and |
| 4764 | * we're in Plus mode), ignore parameters beyond |
| 4765 | * nparam_max. |
| 4766 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4767 | if (m->plus && nparam > m->nparam_max) |
| 4768 | nparam = m->nparam_max; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4769 | /* |
| 4770 | * Then terminate the parameter list, and leave. |
| 4771 | */ |
| 4772 | if (!params) { /* need this special case */ |
| 4773 | params = nasm_malloc(sizeof(*params)); |
| 4774 | nparam = 0; |
| 4775 | } |
| 4776 | params[nparam] = NULL; |
| 4777 | *params_array = params; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4778 | return m; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4779 | } |
| 4780 | /* |
| 4781 | * This one wasn't right: look for the next one with the |
| 4782 | * same name. |
| 4783 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4784 | list_for_each(m, m->next) |
| 4785 | if (!mstrcmp(m->name, tline->text, m->casesense)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4786 | break; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4787 | } |
| 4788 | |
| 4789 | /* |
| 4790 | * After all that, we didn't find one with the right number of |
| 4791 | * parameters. Issue a warning, and fail to expand the macro. |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4792 | *! |
| 4793 | *!macro-params-multi [on] multi-line macro calls with wrong parameter count |
| 4794 | *! warns about \i{multi-line macros} being invoked |
| 4795 | *! with the wrong number of parameters. See \k{mlmacover} for an |
| 4796 | *! example of why you might want to disable this warning. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4797 | */ |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 4798 | nasm_warn(WARN_MACRO_PARAMS_MULTI, |
| 4799 | "multi-line macro `%s' exists, but not taking %d parameter%s", |
| 4800 | tline->text, nparam, (nparam == 1) ? "" : "s"); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4801 | nasm_free(params); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4802 | return NULL; |
| 4803 | } |
| 4804 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4805 | |
| 4806 | /* |
| 4807 | * Save MMacro invocation specific fields in |
| 4808 | * preparation for a recursive macro expansion |
| 4809 | */ |
| 4810 | static void push_mmacro(MMacro *m) |
| 4811 | { |
| 4812 | MMacroInvocation *i; |
| 4813 | |
| 4814 | i = nasm_malloc(sizeof(MMacroInvocation)); |
| 4815 | i->prev = m->prev; |
| 4816 | i->params = m->params; |
| 4817 | i->iline = m->iline; |
| 4818 | i->nparam = m->nparam; |
| 4819 | i->rotate = m->rotate; |
| 4820 | i->paramlen = m->paramlen; |
| 4821 | i->unique = m->unique; |
| 4822 | i->condcnt = m->condcnt; |
| 4823 | m->prev = i; |
| 4824 | } |
| 4825 | |
| 4826 | |
| 4827 | /* |
| 4828 | * Restore MMacro invocation specific fields that were |
| 4829 | * saved during a previous recursive macro expansion |
| 4830 | */ |
| 4831 | static void pop_mmacro(MMacro *m) |
| 4832 | { |
| 4833 | MMacroInvocation *i; |
| 4834 | |
| 4835 | if (m->prev) { |
| 4836 | i = m->prev; |
| 4837 | m->prev = i->prev; |
| 4838 | m->params = i->params; |
| 4839 | m->iline = i->iline; |
| 4840 | m->nparam = i->nparam; |
| 4841 | m->rotate = i->rotate; |
| 4842 | m->paramlen = i->paramlen; |
| 4843 | m->unique = i->unique; |
| 4844 | m->condcnt = i->condcnt; |
| 4845 | nasm_free(i); |
| 4846 | } |
| 4847 | } |
| 4848 | |
| 4849 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4850 | /* |
| 4851 | * Expand the multi-line macro call made by the given line, if |
| 4852 | * 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] | 4853 | * istk->expansion and return 1. Otherwise return 0. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4854 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4855 | static int expand_mmacro(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4856 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4857 | Token *startline = tline; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4858 | Token *label = NULL; |
| 4859 | int dont_prepend = 0; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4860 | Token **params, *t, *tt; |
| 4861 | MMacro *m; |
| 4862 | Line *l, *ll; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4863 | int i, nparam, *paramlen; |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 4864 | const char *mname; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4865 | |
| 4866 | t = tline; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4867 | skip_white_(t); |
H. Peter Anvin | ce2233b | 2008-05-25 21:57:00 -0700 | [diff] [blame] | 4868 | /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */ |
H. Peter Anvin | dce1e2f | 2002-04-30 21:06:37 +0000 | [diff] [blame] | 4869 | 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] | 4870 | return 0; |
| 4871 | m = is_mmacro(t, ¶ms); |
| 4872 | if (m) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4873 | mname = t->text; |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 4874 | } else { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4875 | Token *last; |
| 4876 | /* |
| 4877 | * We have an id which isn't a macro call. We'll assume |
| 4878 | * it might be a label; we'll also check to see if a |
| 4879 | * colon follows it. Then, if there's another id after |
| 4880 | * that lot, we'll check it again for macro-hood. |
| 4881 | */ |
| 4882 | label = last = t; |
| 4883 | t = t->next; |
| 4884 | if (tok_type_(t, TOK_WHITESPACE)) |
| 4885 | last = t, t = t->next; |
| 4886 | if (tok_is_(t, ":")) { |
| 4887 | dont_prepend = 1; |
| 4888 | last = t, t = t->next; |
| 4889 | if (tok_type_(t, TOK_WHITESPACE)) |
| 4890 | last = t, t = t->next; |
| 4891 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4892 | if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, ¶ms))) |
| 4893 | return 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4894 | last->next = NULL; |
Keith Kanios | 891775e | 2009-07-11 06:08:54 -0500 | [diff] [blame] | 4895 | mname = t->text; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4896 | tline = t; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4897 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4898 | |
| 4899 | /* |
| 4900 | * Fix up the parameters: this involves stripping leading and |
| 4901 | * trailing whitespace, then stripping braces if they are |
| 4902 | * present. |
| 4903 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4904 | for (nparam = 0; params[nparam]; nparam++) ; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4905 | paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4906 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4907 | for (i = 0; params[i]; i++) { |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4908 | int brace = 0; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4909 | int comma = (!m->plus || i < nparam - 1); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4910 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4911 | t = params[i]; |
| 4912 | skip_white_(t); |
| 4913 | if (tok_is_(t, "{")) |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4914 | t = t->next, brace++, comma = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4915 | params[i] = t; |
| 4916 | paramlen[i] = 0; |
| 4917 | while (t) { |
| 4918 | if (comma && t->type == TOK_OTHER && !strcmp(t->text, ",")) |
| 4919 | break; /* ... because we have hit a comma */ |
| 4920 | if (comma && t->type == TOK_WHITESPACE |
| 4921 | && tok_is_(t->next, ",")) |
| 4922 | break; /* ... or a space then a comma */ |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4923 | if (brace && t->type == TOK_OTHER) { |
| 4924 | if (t->text[0] == '{') |
| 4925 | brace++; /* ... or a nested opening brace */ |
| 4926 | else if (t->text[0] == '}') |
| 4927 | if (!--brace) |
| 4928 | break; /* ... or a brace */ |
| 4929 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4930 | t = t->next; |
| 4931 | paramlen[i]++; |
| 4932 | } |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4933 | if (brace) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4934 | nasm_nonfatal("macro params should be enclosed in braces"); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4935 | } |
| 4936 | |
| 4937 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4938 | * OK, we have a MMacro structure together with a set of |
| 4939 | * parameters. We must now go through the expansion and push |
| 4940 | * copies of each Line on to istk->expansion. Substitution of |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4941 | * parameter tokens and macro-local tokens doesn't get done |
| 4942 | * until the single-line macro substitution process; this is |
| 4943 | * because delaying them allows us to change the semantics |
| 4944 | * later through %rotate. |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4945 | * |
| 4946 | * First, push an end marker on to istk->expansion, mark this |
| 4947 | * macro as in progress, and set up its invocation-specific |
| 4948 | * variables. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4949 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4950 | ll = nasm_malloc(sizeof(Line)); |
| 4951 | ll->next = istk->expansion; |
| 4952 | ll->finishes = m; |
| 4953 | ll->first = NULL; |
| 4954 | istk->expansion = ll; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4955 | |
| 4956 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4957 | * Save the previous MMacro expansion in the case of |
| 4958 | * macro recursion |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4959 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4960 | if (m->max_depth && m->in_progress) |
| 4961 | push_mmacro(m); |
| 4962 | |
| 4963 | m->in_progress ++; |
| 4964 | m->params = params; |
| 4965 | m->iline = tline; |
| 4966 | m->nparam = nparam; |
| 4967 | m->rotate = 0; |
| 4968 | m->paramlen = paramlen; |
| 4969 | m->unique = unique++; |
| 4970 | m->lineno = 0; |
| 4971 | m->condcnt = 0; |
| 4972 | |
| 4973 | m->next_active = istk->mstk; |
| 4974 | istk->mstk = m; |
| 4975 | |
| 4976 | list_for_each(l, m->expansion) { |
| 4977 | Token **tail; |
| 4978 | |
| 4979 | ll = nasm_malloc(sizeof(Line)); |
| 4980 | ll->finishes = NULL; |
| 4981 | ll->next = istk->expansion; |
| 4982 | istk->expansion = ll; |
| 4983 | tail = &ll->first; |
| 4984 | |
| 4985 | list_for_each(t, l->first) { |
| 4986 | Token *x = t; |
| 4987 | switch (t->type) { |
| 4988 | case TOK_PREPROC_Q: |
| 4989 | tt = *tail = new_Token(NULL, TOK_ID, mname, 0); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4990 | break; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4991 | case TOK_PREPROC_QQ: |
| 4992 | tt = *tail = new_Token(NULL, TOK_ID, m->name, 0); |
| 4993 | break; |
| 4994 | case TOK_PREPROC_ID: |
| 4995 | if (t->text[1] == '0' && t->text[2] == '0') { |
| 4996 | dont_prepend = -1; |
| 4997 | x = label; |
| 4998 | if (!x) |
| 4999 | continue; |
| 5000 | } |
| 5001 | /* fall through */ |
| 5002 | default: |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5003 | tt = *tail = dup_Token(NULL, x); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5004 | break; |
| 5005 | } |
| 5006 | tail = &tt->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5007 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5008 | *tail = NULL; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5009 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5010 | |
| 5011 | /* |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5012 | * If we had a label, push it on as the first line of |
| 5013 | * the macro expansion. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5014 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5015 | if (label) { |
| 5016 | if (dont_prepend < 0) |
| 5017 | free_tlist(startline); |
| 5018 | else { |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5019 | nasm_new(ll); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5020 | ll->finishes = NULL; |
| 5021 | ll->next = istk->expansion; |
| 5022 | istk->expansion = ll; |
| 5023 | ll->first = startline; |
| 5024 | if (!dont_prepend) { |
| 5025 | while (label->next) |
| 5026 | label = label->next; |
| 5027 | label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5028 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5029 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 5030 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5031 | |
H. Peter Anvin | 0d4d431 | 2019-08-07 00:46:27 -0700 | [diff] [blame] | 5032 | lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5033 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5034 | return 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5035 | } |
| 5036 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5037 | /* |
| 5038 | * This function adds macro names to error messages, and suppresses |
| 5039 | * them if necessary. |
| 5040 | */ |
H. Peter Anvin (Intel) | 6bde2ed | 2018-12-13 19:39:41 -0800 | [diff] [blame] | 5041 | 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] | 5042 | { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5043 | char buff[BUFSIZ]; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5044 | MMacro *mmac = NULL; |
| 5045 | int delta = 0; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 5046 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5047 | /* |
| 5048 | * If we're in a dead branch of IF or something like it, ignore the error. |
| 5049 | * However, because %else etc are evaluated in the state context |
| 5050 | * of the previous branch, errors might get lost: |
| 5051 | * %if 0 ... %else trailing garbage ... %endif |
| 5052 | * So %else etc should set the ERR_PP_PRECOND flag. |
| 5053 | */ |
| 5054 | if ((severity & ERR_MASK) < ERR_FATAL && |
| 5055 | istk && istk->conds && |
| 5056 | ((severity & ERR_PP_PRECOND) ? |
| 5057 | istk->conds->state == COND_NEVER : |
H. Peter Anvin | eb6653f | 2016-04-05 13:03:10 -0700 | [diff] [blame] | 5058 | !emitting(istk->conds->state))) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5059 | return; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 5060 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5061 | /* get %macro name */ |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5062 | if (!(severity & ERR_NOFILE) && istk && istk->mstk) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5063 | mmac = istk->mstk; |
| 5064 | /* but %rep blocks should be skipped */ |
| 5065 | while (mmac && !mmac->name) |
| 5066 | mmac = mmac->next_active, delta++; |
Cyrill Gorcunov | 9900c6b | 2011-10-09 18:58:46 +0400 | [diff] [blame] | 5067 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5068 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5069 | if (mmac) { |
| 5070 | vsnprintf(buff, sizeof(buff), fmt, arg); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 5071 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5072 | nasm_set_verror(real_verror); |
| 5073 | nasm_error(severity, "(%s:%d) %s", |
| 5074 | mmac->name, mmac->lineno - delta, buff); |
| 5075 | nasm_set_verror(pp_verror); |
| 5076 | } else { |
| 5077 | real_verror(severity, fmt, arg); |
| 5078 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 5079 | } |
| 5080 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 5081 | static Token * |
| 5082 | stdmac_file(const SMacro *s, Token **params, unsigned int nparams) |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5083 | { |
| 5084 | (void)s; |
| 5085 | (void)params; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 5086 | (void)nparams; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5087 | |
| 5088 | return make_tok_qstr(src_get_fname()); |
| 5089 | } |
| 5090 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 5091 | static Token * |
| 5092 | stdmac_line(const SMacro *s, Token **params, unsigned int nparams) |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5093 | { |
| 5094 | (void)s; |
| 5095 | (void)params; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 5096 | (void)nparams; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5097 | |
| 5098 | return make_tok_num(src_get_linnum()); |
| 5099 | } |
| 5100 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 5101 | static Token * |
| 5102 | stdmac_bits(const SMacro *s, Token **params, unsigned int nparams) |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5103 | { |
| 5104 | (void)s; |
| 5105 | (void)params; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 5106 | (void)nparams; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5107 | |
| 5108 | return make_tok_num(globalbits); |
| 5109 | } |
| 5110 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 5111 | static Token * |
| 5112 | stdmac_ptr(const SMacro *s, Token **params, unsigned int nparams) |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5113 | { |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 5114 | const Token *t; |
Chang S. Bae | fea2269 | 2019-05-28 12:25:16 -0700 | [diff] [blame] | 5115 | static const Token ptr_word = { NULL, "word", 4, TOK_ID }; |
| 5116 | static const Token ptr_dword = { NULL, "dword", 5, TOK_ID }; |
| 5117 | static const Token ptr_qword = { NULL, "qword", 5, TOK_ID }; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5118 | |
| 5119 | (void)s; |
| 5120 | (void)params; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 5121 | (void)nparams; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5122 | |
| 5123 | switch (globalbits) { |
| 5124 | case 16: |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 5125 | t = &ptr_word; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5126 | break; |
| 5127 | case 32: |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 5128 | t = &ptr_dword; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5129 | break; |
| 5130 | case 64: |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 5131 | t = &ptr_qword; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5132 | break; |
| 5133 | default: |
| 5134 | panic(); |
| 5135 | } |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 5136 | |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 5137 | return dup_Token(NULL, t); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5138 | } |
| 5139 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5140 | /* Add magic standard macros */ |
| 5141 | struct magic_macros { |
| 5142 | const char *name; |
| 5143 | int nparams; |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 5144 | ExpandSMacro func; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5145 | }; |
| 5146 | static const struct magic_macros magic_macros[] = |
| 5147 | { |
| 5148 | { "__FILE__", 0, stdmac_file }, |
| 5149 | { "__LINE__", 0, stdmac_line }, |
| 5150 | { "__BITS__", 0, stdmac_bits }, |
| 5151 | { "__PTR__", 0, stdmac_ptr }, |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5152 | { NULL, 0, NULL } |
| 5153 | }; |
| 5154 | |
| 5155 | static void pp_add_magic_stdmac(void) |
| 5156 | { |
| 5157 | const struct magic_macros *m; |
| 5158 | SMacro *s; |
| 5159 | |
| 5160 | for (m = magic_macros; m->name; m++) { |
| 5161 | s = define_smacro(NULL, m->name, true, m->nparams, NULL); |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 5162 | s->expand = m->func; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5163 | } |
| 5164 | } |
| 5165 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5166 | static void |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5167 | 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] | 5168 | { |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5169 | int apass; |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 5170 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5171 | cstk = NULL; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5172 | nasm_new(istk); |
H. Peter Anvin | 3e83cec | 2016-05-25 04:28:46 -0700 | [diff] [blame] | 5173 | istk->fp = nasm_open_read(file, NF_TEXT); |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 5174 | src_set(0, file); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5175 | istk->lineinc = 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5176 | if (!istk->fp) |
Cyrill Gorcunov | c3527dd | 2018-11-24 22:17:47 +0300 | [diff] [blame] | 5177 | nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5178 | defining = NULL; |
Charles Crayne | d4200be | 2008-07-12 16:42:33 -0700 | [diff] [blame] | 5179 | nested_mac_count = 0; |
| 5180 | nested_rep_count = 0; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 5181 | init_macros(); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5182 | unique = 0; |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 5183 | deplist = dep_list; |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5184 | pp_mode = mode; |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 5185 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5186 | pp_add_magic_stdmac(); |
| 5187 | |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 5188 | if (tasm_compatible_mode) |
| 5189 | pp_add_stdmac(nasm_stdmac_tasm); |
| 5190 | |
| 5191 | pp_add_stdmac(nasm_stdmac_nasm); |
| 5192 | pp_add_stdmac(nasm_stdmac_version); |
| 5193 | |
Cyrill Gorcunov | 15ce78f | 2017-01-06 20:21:28 +0300 | [diff] [blame] | 5194 | if (extrastdmac) |
| 5195 | pp_add_stdmac(extrastdmac); |
| 5196 | |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 5197 | stdmacpos = stdmacros[0]; |
| 5198 | stdmacnext = &stdmacros[1]; |
| 5199 | |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 5200 | do_predef = true; |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 5201 | |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 5202 | strlist_add(deplist, file); |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 5203 | |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 5204 | /* |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 5205 | * Define the __PASS__ macro. This is defined here unlike all the |
| 5206 | * other builtins, because it is special -- it varies between |
| 5207 | * passes -- but there is really no particular reason to make it |
| 5208 | * magic. |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5209 | * |
| 5210 | * 0 = dependencies only |
| 5211 | * 1 = preparatory passes |
| 5212 | * 2 = final pass |
| 5213 | * 3 = preproces only |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 5214 | */ |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5215 | switch (mode) { |
| 5216 | case PP_NORMAL: |
| 5217 | apass = pass_final() ? 2 : 1; |
| 5218 | break; |
| 5219 | case PP_DEPS: |
| 5220 | apass = 0; |
| 5221 | break; |
| 5222 | case PP_PREPROC: |
| 5223 | apass = 3; |
| 5224 | break; |
| 5225 | default: |
| 5226 | panic(); |
| 5227 | } |
| 5228 | |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 5229 | define_smacro(NULL, "__PASS__", true, 0, make_tok_num(apass)); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5230 | } |
| 5231 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 5232 | static void pp_init(void) |
| 5233 | { |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 5234 | } |
| 5235 | |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5236 | /* |
| 5237 | * Get a line of tokens. If we popped the macro expansion/include stack, |
| 5238 | * we return a pointer to the dummy token tok_pop; at that point if |
| 5239 | * istk is NULL then we have reached end of input; |
| 5240 | */ |
| 5241 | static Token tok_pop; /* Dummy token placeholder */ |
| 5242 | |
| 5243 | static Token *pp_tokline(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5244 | { |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5245 | Token *tline, *dtline; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5246 | |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5247 | while (true) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5248 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5249 | * Fetch a tokenized line, either from the macro-expansion |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5250 | * buffer or from the input file. |
| 5251 | */ |
| 5252 | tline = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5253 | while (istk->expansion && istk->expansion->finishes) { |
| 5254 | Line *l = istk->expansion; |
| 5255 | if (!l->finishes->name && l->finishes->in_progress > 1) { |
| 5256 | Line *ll; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5257 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5258 | /* |
| 5259 | * This is a macro-end marker for a macro with no |
| 5260 | * name, which means it's not really a macro at all |
| 5261 | * but a %rep block, and the `in_progress' field is |
| 5262 | * more than 1, meaning that we still need to |
| 5263 | * repeat. (1 means the natural last repetition; 0 |
| 5264 | * means termination by %exitrep.) We have |
| 5265 | * therefore expanded up to the %endrep, and must |
| 5266 | * push the whole block on to the expansion buffer |
| 5267 | * again. We don't bother to remove the macro-end |
| 5268 | * marker: we'd only have to generate another one |
| 5269 | * if we did. |
| 5270 | */ |
| 5271 | l->finishes->in_progress--; |
| 5272 | list_for_each(l, l->finishes->expansion) { |
| 5273 | Token *t, *tt, **tail; |
| 5274 | |
| 5275 | ll = nasm_malloc(sizeof(Line)); |
| 5276 | ll->next = istk->expansion; |
| 5277 | ll->finishes = NULL; |
| 5278 | ll->first = NULL; |
| 5279 | tail = &ll->first; |
| 5280 | |
| 5281 | list_for_each(t, l->first) { |
| 5282 | if (t->text || t->type == TOK_WHITESPACE) { |
H. Peter Anvin (Intel) | 1c21a53 | 2019-08-09 02:34:21 -0700 | [diff] [blame] | 5283 | tt = *tail = dup_Token(NULL, t); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5284 | tail = &tt->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5285 | } |
| 5286 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5287 | istk->expansion = ll; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5288 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5289 | } else { |
| 5290 | /* |
| 5291 | * Check whether a `%rep' was started and not ended |
| 5292 | * within this macro expansion. This can happen and |
| 5293 | * should be detected. It's a fatal error because |
| 5294 | * I'm too confused to work out how to recover |
| 5295 | * sensibly from it. |
| 5296 | */ |
| 5297 | if (defining) { |
| 5298 | if (defining->name) |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 5299 | nasm_panic("defining with name in expansion"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5300 | else if (istk->mstk->name) |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 5301 | nasm_fatal("`%%rep' without `%%endrep' within" |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5302 | " expansion of macro `%s'", |
| 5303 | istk->mstk->name); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5304 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5305 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5306 | /* |
| 5307 | * FIXME: investigate the relationship at this point between |
| 5308 | * istk->mstk and l->finishes |
| 5309 | */ |
| 5310 | { |
| 5311 | MMacro *m = istk->mstk; |
| 5312 | istk->mstk = m->next_active; |
| 5313 | if (m->name) { |
| 5314 | /* |
| 5315 | * This was a real macro call, not a %rep, and |
| 5316 | * therefore the parameter information needs to |
| 5317 | * be freed. |
| 5318 | */ |
| 5319 | if (m->prev) { |
| 5320 | pop_mmacro(m); |
| 5321 | l->finishes->in_progress --; |
| 5322 | } else { |
| 5323 | nasm_free(m->params); |
| 5324 | free_tlist(m->iline); |
| 5325 | nasm_free(m->paramlen); |
| 5326 | l->finishes->in_progress = 0; |
| 5327 | } |
Adam Majer | 91e7240 | 2017-07-25 10:42:01 +0200 | [diff] [blame] | 5328 | } |
| 5329 | |
| 5330 | /* |
| 5331 | * FIXME It is incorrect to always free_mmacro here. |
| 5332 | * It leads to usage-after-free. |
| 5333 | * |
| 5334 | * https://bugzilla.nasm.us/show_bug.cgi?id=3392414 |
| 5335 | */ |
| 5336 | #if 0 |
| 5337 | else |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5338 | free_mmacro(m); |
Adam Majer | 91e7240 | 2017-07-25 10:42:01 +0200 | [diff] [blame] | 5339 | #endif |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5340 | } |
| 5341 | istk->expansion = l->next; |
| 5342 | nasm_free(l); |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 5343 | lfmt->downlevel(LIST_MACRO); |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5344 | return &tok_pop; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5345 | } |
| 5346 | } |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5347 | do { /* until we get a line we can use */ |
| 5348 | char *line; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5349 | |
| 5350 | if (istk->expansion) { /* from a macro expansion */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5351 | Line *l = istk->expansion; |
| 5352 | if (istk->mstk) |
| 5353 | istk->mstk->lineno++; |
| 5354 | tline = l->first; |
| 5355 | istk->expansion = l->next; |
| 5356 | nasm_free(l); |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5357 | line = detoken(tline, false); |
| 5358 | lfmt->line(LIST_MACRO, line); |
| 5359 | nasm_free(line); |
| 5360 | } else if ((line = read_line())) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5361 | line = prepreproc(line); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5362 | tline = tokenize(line); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5363 | nasm_free(line); |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5364 | } else { |
| 5365 | /* |
| 5366 | * The current file has ended; work down the istk |
| 5367 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5368 | Include *i = istk; |
| 5369 | fclose(i->fp); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5370 | if (i->conds) { |
| 5371 | /* nasm_error can't be conditionally suppressed */ |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 5372 | nasm_fatal("expected `%%endif' before end of file"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5373 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5374 | /* 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] | 5375 | if (i->next) |
| 5376 | src_set(i->lineno, i->fname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5377 | istk = i->next; |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 5378 | lfmt->downlevel(LIST_INCLUDE); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5379 | nasm_free(i); |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5380 | return &tok_pop; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5381 | } |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5382 | } while (0); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5383 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5384 | /* |
| 5385 | * We must expand MMacro parameters and MMacro-local labels |
| 5386 | * _before_ we plunge into directive processing, to cope |
| 5387 | * with things like `%define something %1' such as STRUC |
| 5388 | * uses. Unless we're _defining_ a MMacro, in which case |
| 5389 | * those tokens should be left alone to go into the |
| 5390 | * definition; and unless we're in a non-emitting |
| 5391 | * condition, in which case we don't want to meddle with |
| 5392 | * anything. |
| 5393 | */ |
| 5394 | if (!defining && !(istk->conds && !emitting(istk->conds->state)) |
| 5395 | && !(istk->mstk && !istk->mstk->in_progress)) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5396 | tline = expand_mmac_params(tline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5397 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5398 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5399 | /* |
| 5400 | * Check the line to see if it's a preprocessor directive. |
| 5401 | */ |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5402 | if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) { |
| 5403 | if (dtline) |
| 5404 | return dtline; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5405 | } else if (defining) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5406 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5407 | * We're defining a multi-line macro. We emit nothing |
| 5408 | * at all, and just |
| 5409 | * shove the tokenized line on to the macro definition. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5410 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5411 | Line *l = nasm_malloc(sizeof(Line)); |
| 5412 | l->next = defining->expansion; |
| 5413 | l->first = tline; |
| 5414 | l->finishes = NULL; |
| 5415 | defining->expansion = l; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5416 | } else if (istk->conds && !emitting(istk->conds->state)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5417 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5418 | * We're in a non-emitting branch of a condition block. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5419 | * Emit nothing at all, not even a blank line: when we |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5420 | * emerge from the condition we'll give a line-number |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5421 | * directive so we keep our place correctly. |
| 5422 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5423 | free_tlist(tline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5424 | } else if (istk->mstk && !istk->mstk->in_progress) { |
| 5425 | /* |
| 5426 | * We're in a %rep block which has been terminated, so |
| 5427 | * we're walking through to the %endrep without |
| 5428 | * emitting anything. Emit nothing at all, not even a |
| 5429 | * blank line: when we emerge from the %rep block we'll |
| 5430 | * give a line-number directive so we keep our place |
| 5431 | * correctly. |
| 5432 | */ |
| 5433 | free_tlist(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5434 | } else { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5435 | tline = expand_smacro(tline); |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5436 | if (!expand_mmacro(tline)) |
| 5437 | return tline; |
| 5438 | } |
| 5439 | } |
| 5440 | } |
| 5441 | |
| 5442 | static char *pp_getline(void) |
| 5443 | { |
| 5444 | char *line = NULL; |
| 5445 | Token *tline; |
| 5446 | |
| 5447 | real_verror = nasm_set_verror(pp_verror); |
| 5448 | |
| 5449 | while (true) { |
| 5450 | tline = pp_tokline(); |
| 5451 | if (tline == &tok_pop) { |
| 5452 | /* |
| 5453 | * We popped the macro/include stack. If istk is empty, |
| 5454 | * we are at end of input, otherwise just loop back. |
| 5455 | */ |
| 5456 | if (!istk) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5457 | break; |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5458 | } else { |
| 5459 | /* |
| 5460 | * De-tokenize the line and emit it. |
| 5461 | */ |
| 5462 | line = detoken(tline, true); |
| 5463 | free_tlist(tline); |
| 5464 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5465 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5466 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5467 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5468 | nasm_set_verror(real_verror); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5469 | return line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5470 | } |
| 5471 | |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5472 | static void pp_cleanup_pass(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5473 | { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5474 | real_verror = nasm_set_verror(pp_verror); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5475 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5476 | if (defining) { |
| 5477 | if (defining->name) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 5478 | nasm_nonfatal("end of file while still defining macro `%s'", |
| 5479 | defining->name); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5480 | } else { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 5481 | nasm_nonfatal("end of file while still in %%rep"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5482 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5483 | |
| 5484 | free_mmacro(defining); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5485 | defining = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5486 | } |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5487 | |
| 5488 | nasm_set_verror(real_verror); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5489 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5490 | while (cstk) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5491 | ctx_pop(); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 5492 | free_macros(); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5493 | while (istk) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5494 | Include *i = istk; |
| 5495 | istk = istk->next; |
| 5496 | fclose(i->fp); |
Cyrill Gorcunov | 8dcfd88 | 2011-03-03 09:18:56 +0300 | [diff] [blame] | 5497 | nasm_free(i); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5498 | } |
| 5499 | while (cstk) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5500 | ctx_pop(); |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 5501 | src_set_fname(NULL); |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5502 | } |
| 5503 | |
| 5504 | static void pp_cleanup_session(void) |
| 5505 | { |
| 5506 | free_llist(predef); |
| 5507 | predef = NULL; |
| 5508 | delete_Blocks(); |
| 5509 | freeTokens = NULL; |
| 5510 | ipath_list = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5511 | } |
| 5512 | |
Cyrill Gorcunov | 8c0666b | 2018-11-24 14:33:48 +0300 | [diff] [blame] | 5513 | static void pp_include_path(struct strlist *list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5514 | { |
Cyrill Gorcunov | 8c0666b | 2018-11-24 14:33:48 +0300 | [diff] [blame] | 5515 | ipath_list = list; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5516 | } |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5517 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5518 | static void pp_pre_include(char *fname) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5519 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5520 | Token *inc, *space, *name; |
| 5521 | Line *l; |
| 5522 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5523 | name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0); |
| 5524 | space = new_Token(name, TOK_WHITESPACE, NULL, 0); |
| 5525 | inc = new_Token(space, TOK_PREPROC_ID, "%include", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5526 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5527 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5528 | l->next = predef; |
| 5529 | l->first = inc; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5530 | l->finishes = NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5531 | predef = l; |
| 5532 | } |
| 5533 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5534 | static void pp_pre_define(char *definition) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5535 | { |
| 5536 | Token *def, *space; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5537 | Line *l; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5538 | char *equals; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5539 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5540 | real_verror = nasm_set_verror(pp_verror); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5541 | |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5542 | equals = strchr(definition, '='); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5543 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 5544 | def = new_Token(space, TOK_PREPROC_ID, "%define", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5545 | if (equals) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5546 | *equals = ' '; |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5547 | space->next = tokenize(definition); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5548 | if (equals) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5549 | *equals = '='; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5550 | |
Cyrill Gorcunov | 6d42e9b | 2015-02-08 11:07:17 +0300 | [diff] [blame] | 5551 | if (space->next->type != TOK_PREPROC_ID && |
| 5552 | space->next->type != TOK_ID) |
H. Peter Anvin (Intel) | 80c4f23 | 2018-12-14 13:33:24 -0800 | [diff] [blame] | 5553 | nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition); |
Cyrill Gorcunov | 6d42e9b | 2015-02-08 11:07:17 +0300 | [diff] [blame] | 5554 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5555 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5556 | l->next = predef; |
| 5557 | l->first = def; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5558 | l->finishes = NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5559 | predef = l; |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5560 | |
| 5561 | nasm_set_verror(real_verror); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5562 | } |
| 5563 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5564 | static void pp_pre_undefine(char *definition) |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5565 | { |
| 5566 | Token *def, *space; |
| 5567 | Line *l; |
| 5568 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5569 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 5570 | def = new_Token(space, TOK_PREPROC_ID, "%undef", 0); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5571 | space->next = tokenize(definition); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5572 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5573 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5574 | l->next = predef; |
| 5575 | l->first = def; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5576 | l->finishes = NULL; |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5577 | predef = l; |
| 5578 | } |
| 5579 | |
H. Peter Anvin | 0599034 | 2018-06-11 13:32:42 -0700 | [diff] [blame] | 5580 | /* Insert an early preprocessor command that doesn't need special handling */ |
| 5581 | static void pp_pre_command(const char *what, char *string) |
| 5582 | { |
| 5583 | char *cmd; |
| 5584 | Token *def, *space; |
| 5585 | Line *l; |
| 5586 | |
| 5587 | def = tokenize(string); |
| 5588 | if (what) { |
| 5589 | cmd = nasm_strcat(what[0] == '%' ? "" : "%", what); |
| 5590 | space = new_Token(def, TOK_WHITESPACE, NULL, 0); |
| 5591 | def = new_Token(space, TOK_PREPROC_ID, cmd, 0); |
| 5592 | } |
| 5593 | |
| 5594 | l = nasm_malloc(sizeof(Line)); |
| 5595 | l->next = predef; |
| 5596 | l->first = def; |
| 5597 | l->finishes = NULL; |
| 5598 | predef = l; |
| 5599 | } |
| 5600 | |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 5601 | static void pp_add_stdmac(macros_t *macros) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5602 | { |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 5603 | macros_t **mp; |
| 5604 | |
| 5605 | /* Find the end of the list and avoid duplicates */ |
| 5606 | for (mp = stdmacros; *mp; mp++) { |
| 5607 | if (*mp == macros) |
| 5608 | return; /* Nothing to do */ |
| 5609 | } |
| 5610 | |
| 5611 | nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]); |
| 5612 | |
| 5613 | *mp = macros; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 5614 | } |
| 5615 | |
Cyrill Gorcunov | 15ce78f | 2017-01-06 20:21:28 +0300 | [diff] [blame] | 5616 | static void pp_extra_stdmac(macros_t *macros) |
| 5617 | { |
| 5618 | extrastdmac = macros; |
| 5619 | } |
| 5620 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5621 | static Token *make_tok_num(int64_t val) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5622 | { |
Cyrill Gorcunov | ce65274 | 2013-05-06 23:43:43 +0400 | [diff] [blame] | 5623 | char numbuf[32]; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5624 | int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val); |
| 5625 | return new_Token(NULL, TOK_NUMBER, numbuf, len); |
| 5626 | } |
| 5627 | |
| 5628 | static Token *make_tok_qstr(const char *str) |
| 5629 | { |
| 5630 | Token *t = new_Token(NULL, TOK_STRING, NULL, 0); |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 5631 | t->text = nasm_quote_cstr(str, &t->len); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5632 | return t; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5633 | } |
| 5634 | |
H. Peter Anvin (Intel) | 6bde2ed | 2018-12-13 19:39:41 -0800 | [diff] [blame] | 5635 | static void pp_list_one_macro(MMacro *m, errflags severity) |
H. Peter Anvin | 3736895 | 2016-05-09 14:10:32 -0700 | [diff] [blame] | 5636 | { |
| 5637 | if (!m) |
| 5638 | return; |
| 5639 | |
| 5640 | /* We need to print the next_active list in reverse order */ |
| 5641 | pp_list_one_macro(m->next_active, severity); |
| 5642 | |
| 5643 | if (m->name && !m->nolist) { |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 5644 | src_set(m->xline + m->lineno, m->fname); |
H. Peter Anvin | ddb2906 | 2018-12-11 00:06:29 -0800 | [diff] [blame] | 5645 | nasm_error(severity, "... from macro `%s' defined", m->name); |
H. Peter Anvin | 3736895 | 2016-05-09 14:10:32 -0700 | [diff] [blame] | 5646 | } |
| 5647 | } |
| 5648 | |
H. Peter Anvin (Intel) | 6bde2ed | 2018-12-13 19:39:41 -0800 | [diff] [blame] | 5649 | static void pp_error_list_macros(errflags severity) |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5650 | { |
H. Peter Anvin | ddb2906 | 2018-12-11 00:06:29 -0800 | [diff] [blame] | 5651 | struct src_location saved; |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5652 | |
H. Peter Anvin | ddb2906 | 2018-12-11 00:06:29 -0800 | [diff] [blame] | 5653 | severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE; |
| 5654 | saved = src_where(); |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5655 | |
Cyrill Gorcunov | 771d04e | 2016-05-10 23:27:03 +0300 | [diff] [blame] | 5656 | if (istk) |
| 5657 | pp_list_one_macro(istk->mstk, severity); |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5658 | |
H. Peter Anvin | ddb2906 | 2018-12-11 00:06:29 -0800 | [diff] [blame] | 5659 | src_update(saved); |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5660 | } |
| 5661 | |
H. Peter Anvin | e746971 | 2016-02-18 02:20:59 -0800 | [diff] [blame] | 5662 | const struct preproc_ops nasmpp = { |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 5663 | pp_init, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5664 | pp_reset, |
| 5665 | pp_getline, |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5666 | pp_cleanup_pass, |
| 5667 | pp_cleanup_session, |
Cyrill Gorcunov | 15ce78f | 2017-01-06 20:21:28 +0300 | [diff] [blame] | 5668 | pp_extra_stdmac, |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5669 | pp_pre_define, |
| 5670 | pp_pre_undefine, |
| 5671 | pp_pre_include, |
H. Peter Anvin | 0599034 | 2018-06-11 13:32:42 -0700 | [diff] [blame] | 5672 | pp_pre_command, |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5673 | pp_include_path, |
| 5674 | pp_error_list_macros, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5675 | }; |