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) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 99 | * Function call for a magic smacro |
| 100 | */ |
| 101 | typedef Token *(*MagicSMacro)(const SMacro *s, Token **params, |
| 102 | unsigned int nparams, bool *free_expansion); |
| 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; |
| 111 | MagicSMacro magic; |
| 112 | intorptr magicpvt; |
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 | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 609 | * Free an MMacro |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 610 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 611 | static void free_mmacro(MMacro * m) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 612 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 613 | nasm_free(m->name); |
| 614 | free_tlist(m->dlist); |
| 615 | nasm_free(m->defaults); |
| 616 | free_llist(m->expansion); |
| 617 | nasm_free(m); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | /* |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 621 | * Clear or free an SMacro |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 622 | */ |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 623 | static void free_smacro_members(SMacro *s) |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 624 | { |
| 625 | nasm_free(s->name); |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 626 | free_tlist(s->expansion); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 627 | nasm_free(s->eval_param); |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | static void clear_smacro(SMacro *s) |
| 631 | { |
| 632 | free_smacro_members(s); |
| 633 | /* Wipe everything except the next pointer */ |
| 634 | memset(&s->next + 1, 0, sizeof *s - sizeof s->next); |
| 635 | } |
| 636 | |
| 637 | /* |
| 638 | * Free an SMacro |
| 639 | */ |
| 640 | static void free_smacro(SMacro *s) |
| 641 | { |
| 642 | free_smacro_members(s); |
| 643 | nasm_free(s); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | /* |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 647 | * Free all currently defined macros, and free the hash tables |
| 648 | */ |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 649 | static void free_smacro_table(struct hash_table *smt) |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 650 | { |
H. Peter Anvin (Intel) | ebb05a0 | 2018-12-11 12:30:25 -0800 | [diff] [blame] | 651 | struct hash_iterator it; |
| 652 | const struct hash_node *np; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 653 | |
H. Peter Anvin (Intel) | ebb05a0 | 2018-12-11 12:30:25 -0800 | [diff] [blame] | 654 | hash_for_each(smt, it, np) { |
| 655 | SMacro *tmp; |
| 656 | SMacro *s = np->data; |
| 657 | nasm_free((void *)np->key); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 658 | list_for_each_safe(s, tmp, s) |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 659 | free_smacro(s); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 660 | } |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 661 | hash_free(smt); |
| 662 | } |
| 663 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 664 | static void free_mmacro_table(struct hash_table *mmt) |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 665 | { |
H. Peter Anvin (Intel) | ebb05a0 | 2018-12-11 12:30:25 -0800 | [diff] [blame] | 666 | struct hash_iterator it; |
| 667 | const struct hash_node *np; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 668 | |
H. Peter Anvin (Intel) | ebb05a0 | 2018-12-11 12:30:25 -0800 | [diff] [blame] | 669 | hash_for_each(mmt, it, np) { |
| 670 | MMacro *tmp; |
| 671 | MMacro *m = np->data; |
| 672 | nasm_free((void *)np->key); |
| 673 | list_for_each_safe(m, tmp, m) |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 674 | free_mmacro(m); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 675 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 676 | hash_free(mmt); |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | static void free_macros(void) |
| 680 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 681 | free_smacro_table(&smacros); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 682 | free_mmacro_table(&mmacros); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | /* |
| 686 | * Initialize the hash tables |
| 687 | */ |
| 688 | static void init_macros(void) |
| 689 | { |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 693 | * Pop the context stack. |
| 694 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 695 | static void ctx_pop(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 696 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 697 | Context *c = cstk; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 698 | |
| 699 | cstk = cstk->next; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 700 | free_smacro_table(&c->localmac); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 701 | nasm_free(c->name); |
| 702 | nasm_free(c); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 703 | } |
| 704 | |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 705 | /* |
| 706 | * Search for a key in the hash index; adding it if necessary |
| 707 | * (in which case we initialize the data pointer to NULL.) |
| 708 | */ |
| 709 | static void ** |
| 710 | hash_findi_add(struct hash_table *hash, const char *str) |
| 711 | { |
| 712 | struct hash_insert hi; |
| 713 | void **r; |
| 714 | char *strx; |
H. Peter Anvin (Intel) | ebb05a0 | 2018-12-11 12:30:25 -0800 | [diff] [blame] | 715 | size_t l = strlen(str) + 1; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 716 | |
H. Peter Anvin (Intel) | ebb05a0 | 2018-12-11 12:30:25 -0800 | [diff] [blame] | 717 | r = hash_findib(hash, str, l, &hi); |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 718 | if (r) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 719 | return r; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 720 | |
H. Peter Anvin (Intel) | ebb05a0 | 2018-12-11 12:30:25 -0800 | [diff] [blame] | 721 | strx = nasm_malloc(l); /* Use a more efficient allocator here? */ |
| 722 | memcpy(strx, str, l); |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 723 | return hash_add(&hi, strx, NULL); |
| 724 | } |
| 725 | |
| 726 | /* |
| 727 | * Like hash_findi, but returns the data element rather than a pointer |
| 728 | * to it. Used only when not adding a new element, hence no third |
| 729 | * argument. |
| 730 | */ |
| 731 | static void * |
| 732 | hash_findix(struct hash_table *hash, const char *str) |
| 733 | { |
| 734 | void **p; |
| 735 | |
| 736 | p = hash_findi(hash, str, NULL); |
| 737 | return p ? *p : NULL; |
| 738 | } |
| 739 | |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 740 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 741 | * read line from standart macros set, |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 742 | * if there no more left -- return NULL |
| 743 | */ |
| 744 | static char *line_from_stdmac(void) |
| 745 | { |
| 746 | unsigned char c; |
| 747 | const unsigned char *p = stdmacpos; |
| 748 | char *line, *q; |
| 749 | size_t len = 0; |
| 750 | |
| 751 | if (!stdmacpos) |
| 752 | return NULL; |
| 753 | |
| 754 | while ((c = *p++)) { |
| 755 | if (c >= 0x80) |
| 756 | len += pp_directives_len[c - 0x80] + 1; |
| 757 | else |
| 758 | len++; |
| 759 | } |
| 760 | |
| 761 | line = nasm_malloc(len + 1); |
| 762 | q = line; |
| 763 | while ((c = *stdmacpos++)) { |
| 764 | if (c >= 0x80) { |
| 765 | memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]); |
| 766 | q += pp_directives_len[c - 0x80]; |
| 767 | *q++ = ' '; |
| 768 | } else { |
| 769 | *q++ = c; |
| 770 | } |
| 771 | } |
| 772 | stdmacpos = p; |
| 773 | *q = '\0'; |
| 774 | |
| 775 | if (!*stdmacpos) { |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 776 | /* This was the last of this particular macro set */ |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 777 | stdmacpos = NULL; |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 778 | if (*stdmacnext) { |
| 779 | stdmacpos = *stdmacnext++; |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 780 | } else if (do_predef) { |
| 781 | Line *pd, *l; |
| 782 | Token *head, **tail, *t; |
| 783 | |
| 784 | /* |
| 785 | * Nasty hack: here we push the contents of |
| 786 | * `predef' on to the top-level expansion stack, |
| 787 | * since this is the most convenient way to |
| 788 | * implement the pre-include and pre-define |
| 789 | * features. |
| 790 | */ |
| 791 | list_for_each(pd, predef) { |
| 792 | head = NULL; |
| 793 | tail = &head; |
| 794 | list_for_each(t, pd->first) { |
| 795 | *tail = new_Token(NULL, t->type, t->text, 0); |
| 796 | tail = &(*tail)->next; |
| 797 | } |
| 798 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 799 | l = nasm_malloc(sizeof(Line)); |
| 800 | l->next = istk->expansion; |
| 801 | l->first = head; |
| 802 | l->finishes = NULL; |
| 803 | |
| 804 | istk->expansion = l; |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 805 | } |
| 806 | do_predef = false; |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | return line; |
| 811 | } |
| 812 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 813 | static char *read_line(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 814 | { |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 815 | int c; |
| 816 | unsigned int size, next; |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 817 | const unsigned int delta = 512; |
| 818 | const unsigned int pad = 8; |
| 819 | unsigned int nr_cont = 0; |
| 820 | bool cont = false; |
| 821 | char *buffer, *p; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 822 | |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 823 | /* Standart macros set (predefined) goes first */ |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 824 | p = line_from_stdmac(); |
| 825 | if (p) |
| 826 | return p; |
H. Peter Anvin | 72edbb8 | 2008-06-19 16:00:04 -0700 | [diff] [blame] | 827 | |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 828 | size = delta; |
| 829 | p = buffer = nasm_malloc(size); |
| 830 | |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 831 | do { |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 832 | c = fgetc(istk->fp); |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 833 | |
| 834 | switch (c) { |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 835 | case EOF: |
| 836 | if (p == buffer) { |
| 837 | nasm_free(buffer); |
| 838 | return NULL; |
| 839 | } |
| 840 | c = 0; |
| 841 | break; |
| 842 | |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 843 | case '\r': |
| 844 | next = fgetc(istk->fp); |
| 845 | if (next != '\n') |
| 846 | ungetc(next, istk->fp); |
| 847 | if (cont) { |
| 848 | cont = false; |
| 849 | continue; |
| 850 | } |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 851 | c = 0; |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 852 | break; |
| 853 | |
| 854 | case '\n': |
| 855 | if (cont) { |
| 856 | cont = false; |
| 857 | continue; |
| 858 | } |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 859 | c = 0; |
| 860 | break; |
| 861 | |
| 862 | case 032: /* ^Z = legacy MS-DOS end of file mark */ |
| 863 | c = 0; |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 864 | break; |
| 865 | |
| 866 | case '\\': |
| 867 | next = fgetc(istk->fp); |
| 868 | ungetc(next, istk->fp); |
Cyrill Gorcunov | 490f85e | 2012-12-27 20:02:17 +0400 | [diff] [blame] | 869 | if (next == '\r' || next == '\n') { |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 870 | cont = true; |
| 871 | nr_cont++; |
| 872 | continue; |
| 873 | } |
| 874 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 875 | } |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 876 | |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 877 | if (p >= (buffer + size - pad)) { |
| 878 | buffer = nasm_realloc(buffer, size + delta); |
| 879 | p = buffer + size - pad; |
| 880 | size += delta; |
| 881 | } |
| 882 | |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 883 | *p++ = c; |
| 884 | } while (c); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 885 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 886 | src_set_linnum(src_get_linnum() + istk->lineinc + |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 887 | (nr_cont * istk->lineinc)); |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 888 | lfmt->line(LIST_READ, buffer); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 889 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 890 | return buffer; |
| 891 | } |
| 892 | |
| 893 | /* |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 894 | * 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] | 895 | * don't need to parse the value out of e.g. numeric tokens: we |
| 896 | * simply split one string into many. |
| 897 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 898 | static Token *tokenize(char *line) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 899 | { |
H. Peter Anvin | ca544db | 2008-10-19 19:30:11 -0700 | [diff] [blame] | 900 | char c, *p = line; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 901 | enum pp_token_type type; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 902 | Token *list = NULL; |
| 903 | Token *t, **tail = &list; |
| 904 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 905 | while (*line) { |
| 906 | p = line; |
| 907 | if (*p == '%') { |
| 908 | p++; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 909 | if (*p == '+' && !nasm_isdigit(p[1])) { |
| 910 | p++; |
| 911 | type = TOK_PASTE; |
| 912 | } else if (nasm_isdigit(*p) || |
| 913 | ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 914 | do { |
| 915 | p++; |
| 916 | } |
H. Peter Anvin | bda7a6e | 2008-06-21 10:23:17 -0700 | [diff] [blame] | 917 | while (nasm_isdigit(*p)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 918 | type = TOK_PREPROC_ID; |
| 919 | } else if (*p == '{') { |
| 920 | p++; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 921 | while (*p) { |
| 922 | if (*p == '}') |
| 923 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 924 | p[-1] = *p; |
| 925 | p++; |
| 926 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 927 | if (*p != '}') |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 928 | nasm_warn(WARN_OTHER, "unterminated %%{ construct"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 929 | p[-1] = '\0'; |
| 930 | if (*p) |
| 931 | p++; |
| 932 | type = TOK_PREPROC_ID; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 933 | } else if (*p == '[') { |
| 934 | int lvl = 1; |
| 935 | line += 2; /* Skip the leading %[ */ |
| 936 | p++; |
| 937 | while (lvl && (c = *p++)) { |
| 938 | switch (c) { |
| 939 | case ']': |
| 940 | lvl--; |
| 941 | break; |
| 942 | case '%': |
| 943 | if (*p == '[') |
| 944 | lvl++; |
| 945 | break; |
| 946 | case '\'': |
| 947 | case '\"': |
| 948 | case '`': |
Cyrill Gorcunov | 3144e84 | 2017-10-22 10:50:55 +0300 | [diff] [blame] | 949 | p = nasm_skip_string(p - 1); |
| 950 | if (*p) |
| 951 | p++; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 952 | break; |
| 953 | default: |
| 954 | break; |
| 955 | } |
| 956 | } |
| 957 | p--; |
| 958 | if (*p) |
| 959 | *p++ = '\0'; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 960 | if (lvl) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 961 | nasm_nonfatalf(ERR_PASS1, "unterminated %%[ construct"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 962 | type = TOK_INDIRECT; |
| 963 | } else if (*p == '?') { |
| 964 | type = TOK_PREPROC_Q; /* %? */ |
| 965 | p++; |
| 966 | if (*p == '?') { |
| 967 | type = TOK_PREPROC_QQ; /* %?? */ |
| 968 | p++; |
| 969 | } |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 970 | } else if (*p == '!') { |
| 971 | type = TOK_PREPROC_ID; |
| 972 | p++; |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 973 | if (nasm_isidchar(*p)) { |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 974 | do { |
| 975 | p++; |
| 976 | } |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 977 | while (nasm_isidchar(*p)); |
H. Peter Anvin | 53e2e4c | 2018-11-28 15:01:40 -0800 | [diff] [blame] | 978 | } else if (nasm_isquote(*p)) { |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 979 | p = nasm_skip_string(p); |
| 980 | if (*p) |
| 981 | p++; |
| 982 | else |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 983 | nasm_nonfatalf(ERR_PASS1, "unterminated %%! string"); |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 984 | } else { |
| 985 | /* %! without string or identifier */ |
| 986 | type = TOK_OTHER; /* Legacy behavior... */ |
| 987 | } |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 988 | } else if (nasm_isidchar(*p) || |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 989 | ((*p == '!' || *p == '%' || *p == '$') && |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 990 | nasm_isidchar(p[1]))) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 991 | do { |
| 992 | p++; |
| 993 | } |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 994 | while (nasm_isidchar(*p)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 995 | type = TOK_PREPROC_ID; |
| 996 | } else { |
| 997 | type = TOK_OTHER; |
| 998 | if (*p == '%') |
| 999 | p++; |
| 1000 | } |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 1001 | } else if (nasm_isidstart(*p) || (*p == '$' && nasm_isidstart(p[1]))) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1002 | type = TOK_ID; |
| 1003 | p++; |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 1004 | while (*p && nasm_isidchar(*p)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1005 | p++; |
H. Peter Anvin | 53e2e4c | 2018-11-28 15:01:40 -0800 | [diff] [blame] | 1006 | } else if (nasm_isquote(*p)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1007 | /* |
| 1008 | * A string token. |
| 1009 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1010 | type = TOK_STRING; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1011 | p = nasm_skip_string(p); |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1012 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1013 | if (*p) { |
| 1014 | p++; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1015 | } else { |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 1016 | nasm_warn(WARN_OTHER, "unterminated string"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1017 | /* Handling unterminated strings by UNV */ |
| 1018 | /* type = -1; */ |
| 1019 | } |
Victor van den Elzen | fb5f251 | 2009-04-17 16:17:59 +0200 | [diff] [blame] | 1020 | } else if (p[0] == '$' && p[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1021 | type = TOK_OTHER; /* TOKEN_BASE */ |
Victor van den Elzen | fb5f251 | 2009-04-17 16:17:59 +0200 | [diff] [blame] | 1022 | p += 2; |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 1023 | } else if (nasm_isnumstart(*p)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1024 | bool is_hex = false; |
| 1025 | bool is_float = false; |
| 1026 | bool has_e = false; |
| 1027 | char c, *r; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1028 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1029 | /* |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1030 | * A numeric token. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1031 | */ |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1032 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1033 | if (*p == '$') { |
| 1034 | p++; |
| 1035 | is_hex = true; |
| 1036 | } |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1037 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1038 | for (;;) { |
| 1039 | c = *p++; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1040 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1041 | if (!is_hex && (c == 'e' || c == 'E')) { |
| 1042 | has_e = true; |
| 1043 | if (*p == '+' || *p == '-') { |
| 1044 | /* |
| 1045 | * e can only be followed by +/- if it is either a |
| 1046 | * prefixed hex number or a floating-point number |
| 1047 | */ |
| 1048 | p++; |
| 1049 | is_float = true; |
| 1050 | } |
| 1051 | } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') { |
| 1052 | is_hex = true; |
| 1053 | } else if (c == 'P' || c == 'p') { |
| 1054 | is_float = true; |
| 1055 | if (*p == '+' || *p == '-') |
| 1056 | p++; |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 1057 | } else if (nasm_isnumchar(c)) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1058 | ; /* just advance */ |
| 1059 | else if (c == '.') { |
| 1060 | /* |
| 1061 | * we need to deal with consequences of the legacy |
| 1062 | * parser, like "1.nolist" being two tokens |
| 1063 | * (TOK_NUMBER, TOK_ID) here; at least give it |
| 1064 | * a shot for now. In the future, we probably need |
| 1065 | * a flex-based scanner with proper pattern matching |
| 1066 | * to do it as well as it can be done. Nothing in |
| 1067 | * the world is going to help the person who wants |
| 1068 | * 0x123.p16 interpreted as two tokens, though. |
| 1069 | */ |
| 1070 | r = p; |
| 1071 | while (*r == '_') |
| 1072 | r++; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1073 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1074 | if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) || |
| 1075 | (!is_hex && (*r == 'e' || *r == 'E')) || |
| 1076 | (*r == 'p' || *r == 'P')) { |
| 1077 | p = r; |
| 1078 | is_float = true; |
| 1079 | } else |
| 1080 | break; /* Terminate the token */ |
| 1081 | } else |
| 1082 | break; |
| 1083 | } |
| 1084 | p--; /* Point to first character beyond number */ |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1085 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1086 | if (p == line+1 && *line == '$') { |
| 1087 | type = TOK_OTHER; /* TOKEN_HERE */ |
| 1088 | } else { |
| 1089 | if (has_e && !is_hex) { |
| 1090 | /* 1e13 is floating-point, but 1e13h is not */ |
| 1091 | is_float = true; |
| 1092 | } |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 1093 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1094 | type = is_float ? TOK_FLOAT : TOK_NUMBER; |
| 1095 | } |
H. Peter Anvin | bda7a6e | 2008-06-21 10:23:17 -0700 | [diff] [blame] | 1096 | } else if (nasm_isspace(*p)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1097 | type = TOK_WHITESPACE; |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 1098 | p = nasm_skip_spaces(p); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1099 | /* |
| 1100 | * Whitespace just before end-of-line is discarded by |
| 1101 | * pretending it's a comment; whitespace just before a |
| 1102 | * comment gets lumped into the comment. |
| 1103 | */ |
| 1104 | if (!*p || *p == ';') { |
| 1105 | type = TOK_COMMENT; |
| 1106 | while (*p) |
| 1107 | p++; |
| 1108 | } |
| 1109 | } else if (*p == ';') { |
| 1110 | type = TOK_COMMENT; |
| 1111 | while (*p) |
| 1112 | p++; |
| 1113 | } else { |
| 1114 | /* |
| 1115 | * Anything else is an operator of some kind. We check |
| 1116 | * for all the double-character operators (>>, <<, //, |
| 1117 | * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1118 | * else is a single-character operator. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1119 | */ |
| 1120 | type = TOK_OTHER; |
| 1121 | if ((p[0] == '>' && p[1] == '>') || |
| 1122 | (p[0] == '<' && p[1] == '<') || |
| 1123 | (p[0] == '/' && p[1] == '/') || |
| 1124 | (p[0] == '<' && p[1] == '=') || |
| 1125 | (p[0] == '>' && p[1] == '=') || |
| 1126 | (p[0] == '=' && p[1] == '=') || |
| 1127 | (p[0] == '!' && p[1] == '=') || |
| 1128 | (p[0] == '<' && p[1] == '>') || |
| 1129 | (p[0] == '&' && p[1] == '&') || |
| 1130 | (p[0] == '|' && p[1] == '|') || |
| 1131 | (p[0] == '^' && p[1] == '^')) { |
| 1132 | p++; |
| 1133 | } |
| 1134 | p++; |
| 1135 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1136 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1137 | /* Handling unterminated string by UNV */ |
| 1138 | /*if (type == -1) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1139 | { |
| 1140 | *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1); |
| 1141 | t->text[p-line] = *line; |
| 1142 | tail = &t->next; |
| 1143 | } |
| 1144 | else */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1145 | if (type != TOK_COMMENT) { |
| 1146 | *tail = t = new_Token(NULL, type, line, p - line); |
| 1147 | tail = &t->next; |
| 1148 | } |
| 1149 | line = p; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1150 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1151 | return list; |
| 1152 | } |
| 1153 | |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1154 | /* |
| 1155 | * this function allocates a new managed block of memory and |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1156 | * returns a pointer to the block. The managed blocks are |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1157 | * deleted only all at once by the delete_Blocks function. |
| 1158 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1159 | static void *new_Block(size_t size) |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1160 | { |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1161 | Blocks *b = &blocks; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1162 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1163 | /* first, get to the end of the linked list */ |
| 1164 | while (b->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1165 | b = b->next; |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1166 | /* now allocate the requested chunk */ |
| 1167 | b->chunk = nasm_malloc(size); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1168 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1169 | /* now allocate a new block for the next request */ |
Cyrill Gorcunov | c31767c | 2014-06-28 02:22:17 +0400 | [diff] [blame] | 1170 | b->next = nasm_zalloc(sizeof(Blocks)); |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1171 | return b->chunk; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | /* |
| 1175 | * this function deletes all managed blocks of memory |
| 1176 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1177 | static void delete_Blocks(void) |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1178 | { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1179 | Blocks *a, *b = &blocks; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1180 | |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1181 | /* |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1182 | * keep in mind that the first block, pointed to by blocks |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1183 | * is a static and not dynamically allocated, so we don't |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1184 | * free it. |
| 1185 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1186 | while (b) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1187 | if (b->chunk) |
| 1188 | nasm_free(b->chunk); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1189 | a = b; |
| 1190 | b = b->next; |
| 1191 | if (a != &blocks) |
| 1192 | nasm_free(a); |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1193 | } |
Cyrill Gorcunov | dae24d7 | 2014-06-28 10:17:39 +0400 | [diff] [blame] | 1194 | memset(&blocks, 0, sizeof(blocks)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1195 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1196 | |
| 1197 | /* |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1198 | * 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] | 1199 | * 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] | 1200 | */ |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1201 | 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] | 1202 | const char *text, size_t txtlen) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1203 | { |
| 1204 | Token *t; |
| 1205 | int i; |
| 1206 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1207 | if (!freeTokens) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1208 | freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token)); |
| 1209 | for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++) |
| 1210 | freeTokens[i].next = &freeTokens[i + 1]; |
| 1211 | freeTokens[i].next = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1212 | } |
| 1213 | t = freeTokens; |
| 1214 | freeTokens = t->next; |
| 1215 | t->next = next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1216 | t->type = type; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1217 | if (type == TOK_WHITESPACE || !text) { |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 1218 | t->len = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1219 | t->text = NULL; |
| 1220 | } else { |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 1221 | if (txtlen == 0 && text[0]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1222 | txtlen = strlen(text); |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 1223 | t->len = txtlen; |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1224 | t->text = nasm_malloc(txtlen+1); |
| 1225 | memcpy(t->text, text, txtlen); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1226 | t->text[txtlen] = '\0'; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1227 | } |
| 1228 | return t; |
| 1229 | } |
| 1230 | |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 1231 | static Token *dup_Token(Token *next, const Token *src) |
| 1232 | { |
| 1233 | return new_Token(next, src->type, src->text, src->len); |
| 1234 | } |
| 1235 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1236 | static Token *delete_Token(Token * t) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1237 | { |
| 1238 | Token *next = t->next; |
| 1239 | nasm_free(t->text); |
H. Peter Anvin | 788e6c1 | 2002-04-30 21:02:01 +0000 | [diff] [blame] | 1240 | t->next = freeTokens; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1241 | freeTokens = t; |
| 1242 | return next; |
| 1243 | } |
| 1244 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1245 | /* |
| 1246 | * Convert a line of tokens back into text. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1247 | * If expand_locals is not zero, identifiers of the form "%$*xxx" |
| 1248 | * will be transformed into ..@ctxnum.xxx |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1249 | */ |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 1250 | static char *detoken(Token * tlist, bool expand_locals) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1251 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1252 | Token *t; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1253 | char *line, *p; |
H. Peter Anvin | b4daadc | 2008-01-21 16:31:57 -0800 | [diff] [blame] | 1254 | const char *q; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1255 | int len = 0; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1256 | |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1257 | list_for_each(t, tlist) { |
Cyrill Gorcunov | 9b7ee09 | 2017-10-22 21:42:59 +0300 | [diff] [blame] | 1258 | if (t->type == TOK_PREPROC_ID && t->text && |
| 1259 | t->text[0] && t->text[1] == '!') { |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1260 | char *v; |
| 1261 | char *q = t->text; |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1262 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1263 | v = t->text + 2; |
H. Peter Anvin | 53e2e4c | 2018-11-28 15:01:40 -0800 | [diff] [blame] | 1264 | if (nasm_isquote(*v)) { |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1265 | size_t len = nasm_unquote(v, NULL); |
| 1266 | size_t clen = strlen(v); |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1267 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1268 | if (len != clen) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1269 | nasm_nonfatalf(ERR_PASS1, "NUL character in %%! string"); |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1270 | v = NULL; |
| 1271 | } |
| 1272 | } |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1273 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1274 | if (v) { |
| 1275 | char *p = getenv(v); |
| 1276 | if (!p) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1277 | nasm_nonfatalf(ERR_PASS1, "nonexistent environment variable `%s'", v); |
Cyrill Gorcunov | bbb7a1a | 2016-06-19 12:15:24 +0300 | [diff] [blame] | 1278 | /* |
| 1279 | * FIXME We better should investigate if accessing |
| 1280 | * ->text[1] without ->text[0] is safe enough. |
| 1281 | */ |
| 1282 | t->text = nasm_zalloc(2); |
| 1283 | } else |
| 1284 | t->text = nasm_strdup(p); |
Cyrill Gorcunov | 7500487 | 2017-07-26 01:21:16 +0300 | [diff] [blame] | 1285 | nasm_free(q); |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1286 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1287 | } |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1288 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1289 | /* Expand local macros here and not during preprocessing */ |
| 1290 | if (expand_locals && |
| 1291 | t->type == TOK_PREPROC_ID && t->text && |
| 1292 | t->text[0] == '%' && t->text[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1293 | const char *q; |
| 1294 | char *p; |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1295 | Context *ctx = get_ctx(t->text, &q); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1296 | if (ctx) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1297 | char buffer[40]; |
Keith Kanios | 93f2e9a | 2007-04-14 00:10:59 +0000 | [diff] [blame] | 1298 | snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1299 | p = nasm_strcat(buffer, q); |
| 1300 | nasm_free(t->text); |
| 1301 | t->text = p; |
| 1302 | } |
| 1303 | } |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1304 | if (t->type == TOK_WHITESPACE) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1305 | len++; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1306 | else if (t->text) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1307 | len += strlen(t->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1308 | } |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1309 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1310 | p = line = nasm_malloc(len + 1); |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1311 | |
| 1312 | list_for_each(t, tlist) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1313 | if (t->type == TOK_WHITESPACE) { |
H. Peter Anvin | b4daadc | 2008-01-21 16:31:57 -0800 | [diff] [blame] | 1314 | *p++ = ' '; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1315 | } else if (t->text) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1316 | q = t->text; |
| 1317 | while (*q) |
| 1318 | *p++ = *q++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1319 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1320 | } |
| 1321 | *p = '\0'; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1322 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1323 | return line; |
| 1324 | } |
| 1325 | |
| 1326 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1327 | * A scanner, suitable for use by the expression evaluator, which |
| 1328 | * operates on a line of Tokens. Expects a pointer to a pointer to |
| 1329 | * the first token in the line to be passed in as its private_data |
| 1330 | * field. |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1331 | * |
| 1332 | * FIX: This really needs to be unified with stdscan. |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1333 | */ |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 1334 | struct ppscan { |
| 1335 | Token *tptr; |
| 1336 | int ntokens; |
| 1337 | }; |
| 1338 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1339 | static int ppscan(void *private_data, struct tokenval *tokval) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1340 | { |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 1341 | struct ppscan *pps = private_data; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1342 | Token *tline; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1343 | char ourcopy[MAX_KEYWORD+1], *p, *r, *s; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1344 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1345 | do { |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 1346 | if (pps->ntokens && (tline = pps->tptr)) { |
| 1347 | pps->ntokens--; |
| 1348 | pps->tptr = tline->next; |
| 1349 | } else { |
| 1350 | pps->tptr = NULL; |
| 1351 | pps->ntokens = 0; |
| 1352 | return tokval->t_type = TOKEN_EOS; |
| 1353 | } |
| 1354 | } while (tline->type == TOK_WHITESPACE || tline->type == TOK_COMMENT); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1355 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1356 | tokval->t_charptr = tline->text; |
| 1357 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1358 | if (tline->text[0] == '$' && !tline->text[1]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1359 | return tokval->t_type = TOKEN_HERE; |
H. Peter Anvin | 7cf897e | 2002-05-30 21:30:33 +0000 | [diff] [blame] | 1360 | if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1361 | return tokval->t_type = TOKEN_BASE; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1362 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1363 | if (tline->type == TOK_ID) { |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1364 | p = tokval->t_charptr = tline->text; |
| 1365 | if (p[0] == '$') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1366 | tokval->t_charptr++; |
| 1367 | return tokval->t_type = TOKEN_ID; |
| 1368 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1369 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1370 | for (r = p, s = ourcopy; *r; r++) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1371 | if (r >= p+MAX_KEYWORD) |
| 1372 | return tokval->t_type = TOKEN_ID; /* Not a keyword */ |
H. Peter Anvin | ac8f8fc | 2008-06-11 15:49:41 -0700 | [diff] [blame] | 1373 | *s++ = nasm_tolower(*r); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1374 | } |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1375 | *s = '\0'; |
| 1376 | /* right, so we have an identifier sitting in temp storage. now, |
| 1377 | * is it actually a register or instruction name, or what? */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1378 | return nasm_token_hash(ourcopy, tokval); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1379 | } |
| 1380 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1381 | if (tline->type == TOK_NUMBER) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1382 | bool rn_error; |
| 1383 | tokval->t_integer = readnum(tline->text, &rn_error); |
| 1384 | tokval->t_charptr = tline->text; |
| 1385 | if (rn_error) |
| 1386 | return tokval->t_type = TOKEN_ERRNUM; |
| 1387 | else |
| 1388 | return tokval->t_type = TOKEN_NUM; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1389 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1390 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1391 | if (tline->type == TOK_FLOAT) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1392 | return tokval->t_type = TOKEN_FLOAT; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1393 | } |
| 1394 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1395 | if (tline->type == TOK_STRING) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1396 | char bq, *ep; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1397 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1398 | bq = tline->text[0]; |
H. Peter Anvin | 1162704 | 2008-06-09 20:45:19 -0700 | [diff] [blame] | 1399 | tokval->t_charptr = tline->text; |
| 1400 | tokval->t_inttwo = nasm_unquote(tline->text, &ep); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 1401 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1402 | if (ep[0] != bq || ep[1] != '\0') |
| 1403 | return tokval->t_type = TOKEN_ERRSTR; |
| 1404 | else |
| 1405 | return tokval->t_type = TOKEN_STR; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1408 | if (tline->type == TOK_OTHER) { |
| 1409 | if (!strcmp(tline->text, "<<")) |
| 1410 | return tokval->t_type = TOKEN_SHL; |
| 1411 | if (!strcmp(tline->text, ">>")) |
| 1412 | return tokval->t_type = TOKEN_SHR; |
| 1413 | if (!strcmp(tline->text, "//")) |
| 1414 | return tokval->t_type = TOKEN_SDIV; |
| 1415 | if (!strcmp(tline->text, "%%")) |
| 1416 | return tokval->t_type = TOKEN_SMOD; |
| 1417 | if (!strcmp(tline->text, "==")) |
| 1418 | return tokval->t_type = TOKEN_EQ; |
| 1419 | if (!strcmp(tline->text, "<>")) |
| 1420 | return tokval->t_type = TOKEN_NE; |
| 1421 | if (!strcmp(tline->text, "!=")) |
| 1422 | return tokval->t_type = TOKEN_NE; |
| 1423 | if (!strcmp(tline->text, "<=")) |
| 1424 | return tokval->t_type = TOKEN_LE; |
| 1425 | if (!strcmp(tline->text, ">=")) |
| 1426 | return tokval->t_type = TOKEN_GE; |
| 1427 | if (!strcmp(tline->text, "&&")) |
| 1428 | return tokval->t_type = TOKEN_DBL_AND; |
| 1429 | if (!strcmp(tline->text, "^^")) |
| 1430 | return tokval->t_type = TOKEN_DBL_XOR; |
| 1431 | if (!strcmp(tline->text, "||")) |
| 1432 | return tokval->t_type = TOKEN_DBL_OR; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | /* |
| 1436 | * We have no other options: just return the first character of |
| 1437 | * the token text. |
| 1438 | */ |
| 1439 | return tokval->t_type = tline->text[0]; |
| 1440 | } |
| 1441 | |
| 1442 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1443 | * Compare a string to the name of an existing macro; this is a |
| 1444 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 1445 | * depending on the value of the `casesense' parameter. |
| 1446 | */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 1447 | static int mstrcmp(const char *p, const char *q, bool casesense) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1448 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1449 | return casesense ? strcmp(p, q) : nasm_stricmp(p, q); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1450 | } |
| 1451 | |
| 1452 | /* |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1453 | * Compare a string to the name of an existing macro; this is a |
| 1454 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 1455 | * depending on the value of the `casesense' parameter. |
| 1456 | */ |
| 1457 | static int mmemcmp(const char *p, const char *q, size_t l, bool casesense) |
| 1458 | { |
| 1459 | return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l); |
| 1460 | } |
| 1461 | |
| 1462 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1463 | * Return the Context structure associated with a %$ token. Return |
| 1464 | * NULL, having _already_ reported an error condition, if the |
| 1465 | * context stack isn't deep enough for the supplied number of $ |
| 1466 | * signs. |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1467 | * |
| 1468 | * If "namep" is non-NULL, set it to the pointer to the macro name |
| 1469 | * tail, i.e. the part beyond %$... |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1470 | */ |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1471 | static Context *get_ctx(const char *name, const char **namep) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1472 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1473 | Context *ctx; |
| 1474 | int i; |
| 1475 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1476 | if (namep) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1477 | *namep = name; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1478 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1479 | if (!name || name[0] != '%' || name[1] != '$') |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1480 | return NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1481 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1482 | if (!cstk) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1483 | nasm_nonfatal("`%s': context stack is empty", name); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1484 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1487 | name += 2; |
| 1488 | ctx = cstk; |
| 1489 | i = 0; |
| 1490 | while (ctx && *name == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1491 | name++; |
| 1492 | i++; |
| 1493 | ctx = ctx->next; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1494 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1495 | if (!ctx) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1496 | nasm_nonfatal("`%s': context stack is only" |
| 1497 | " %d level%s deep", name, i, (i == 1 ? "" : "s")); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1498 | return NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1499 | } |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1500 | |
| 1501 | if (namep) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1502 | *namep = name; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1503 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1504 | return ctx; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1505 | } |
| 1506 | |
| 1507 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1508 | * Open an include file. This routine must always return a valid |
| 1509 | * file pointer if it returns - it's responsible for throwing an |
| 1510 | * ERR_FATAL and bombing out completely if not. It should also try |
| 1511 | * the include path one by one until it finds the file or reaches |
| 1512 | * the end of the path. |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1513 | * |
| 1514 | * Note: for INC_PROBE the function returns NULL at all times; |
| 1515 | * instead look for the |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1516 | */ |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1517 | enum incopen_mode { |
| 1518 | INC_NEEDED, /* File must exist */ |
| 1519 | INC_OPTIONAL, /* Missing is OK */ |
| 1520 | INC_PROBE /* Only an existence probe */ |
| 1521 | }; |
| 1522 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1523 | /* This is conducts a full pathname search */ |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1524 | static FILE *inc_fopen_search(const char *file, char **slpath, |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1525 | enum incopen_mode omode, enum file_flags fmode) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1526 | { |
H. Peter Anvin (Intel) | 6447109 | 2018-12-11 13:06:14 -0800 | [diff] [blame] | 1527 | const struct strlist_entry *ip = strlist_head(ipath_list); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1528 | FILE *fp; |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1529 | const char *prefix = ""; |
night199uk | fdb1a1b | 2018-10-18 23:19:47 +0200 | [diff] [blame] | 1530 | char *sp; |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1531 | bool found; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1532 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1533 | while (1) { |
night199uk | fdb1a1b | 2018-10-18 23:19:47 +0200 | [diff] [blame] | 1534 | sp = nasm_catfile(prefix, file); |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1535 | if (omode == INC_PROBE) { |
| 1536 | fp = NULL; |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1537 | found = nasm_file_exists(sp); |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1538 | } else { |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1539 | fp = nasm_open_read(sp, fmode); |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1540 | found = (fp != NULL); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1541 | } |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1542 | if (found) { |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1543 | *slpath = sp; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1544 | return fp; |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1545 | } |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 1546 | |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1547 | nasm_free(sp); |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 1548 | |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1549 | if (!ip) { |
| 1550 | *slpath = NULL; |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1551 | return NULL; |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1552 | } |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1553 | |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1554 | prefix = ip->str; |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1555 | ip = ip->next; |
| 1556 | } |
| 1557 | } |
| 1558 | |
| 1559 | /* |
| 1560 | * Open a file, or test for the presence of one (depending on omode), |
| 1561 | * considering the include path. |
| 1562 | */ |
| 1563 | static FILE *inc_fopen(const char *file, |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 1564 | struct strlist *dhead, |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 1565 | const char **found_path, |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1566 | enum incopen_mode omode, |
| 1567 | enum file_flags fmode) |
| 1568 | { |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1569 | struct hash_insert hi; |
| 1570 | void **hp; |
| 1571 | char *path; |
| 1572 | FILE *fp = NULL; |
| 1573 | |
| 1574 | hp = hash_find(&FileHash, file, &hi); |
| 1575 | if (hp) { |
| 1576 | path = *hp; |
Martin Storsjö | f283c8f | 2017-08-13 17:28:46 +0300 | [diff] [blame] | 1577 | if (path || omode != INC_NEEDED) { |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 1578 | strlist_add(dhead, path ? path : file); |
Martin Storsjö | f283c8f | 2017-08-13 17:28:46 +0300 | [diff] [blame] | 1579 | } |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1580 | } else { |
| 1581 | /* Need to do the actual path search */ |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1582 | fp = inc_fopen_search(file, &path, omode, fmode); |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1583 | |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1584 | /* Positive or negative result */ |
| 1585 | hash_add(&hi, nasm_strdup(file), path); |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1586 | |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 1587 | /* |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1588 | * Add file to dependency path. |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 1589 | */ |
| 1590 | if (path || omode != INC_NEEDED) |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 1591 | strlist_add(dhead, file); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1592 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1593 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1594 | if (!path) { |
| 1595 | if (omode == INC_NEEDED) |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 1596 | nasm_fatal("unable to open include file `%s'", file); |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1597 | } else { |
| 1598 | if (!fp && omode != INC_PROBE) |
| 1599 | fp = nasm_open_read(path, fmode); |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1600 | } |
| 1601 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1602 | if (found_path) |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 1603 | *found_path = path; |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1604 | |
| 1605 | return fp; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | /* |
Fabian Giesen | 0bbc38d | 2016-04-28 13:48:14 -0700 | [diff] [blame] | 1609 | * Opens an include or input file. Public version, for use by modules |
| 1610 | * that get a file:lineno pair and need to look at the file again |
| 1611 | * (e.g. the CodeView debug backend). Returns NULL on failure. |
| 1612 | */ |
H. Peter Anvin | 3e83cec | 2016-05-25 04:28:46 -0700 | [diff] [blame] | 1613 | FILE *pp_input_fopen(const char *filename, enum file_flags mode) |
Fabian Giesen | 0bbc38d | 2016-04-28 13:48:14 -0700 | [diff] [blame] | 1614 | { |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 1615 | return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode); |
Fabian Giesen | 0bbc38d | 2016-04-28 13:48:14 -0700 | [diff] [blame] | 1616 | } |
| 1617 | |
| 1618 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1619 | * 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] | 1620 | * 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] | 1621 | * return true if _any_ single-line macro of that name is defined. |
| 1622 | * Otherwise, will return true if a single-line macro with either |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1623 | * `nparam' or no parameters is defined. |
| 1624 | * |
| 1625 | * If a macro with precisely the right number of parameters is |
H. Peter Anvin | ef7468f | 2002-04-30 20:57:59 +0000 | [diff] [blame] | 1626 | * defined, or nparam is -1, the address of the definition structure |
| 1627 | * 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] | 1628 | * is NULL, no action will be taken regarding its contents, and no |
| 1629 | * error will occur. |
| 1630 | * |
| 1631 | * Note that this is also called with nparam zero to resolve |
| 1632 | * `ifdef'. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1633 | * |
| 1634 | * If you already know which context macro belongs to, you can pass |
| 1635 | * the context pointer as first parameter; if you won't but name begins |
| 1636 | * with %$ the context will be automatically computed. If all_contexts |
| 1637 | * is true, macro will be searched in outer contexts as well. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1638 | */ |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1639 | static bool |
H. Peter Anvin | b2a5fda | 2008-06-19 21:42:42 -0700 | [diff] [blame] | 1640 | smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn, |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1641 | bool nocase) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1642 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1643 | struct hash_table *smtbl; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1644 | SMacro *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1645 | |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1646 | if (ctx) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1647 | smtbl = &ctx->localmac; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1648 | } else if (name[0] == '%' && name[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1649 | if (cstk) |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1650 | ctx = get_ctx(name, &name); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1651 | if (!ctx) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1652 | return false; /* got to return _something_ */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1653 | smtbl = &ctx->localmac; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1654 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1655 | smtbl = &smacros; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1656 | } |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1657 | m = (SMacro *) hash_findix(smtbl, name); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1658 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1659 | while (m) { |
| 1660 | if (!mstrcmp(m->name, name, m->casesense && nocase) && |
Charles Crayne | 192d5b5 | 2007-10-18 19:02:42 -0700 | [diff] [blame] | 1661 | (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1662 | if (defn) { |
Charles Crayne | 192d5b5 | 2007-10-18 19:02:42 -0700 | [diff] [blame] | 1663 | if (nparam == (int) m->nparam || nparam == -1) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1664 | *defn = m; |
| 1665 | else |
| 1666 | *defn = NULL; |
| 1667 | } |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1668 | return true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1669 | } |
| 1670 | m = m->next; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1671 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1672 | |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1673 | return false; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 1676 | /* param should be a natural number [0; INT_MAX] */ |
| 1677 | static int read_param_count(const char *str) |
| 1678 | { |
| 1679 | int result; |
| 1680 | bool err; |
| 1681 | |
| 1682 | result = readnum(str, &err); |
| 1683 | if (result < 0 || result > INT_MAX) { |
| 1684 | result = 0; |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1685 | nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]", |
| 1686 | str, 0, INT_MAX); |
| 1687 | } else if (err) |
| 1688 | nasm_nonfatal("unable to parse parameter count `%s'", str); |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 1689 | return result; |
| 1690 | } |
| 1691 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1692 | /* |
| 1693 | * Count and mark off the parameters in a multi-line macro call. |
| 1694 | * This is called both from within the multi-line macro expansion |
| 1695 | * code, and also to mark off the default parameters when provided |
| 1696 | * in a %macro definition line. |
| 1697 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1698 | static void count_mmac_params(Token * t, int *nparam, Token *** params) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1699 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1700 | int paramsize, brace; |
| 1701 | |
| 1702 | *nparam = paramsize = 0; |
| 1703 | *params = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1704 | while (t) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1705 | /* +1: we need space for the final NULL */ |
H. Peter Anvin | 91fb6f1 | 2008-09-01 10:56:33 -0700 | [diff] [blame] | 1706 | if (*nparam+1 >= paramsize) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1707 | paramsize += PARAM_DELTA; |
| 1708 | *params = nasm_realloc(*params, sizeof(**params) * paramsize); |
| 1709 | } |
| 1710 | skip_white_(t); |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1711 | brace = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1712 | if (tok_is_(t, "{")) |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1713 | brace++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1714 | (*params)[(*nparam)++] = t; |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1715 | if (brace) { |
| 1716 | while (brace && (t = t->next) != NULL) { |
| 1717 | if (tok_is_(t, "{")) |
| 1718 | brace++; |
| 1719 | else if (tok_is_(t, "}")) |
| 1720 | brace--; |
| 1721 | } |
| 1722 | |
| 1723 | if (t) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1724 | /* |
| 1725 | * Now we've found the closing brace, look further |
| 1726 | * for the comma. |
| 1727 | */ |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1728 | t = t->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1729 | skip_white_(t); |
| 1730 | if (tok_isnt_(t, ",")) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1731 | nasm_nonfatal("braces do not enclose all of macro parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1732 | while (tok_isnt_(t, ",")) |
| 1733 | t = t->next; |
| 1734 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1735 | } |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1736 | } else { |
| 1737 | while (tok_isnt_(t, ",")) |
| 1738 | t = t->next; |
| 1739 | } |
| 1740 | if (t) { /* got a comma/brace */ |
| 1741 | t = t->next; /* eat the comma */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1742 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1743 | } |
| 1744 | } |
| 1745 | |
| 1746 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1747 | * Determine whether one of the various `if' conditions is true or |
| 1748 | * not. |
| 1749 | * |
| 1750 | * We must free the tline we get passed. |
| 1751 | */ |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1752 | static bool if_condition(Token * tline, enum preproc_token ct) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1753 | { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1754 | enum pp_conditional i = PP_COND(ct); |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1755 | bool j; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 1756 | Token *t, *tt, *origline; |
| 1757 | struct ppscan pps; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1758 | struct tokenval tokval; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1759 | expr *evalresult; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1760 | enum pp_token_type needtype; |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1761 | char *p; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1762 | |
| 1763 | origline = tline; |
| 1764 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1765 | switch (i) { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1766 | case PPC_IFCTX: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1767 | j = false; /* have we matched yet? */ |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1768 | while (true) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1769 | skip_white_(tline); |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1770 | if (!tline) |
| 1771 | break; |
| 1772 | if (tline->type != TOK_ID) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1773 | nasm_nonfatal("`%s' expects context identifiers", |
| 1774 | pp_directives[ct]); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1775 | free_tlist(origline); |
| 1776 | return -1; |
| 1777 | } |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1778 | if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1779 | j = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1780 | tline = tline->next; |
| 1781 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1782 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1783 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1784 | case PPC_IFDEF: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1785 | j = false; /* have we matched yet? */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1786 | while (tline) { |
| 1787 | skip_white_(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1788 | if (!tline || (tline->type != TOK_ID && |
| 1789 | (tline->type != TOK_PREPROC_ID || |
| 1790 | tline->text[1] != '$'))) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1791 | nasm_nonfatal("`%s' expects macro identifiers", |
| 1792 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1793 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1794 | } |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1795 | if (smacro_defined(NULL, tline->text, 0, NULL, true)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1796 | j = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1797 | tline = tline->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1798 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1799 | break; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1800 | |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1801 | case PPC_IFENV: |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1802 | tline = expand_smacro(tline); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1803 | j = false; /* have we matched yet? */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1804 | while (tline) { |
| 1805 | skip_white_(tline); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1806 | if (!tline || (tline->type != TOK_ID && |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1807 | tline->type != TOK_STRING && |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1808 | (tline->type != TOK_PREPROC_ID || |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1809 | tline->text[1] != '!'))) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1810 | nasm_nonfatal("`%s' expects environment variable names", |
| 1811 | pp_directives[ct]); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1812 | goto fail; |
| 1813 | } |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1814 | p = tline->text; |
| 1815 | if (tline->type == TOK_PREPROC_ID) |
| 1816 | p += 2; /* Skip leading %! */ |
H. Peter Anvin | 53e2e4c | 2018-11-28 15:01:40 -0800 | [diff] [blame] | 1817 | if (nasm_isquote(*p)) |
H. Peter Anvin | bb42d30 | 2019-04-22 14:29:29 -0700 | [diff] [blame] | 1818 | nasm_unquote_cstr(p, NULL); |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1819 | if (getenv(p)) |
| 1820 | j = true; |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1821 | tline = tline->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1822 | } |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1823 | break; |
| 1824 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1825 | case PPC_IFIDN: |
| 1826 | case PPC_IFIDNI: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1827 | tline = expand_smacro(tline); |
| 1828 | t = tt = tline; |
| 1829 | while (tok_isnt_(tt, ",")) |
| 1830 | tt = tt->next; |
| 1831 | if (!tt) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1832 | nasm_nonfatal("`%s' expects two comma-separated arguments", |
| 1833 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1834 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1835 | } |
| 1836 | tt = tt->next; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1837 | j = true; /* assume equality unless proved not */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1838 | while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) { |
| 1839 | if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1840 | nasm_nonfatal("`%s': more than one comma on line", |
| 1841 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1842 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1843 | } |
| 1844 | if (t->type == TOK_WHITESPACE) { |
| 1845 | t = t->next; |
| 1846 | continue; |
| 1847 | } |
| 1848 | if (tt->type == TOK_WHITESPACE) { |
| 1849 | tt = tt->next; |
| 1850 | continue; |
| 1851 | } |
| 1852 | if (tt->type != t->type) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1853 | j = false; /* found mismatching tokens */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1854 | break; |
| 1855 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1856 | /* When comparing strings, need to unquote them first */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1857 | if (t->type == TOK_STRING) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1858 | size_t l1 = nasm_unquote(t->text, NULL); |
| 1859 | size_t l2 = nasm_unquote(tt->text, NULL); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 1860 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1861 | if (l1 != l2) { |
| 1862 | j = false; |
| 1863 | break; |
| 1864 | } |
| 1865 | if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) { |
| 1866 | j = false; |
| 1867 | break; |
| 1868 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1869 | } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1870 | j = false; /* found mismatching tokens */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1871 | break; |
| 1872 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1873 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1874 | t = t->next; |
| 1875 | tt = tt->next; |
| 1876 | } |
| 1877 | if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1878 | j = false; /* trailing gunk on one end or other */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1879 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1880 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1881 | case PPC_IFMACRO: |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1882 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1883 | bool found = false; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1884 | MMacro searching, *mmac; |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 1885 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1886 | skip_white_(tline); |
| 1887 | tline = expand_id(tline); |
| 1888 | if (!tok_type_(tline, TOK_ID)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1889 | nasm_nonfatal("`%s' expects a macro name", pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1890 | goto fail; |
| 1891 | } |
| 1892 | searching.name = nasm_strdup(tline->text); |
| 1893 | searching.casesense = true; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1894 | searching.plus = false; |
| 1895 | searching.nolist = false; |
| 1896 | searching.in_progress = 0; |
| 1897 | searching.max_depth = 0; |
| 1898 | searching.rep_nest = NULL; |
| 1899 | searching.nparam_min = 0; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1900 | searching.nparam_max = INT_MAX; |
| 1901 | tline = expand_smacro(tline->next); |
| 1902 | skip_white_(tline); |
| 1903 | if (!tline) { |
| 1904 | } else if (!tok_type_(tline, TOK_NUMBER)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1905 | nasm_nonfatal("`%s' expects a parameter count or nothing", |
| 1906 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1907 | } else { |
| 1908 | searching.nparam_min = searching.nparam_max = |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 1909 | read_param_count(tline->text); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1910 | } |
| 1911 | if (tline && tok_is_(tline->next, "-")) { |
| 1912 | tline = tline->next->next; |
| 1913 | if (tok_is_(tline, "*")) |
| 1914 | searching.nparam_max = INT_MAX; |
| 1915 | else if (!tok_type_(tline, TOK_NUMBER)) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1916 | nasm_nonfatal("`%s' expects a parameter count after `-'", |
| 1917 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1918 | else { |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 1919 | searching.nparam_max = read_param_count(tline->text); |
Cyrill Gorcunov | c9244ea | 2017-10-22 15:25:48 +0300 | [diff] [blame] | 1920 | if (searching.nparam_min > searching.nparam_max) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1921 | nasm_nonfatal("minimum parameter count exceeds maximum"); |
Cyrill Gorcunov | c9244ea | 2017-10-22 15:25:48 +0300 | [diff] [blame] | 1922 | searching.nparam_max = searching.nparam_min; |
| 1923 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1924 | } |
| 1925 | } |
| 1926 | if (tline && tok_is_(tline->next, "+")) { |
| 1927 | tline = tline->next; |
| 1928 | searching.plus = true; |
| 1929 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1930 | mmac = (MMacro *) hash_findix(&mmacros, searching.name); |
| 1931 | while (mmac) { |
| 1932 | if (!strcmp(mmac->name, searching.name) && |
| 1933 | (mmac->nparam_min <= searching.nparam_max |
| 1934 | || searching.plus) |
| 1935 | && (searching.nparam_min <= mmac->nparam_max |
| 1936 | || mmac->plus)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1937 | found = true; |
| 1938 | break; |
| 1939 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1940 | mmac = mmac->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1941 | } |
| 1942 | if (tline && tline->next) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 1943 | nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1944 | nasm_free(searching.name); |
| 1945 | j = found; |
| 1946 | break; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1947 | } |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 1948 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1949 | case PPC_IFID: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1950 | needtype = TOK_ID; |
| 1951 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1952 | case PPC_IFNUM: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1953 | needtype = TOK_NUMBER; |
| 1954 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1955 | case PPC_IFSTR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1956 | needtype = TOK_STRING; |
| 1957 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1958 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1959 | iftype: |
| 1960 | t = tline = expand_smacro(tline); |
H. Peter Anvin | d85d250 | 2008-05-04 17:53:31 -0700 | [diff] [blame] | 1961 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1962 | while (tok_type_(t, TOK_WHITESPACE) || |
| 1963 | (needtype == TOK_NUMBER && |
| 1964 | tok_type_(t, TOK_OTHER) && |
| 1965 | (t->text[0] == '-' || t->text[0] == '+') && |
| 1966 | !t->text[1])) |
| 1967 | t = t->next; |
H. Peter Anvin | d85d250 | 2008-05-04 17:53:31 -0700 | [diff] [blame] | 1968 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1969 | j = tok_type_(t, needtype); |
| 1970 | break; |
H. Peter Anvin | cbf768d | 2008-02-16 16:41:25 -0800 | [diff] [blame] | 1971 | |
| 1972 | case PPC_IFTOKEN: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1973 | t = tline = expand_smacro(tline); |
| 1974 | while (tok_type_(t, TOK_WHITESPACE)) |
| 1975 | t = t->next; |
H. Peter Anvin | cbf768d | 2008-02-16 16:41:25 -0800 | [diff] [blame] | 1976 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1977 | j = false; |
| 1978 | if (t) { |
| 1979 | t = t->next; /* Skip the actual token */ |
| 1980 | while (tok_type_(t, TOK_WHITESPACE)) |
| 1981 | t = t->next; |
| 1982 | j = !t; /* Should be nothing left */ |
| 1983 | } |
| 1984 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1985 | |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 1986 | case PPC_IFEMPTY: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1987 | t = tline = expand_smacro(tline); |
| 1988 | while (tok_type_(t, TOK_WHITESPACE)) |
| 1989 | t = t->next; |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 1990 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1991 | j = !t; /* Should be empty */ |
| 1992 | break; |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 1993 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1994 | case PPC_IF: |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 1995 | pps.tptr = tline = expand_smacro(tline); |
| 1996 | pps.ntokens = -1; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1997 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 1998 | evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1999 | if (!evalresult) |
| 2000 | return -1; |
| 2001 | if (tokval.t_type) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2002 | nasm_warn(WARN_OTHER, "trailing garbage after expression ignored"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2003 | if (!is_simple(evalresult)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2004 | nasm_nonfatal("non-constant value given to `%s'", |
| 2005 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2006 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2007 | } |
Chuck Crayne | 60ae75d | 2007-05-02 01:59:16 +0000 | [diff] [blame] | 2008 | j = reloc_value(evalresult) != 0; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2009 | break; |
H. Peter Anvin | 95e2882 | 2007-09-12 04:20:08 +0000 | [diff] [blame] | 2010 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2011 | default: |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2012 | nasm_fatal("preprocessor directive `%s' not yet implemented", |
| 2013 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2014 | goto fail; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2015 | } |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2016 | |
| 2017 | free_tlist(origline); |
| 2018 | return j ^ PP_NEGATIVE(ct); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2019 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2020 | fail: |
| 2021 | free_tlist(origline); |
| 2022 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2023 | } |
| 2024 | |
| 2025 | /* |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2026 | * Common code for defining an smacro |
| 2027 | */ |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2028 | static SMacro *define_smacro(Context *ctx, const char *mname, |
| 2029 | bool casesense, int nparam, Token *expansion) |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2030 | { |
| 2031 | SMacro *smac, **smhead; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2032 | struct hash_table *smtbl; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2033 | |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2034 | if (smacro_defined(ctx, mname, nparam, &smac, casesense)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2035 | if (!smac) { |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2036 | nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and" |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2037 | " without parameters", mname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2038 | /* |
| 2039 | * Some instances of the old code considered this a failure, |
| 2040 | * some others didn't. What is the right thing to do here? |
| 2041 | */ |
| 2042 | free_tlist(expansion); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2043 | return NULL; /* Failure */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2044 | } else { |
| 2045 | /* |
| 2046 | * We're redefining, so we have to take over an |
| 2047 | * existing SMacro structure. This means freeing |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2048 | * what was already in it, but not the structure itself. |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2049 | */ |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 2050 | clear_smacro(smac); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2051 | } |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2052 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2053 | smtbl = ctx ? &ctx->localmac : &smacros; |
| 2054 | smhead = (SMacro **) hash_findi_add(smtbl, mname); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2055 | nasm_new(smac); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2056 | smac->next = *smhead; |
| 2057 | *smhead = smac; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2058 | } |
| 2059 | smac->name = nasm_strdup(mname); |
| 2060 | smac->casesense = casesense; |
| 2061 | smac->nparam = nparam; |
| 2062 | smac->expansion = expansion; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2063 | return smac; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2064 | } |
| 2065 | |
| 2066 | /* |
| 2067 | * Undefine an smacro |
| 2068 | */ |
| 2069 | static void undef_smacro(Context *ctx, const char *mname) |
| 2070 | { |
| 2071 | SMacro **smhead, *s, **sp; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2072 | struct hash_table *smtbl; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2073 | |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2074 | smtbl = ctx ? &ctx->localmac : &smacros; |
| 2075 | smhead = (SMacro **)hash_findi(smtbl, mname, NULL); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2076 | |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2077 | if (smhead) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2078 | /* |
| 2079 | * We now have a macro name... go hunt for it. |
| 2080 | */ |
| 2081 | sp = smhead; |
| 2082 | while ((s = *sp) != NULL) { |
| 2083 | if (!mstrcmp(s->name, mname, s->casesense)) { |
| 2084 | *sp = s->next; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 2085 | free_smacro(s); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2086 | } else { |
| 2087 | sp = &s->next; |
| 2088 | } |
| 2089 | } |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2090 | } |
| 2091 | } |
| 2092 | |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2093 | /* |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2094 | * Parse a mmacro specification. |
| 2095 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2096 | 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] | 2097 | { |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2098 | tline = tline->next; |
| 2099 | skip_white_(tline); |
| 2100 | tline = expand_id(tline); |
| 2101 | if (!tok_type_(tline, TOK_ID)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2102 | nasm_nonfatal("`%s' expects a macro name", directive); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2103 | return false; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2104 | } |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2105 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2106 | def->prev = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2107 | def->name = nasm_strdup(tline->text); |
| 2108 | def->plus = false; |
| 2109 | def->nolist = false; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2110 | def->in_progress = 0; |
| 2111 | def->rep_nest = NULL; |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2112 | def->nparam_min = 0; |
| 2113 | def->nparam_max = 0; |
| 2114 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2115 | tline = expand_smacro(tline->next); |
| 2116 | skip_white_(tline); |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2117 | if (!tok_type_(tline, TOK_NUMBER)) |
| 2118 | nasm_nonfatal("`%s' expects a parameter count", directive); |
| 2119 | else |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 2120 | def->nparam_min = def->nparam_max = read_param_count(tline->text); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2121 | if (tline && tok_is_(tline->next, "-")) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2122 | tline = tline->next->next; |
| 2123 | if (tok_is_(tline, "*")) { |
| 2124 | def->nparam_max = INT_MAX; |
| 2125 | } else if (!tok_type_(tline, TOK_NUMBER)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2126 | nasm_nonfatal("`%s' expects a parameter count after `-'", directive); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2127 | } else { |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 2128 | def->nparam_max = read_param_count(tline->text); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2129 | if (def->nparam_min > def->nparam_max) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2130 | nasm_nonfatal("minimum parameter count exceeds maximum"); |
Cyrill Gorcunov | c9244ea | 2017-10-22 15:25:48 +0300 | [diff] [blame] | 2131 | def->nparam_max = def->nparam_min; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2132 | } |
| 2133 | } |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2134 | } |
| 2135 | if (tline && tok_is_(tline->next, "+")) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2136 | tline = tline->next; |
| 2137 | def->plus = true; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2138 | } |
| 2139 | if (tline && tok_type_(tline->next, TOK_ID) && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2140 | !nasm_stricmp(tline->next->text, ".nolist")) { |
| 2141 | tline = tline->next; |
| 2142 | def->nolist = true; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2143 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2144 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2145 | /* |
| 2146 | * Handle default parameters. |
| 2147 | */ |
| 2148 | if (tline && tline->next) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2149 | def->dlist = tline->next; |
| 2150 | tline->next = NULL; |
| 2151 | count_mmac_params(def->dlist, &def->ndefs, &def->defaults); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2152 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2153 | def->dlist = NULL; |
| 2154 | def->defaults = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2155 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2156 | def->expansion = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2157 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2158 | 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] | 2159 | !def->plus) { |
| 2160 | /* |
| 2161 | *!macro-defaults [on] macros with more default than optional parameters |
| 2162 | *! warns when a macro has more default parameters than optional parameters. |
| 2163 | *! See \k{mlmacdef} for why might want to disable this warning. |
| 2164 | */ |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2165 | nasm_warn(WARN_MACRO_DEFAULTS, |
H. Peter Anvin (Intel) | 723ab48 | 2018-12-13 21:53:31 -0800 | [diff] [blame] | 2166 | "too many default macro parameters in macro `%s'", def->name); |
| 2167 | } |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2168 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2169 | return true; |
| 2170 | } |
| 2171 | |
| 2172 | |
| 2173 | /* |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2174 | * Decode a size directive |
| 2175 | */ |
| 2176 | static int parse_size(const char *str) { |
| 2177 | static const char *size_names[] = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2178 | { "byte", "dword", "oword", "qword", "tword", "word", "yword" }; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2179 | static const int sizes[] = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2180 | { 0, 1, 4, 16, 8, 10, 2, 32 }; |
Cyrill Gorcunov | c713b5f | 2018-09-29 14:30:14 +0300 | [diff] [blame] | 2181 | 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] | 2182 | } |
| 2183 | |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2184 | /* |
| 2185 | * Process a preprocessor %pragma directive. Currently there are none. |
| 2186 | * Gets passed the token list starting with the "preproc" token from |
| 2187 | * "%pragma preproc". |
| 2188 | */ |
| 2189 | static void do_pragma_preproc(Token *tline) |
| 2190 | { |
| 2191 | /* Skip to the real stuff */ |
| 2192 | tline = tline->next; |
| 2193 | skip_white_(tline); |
| 2194 | if (!tline) |
| 2195 | return; |
| 2196 | |
| 2197 | (void)tline; /* Nothing else to do at present */ |
| 2198 | } |
| 2199 | |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2200 | /** |
| 2201 | * find and process preprocessor directive in passed line |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2202 | * Find out if a line contains a preprocessor directive, and deal |
| 2203 | * with it if so. |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2204 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2205 | * If a directive _is_ found, it is the responsibility of this routine |
| 2206 | * (and not the caller) to free_tlist() the line. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2207 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2208 | * @param tline a pointer to the current tokeninzed line linked list |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2209 | * @param output if this directive generated output |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2210 | * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2211 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2212 | */ |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 2213 | static int do_directive(Token *tline, Token **output) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2214 | { |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2215 | enum preproc_token i; |
| 2216 | int j; |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 2217 | bool err; |
| 2218 | int nparam; |
| 2219 | bool nolist; |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 2220 | bool casesense; |
H. Peter Anvin | 8cfdb9d | 2007-09-14 18:36:01 -0700 | [diff] [blame] | 2221 | int k, m; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2222 | int offset; |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 2223 | char *p, *pp; |
| 2224 | const char *found_path; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 2225 | const char *mname; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2226 | struct ppscan pps; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2227 | Include *inc; |
| 2228 | Context *ctx; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2229 | Cond *cond; |
| 2230 | MMacro *mmac, **mmhead; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2231 | Token *t = NULL, *tt, *param_start, *macro_start, *last, *origline; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2232 | Line *l; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2233 | struct tokenval tokval; |
| 2234 | expr *evalresult; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2235 | MMacro *tmp_defining; /* Used when manipulating rep_nest */ |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 2236 | int64_t count; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 2237 | size_t len; |
H. Peter Anvin (Intel) | 6bde2ed | 2018-12-13 19:39:41 -0800 | [diff] [blame] | 2238 | errflags severity; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2239 | const char *dname; /* Name of directive, for messages */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2240 | |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2241 | *output = NULL; /* No output generated */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2242 | origline = tline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2243 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2244 | skip_white_(tline); |
H. Peter Anvin | f2936d7 | 2008-06-04 15:11:23 -0700 | [diff] [blame] | 2245 | if (!tline || !tok_type_(tline, TOK_PREPROC_ID) || |
Cyrill Gorcunov | 4b5b737 | 2018-10-29 22:54:08 +0300 | [diff] [blame] | 2246 | (tline->text[0] && (tline->text[1] == '%' || |
| 2247 | tline->text[1] == '$' || |
| 2248 | tline->text[1] == '!'))) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2249 | return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2250 | |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2251 | i = pp_token_hash(tline->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2252 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2253 | /* |
| 2254 | * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO |
| 2255 | * since they are known to be buggy at moment, we need to fix them |
| 2256 | * in future release (2.09-2.10) |
| 2257 | */ |
Philipp Kloke | b432f57 | 2013-03-31 12:01:23 +0200 | [diff] [blame] | 2258 | if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2259 | nasm_nonfatal("unknown preprocessor directive `%s'", tline->text); |
| 2260 | return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2261 | } |
| 2262 | |
| 2263 | /* |
| 2264 | * If we're in a non-emitting branch of a condition construct, |
| 2265 | * or walking to the end of an already terminated %rep block, |
| 2266 | * we should ignore all directives except for condition |
| 2267 | * directives. |
| 2268 | */ |
| 2269 | if (((istk->conds && !emitting(istk->conds->state)) || |
| 2270 | (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) { |
| 2271 | return NO_DIRECTIVE_FOUND; |
| 2272 | } |
| 2273 | |
| 2274 | /* |
| 2275 | * If we're defining a macro or reading a %rep block, we should |
| 2276 | * ignore all directives except for %macro/%imacro (which nest), |
| 2277 | * %endm/%endmacro, and (only if we're in a %rep block) %endrep. |
| 2278 | * If we're in a %rep block, another %rep nests, so should be let through. |
| 2279 | */ |
| 2280 | if (defining && i != PP_MACRO && i != PP_IMACRO && |
| 2281 | i != PP_RMACRO && i != PP_IRMACRO && |
| 2282 | i != PP_ENDMACRO && i != PP_ENDM && |
| 2283 | (defining->name || (i != PP_ENDREP && i != PP_REP))) { |
| 2284 | return NO_DIRECTIVE_FOUND; |
| 2285 | } |
| 2286 | |
| 2287 | if (defining) { |
| 2288 | if (i == PP_MACRO || i == PP_IMACRO || |
| 2289 | i == PP_RMACRO || i == PP_IRMACRO) { |
| 2290 | nested_mac_count++; |
| 2291 | return NO_DIRECTIVE_FOUND; |
| 2292 | } else if (nested_mac_count > 0) { |
| 2293 | if (i == PP_ENDMACRO) { |
| 2294 | nested_mac_count--; |
| 2295 | return NO_DIRECTIVE_FOUND; |
| 2296 | } |
| 2297 | } |
| 2298 | if (!defining->name) { |
| 2299 | if (i == PP_REP) { |
| 2300 | nested_rep_count++; |
| 2301 | return NO_DIRECTIVE_FOUND; |
| 2302 | } else if (nested_rep_count > 0) { |
| 2303 | if (i == PP_ENDREP) { |
| 2304 | nested_rep_count--; |
| 2305 | return NO_DIRECTIVE_FOUND; |
| 2306 | } |
| 2307 | } |
| 2308 | } |
| 2309 | } |
| 2310 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2311 | dname = pp_directives[i]; /* Directive name, for error messages */ |
| 2312 | casesense = true; /* Default to case sensitive */ |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2313 | switch (i) { |
| 2314 | case PP_INVALID: |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2315 | nasm_nonfatal("unknown preprocessor directive `%s'", tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2316 | return NO_DIRECTIVE_FOUND; /* didn't get it */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2317 | |
H. Peter Anvin | 3f87a2a | 2016-10-04 14:07:19 -0700 | [diff] [blame] | 2318 | case PP_PRAGMA: |
| 2319 | /* |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2320 | * %pragma namespace options... |
| 2321 | * |
| 2322 | * The namespace "preproc" is reserved for the preprocessor; |
| 2323 | * all other namespaces generate a [pragma] assembly directive. |
| 2324 | * |
| 2325 | * Invalid %pragmas are ignored and may have different |
| 2326 | * meaning in future versions of NASM. |
H. Peter Anvin | 3f87a2a | 2016-10-04 14:07:19 -0700 | [diff] [blame] | 2327 | */ |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2328 | tline = tline->next; |
| 2329 | skip_white_(tline); |
| 2330 | tline = expand_smacro(tline); |
| 2331 | if (tok_type_(tline, TOK_ID)) { |
| 2332 | if (!nasm_stricmp(tline->text, "preproc")) { |
| 2333 | /* Preprocessor pragma */ |
| 2334 | do_pragma_preproc(tline); |
| 2335 | } else { |
| 2336 | /* Build the assembler directive */ |
| 2337 | t = new_Token(NULL, TOK_OTHER, "[", 1); |
| 2338 | t->next = new_Token(NULL, TOK_ID, "pragma", 6); |
| 2339 | t->next->next = new_Token(tline, TOK_WHITESPACE, NULL, 0); |
| 2340 | tline = t; |
| 2341 | for (t = tline; t->next; t = t->next) |
| 2342 | ; |
| 2343 | t->next = new_Token(NULL, TOK_OTHER, "]", 1); |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 2344 | *output = tline; |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2345 | } |
| 2346 | } |
H. Peter Anvin | 3f87a2a | 2016-10-04 14:07:19 -0700 | [diff] [blame] | 2347 | free_tlist(origline); |
| 2348 | return DIRECTIVE_FOUND; |
| 2349 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2350 | case PP_STACKSIZE: |
| 2351 | /* Directive to tell NASM what the default stack size is. The |
| 2352 | * default is for a 16-bit stack, and this can be overriden with |
| 2353 | * %stacksize large. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2354 | */ |
| 2355 | tline = tline->next; |
| 2356 | if (tline && tline->type == TOK_WHITESPACE) |
| 2357 | tline = tline->next; |
| 2358 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2359 | nasm_nonfatal("`%s' missing size parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2360 | free_tlist(origline); |
| 2361 | return DIRECTIVE_FOUND; |
| 2362 | } |
| 2363 | if (nasm_stricmp(tline->text, "flat") == 0) { |
| 2364 | /* All subsequent ARG directives are for a 32-bit stack */ |
| 2365 | StackSize = 4; |
| 2366 | StackPointer = "ebp"; |
| 2367 | ArgOffset = 8; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2368 | LocalOffset = 0; |
Charles Crayne | 7eaf919 | 2007-11-08 22:11:14 -0800 | [diff] [blame] | 2369 | } else if (nasm_stricmp(tline->text, "flat64") == 0) { |
| 2370 | /* All subsequent ARG directives are for a 64-bit stack */ |
| 2371 | StackSize = 8; |
| 2372 | StackPointer = "rbp"; |
Per Jessen | 53252e0 | 2010-02-11 00:16:59 +0300 | [diff] [blame] | 2373 | ArgOffset = 16; |
Charles Crayne | 7eaf919 | 2007-11-08 22:11:14 -0800 | [diff] [blame] | 2374 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2375 | } else if (nasm_stricmp(tline->text, "large") == 0) { |
| 2376 | /* All subsequent ARG directives are for a 16-bit stack, |
| 2377 | * far function call. |
| 2378 | */ |
| 2379 | StackSize = 2; |
| 2380 | StackPointer = "bp"; |
| 2381 | ArgOffset = 4; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2382 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2383 | } else if (nasm_stricmp(tline->text, "small") == 0) { |
| 2384 | /* All subsequent ARG directives are for a 16-bit stack, |
| 2385 | * far function call. We don't support near functions. |
| 2386 | */ |
| 2387 | StackSize = 2; |
| 2388 | StackPointer = "bp"; |
| 2389 | ArgOffset = 6; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2390 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2391 | } else { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2392 | nasm_nonfatal("`%s' invalid size type", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2393 | free_tlist(origline); |
| 2394 | return DIRECTIVE_FOUND; |
| 2395 | } |
| 2396 | free_tlist(origline); |
| 2397 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2398 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2399 | case PP_ARG: |
| 2400 | /* TASM like ARG directive to define arguments to functions, in |
| 2401 | * the following form: |
| 2402 | * |
| 2403 | * ARG arg1:WORD, arg2:DWORD, arg4:QWORD |
| 2404 | */ |
| 2405 | offset = ArgOffset; |
| 2406 | do { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 2407 | char *arg, directive[256]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2408 | int size = StackSize; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2409 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2410 | /* Find the argument name */ |
| 2411 | tline = tline->next; |
| 2412 | if (tline && tline->type == TOK_WHITESPACE) |
| 2413 | tline = tline->next; |
| 2414 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2415 | nasm_nonfatal("`%s' missing argument parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2416 | free_tlist(origline); |
| 2417 | return DIRECTIVE_FOUND; |
| 2418 | } |
| 2419 | arg = tline->text; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2420 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2421 | /* Find the argument size type */ |
| 2422 | tline = tline->next; |
| 2423 | if (!tline || tline->type != TOK_OTHER |
| 2424 | || tline->text[0] != ':') { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2425 | nasm_nonfatal("syntax error processing `%s' directive", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2426 | free_tlist(origline); |
| 2427 | return DIRECTIVE_FOUND; |
| 2428 | } |
| 2429 | tline = tline->next; |
| 2430 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2431 | nasm_nonfatal("`%s' missing size type parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2432 | free_tlist(origline); |
| 2433 | return DIRECTIVE_FOUND; |
| 2434 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2435 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2436 | /* Allow macro expansion of type parameter */ |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2437 | tt = tokenize(tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2438 | tt = expand_smacro(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2439 | size = parse_size(tt->text); |
| 2440 | if (!size) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2441 | nasm_nonfatal("invalid size type for `%s' missing directive", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2442 | free_tlist(tt); |
| 2443 | free_tlist(origline); |
| 2444 | return DIRECTIVE_FOUND; |
| 2445 | } |
| 2446 | free_tlist(tt); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2447 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2448 | /* Round up to even stack slots */ |
| 2449 | size = ALIGN(size, StackSize); |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2450 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2451 | /* Now define the macro for the argument */ |
| 2452 | snprintf(directive, sizeof(directive), "%%define %s (%s+%d)", |
| 2453 | arg, StackPointer, offset); |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2454 | do_directive(tokenize(directive), output); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2455 | offset += size; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2456 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2457 | /* Move to the next argument in the list */ |
| 2458 | tline = tline->next; |
| 2459 | if (tline && tline->type == TOK_WHITESPACE) |
| 2460 | tline = tline->next; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2461 | } while (tline && tline->type == TOK_OTHER && tline->text[0] == ','); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2462 | ArgOffset = offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2463 | free_tlist(origline); |
| 2464 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2465 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2466 | case PP_LOCAL: |
| 2467 | /* TASM like LOCAL directive to define local variables for a |
| 2468 | * function, in the following form: |
| 2469 | * |
| 2470 | * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize |
| 2471 | * |
| 2472 | * The '= LocalSize' at the end is ignored by NASM, but is |
| 2473 | * required by TASM to define the local parameter size (and used |
| 2474 | * by the TASM macro package). |
| 2475 | */ |
| 2476 | offset = LocalOffset; |
| 2477 | do { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 2478 | char *local, directive[256]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2479 | int size = StackSize; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2480 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2481 | /* Find the argument name */ |
| 2482 | tline = tline->next; |
| 2483 | if (tline && tline->type == TOK_WHITESPACE) |
| 2484 | tline = tline->next; |
| 2485 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2486 | nasm_nonfatal("`%s' missing argument parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2487 | free_tlist(origline); |
| 2488 | return DIRECTIVE_FOUND; |
| 2489 | } |
| 2490 | local = tline->text; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2491 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2492 | /* Find the argument size type */ |
| 2493 | tline = tline->next; |
| 2494 | if (!tline || tline->type != TOK_OTHER |
| 2495 | || tline->text[0] != ':') { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2496 | nasm_nonfatal("syntax error processing `%s' directive", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2497 | free_tlist(origline); |
| 2498 | return DIRECTIVE_FOUND; |
| 2499 | } |
| 2500 | tline = tline->next; |
| 2501 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2502 | nasm_nonfatal("`%s' missing size type parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2503 | free_tlist(origline); |
| 2504 | return DIRECTIVE_FOUND; |
| 2505 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2506 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2507 | /* Allow macro expansion of type parameter */ |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2508 | tt = tokenize(tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2509 | tt = expand_smacro(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2510 | size = parse_size(tt->text); |
| 2511 | if (!size) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2512 | nasm_nonfatal("invalid size type for `%s' missing directive", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2513 | free_tlist(tt); |
| 2514 | free_tlist(origline); |
| 2515 | return DIRECTIVE_FOUND; |
| 2516 | } |
| 2517 | free_tlist(tt); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2518 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2519 | /* Round up to even stack slots */ |
| 2520 | size = ALIGN(size, StackSize); |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2521 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2522 | offset += size; /* Negative offset, increment before */ |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2523 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2524 | /* Now define the macro for the argument */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2525 | snprintf(directive, sizeof(directive), "%%define %s (%s-%d)", |
| 2526 | local, StackPointer, offset); |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2527 | do_directive(tokenize(directive), output); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2528 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2529 | /* Now define the assign to setup the enter_c macro correctly */ |
| 2530 | snprintf(directive, sizeof(directive), |
| 2531 | "%%assign %%$localsize %%$localsize+%d", size); |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2532 | do_directive(tokenize(directive), output); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2533 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2534 | /* Move to the next argument in the list */ |
| 2535 | tline = tline->next; |
| 2536 | if (tline && tline->type == TOK_WHITESPACE) |
| 2537 | tline = tline->next; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2538 | } while (tline && tline->type == TOK_OTHER && tline->text[0] == ','); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2539 | LocalOffset = offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2540 | free_tlist(origline); |
| 2541 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2542 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2543 | case PP_CLEAR: |
| 2544 | if (tline->next) |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2545 | nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2546 | free_macros(); |
| 2547 | init_macros(); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2548 | free_tlist(origline); |
| 2549 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2550 | |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2551 | case PP_DEPEND: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2552 | t = tline->next = expand_smacro(tline->next); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2553 | skip_white_(t); |
| 2554 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2555 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2556 | nasm_nonfatal("`%s' expects a file name", dname); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2557 | free_tlist(origline); |
| 2558 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2559 | } |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2560 | if (t->next) |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2561 | nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2562 | p = t->text; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2563 | if (t->type != TOK_INTERNAL_STRING) |
H. Peter Anvin | bb42d30 | 2019-04-22 14:29:29 -0700 | [diff] [blame] | 2564 | nasm_unquote_cstr(p, NULL); |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 2565 | strlist_add(deplist, p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2566 | free_tlist(origline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2567 | return DIRECTIVE_FOUND; |
| 2568 | |
| 2569 | case PP_INCLUDE: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2570 | t = tline->next = expand_smacro(tline->next); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2571 | skip_white_(t); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2572 | |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2573 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2574 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2575 | nasm_nonfatal("`%s' expects a file name", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2576 | free_tlist(origline); |
| 2577 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2578 | } |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2579 | if (t->next) |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2580 | nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2581 | p = t->text; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2582 | if (t->type != TOK_INTERNAL_STRING) |
H. Peter Anvin | bb42d30 | 2019-04-22 14:29:29 -0700 | [diff] [blame] | 2583 | nasm_unquote_cstr(p, NULL); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2584 | inc = nasm_malloc(sizeof(Include)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2585 | inc->next = istk; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2586 | inc->conds = NULL; |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 2587 | found_path = NULL; |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 2588 | inc->fp = inc_fopen(p, deplist, &found_path, |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 2589 | (pp_mode == PP_DEPS) |
| 2590 | ? INC_OPTIONAL : INC_NEEDED, NF_TEXT); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2591 | if (!inc->fp) { |
| 2592 | /* -MG given but file not found */ |
| 2593 | nasm_free(inc); |
| 2594 | } else { |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 2595 | inc->fname = src_set_fname(found_path ? found_path : p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2596 | inc->lineno = src_set_linnum(0); |
| 2597 | inc->lineinc = 1; |
| 2598 | inc->expansion = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2599 | inc->mstk = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2600 | istk = inc; |
H. Peter Anvin | 0d4d431 | 2019-08-07 00:46:27 -0700 | [diff] [blame] | 2601 | lfmt->uplevel(LIST_INCLUDE, 0); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2602 | } |
| 2603 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2604 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2605 | |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2606 | case PP_USE: |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2607 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2608 | static macros_t *use_pkg; |
| 2609 | const char *pkg_macro = NULL; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2610 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2611 | tline = tline->next; |
| 2612 | skip_white_(tline); |
| 2613 | tline = expand_id(tline); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2614 | |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2615 | if (!tline || (tline->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2616 | tline->type != TOK_INTERNAL_STRING && |
| 2617 | tline->type != TOK_ID)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2618 | nasm_nonfatal("`%s' expects a package name", dname); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2619 | free_tlist(origline); |
| 2620 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2621 | } |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2622 | if (tline->next) |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2623 | nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2624 | if (tline->type == TOK_STRING) |
H. Peter Anvin | bb42d30 | 2019-04-22 14:29:29 -0700 | [diff] [blame] | 2625 | nasm_unquote_cstr(tline->text, NULL); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2626 | use_pkg = nasm_stdmac_find_package(tline->text); |
| 2627 | if (!use_pkg) |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2628 | nasm_nonfatal("unknown `%s' package: %s", dname, tline->text); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2629 | else |
| 2630 | 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] | 2631 | if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2632 | /* Not already included, go ahead and include it */ |
| 2633 | stdmacpos = use_pkg; |
| 2634 | } |
| 2635 | free_tlist(origline); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2636 | return DIRECTIVE_FOUND; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2637 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2638 | case PP_PUSH: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2639 | case PP_REPL: |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2640 | case PP_POP: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2641 | tline = tline->next; |
| 2642 | skip_white_(tline); |
| 2643 | tline = expand_id(tline); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2644 | if (tline) { |
| 2645 | if (!tok_type_(tline, TOK_ID)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2646 | nasm_nonfatal("`%s' expects a context identifier", |
| 2647 | pp_directives[i]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2648 | free_tlist(origline); |
| 2649 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2650 | } |
| 2651 | if (tline->next) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2652 | nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2653 | pp_directives[i]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2654 | p = nasm_strdup(tline->text); |
| 2655 | } else { |
| 2656 | p = NULL; /* Anonymous */ |
| 2657 | } |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2658 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2659 | if (i == PP_PUSH) { |
H. Peter Anvin (Intel) | ebb05a0 | 2018-12-11 12:30:25 -0800 | [diff] [blame] | 2660 | nasm_new(ctx); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2661 | ctx->next = cstk; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2662 | ctx->name = p; |
| 2663 | ctx->number = unique++; |
| 2664 | cstk = ctx; |
| 2665 | } else { |
| 2666 | /* %pop or %repl */ |
| 2667 | if (!cstk) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2668 | nasm_nonfatal("`%s': context stack is empty", |
| 2669 | pp_directives[i]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2670 | } else if (i == PP_POP) { |
| 2671 | if (p && (!cstk->name || nasm_stricmp(p, cstk->name))) |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2672 | nasm_nonfatal("`%s' in wrong context: %s, " |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2673 | "expected %s", |
| 2674 | dname, cstk->name ? cstk->name : "anonymous", p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2675 | else |
| 2676 | ctx_pop(); |
| 2677 | } else { |
| 2678 | /* i == PP_REPL */ |
| 2679 | nasm_free(cstk->name); |
| 2680 | cstk->name = p; |
| 2681 | p = NULL; |
| 2682 | } |
| 2683 | nasm_free(p); |
| 2684 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2685 | free_tlist(origline); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2686 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2687 | case PP_FATAL: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2688 | severity = ERR_FATAL; |
| 2689 | goto issue_error; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2690 | case PP_ERROR: |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2691 | severity = ERR_NONFATAL|ERR_PASS2; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2692 | goto issue_error; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2693 | case PP_WARNING: |
H. Peter Anvin (Intel) | 723ab48 | 2018-12-13 21:53:31 -0800 | [diff] [blame] | 2694 | /*! |
| 2695 | *!user [on] %warning directives |
| 2696 | *! controls output of \c{%warning} directives (see \k{pperror}). |
| 2697 | */ |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2698 | severity = ERR_WARNING|WARN_USER|ERR_PASS2; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2699 | goto issue_error; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2700 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2701 | issue_error: |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2702 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2703 | /* Only error out if this is the final pass */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2704 | tline->next = expand_smacro(tline->next); |
| 2705 | tline = tline->next; |
| 2706 | skip_white_(tline); |
| 2707 | t = tline ? tline->next : NULL; |
| 2708 | skip_white_(t); |
| 2709 | if (tok_type_(tline, TOK_STRING) && !t) { |
| 2710 | /* The line contains only a quoted string */ |
| 2711 | p = tline->text; |
| 2712 | nasm_unquote(p, NULL); /* Ignore NUL character truncation */ |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2713 | nasm_error(severity, "%s", p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2714 | } else { |
| 2715 | /* Not a quoted string, or more than a quoted string */ |
| 2716 | p = detoken(tline, false); |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2717 | nasm_error(severity, "%s", p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2718 | nasm_free(p); |
| 2719 | } |
| 2720 | free_tlist(origline); |
| 2721 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2722 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2723 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2724 | CASE_PP_IF: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2725 | if (istk->conds && !emitting(istk->conds->state)) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2726 | j = COND_NEVER; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2727 | else { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2728 | j = if_condition(tline->next, i); |
| 2729 | tline->next = NULL; /* it got freed */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2730 | j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2731 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2732 | cond = nasm_malloc(sizeof(Cond)); |
| 2733 | cond->next = istk->conds; |
| 2734 | cond->state = j; |
| 2735 | istk->conds = cond; |
| 2736 | if(istk->mstk) |
| 2737 | istk->mstk->condcnt ++; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2738 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2739 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2740 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2741 | CASE_PP_ELIF: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2742 | if (!istk->conds) |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2743 | nasm_fatal("`%s': no matching `%%if'", dname); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2744 | switch(istk->conds->state) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2745 | case COND_IF_TRUE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2746 | istk->conds->state = COND_DONE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2747 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2748 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2749 | case COND_DONE: |
| 2750 | case COND_NEVER: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2751 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2752 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2753 | case COND_ELSE_TRUE: |
| 2754 | case COND_ELSE_FALSE: |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2755 | nasm_warn(WARN_OTHER|ERR_PP_PRECOND, |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2756 | "`%%elif' after `%%else' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2757 | istk->conds->state = COND_NEVER; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2758 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2759 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2760 | case COND_IF_FALSE: |
| 2761 | /* |
| 2762 | * IMPORTANT: In the case of %if, we will already have |
| 2763 | * called expand_mmac_params(); however, if we're |
| 2764 | * processing an %elif we must have been in a |
| 2765 | * non-emitting mode, which would have inhibited |
| 2766 | * the normal invocation of expand_mmac_params(). |
| 2767 | * Therefore, we have to do it explicitly here. |
| 2768 | */ |
| 2769 | j = if_condition(expand_mmac_params(tline->next), i); |
| 2770 | tline->next = NULL; /* it got freed */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2771 | istk->conds->state = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2772 | j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; |
| 2773 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2774 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2775 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2776 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2777 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2778 | case PP_ELSE: |
| 2779 | if (tline->next) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2780 | nasm_warn(WARN_OTHER|ERR_PP_PRECOND, |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2781 | "trailing garbage after `%%else' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2782 | if (!istk->conds) |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 2783 | nasm_fatal("`%%else: no matching `%%if'"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2784 | switch(istk->conds->state) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2785 | case COND_IF_TRUE: |
| 2786 | case COND_DONE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2787 | istk->conds->state = COND_ELSE_FALSE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2788 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2789 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2790 | case COND_NEVER: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2791 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2792 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2793 | case COND_IF_FALSE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2794 | istk->conds->state = COND_ELSE_TRUE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2795 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2796 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2797 | case COND_ELSE_TRUE: |
| 2798 | case COND_ELSE_FALSE: |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2799 | nasm_warn(WARN_OTHER|ERR_PP_PRECOND, |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2800 | "`%%else' after `%%else' ignored."); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2801 | istk->conds->state = COND_NEVER; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2802 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2803 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2804 | free_tlist(origline); |
| 2805 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2806 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2807 | case PP_ENDIF: |
| 2808 | if (tline->next) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2809 | nasm_warn(WARN_OTHER|ERR_PP_PRECOND, |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2810 | "trailing garbage after `%%endif' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2811 | if (!istk->conds) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2812 | nasm_fatal("`%%endif': no matching `%%if'"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2813 | cond = istk->conds; |
| 2814 | istk->conds = cond->next; |
| 2815 | nasm_free(cond); |
| 2816 | if(istk->mstk) |
| 2817 | istk->mstk->condcnt --; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2818 | free_tlist(origline); |
| 2819 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2820 | |
H. Peter Anvin | db8f96e | 2009-07-15 09:07:29 -0400 | [diff] [blame] | 2821 | case PP_IRMACRO: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2822 | case PP_IMACRO: |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2823 | casesense = false; |
| 2824 | /* fall through */ |
| 2825 | case PP_RMACRO: |
| 2826 | case PP_MACRO: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2827 | if (defining) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2828 | nasm_fatal("`%s': already defining a macro", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2829 | return DIRECTIVE_FOUND; |
| 2830 | } |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 2831 | defining = nasm_zalloc(sizeof(MMacro)); |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 2832 | defining->max_depth = ((i == PP_RMACRO) || (i == PP_IRMACRO)) |
| 2833 | ? nasm_limit[LIMIT_MACROS] : 0; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2834 | defining->casesense = casesense; |
| 2835 | if (!parse_mmacro_spec(tline, defining, dname)) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2836 | nasm_free(defining); |
| 2837 | defining = NULL; |
| 2838 | return DIRECTIVE_FOUND; |
| 2839 | } |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2840 | |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 2841 | src_get(&defining->xline, &defining->fname); |
| 2842 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2843 | mmac = (MMacro *) hash_findix(&mmacros, defining->name); |
| 2844 | while (mmac) { |
| 2845 | if (!strcmp(mmac->name, defining->name) && |
| 2846 | (mmac->nparam_min <= defining->nparam_max |
| 2847 | || defining->plus) |
| 2848 | && (defining->nparam_min <= mmac->nparam_max |
| 2849 | || mmac->plus)) { |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2850 | nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'", |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2851 | defining->name); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2852 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2853 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2854 | mmac = mmac->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2855 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2856 | free_tlist(origline); |
| 2857 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2858 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2859 | case PP_ENDM: |
| 2860 | case PP_ENDMACRO: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2861 | if (! (defining && defining->name)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2862 | nasm_nonfatal("`%s': not defining a macro", tline->text); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2863 | return DIRECTIVE_FOUND; |
| 2864 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2865 | mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name); |
| 2866 | defining->next = *mmhead; |
| 2867 | *mmhead = defining; |
| 2868 | defining = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2869 | free_tlist(origline); |
| 2870 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2871 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2872 | case PP_EXITMACRO: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2873 | /* |
| 2874 | * We must search along istk->expansion until we hit a |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2875 | * macro-end marker for a macro with a name. Then we |
| 2876 | * bypass all lines between exitmacro and endmacro. |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2877 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2878 | list_for_each(l, istk->expansion) |
| 2879 | if (l->finishes && l->finishes->name) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2880 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2881 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2882 | if (l) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2883 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2884 | * Remove all conditional entries relative to this |
| 2885 | * macro invocation. (safe to do in this context) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2886 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2887 | for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) { |
| 2888 | cond = istk->conds; |
| 2889 | istk->conds = cond->next; |
| 2890 | nasm_free(cond); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2891 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2892 | istk->expansion = l; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2893 | } else { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2894 | nasm_nonfatal("`%%exitmacro' not within `%%macro' block"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2895 | } |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 2896 | free_tlist(origline); |
| 2897 | return DIRECTIVE_FOUND; |
| 2898 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2899 | case PP_UNIMACRO: |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2900 | casesense = false; |
| 2901 | /* fall through */ |
| 2902 | case PP_UNMACRO: |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2903 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2904 | MMacro **mmac_p; |
| 2905 | MMacro spec; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2906 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2907 | spec.casesense = casesense; |
| 2908 | if (!parse_mmacro_spec(tline, &spec, dname)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2909 | return DIRECTIVE_FOUND; |
| 2910 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2911 | mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL); |
| 2912 | while (mmac_p && *mmac_p) { |
| 2913 | mmac = *mmac_p; |
| 2914 | if (mmac->casesense == spec.casesense && |
| 2915 | !mstrcmp(mmac->name, spec.name, spec.casesense) && |
| 2916 | mmac->nparam_min == spec.nparam_min && |
| 2917 | mmac->nparam_max == spec.nparam_max && |
| 2918 | mmac->plus == spec.plus) { |
| 2919 | *mmac_p = mmac->next; |
| 2920 | free_mmacro(mmac); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2921 | } else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2922 | mmac_p = &mmac->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2923 | } |
| 2924 | } |
| 2925 | free_tlist(origline); |
| 2926 | free_tlist(spec.dlist); |
| 2927 | return DIRECTIVE_FOUND; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2928 | } |
| 2929 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2930 | case PP_ROTATE: |
| 2931 | if (tline->next && tline->next->type == TOK_WHITESPACE) |
| 2932 | tline = tline->next; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2933 | if (!tline->next) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2934 | free_tlist(origline); |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2935 | nasm_nonfatal("`%%rotate' missing rotate count"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2936 | return DIRECTIVE_FOUND; |
| 2937 | } |
| 2938 | t = expand_smacro(tline->next); |
| 2939 | tline->next = NULL; |
| 2940 | free_tlist(origline); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2941 | pps.tptr = tline = t; |
| 2942 | pps.ntokens = -1; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2943 | tokval.t_type = TOKEN_INVALID; |
| 2944 | evalresult = |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2945 | evaluate(ppscan, &pps, &tokval, NULL, true, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2946 | free_tlist(tline); |
| 2947 | if (!evalresult) |
| 2948 | return DIRECTIVE_FOUND; |
| 2949 | if (tokval.t_type) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2950 | nasm_warn(WARN_OTHER, "trailing garbage after expression ignored"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2951 | if (!is_simple(evalresult)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2952 | nasm_nonfatal("non-constant value given to `%%rotate'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2953 | return DIRECTIVE_FOUND; |
| 2954 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2955 | mmac = istk->mstk; |
| 2956 | while (mmac && !mmac->name) /* avoid mistaking %reps for macros */ |
| 2957 | mmac = mmac->next_active; |
| 2958 | if (!mmac) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2959 | nasm_nonfatal("`%%rotate' invoked outside a macro call"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2960 | } else if (mmac->nparam == 0) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2961 | nasm_nonfatal("`%%rotate' invoked within macro without parameters"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2962 | } else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2963 | int rotate = mmac->rotate + reloc_value(evalresult); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2964 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2965 | rotate %= (int)mmac->nparam; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2966 | if (rotate < 0) |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2967 | rotate += mmac->nparam; |
| 2968 | |
| 2969 | mmac->rotate = rotate; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2970 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2971 | return DIRECTIVE_FOUND; |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 2972 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2973 | case PP_REP: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 2974 | nolist = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2975 | do { |
| 2976 | tline = tline->next; |
| 2977 | } while (tok_type_(tline, TOK_WHITESPACE)); |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 2978 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2979 | if (tok_type_(tline, TOK_ID) && |
| 2980 | nasm_stricmp(tline->text, ".nolist") == 0) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 2981 | nolist = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2982 | do { |
| 2983 | tline = tline->next; |
| 2984 | } while (tok_type_(tline, TOK_WHITESPACE)); |
| 2985 | } |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 2986 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2987 | if (tline) { |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2988 | pps.tptr = expand_smacro(tline); |
| 2989 | pps.ntokens = -1; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2990 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2991 | /* XXX: really critical?! */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2992 | evalresult = |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 2993 | evaluate(ppscan, &pps, &tokval, NULL, true, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2994 | if (!evalresult) { |
| 2995 | free_tlist(origline); |
| 2996 | return DIRECTIVE_FOUND; |
| 2997 | } |
| 2998 | if (tokval.t_type) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2999 | nasm_warn(WARN_OTHER, "trailing garbage after expression ignored"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3000 | if (!is_simple(evalresult)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3001 | nasm_nonfatal("non-constant value given to `%%rep'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3002 | return DIRECTIVE_FOUND; |
| 3003 | } |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 3004 | count = reloc_value(evalresult); |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 3005 | if (count > nasm_limit[LIMIT_REP]) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3006 | nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")", |
| 3007 | count, nasm_limit[LIMIT_REP]); |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 3008 | count = 0; |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 3009 | } else if (count < 0) { |
H. Peter Anvin (Intel) | 723ab48 | 2018-12-13 21:53:31 -0800 | [diff] [blame] | 3010 | /*! |
| 3011 | *!negative-rep [on] regative %rep count |
| 3012 | *! warns about negative counts given to the \c{%rep} |
| 3013 | *! preprocessor directive. |
| 3014 | */ |
H. Peter Anvin (Intel) | 80c4f23 | 2018-12-14 13:33:24 -0800 | [diff] [blame] | 3015 | nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP, |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 3016 | "negative `%%rep' count: %"PRId64, count); |
| 3017 | count = 0; |
| 3018 | } else { |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 3019 | count++; |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 3020 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3021 | } else { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3022 | nasm_nonfatal("`%%rep' expects a repeat count"); |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 3023 | count = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3024 | } |
| 3025 | free_tlist(origline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3026 | |
| 3027 | tmp_defining = defining; |
| 3028 | defining = nasm_malloc(sizeof(MMacro)); |
| 3029 | defining->prev = NULL; |
| 3030 | defining->name = NULL; /* flags this macro as a %rep block */ |
| 3031 | defining->casesense = false; |
| 3032 | defining->plus = false; |
| 3033 | defining->nolist = nolist; |
| 3034 | defining->in_progress = count; |
| 3035 | defining->max_depth = 0; |
| 3036 | defining->nparam_min = defining->nparam_max = 0; |
| 3037 | defining->defaults = NULL; |
| 3038 | defining->dlist = NULL; |
| 3039 | defining->expansion = NULL; |
| 3040 | defining->next_active = istk->mstk; |
| 3041 | defining->rep_nest = tmp_defining; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3042 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3043 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3044 | case PP_ENDREP: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3045 | if (!defining || defining->name) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3046 | nasm_nonfatal("`%%endrep': no matching `%%rep'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3047 | return DIRECTIVE_FOUND; |
| 3048 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3049 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3050 | /* |
| 3051 | * Now we have a "macro" defined - although it has no name |
| 3052 | * and we won't be entering it in the hash tables - we must |
| 3053 | * push a macro-end marker for it on to istk->expansion. |
| 3054 | * After that, it will take care of propagating itself (a |
| 3055 | * macro-end marker line for a macro which is really a %rep |
| 3056 | * block will cause the macro to be re-expanded, complete |
| 3057 | * with another macro-end marker to ensure the process |
| 3058 | * continues) until the whole expansion is forcibly removed |
| 3059 | * from istk->expansion by a %exitrep. |
| 3060 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3061 | l = nasm_malloc(sizeof(Line)); |
| 3062 | l->next = istk->expansion; |
| 3063 | l->finishes = defining; |
| 3064 | l->first = NULL; |
| 3065 | istk->expansion = l; |
| 3066 | |
| 3067 | istk->mstk = defining; |
| 3068 | |
H. Peter Anvin | 0d4d431 | 2019-08-07 00:46:27 -0700 | [diff] [blame] | 3069 | lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3070 | tmp_defining = defining; |
| 3071 | defining = defining->rep_nest; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3072 | free_tlist(origline); |
| 3073 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3074 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3075 | case PP_EXITREP: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3076 | /* |
| 3077 | * We must search along istk->expansion until we hit a |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3078 | * macro-end marker for a macro with no name. Then we set |
| 3079 | * its `in_progress' flag to 0. |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3080 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3081 | list_for_each(l, istk->expansion) |
| 3082 | if (l->finishes && !l->finishes->name) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3083 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3084 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3085 | if (l) |
| 3086 | l->finishes->in_progress = 1; |
| 3087 | else |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3088 | nasm_nonfatal("`%%exitrep' not within `%%rep' block"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3089 | free_tlist(origline); |
| 3090 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3091 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3092 | case PP_IDEFINE: |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3093 | case PP_IXDEFINE: |
| 3094 | casesense = false; |
| 3095 | /* fall through */ |
| 3096 | case PP_DEFINE: |
| 3097 | case PP_XDEFINE: |
| 3098 | { |
| 3099 | SMacro *s; |
| 3100 | bool have_eval_params = false; |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3101 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3102 | tline = tline->next; |
| 3103 | skip_white_(tline); |
| 3104 | tline = expand_id(tline); |
| 3105 | if (!tline || (tline->type != TOK_ID && |
| 3106 | (tline->type != TOK_PREPROC_ID || |
| 3107 | tline->text[1] != '$'))) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3108 | nasm_nonfatal("`%s' expects a macro identifier", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3109 | free_tlist(origline); |
| 3110 | return DIRECTIVE_FOUND; |
| 3111 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3112 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3113 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3114 | last = tline; |
| 3115 | param_start = tline = tline->next; |
| 3116 | nparam = 0; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3117 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3118 | if (tok_is_(tline, "(")) { |
| 3119 | /* |
| 3120 | * This macro has parameters. |
| 3121 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3122 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3123 | tline = tline->next; |
| 3124 | while (1) { |
| 3125 | skip_white_(tline); |
| 3126 | if (!tline) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3127 | nasm_nonfatal("parameter identifier expected"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3128 | free_tlist(origline); |
| 3129 | return DIRECTIVE_FOUND; |
| 3130 | } |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3131 | if (tok_is_(tline, "=")) { |
| 3132 | have_eval_params = true; |
| 3133 | tline = tline->next; |
| 3134 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3135 | if (tline->type != TOK_ID) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3136 | nasm_nonfatal("`%s': parameter identifier expected", |
| 3137 | tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3138 | free_tlist(origline); |
| 3139 | return DIRECTIVE_FOUND; |
| 3140 | } |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3141 | tline->type = tok_smac_param(nparam++); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3142 | tline = tline->next; |
| 3143 | skip_white_(tline); |
| 3144 | if (tok_is_(tline, ",")) { |
| 3145 | tline = tline->next; |
H. Peter Anvin | ca348b6 | 2008-07-23 10:49:26 -0400 | [diff] [blame] | 3146 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3147 | if (!tok_is_(tline, ")")) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3148 | nasm_nonfatal("`)' expected to terminate macro template"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3149 | free_tlist(origline); |
| 3150 | return DIRECTIVE_FOUND; |
| 3151 | } |
| 3152 | break; |
| 3153 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3154 | } |
| 3155 | last = tline; |
| 3156 | tline = tline->next; |
| 3157 | } |
| 3158 | if (tok_type_(tline, TOK_WHITESPACE)) |
| 3159 | last = tline, tline = tline->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3160 | last->next = NULL; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3161 | |
| 3162 | /* Expand the macro definition now for %xdefine and %ixdefine */ |
| 3163 | if ((i == PP_XDEFINE) || (i == PP_IXDEFINE)) |
| 3164 | tline = expand_smacro(tline); |
| 3165 | |
| 3166 | macro_start = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3167 | t = tline; |
| 3168 | while (t) { |
| 3169 | if (t->type == TOK_ID) { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3170 | list_for_each(tt, param_start) |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 3171 | if (is_smac_param(tt->type) && |
| 3172 | tt->len == t->len && |
| 3173 | !memcmp(tt->text, t->text, tt->len)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3174 | t->type = tt->type; |
| 3175 | } |
| 3176 | tt = t->next; |
| 3177 | t->next = macro_start; |
| 3178 | macro_start = t; |
| 3179 | t = tt; |
| 3180 | } |
| 3181 | /* |
| 3182 | * Good. We now have a macro name, a parameter count, and a |
| 3183 | * token list (in reverse order) for an expansion. We ought |
| 3184 | * to be OK just to create an SMacro, store it, and let |
| 3185 | * free_tlist have the rest of the line (which we have |
| 3186 | * carefully re-terminated after chopping off the expansion |
| 3187 | * from the end). |
| 3188 | */ |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3189 | s = define_smacro(ctx, mname, casesense, nparam, macro_start); |
| 3190 | |
| 3191 | if (have_eval_params) { |
| 3192 | /* Create evaluated parameters table */ |
| 3193 | bool is_eval = false; |
| 3194 | |
| 3195 | nasm_newn(s->eval_param, nparam); |
| 3196 | list_for_each(tt, param_start) { |
| 3197 | if (is_smac_param(tt->type)) |
| 3198 | s->eval_param[smac_nparam(tt->type)] = is_eval; |
| 3199 | is_eval = tok_is_(tt, "="); |
| 3200 | } |
| 3201 | } |
| 3202 | |
| 3203 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3204 | free_tlist(origline); |
| 3205 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3206 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3207 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3208 | case PP_UNDEF: |
| 3209 | tline = tline->next; |
| 3210 | skip_white_(tline); |
| 3211 | tline = expand_id(tline); |
| 3212 | if (!tline || (tline->type != TOK_ID && |
| 3213 | (tline->type != TOK_PREPROC_ID || |
| 3214 | tline->text[1] != '$'))) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3215 | nasm_nonfatal("`%%undef' expects a macro identifier"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3216 | free_tlist(origline); |
| 3217 | return DIRECTIVE_FOUND; |
| 3218 | } |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3219 | if (tline->next) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 3220 | nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored"); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3221 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3222 | /* Find the context that symbol belongs to */ |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3223 | ctx = get_ctx(tline->text, &mname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3224 | undef_smacro(ctx, mname); |
| 3225 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3226 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3227 | |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3228 | case PP_IDEFSTR: |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3229 | casesense = false; |
| 3230 | /* fall through */ |
| 3231 | case PP_DEFSTR: |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3232 | tline = tline->next; |
| 3233 | skip_white_(tline); |
| 3234 | tline = expand_id(tline); |
| 3235 | if (!tline || (tline->type != TOK_ID && |
| 3236 | (tline->type != TOK_PREPROC_ID || |
| 3237 | tline->text[1] != '$'))) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3238 | nasm_nonfatal("`%s' expects a macro identifier", dname); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3239 | free_tlist(origline); |
| 3240 | return DIRECTIVE_FOUND; |
| 3241 | } |
| 3242 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3243 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3244 | last = tline; |
| 3245 | tline = expand_smacro(tline->next); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3246 | last->next = NULL; |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3247 | |
| 3248 | while (tok_type_(tline, TOK_WHITESPACE)) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3249 | tline = delete_Token(tline); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3250 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3251 | p = detoken(tline, false); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3252 | macro_start = make_tok_qstr(p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3253 | nasm_free(p); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3254 | |
| 3255 | /* |
| 3256 | * We now have a macro name, an implicit parameter count of |
| 3257 | * zero, and a string token to use as an expansion. Create |
| 3258 | * and store an SMacro. |
| 3259 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3260 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3261 | free_tlist(origline); |
| 3262 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3263 | |
H. Peter Anvin | 2f55bda | 2009-07-14 15:04:04 -0400 | [diff] [blame] | 3264 | case PP_IDEFTOK: |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3265 | casesense = false; |
| 3266 | /* fall through */ |
| 3267 | case PP_DEFTOK: |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3268 | tline = tline->next; |
| 3269 | skip_white_(tline); |
| 3270 | tline = expand_id(tline); |
| 3271 | if (!tline || (tline->type != TOK_ID && |
| 3272 | (tline->type != TOK_PREPROC_ID || |
| 3273 | tline->text[1] != '$'))) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3274 | nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3275 | free_tlist(origline); |
| 3276 | return DIRECTIVE_FOUND; |
| 3277 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3278 | ctx = get_ctx(tline->text, &mname); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3279 | last = tline; |
| 3280 | tline = expand_smacro(tline->next); |
| 3281 | last->next = NULL; |
| 3282 | |
| 3283 | t = tline; |
| 3284 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3285 | t = t->next; |
| 3286 | /* t should now point to the string */ |
Cyrill Gorcunov | 6908e58 | 2010-09-06 19:36:15 +0400 | [diff] [blame] | 3287 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3288 | nasm_nonfatal("`%s` requires string as second parameter", dname); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3289 | free_tlist(tline); |
| 3290 | free_tlist(origline); |
| 3291 | return DIRECTIVE_FOUND; |
| 3292 | } |
| 3293 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 3294 | /* |
| 3295 | * Convert the string to a token stream. Note that smacros |
| 3296 | * are stored with the token stream reversed, so we have to |
| 3297 | * reverse the output of tokenize(). |
| 3298 | */ |
H. Peter Anvin | bb42d30 | 2019-04-22 14:29:29 -0700 | [diff] [blame] | 3299 | nasm_unquote_cstr(t->text, NULL); |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 3300 | macro_start = reverse_tokens(tokenize(t->text)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3301 | |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3302 | /* |
| 3303 | * We now have a macro name, an implicit parameter count of |
| 3304 | * zero, and a numeric token to use as an expansion. Create |
| 3305 | * and store an SMacro. |
| 3306 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3307 | define_smacro(ctx, mname, casesense, 0, macro_start); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3308 | free_tlist(tline); |
| 3309 | free_tlist(origline); |
| 3310 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3311 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3312 | case PP_IPATHSEARCH: |
| 3313 | casesense = false; |
| 3314 | /* fall through */ |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3315 | case PP_PATHSEARCH: |
| 3316 | { |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 3317 | const char *found_path; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3318 | |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3319 | tline = tline->next; |
| 3320 | skip_white_(tline); |
| 3321 | tline = expand_id(tline); |
| 3322 | if (!tline || (tline->type != TOK_ID && |
| 3323 | (tline->type != TOK_PREPROC_ID || |
| 3324 | tline->text[1] != '$'))) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3325 | nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3326 | free_tlist(origline); |
| 3327 | return DIRECTIVE_FOUND; |
| 3328 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3329 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3330 | last = tline; |
| 3331 | tline = expand_smacro(tline->next); |
| 3332 | last->next = NULL; |
| 3333 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3334 | t = tline; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3335 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3336 | t = t->next; |
| 3337 | |
| 3338 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3339 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3340 | nasm_nonfatal("`%s' expects a file name", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3341 | free_tlist(tline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3342 | free_tlist(origline); |
| 3343 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 3344 | } |
| 3345 | if (t->next) |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3346 | nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3347 | p = t->text; |
H. Peter Anvin | 427cc91 | 2008-06-01 21:43:03 -0700 | [diff] [blame] | 3348 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3349 | nasm_unquote(p, NULL); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3350 | |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 3351 | inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY); |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 3352 | if (!found_path) |
| 3353 | found_path = p; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3354 | macro_start = make_tok_qstr(found_path); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3355 | |
| 3356 | /* |
| 3357 | * We now have a macro name, an implicit parameter count of |
| 3358 | * zero, and a string token to use as an expansion. Create |
| 3359 | * and store an SMacro. |
| 3360 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3361 | define_smacro(ctx, mname, casesense, 0, macro_start); |
| 3362 | free_tlist(tline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3363 | free_tlist(origline); |
| 3364 | return DIRECTIVE_FOUND; |
| 3365 | } |
| 3366 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3367 | case PP_ISTRLEN: |
| 3368 | casesense = false; |
| 3369 | /* fall through */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3370 | case PP_STRLEN: |
| 3371 | tline = tline->next; |
| 3372 | skip_white_(tline); |
| 3373 | tline = expand_id(tline); |
| 3374 | if (!tline || (tline->type != TOK_ID && |
| 3375 | (tline->type != TOK_PREPROC_ID || |
| 3376 | tline->text[1] != '$'))) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3377 | nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3378 | free_tlist(origline); |
| 3379 | return DIRECTIVE_FOUND; |
| 3380 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3381 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3382 | last = tline; |
| 3383 | tline = expand_smacro(tline->next); |
| 3384 | last->next = NULL; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3385 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3386 | t = tline; |
| 3387 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3388 | t = t->next; |
| 3389 | /* t should now point to the string */ |
Cyrill Gorcunov | 4e1d5ab | 2010-07-23 18:51:51 +0400 | [diff] [blame] | 3390 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3391 | nasm_nonfatal("`%s' requires string as second parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3392 | free_tlist(tline); |
| 3393 | free_tlist(origline); |
| 3394 | return DIRECTIVE_FOUND; |
| 3395 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3396 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3397 | macro_start = make_tok_num(nasm_unquote(t->text, NULL)); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3398 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3399 | /* |
| 3400 | * We now have a macro name, an implicit parameter count of |
| 3401 | * zero, and a numeric token to use as an expansion. Create |
| 3402 | * and store an SMacro. |
| 3403 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3404 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3405 | free_tlist(tline); |
| 3406 | free_tlist(origline); |
| 3407 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3408 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3409 | case PP_ISTRCAT: |
| 3410 | casesense = false; |
| 3411 | /* fall through */ |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3412 | case PP_STRCAT: |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3413 | tline = tline->next; |
| 3414 | skip_white_(tline); |
| 3415 | tline = expand_id(tline); |
| 3416 | if (!tline || (tline->type != TOK_ID && |
| 3417 | (tline->type != TOK_PREPROC_ID || |
| 3418 | tline->text[1] != '$'))) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3419 | nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3420 | free_tlist(origline); |
| 3421 | return DIRECTIVE_FOUND; |
| 3422 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3423 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3424 | last = tline; |
| 3425 | tline = expand_smacro(tline->next); |
| 3426 | last->next = NULL; |
| 3427 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3428 | len = 0; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3429 | list_for_each(t, tline) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3430 | switch (t->type) { |
| 3431 | case TOK_WHITESPACE: |
| 3432 | break; |
| 3433 | case TOK_STRING: |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 3434 | len += t->len = nasm_unquote(t->text, NULL); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3435 | break; |
| 3436 | case TOK_OTHER: |
| 3437 | if (!strcmp(t->text, ",")) /* permit comma separators */ |
| 3438 | break; |
| 3439 | /* else fall through */ |
| 3440 | default: |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3441 | nasm_nonfatal("non-string passed to `%s': %s", dname, t->text); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3442 | free_tlist(tline); |
| 3443 | free_tlist(origline); |
| 3444 | return DIRECTIVE_FOUND; |
| 3445 | } |
| 3446 | } |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3447 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3448 | p = pp = nasm_malloc(len); |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3449 | list_for_each(t, tline) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3450 | if (t->type == TOK_STRING) { |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 3451 | memcpy(p, t->text, t->len); |
| 3452 | p += t->len; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3453 | } |
| 3454 | } |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3455 | |
| 3456 | /* |
| 3457 | * We now have a macro name, an implicit parameter count of |
| 3458 | * zero, and a numeric token to use as an expansion. Create |
| 3459 | * and store an SMacro. |
| 3460 | */ |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3461 | macro_start = make_tok_qstr(pp); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3462 | nasm_free(pp); |
| 3463 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3464 | free_tlist(tline); |
| 3465 | free_tlist(origline); |
| 3466 | return DIRECTIVE_FOUND; |
| 3467 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3468 | case PP_ISUBSTR: |
| 3469 | casesense = false; |
| 3470 | /* fall through */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3471 | case PP_SUBSTR: |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3472 | { |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3473 | int64_t start, count; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3474 | size_t len; |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 3475 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3476 | tline = tline->next; |
| 3477 | skip_white_(tline); |
| 3478 | tline = expand_id(tline); |
| 3479 | if (!tline || (tline->type != TOK_ID && |
| 3480 | (tline->type != TOK_PREPROC_ID || |
| 3481 | tline->text[1] != '$'))) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3482 | nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3483 | free_tlist(origline); |
| 3484 | return DIRECTIVE_FOUND; |
| 3485 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3486 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3487 | last = tline; |
| 3488 | tline = expand_smacro(tline->next); |
| 3489 | last->next = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3490 | |
Cyrill Gorcunov | 35519d6 | 2010-09-06 23:49:52 +0400 | [diff] [blame] | 3491 | if (tline) /* skip expanded id */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3492 | t = tline->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3493 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3494 | t = t->next; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3495 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3496 | /* t should now point to the string */ |
Cyrill Gorcunov | 35519d6 | 2010-09-06 23:49:52 +0400 | [diff] [blame] | 3497 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3498 | nasm_nonfatal("`%s' requires string as second parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3499 | free_tlist(tline); |
| 3500 | free_tlist(origline); |
| 3501 | return DIRECTIVE_FOUND; |
| 3502 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3503 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3504 | pps.tptr = t->next; |
| 3505 | pps.ntokens = -1; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3506 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3507 | evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3508 | if (!evalresult) { |
| 3509 | free_tlist(tline); |
| 3510 | free_tlist(origline); |
| 3511 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3512 | } else if (!is_simple(evalresult)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3513 | nasm_nonfatal("non-constant value given to `%s'", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3514 | free_tlist(tline); |
| 3515 | free_tlist(origline); |
| 3516 | return DIRECTIVE_FOUND; |
| 3517 | } |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3518 | start = evalresult->value - 1; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3519 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3520 | while (tok_type_(pps.tptr, TOK_WHITESPACE)) |
| 3521 | pps.tptr = pps.tptr->next; |
| 3522 | if (!pps.tptr) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3523 | count = 1; /* Backwards compatibility: one character */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3524 | } else { |
| 3525 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3526 | evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3527 | if (!evalresult) { |
| 3528 | free_tlist(tline); |
| 3529 | free_tlist(origline); |
| 3530 | return DIRECTIVE_FOUND; |
| 3531 | } else if (!is_simple(evalresult)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3532 | nasm_nonfatal("non-constant value given to `%s'", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3533 | free_tlist(tline); |
| 3534 | free_tlist(origline); |
| 3535 | return DIRECTIVE_FOUND; |
| 3536 | } |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3537 | count = evalresult->value; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3538 | } |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3539 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3540 | len = nasm_unquote(t->text, NULL); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3541 | |
Cyrill Gorcunov | cff031e | 2010-09-07 20:31:11 +0400 | [diff] [blame] | 3542 | /* make start and count being in range */ |
| 3543 | if (start < 0) |
| 3544 | start = 0; |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3545 | if (count < 0) |
| 3546 | count = len + count + 1 - start; |
| 3547 | if (start + count > (int64_t)len) |
Cyrill Gorcunov | cff031e | 2010-09-07 20:31:11 +0400 | [diff] [blame] | 3548 | count = len - start; |
| 3549 | if (!len || count < 0 || start >=(int64_t)len) |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3550 | start = -1, count = 0; /* empty string */ |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3551 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3552 | macro_start = new_Token(NULL, TOK_STRING, NULL, 0); |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 3553 | macro_start->len = count; |
| 3554 | macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, |
| 3555 | ¯o_start->len); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3556 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3557 | /* |
| 3558 | * We now have a macro name, an implicit parameter count of |
| 3559 | * zero, and a numeric token to use as an expansion. Create |
| 3560 | * and store an SMacro. |
| 3561 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3562 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3563 | free_tlist(tline); |
| 3564 | free_tlist(origline); |
| 3565 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3566 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3567 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3568 | case PP_IASSIGN: |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3569 | casesense = false; |
| 3570 | /* fall through */ |
| 3571 | case PP_ASSIGN: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3572 | tline = tline->next; |
| 3573 | skip_white_(tline); |
| 3574 | tline = expand_id(tline); |
| 3575 | if (!tline || (tline->type != TOK_ID && |
| 3576 | (tline->type != TOK_PREPROC_ID || |
| 3577 | tline->text[1] != '$'))) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3578 | nasm_nonfatal("`%s' expects a macro identifier", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3579 | free_tlist(origline); |
| 3580 | return DIRECTIVE_FOUND; |
| 3581 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3582 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3583 | last = tline; |
| 3584 | tline = expand_smacro(tline->next); |
| 3585 | last->next = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3586 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3587 | pps.tptr = tline; |
| 3588 | pps.ntokens = -1; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3589 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3590 | evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3591 | free_tlist(tline); |
| 3592 | if (!evalresult) { |
| 3593 | free_tlist(origline); |
| 3594 | return DIRECTIVE_FOUND; |
| 3595 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3596 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3597 | if (tokval.t_type) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 3598 | nasm_warn(WARN_OTHER, "trailing garbage after expression ignored"); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3599 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3600 | if (!is_simple(evalresult)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3601 | nasm_nonfatal("non-constant value given to `%s'", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3602 | free_tlist(origline); |
| 3603 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3604 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3605 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3606 | macro_start = make_tok_num(reloc_value(evalresult)); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3607 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3608 | /* |
| 3609 | * We now have a macro name, an implicit parameter count of |
| 3610 | * zero, and a numeric token to use as an expansion. Create |
| 3611 | * and store an SMacro. |
| 3612 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3613 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3614 | free_tlist(origline); |
| 3615 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3616 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3617 | case PP_LINE: |
| 3618 | /* |
| 3619 | * Syntax is `%line nnn[+mmm] [filename]' |
| 3620 | */ |
H. Peter Anvin (Intel) | 800c168 | 2018-12-14 12:22:11 -0800 | [diff] [blame] | 3621 | if (unlikely(pp_noline)) { |
| 3622 | free_tlist(origline); |
| 3623 | return DIRECTIVE_FOUND; |
| 3624 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3625 | tline = tline->next; |
| 3626 | skip_white_(tline); |
| 3627 | if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3628 | nasm_nonfatal("`%s' expects line number", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3629 | free_tlist(origline); |
| 3630 | return DIRECTIVE_FOUND; |
| 3631 | } |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 3632 | k = readnum(tline->text, &err); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3633 | m = 1; |
| 3634 | tline = tline->next; |
| 3635 | if (tok_is_(tline, "+")) { |
| 3636 | tline = tline->next; |
| 3637 | if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3638 | nasm_nonfatal("`%s' expects line increment", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3639 | free_tlist(origline); |
| 3640 | return DIRECTIVE_FOUND; |
| 3641 | } |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 3642 | m = readnum(tline->text, &err); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3643 | tline = tline->next; |
| 3644 | } |
| 3645 | skip_white_(tline); |
| 3646 | src_set_linnum(k); |
| 3647 | istk->lineinc = m; |
| 3648 | if (tline) { |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 3649 | char *fname = detoken(tline, false); |
| 3650 | src_set_fname(fname); |
| 3651 | nasm_free(fname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3652 | } |
| 3653 | free_tlist(origline); |
| 3654 | return DIRECTIVE_FOUND; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3655 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3656 | default: |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 3657 | nasm_nonfatal("preprocessor directive `%s' not yet implemented", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3658 | return DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3659 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3660 | } |
| 3661 | |
| 3662 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3663 | * Ensure that a macro parameter contains a condition code and |
| 3664 | * nothing else. Return the condition code index if so, or -1 |
| 3665 | * otherwise. |
| 3666 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3667 | static int find_cc(Token * t) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3668 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3669 | Token *tt; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3670 | |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3671 | if (!t) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3672 | return -1; /* Probably a %+ without a space */ |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3673 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3674 | skip_white_(t); |
Cyrill Gorcunov | 7524cfd | 2017-10-22 19:01:16 +0300 | [diff] [blame] | 3675 | if (!t) |
| 3676 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3677 | if (t->type != TOK_ID) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3678 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3679 | tt = t->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3680 | skip_white_(tt); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3681 | if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ","))) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3682 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3683 | |
Cyrill Gorcunov | 1945639 | 2012-05-02 00:18:56 +0400 | [diff] [blame] | 3684 | return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3685 | } |
| 3686 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3687 | /* |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 3688 | * This routines walks over tokens strem and handles tokens |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3689 | * pasting, if @handle_explicit passed then explicit pasting |
| 3690 | * term is handled, otherwise -- implicit pastings only. |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 3691 | * The @m array can contain a series of token types which are |
| 3692 | * executed as separate passes. |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3693 | */ |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 3694 | static bool paste_tokens(Token **head, const struct tokseq_match *m, |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3695 | size_t mnum, bool handle_explicit) |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3696 | { |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3697 | Token *tok, *next, **prev_next, **prev_nonspace; |
| 3698 | bool pasted = false; |
| 3699 | char *buf, *p; |
| 3700 | size_t len, i; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3701 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3702 | /* |
| 3703 | * The last token before pasting. We need it |
| 3704 | * to be able to connect new handled tokens. |
| 3705 | * In other words if there were a tokens stream |
| 3706 | * |
| 3707 | * A -> B -> C -> D |
| 3708 | * |
| 3709 | * and we've joined tokens B and C, the resulting |
| 3710 | * stream should be |
| 3711 | * |
| 3712 | * A -> BC -> D |
| 3713 | */ |
| 3714 | tok = *head; |
| 3715 | prev_next = NULL; |
| 3716 | |
| 3717 | if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE)) |
| 3718 | prev_nonspace = head; |
| 3719 | else |
| 3720 | prev_nonspace = NULL; |
| 3721 | |
| 3722 | while (tok && (next = tok->next)) { |
| 3723 | |
| 3724 | switch (tok->type) { |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3725 | case TOK_WHITESPACE: |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3726 | /* Zap redundant whitespaces */ |
| 3727 | while (tok_type_(next, TOK_WHITESPACE)) |
| 3728 | next = delete_Token(next); |
| 3729 | tok->next = next; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3730 | break; |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3731 | |
| 3732 | case TOK_PASTE: |
| 3733 | /* Explicit pasting */ |
| 3734 | if (!handle_explicit) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3735 | break; |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3736 | next = delete_Token(tok); |
| 3737 | |
| 3738 | while (tok_type_(next, TOK_WHITESPACE)) |
| 3739 | next = delete_Token(next); |
| 3740 | |
| 3741 | if (!pasted) |
| 3742 | pasted = true; |
| 3743 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3744 | /* Left pasting token is start of line */ |
| 3745 | if (!prev_nonspace) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3746 | nasm_fatal("No lvalue found on pasting"); |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3747 | |
Cyrill Gorcunov | 8b5c9fb | 2013-02-04 01:24:54 +0400 | [diff] [blame] | 3748 | /* |
| 3749 | * No ending token, this might happen in two |
| 3750 | * cases |
| 3751 | * |
| 3752 | * 1) There indeed no right token at all |
| 3753 | * 2) There is a bare "%define ID" statement, |
| 3754 | * and @ID does expand to whitespace. |
| 3755 | * |
| 3756 | * So technically we need to do a grammar analysis |
| 3757 | * in another stage of parsing, but for now lets don't |
| 3758 | * change the behaviour people used to. Simply allow |
| 3759 | * whitespace after paste token. |
| 3760 | */ |
| 3761 | if (!next) { |
| 3762 | /* |
| 3763 | * Zap ending space tokens and that's all. |
| 3764 | */ |
| 3765 | tok = (*prev_nonspace)->next; |
| 3766 | while (tok_type_(tok, TOK_WHITESPACE)) |
| 3767 | tok = delete_Token(tok); |
| 3768 | tok = *prev_nonspace; |
| 3769 | tok->next = NULL; |
| 3770 | break; |
| 3771 | } |
| 3772 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3773 | tok = *prev_nonspace; |
| 3774 | while (tok_type_(tok, TOK_WHITESPACE)) |
| 3775 | tok = delete_Token(tok); |
| 3776 | len = strlen(tok->text); |
| 3777 | len += strlen(next->text); |
| 3778 | |
| 3779 | p = buf = nasm_malloc(len + 1); |
| 3780 | strcpy(p, tok->text); |
| 3781 | p = strchr(p, '\0'); |
| 3782 | strcpy(p, next->text); |
| 3783 | |
| 3784 | delete_Token(tok); |
| 3785 | |
| 3786 | tok = tokenize(buf); |
| 3787 | nasm_free(buf); |
| 3788 | |
| 3789 | *prev_nonspace = tok; |
| 3790 | while (tok && tok->next) |
| 3791 | tok = tok->next; |
| 3792 | |
| 3793 | tok->next = delete_Token(next); |
| 3794 | |
| 3795 | /* Restart from pasted tokens head */ |
| 3796 | tok = *prev_nonspace; |
| 3797 | break; |
| 3798 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3799 | default: |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3800 | /* implicit pasting */ |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 3801 | for (i = 0; i < mnum; i++) { |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3802 | if (!(PP_CONCAT_MATCH(tok, m[i].mask_head))) |
| 3803 | continue; |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 3804 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3805 | len = 0; |
| 3806 | while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) { |
| 3807 | len += strlen(next->text); |
| 3808 | next = next->next; |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 3809 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3810 | |
Cyrill Gorcunov | 6f8109e | 2017-10-22 21:26:36 +0300 | [diff] [blame] | 3811 | /* No match or no text to process */ |
| 3812 | if (tok == next || len == 0) |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3813 | break; |
| 3814 | |
| 3815 | len += strlen(tok->text); |
| 3816 | p = buf = nasm_malloc(len + 1); |
| 3817 | |
Adam Majer | 1a06943 | 2017-07-25 11:12:35 +0200 | [diff] [blame] | 3818 | strcpy(p, tok->text); |
| 3819 | p = strchr(p, '\0'); |
| 3820 | tok = delete_Token(tok); |
| 3821 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3822 | while (tok != next) { |
Adam Majer | 1a06943 | 2017-07-25 11:12:35 +0200 | [diff] [blame] | 3823 | if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) { |
| 3824 | strcpy(p, tok->text); |
| 3825 | p = strchr(p, '\0'); |
| 3826 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3827 | tok = delete_Token(tok); |
| 3828 | } |
| 3829 | |
| 3830 | tok = tokenize(buf); |
| 3831 | nasm_free(buf); |
| 3832 | |
| 3833 | if (prev_next) |
| 3834 | *prev_next = tok; |
| 3835 | else |
| 3836 | *head = tok; |
| 3837 | |
| 3838 | /* |
| 3839 | * Connect pasted into original stream, |
| 3840 | * ie A -> new-tokens -> B |
| 3841 | */ |
| 3842 | while (tok && tok->next) |
| 3843 | tok = tok->next; |
| 3844 | tok->next = next; |
| 3845 | |
| 3846 | if (!pasted) |
| 3847 | pasted = true; |
| 3848 | |
| 3849 | /* Restart from pasted tokens head */ |
| 3850 | tok = prev_next ? *prev_next : *head; |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 3851 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3852 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3853 | break; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3854 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3855 | |
| 3856 | prev_next = &tok->next; |
| 3857 | |
| 3858 | if (tok->next && |
| 3859 | !tok_type_(tok->next, TOK_WHITESPACE) && |
| 3860 | !tok_type_(tok->next, TOK_PASTE)) |
| 3861 | prev_nonspace = prev_next; |
| 3862 | |
| 3863 | tok = tok->next; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3864 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3865 | |
| 3866 | return pasted; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3867 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3868 | |
| 3869 | /* |
| 3870 | * expands to a list of tokens from %{x:y} |
| 3871 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3872 | static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3873 | { |
| 3874 | Token *t = tline, **tt, *tm, *head; |
| 3875 | char *pos; |
| 3876 | int fst, lst, j, i; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3877 | |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3878 | pos = strchr(tline->text, ':'); |
| 3879 | nasm_assert(pos); |
| 3880 | |
| 3881 | lst = atoi(pos + 1); |
| 3882 | fst = atoi(tline->text + 1); |
| 3883 | |
| 3884 | /* |
| 3885 | * only macros params are accounted so |
| 3886 | * if someone passes %0 -- we reject such |
| 3887 | * value(s) |
| 3888 | */ |
| 3889 | if (lst == 0 || fst == 0) |
| 3890 | goto err; |
| 3891 | |
| 3892 | /* the values should be sane */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3893 | if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) || |
| 3894 | (lst > (int)mac->nparam || lst < (-(int)mac->nparam))) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3895 | goto err; |
| 3896 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3897 | fst = fst < 0 ? fst + (int)mac->nparam + 1: fst; |
| 3898 | lst = lst < 0 ? lst + (int)mac->nparam + 1: lst; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3899 | |
| 3900 | /* counted from zero */ |
| 3901 | fst--, lst--; |
| 3902 | |
| 3903 | /* |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3904 | * It will be at least one token. Note we |
| 3905 | * need to scan params until separator, otherwise |
| 3906 | * only first token will be passed. |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3907 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3908 | tm = mac->params[(fst + mac->rotate) % mac->nparam]; |
Cyrill Gorcunov | 67f2ca2 | 2018-10-13 19:41:01 +0300 | [diff] [blame] | 3909 | if (!tm) |
| 3910 | goto err; |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3911 | head = new_Token(NULL, tm->type, tm->text, 0); |
| 3912 | tt = &head->next, tm = tm->next; |
| 3913 | while (tok_isnt_(tm, ",")) { |
| 3914 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 3915 | *tt = t, tt = &t->next, tm = tm->next; |
| 3916 | } |
| 3917 | |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3918 | if (fst < lst) { |
| 3919 | for (i = fst + 1; i <= lst; i++) { |
| 3920 | t = new_Token(NULL, TOK_OTHER, ",", 0); |
| 3921 | *tt = t, tt = &t->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3922 | j = (i + mac->rotate) % mac->nparam; |
| 3923 | tm = mac->params[j]; |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3924 | while (tok_isnt_(tm, ",")) { |
| 3925 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 3926 | *tt = t, tt = &t->next, tm = tm->next; |
| 3927 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3928 | } |
| 3929 | } else { |
| 3930 | for (i = fst - 1; i >= lst; i--) { |
| 3931 | t = new_Token(NULL, TOK_OTHER, ",", 0); |
| 3932 | *tt = t, tt = &t->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3933 | j = (i + mac->rotate) % mac->nparam; |
| 3934 | tm = mac->params[j]; |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3935 | while (tok_isnt_(tm, ",")) { |
| 3936 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 3937 | *tt = t, tt = &t->next, tm = tm->next; |
| 3938 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3939 | } |
| 3940 | } |
| 3941 | |
| 3942 | *last = tt; |
| 3943 | return head; |
| 3944 | |
| 3945 | err: |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3946 | nasm_nonfatal("`%%{%s}': macro parameters out of range", |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3947 | &tline->text[1]); |
| 3948 | return tline; |
| 3949 | } |
| 3950 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3951 | /* |
| 3952 | * Expand MMacro-local things: parameter references (%0, %n, %+n, |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 3953 | * %-n) and MMacro-local identifiers (%%foo) as well as |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3954 | * macro indirection (%[...]) and range (%{..:..}). |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3955 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3956 | static Token *expand_mmac_params(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3957 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3958 | Token *t, *tt, **tail, *thead; |
H. Peter Anvin | 6125b62 | 2009-04-08 14:02:25 -0700 | [diff] [blame] | 3959 | bool changed = false; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3960 | char *pos; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3961 | |
| 3962 | tail = &thead; |
| 3963 | thead = NULL; |
| 3964 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3965 | while (tline) { |
Cyrill Gorcunov | 661f723 | 2018-10-28 20:39:34 +0300 | [diff] [blame] | 3966 | if (tline->type == TOK_PREPROC_ID && tline->text && tline->text[0] && |
Cyrill Gorcunov | ca61119 | 2010-06-04 09:22:12 +0400 | [diff] [blame] | 3967 | (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) || |
| 3968 | (tline->text[1] >= '0' && tline->text[1] <= '9') || |
| 3969 | tline->text[1] == '%')) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 3970 | char *text = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3971 | int type = 0, cc; /* type = 0 to placate optimisers */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 3972 | char tmpbuf[30]; |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3973 | unsigned int n; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3974 | int i; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3975 | MMacro *mac; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3976 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3977 | t = tline; |
| 3978 | tline = tline->next; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3979 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3980 | mac = istk->mstk; |
| 3981 | while (mac && !mac->name) /* avoid mistaking %reps for macros */ |
| 3982 | mac = mac->next_active; |
| 3983 | if (!mac) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3984 | nasm_nonfatal("`%s': not in a macro call", t->text); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3985 | } else { |
| 3986 | pos = strchr(t->text, ':'); |
| 3987 | if (!pos) { |
| 3988 | switch (t->text[1]) { |
| 3989 | /* |
| 3990 | * We have to make a substitution of one of the |
| 3991 | * forms %1, %-1, %+1, %%foo, %0. |
| 3992 | */ |
| 3993 | case '0': |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3994 | type = TOK_NUMBER; |
| 3995 | snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam); |
| 3996 | text = nasm_strdup(tmpbuf); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3997 | break; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3998 | case '%': |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3999 | type = TOK_ID; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4000 | snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".", |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4001 | mac->unique); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4002 | text = nasm_strcat(tmpbuf, t->text + 2); |
| 4003 | break; |
| 4004 | case '-': |
| 4005 | n = atoi(t->text + 2) - 1; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4006 | if (n >= mac->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4007 | tt = NULL; |
| 4008 | else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4009 | if (mac->nparam > 1) |
| 4010 | n = (n + mac->rotate) % mac->nparam; |
| 4011 | tt = mac->params[n]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4012 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4013 | cc = find_cc(tt); |
| 4014 | if (cc == -1) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4015 | nasm_nonfatal("macro parameter %d is not a condition code", |
| 4016 | n + 1); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4017 | text = NULL; |
| 4018 | } else { |
| 4019 | type = TOK_ID; |
| 4020 | if (inverse_ccs[cc] == -1) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4021 | nasm_nonfatal("condition code `%s' is not invertible", |
| 4022 | conditions[cc]); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4023 | text = NULL; |
| 4024 | } else |
| 4025 | text = nasm_strdup(conditions[inverse_ccs[cc]]); |
| 4026 | } |
| 4027 | break; |
| 4028 | case '+': |
| 4029 | n = atoi(t->text + 2) - 1; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4030 | if (n >= mac->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4031 | tt = NULL; |
| 4032 | else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4033 | if (mac->nparam > 1) |
| 4034 | n = (n + mac->rotate) % mac->nparam; |
| 4035 | tt = mac->params[n]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4036 | } |
| 4037 | cc = find_cc(tt); |
| 4038 | if (cc == -1) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4039 | nasm_nonfatal("macro parameter %d is not a condition code", |
| 4040 | n + 1); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4041 | text = NULL; |
| 4042 | } else { |
| 4043 | type = TOK_ID; |
| 4044 | text = nasm_strdup(conditions[cc]); |
| 4045 | } |
| 4046 | break; |
| 4047 | default: |
| 4048 | n = atoi(t->text + 1) - 1; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4049 | if (n >= mac->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4050 | tt = NULL; |
| 4051 | else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4052 | if (mac->nparam > 1) |
| 4053 | n = (n + mac->rotate) % mac->nparam; |
| 4054 | tt = mac->params[n]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4055 | } |
| 4056 | if (tt) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4057 | for (i = 0; i < mac->paramlen[n]; i++) { |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4058 | *tail = new_Token(NULL, tt->type, tt->text, 0); |
| 4059 | tail = &(*tail)->next; |
| 4060 | tt = tt->next; |
| 4061 | } |
| 4062 | } |
| 4063 | text = NULL; /* we've done it here */ |
| 4064 | break; |
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 | } else { |
| 4067 | /* |
| 4068 | * seems we have a parameters range here |
| 4069 | */ |
| 4070 | Token *head, **last; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4071 | head = expand_mmac_params_range(mac, t, &last); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4072 | if (head != t) { |
| 4073 | *tail = head; |
| 4074 | *last = tline; |
| 4075 | tline = head; |
| 4076 | text = NULL; |
| 4077 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4078 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4079 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4080 | if (!text) { |
| 4081 | delete_Token(t); |
| 4082 | } else { |
| 4083 | *tail = t; |
| 4084 | tail = &t->next; |
| 4085 | t->type = type; |
| 4086 | nasm_free(t->text); |
| 4087 | t->text = text; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4088 | t->len = strlen(text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4089 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4090 | changed = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4091 | continue; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4092 | } else if (tline->type == TOK_INDIRECT) { |
| 4093 | t = tline; |
| 4094 | tline = tline->next; |
| 4095 | tt = tokenize(t->text); |
| 4096 | tt = expand_mmac_params(tt); |
| 4097 | tt = expand_smacro(tt); |
| 4098 | *tail = tt; |
| 4099 | while (tt) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4100 | tail = &tt->next; |
| 4101 | tt = tt->next; |
| 4102 | } |
| 4103 | delete_Token(t); |
| 4104 | changed = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4105 | } else { |
| 4106 | t = *tail = tline; |
| 4107 | tline = tline->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4108 | tail = &t->next; |
| 4109 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4110 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4111 | *tail = NULL; |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 4112 | |
Cyrill Gorcunov | c6a742c | 2011-06-27 01:23:09 +0400 | [diff] [blame] | 4113 | if (changed) { |
| 4114 | const struct tokseq_match t[] = { |
| 4115 | { |
| 4116 | PP_CONCAT_MASK(TOK_ID) | |
| 4117 | PP_CONCAT_MASK(TOK_FLOAT), /* head */ |
| 4118 | PP_CONCAT_MASK(TOK_ID) | |
| 4119 | PP_CONCAT_MASK(TOK_NUMBER) | |
| 4120 | PP_CONCAT_MASK(TOK_FLOAT) | |
| 4121 | PP_CONCAT_MASK(TOK_OTHER) /* tail */ |
| 4122 | }, |
| 4123 | { |
| 4124 | PP_CONCAT_MASK(TOK_NUMBER), /* head */ |
| 4125 | PP_CONCAT_MASK(TOK_NUMBER) /* tail */ |
| 4126 | } |
| 4127 | }; |
| 4128 | paste_tokens(&thead, t, ARRAY_SIZE(t), false); |
| 4129 | } |
H. Peter Anvin | 6125b62 | 2009-04-08 14:02:25 -0700 | [diff] [blame] | 4130 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4131 | return thead; |
| 4132 | } |
| 4133 | |
| 4134 | /* |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4135 | * Expand *one* single-line macro instance. If the first token is not |
| 4136 | * a macro at all, it is simply copied to the output. tpp should be |
| 4137 | * a pointer to a pointer (usually the next pointer of the previous token) |
| 4138 | * to the first token. **tpp is updated to point to the pointer to first token |
| 4139 | * of the expansion, which is also the return value, and then *tpp will point to |
| 4140 | * the pointer to the first input token after the expansion. |
| 4141 | * |
| 4142 | * If the expansion is empty, then these two values point to the same token. |
| 4143 | * |
| 4144 | */ |
| 4145 | static Token *expand_smacro_noreset(Token * tline); |
| 4146 | |
| 4147 | static Token *expand_one_smacro(Token ***tpp) |
| 4148 | { |
| 4149 | Token **params = NULL; |
| 4150 | const char *mname; |
| 4151 | Token *tline = **tpp; |
| 4152 | Token **tnextp; |
| 4153 | SMacro *head, *m; |
| 4154 | unsigned int nparam, i; |
| 4155 | Token *expansion; |
| 4156 | Token *t, *mstart, **ttail; |
| 4157 | bool free_expansion; |
| 4158 | |
| 4159 | if (!tline) |
| 4160 | return NULL; |
| 4161 | |
| 4162 | mname = tline->text; |
| 4163 | tnextp = &tline->next; /* By default we shift out one token */ |
| 4164 | |
| 4165 | if (tline->type == TOK_ID) { |
| 4166 | head = (SMacro *)hash_findix(&smacros, mname); |
| 4167 | } else if (tline->type == TOK_PREPROC_ID) { |
| 4168 | Context *ctx = get_ctx(mname, &mname); |
| 4169 | head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL; |
| 4170 | } else { |
| 4171 | goto not_a_macro; |
| 4172 | } |
| 4173 | |
| 4174 | /* |
| 4175 | * We've hit an identifier of some sort. First check whether the |
| 4176 | * identifier is a single-line macro at all, then think about |
| 4177 | * checking for parameters if necessary. |
| 4178 | */ |
| 4179 | list_for_each(m, head) { |
| 4180 | if (!mstrcmp(m->name, mname, m->casesense)) |
| 4181 | break; |
| 4182 | } |
| 4183 | |
| 4184 | if (!m) { |
| 4185 | goto not_a_macro; |
| 4186 | } |
| 4187 | |
| 4188 | /* Parse parameters, if applicable */ |
| 4189 | |
| 4190 | params = NULL; |
| 4191 | nparam = 0; |
| 4192 | |
| 4193 | if (m->nparam == 0) { |
| 4194 | /* |
| 4195 | * Simple case: the macro is parameterless. |
| 4196 | * Nothing to parse; the expansion code will |
| 4197 | * drop the macro name token. |
| 4198 | */ |
| 4199 | } else { |
| 4200 | /* |
| 4201 | * Complicated case: at least one macro with this name |
| 4202 | * exists and takes parameters. We must find the |
| 4203 | * parameters in the call, count them, find the SMacro |
| 4204 | * that corresponds to that form of the macro call, and |
| 4205 | * substitute for the parameters when we expand. What a |
| 4206 | * pain. |
| 4207 | */ |
| 4208 | Token **pep; |
| 4209 | int paren; |
| 4210 | int white = 0; |
| 4211 | int brackets = 0; |
| 4212 | bool bracketed = false; |
| 4213 | bool bad_bracket = false; |
| 4214 | unsigned int sparam = PARAM_DELTA; |
| 4215 | |
| 4216 | tline = tline->next; |
| 4217 | |
| 4218 | while (tok_type_(tline, TOK_WHITESPACE)) { |
| 4219 | tline = tline->next; |
| 4220 | } |
| 4221 | if (!tok_is_(tline, "(")) { |
| 4222 | /* |
| 4223 | * This macro wasn't called with parameters: ignore |
| 4224 | * the call. (Behaviour borrowed from gnu cpp.) |
| 4225 | */ |
| 4226 | goto not_a_macro; |
| 4227 | } |
| 4228 | |
| 4229 | paren = 1; |
| 4230 | nparam = 0; |
| 4231 | params = nasm_malloc(sparam * sizeof *params); |
| 4232 | params[0] = NULL; |
| 4233 | pep = ¶ms[0]; |
| 4234 | |
| 4235 | while (true) { |
| 4236 | bool skip = false; |
| 4237 | char ch; |
| 4238 | |
| 4239 | if (!tline->next) { |
| 4240 | nasm_nonfatal("macro call expects terminating `)'"); |
| 4241 | break; |
| 4242 | } |
| 4243 | tline = tline->next; |
| 4244 | |
| 4245 | ch = (tline->type == TOK_OTHER && !tline->text[1]) |
| 4246 | ? tline->text[0] : 0; |
| 4247 | |
| 4248 | if (!brackets) { |
| 4249 | if (tline->type == TOK_WHITESPACE) { |
| 4250 | if (params[nparam]) { |
| 4251 | /* Keep interior whitespace */ |
| 4252 | white++; |
| 4253 | } |
| 4254 | skip = true; |
| 4255 | } |
| 4256 | |
| 4257 | switch (ch) { |
| 4258 | case ',': |
| 4259 | if (!paren) { |
| 4260 | if (++nparam >= sparam) { |
| 4261 | sparam += PARAM_DELTA; |
| 4262 | params = nasm_realloc(params, sparam * sizeof *params); |
| 4263 | } |
| 4264 | params[nparam] = NULL; |
| 4265 | pep = ¶ms[nparam]; |
| 4266 | white = 0; |
| 4267 | bracketed = false; |
| 4268 | } |
| 4269 | break; |
| 4270 | |
| 4271 | case '{': |
| 4272 | brackets++; |
| 4273 | bracketed = skip = !params[nparam]; |
| 4274 | break; |
| 4275 | |
| 4276 | case '(': |
| 4277 | paren++; |
| 4278 | break; |
| 4279 | |
| 4280 | case ')': |
| 4281 | paren--; |
| 4282 | break; |
| 4283 | |
| 4284 | default: |
| 4285 | break; /* Just advance to the next token */ |
| 4286 | } |
| 4287 | } else { |
| 4288 | if (ch == '}') |
| 4289 | brackets--; |
| 4290 | |
| 4291 | if (brackets == 0) |
| 4292 | skip = bracketed; |
| 4293 | } |
| 4294 | |
| 4295 | if (!paren) |
| 4296 | break; /* We are done */ |
| 4297 | |
| 4298 | if (!skip) { |
| 4299 | Token *t; |
| 4300 | |
| 4301 | if (bracketed && !brackets) |
| 4302 | bad_bracket = true; |
| 4303 | |
| 4304 | if (white) { |
| 4305 | t = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 4306 | *pep = t; |
| 4307 | pep = &t->next; |
| 4308 | } |
| 4309 | |
| 4310 | t = new_Token(NULL, tline->type, tline->text, 0); |
| 4311 | *pep = t; |
| 4312 | pep = &t->next; |
| 4313 | white = 0; |
| 4314 | } |
| 4315 | } |
| 4316 | nparam++; /* Count the last parameter */ |
| 4317 | |
| 4318 | if (bad_bracket) |
| 4319 | nasm_nonfatal("braces do not enclose all of macro parameter"); |
| 4320 | |
| 4321 | /* Look for a macro matching in both name and parameter count */ |
| 4322 | while (m && (m->nparam != nparam || |
| 4323 | mstrcmp(m->name, mname, m->casesense))) |
| 4324 | m = m->next; |
| 4325 | |
| 4326 | if (!m) { |
| 4327 | /*! |
| 4328 | *!macro-params [on] macro calls with wrong parameter count |
| 4329 | *! covers warnings about \i{multi-line macros} being invoked |
| 4330 | *! with the wrong number of parameters. See \k{mlmacover} for an |
| 4331 | *! example of why you might want to disable this warning. |
| 4332 | */ |
| 4333 | nasm_warn(WARN_MACRO_PARAMS, |
| 4334 | "macro `%s' exists, " |
| 4335 | "but not taking %d parameters", |
| 4336 | mname, nparam); |
| 4337 | goto not_a_macro_cleanup; |
| 4338 | } |
| 4339 | } |
| 4340 | |
| 4341 | if (m->in_progress) |
| 4342 | goto not_a_macro_cleanup; |
| 4343 | |
| 4344 | /* Expand each parameter */ |
| 4345 | m->in_progress = true; |
| 4346 | |
| 4347 | for (i = 0; i < nparam; i++) { |
| 4348 | params[i] = expand_smacro_noreset(params[i]); |
| 4349 | |
| 4350 | if (m->eval_param && m->eval_param[i]) { |
| 4351 | /* Evaluate this parameter as a number */ |
| 4352 | struct ppscan pps; |
| 4353 | struct tokenval tokval; |
| 4354 | expr *evalresult; |
| 4355 | |
| 4356 | pps.tptr = params[i]; |
| 4357 | pps.ntokens = -1; |
| 4358 | tokval.t_type = TOKEN_INVALID; |
| 4359 | evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL); |
| 4360 | |
| 4361 | if (!evalresult) { |
| 4362 | /* Nothing meaningful to do */ |
| 4363 | } else if (tokval.t_type) { |
| 4364 | nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name); |
| 4365 | } else if (!is_simple(evalresult)) { |
| 4366 | nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name); |
| 4367 | } else { |
| 4368 | free_tlist(params[i]); |
| 4369 | params[i] = make_tok_num(reloc_value(evalresult)); |
| 4370 | } |
| 4371 | } |
| 4372 | |
| 4373 | /* Put tokens in reverse order like the expansion list */ |
| 4374 | params[i] = reverse_tokens(params[i]); |
| 4375 | } |
| 4376 | |
| 4377 | t = tline; |
| 4378 | tline = tline->next; |
| 4379 | t->next = NULL; /* Remove the macro call from the input */ |
| 4380 | |
| 4381 | if (unlikely(m->magic)) { |
| 4382 | expansion = m->magic(m, params, nparam, &free_expansion); |
| 4383 | } else { |
| 4384 | expansion = m->expansion; |
| 4385 | free_expansion = false; |
| 4386 | } |
| 4387 | |
| 4388 | ttail = NULL; |
| 4389 | |
| 4390 | list_for_each(t, expansion) { |
| 4391 | if (is_smac_param(t->type)) { |
| 4392 | Token *ttt; |
| 4393 | list_for_each(ttt, params[smac_nparam(t->type)]) { |
| 4394 | tline = dup_Token(tline, ttt); |
| 4395 | if (!ttail) |
| 4396 | ttail = &tline->next; |
| 4397 | } |
| 4398 | } else { |
| 4399 | if (t->type == TOK_PREPROC_Q) { |
| 4400 | tline = new_Token(tline, TOK_ID, mname, 0); |
| 4401 | } else if (t->type == TOK_PREPROC_QQ) { |
| 4402 | tline = new_Token(tline, TOK_ID, m->name, 0); |
| 4403 | } else { |
| 4404 | tline = dup_Token(tline, t); |
| 4405 | } |
| 4406 | if (!ttail) |
| 4407 | ttail = &tline->next; |
| 4408 | } |
| 4409 | } |
| 4410 | |
| 4411 | if (free_expansion) |
| 4412 | free_tlist(expansion); |
| 4413 | |
| 4414 | m->in_progress = false; |
| 4415 | |
| 4416 | /* Don't do this until after expansion or we will clobber mname */ |
| 4417 | mstart = **tpp; |
| 4418 | tnextp = ttail ? ttail : *tpp; |
| 4419 | **tpp = tline; |
| 4420 | free_tlist(mstart); |
| 4421 | |
| 4422 | /* |
| 4423 | * No macro expansion needed; roll back to mstart (if necessary) |
| 4424 | * and then advance to the next input token. |
| 4425 | */ |
| 4426 | not_a_macro_cleanup: |
| 4427 | nasm_free(params); |
| 4428 | |
| 4429 | not_a_macro: |
| 4430 | t = **tpp; |
| 4431 | *tpp = tnextp; |
| 4432 | return t; |
| 4433 | } |
| 4434 | |
| 4435 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4436 | * Expand all single-line macro calls made in the given line. |
| 4437 | * Return the expanded version of the line. The original is deemed |
| 4438 | * to be destroyed in the process. (In reality we'll just move |
| 4439 | * Tokens from input to output a lot of the time, rather than |
| 4440 | * actually bothering to destroy and replicate.) |
| 4441 | */ |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4442 | static int64_t smacro_deadman; |
| 4443 | static Token *expand_smacro(Token *tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4444 | { |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4445 | smacro_deadman = nasm_limit[LIMIT_MACROS]; |
| 4446 | return expand_smacro_noreset(tline); |
| 4447 | } |
| 4448 | |
| 4449 | static Token *expand_smacro_noreset(Token * tline) |
| 4450 | { |
| 4451 | Token *t, **tail, *thead; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4452 | Token *org_tline = tline; |
H. Peter Anvin | 8287daf | 2009-07-07 16:00:58 -0700 | [diff] [blame] | 4453 | bool expanded; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4454 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4455 | /* |
| 4456 | * Trick: we should avoid changing the start token pointer since it can |
| 4457 | * be contained in "next" field of other token. Because of this |
| 4458 | * we allocate a copy of first token and work with it; at the end of |
| 4459 | * routine we copy it back |
| 4460 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4461 | if (org_tline) { |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4462 | tline = new_Token(org_tline->next, org_tline->type, |
| 4463 | org_tline->text, 0); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4464 | nasm_free(org_tline->text); |
| 4465 | org_tline->text = NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4466 | } |
| 4467 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4468 | expanded = true; /* Always expand %+ at least once */ |
H. Peter Anvin | 8287daf | 2009-07-07 16:00:58 -0700 | [diff] [blame] | 4469 | |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4470 | again: |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4471 | thead = tline; |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4472 | tail = &thead; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4473 | |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4474 | while ((t = *tail)) { /* main token loop */ |
| 4475 | if (!--smacro_deadman) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4476 | nasm_nonfatal("interminable macro recursion"); |
Cyrill Gorcunov | bd38c8f | 2009-11-21 11:11:23 +0300 | [diff] [blame] | 4477 | goto err; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4478 | } |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4479 | |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4480 | /* |
| 4481 | * An expansion happened if and only if the first token of the |
| 4482 | * expansion was the first token of the original simply moved over. |
| 4483 | */ |
| 4484 | expanded |= (expand_one_smacro(&tail) != t); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4485 | } |
| 4486 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4487 | /* |
| 4488 | * Now scan the entire line and look for successive TOK_IDs that resulted |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 4489 | * after expansion (they can't be produced by tokenize()). The successive |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4490 | * TOK_IDs should be concatenated. |
| 4491 | * Also we look for %+ tokens and concatenate the tokens before and after |
| 4492 | * them (without white spaces in between). |
| 4493 | */ |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4494 | if (expanded) { |
Cyrill Gorcunov | c6a742c | 2011-06-27 01:23:09 +0400 | [diff] [blame] | 4495 | const struct tokseq_match t[] = { |
| 4496 | { |
| 4497 | PP_CONCAT_MASK(TOK_ID) | |
| 4498 | PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */ |
| 4499 | PP_CONCAT_MASK(TOK_ID) | |
| 4500 | PP_CONCAT_MASK(TOK_PREPROC_ID) | |
| 4501 | PP_CONCAT_MASK(TOK_NUMBER) /* tail */ |
| 4502 | } |
| 4503 | }; |
| 4504 | if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) { |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4505 | /* |
| 4506 | * If we concatenated something, *and* we had previously expanded |
| 4507 | * an actual macro, scan the lines again for macros... |
| 4508 | */ |
| 4509 | tline = thead; |
| 4510 | expanded = false; |
| 4511 | goto again; |
| 4512 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4513 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4514 | |
Cyrill Gorcunov | bd38c8f | 2009-11-21 11:11:23 +0300 | [diff] [blame] | 4515 | err: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4516 | if (org_tline) { |
| 4517 | if (thead) { |
| 4518 | *org_tline = *thead; |
| 4519 | /* since we just gave text to org_line, don't free it */ |
| 4520 | thead->text = NULL; |
| 4521 | delete_Token(thead); |
| 4522 | } else { |
| 4523 | /* the expression expanded to empty line; |
| 4524 | we can't return NULL for some reasons |
| 4525 | we just set the line to a single WHITESPACE token. */ |
| 4526 | memset(org_tline, 0, sizeof(*org_tline)); |
| 4527 | org_tline->text = NULL; |
| 4528 | org_tline->type = TOK_WHITESPACE; |
| 4529 | } |
| 4530 | thead = org_tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4531 | } |
| 4532 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4533 | return thead; |
| 4534 | } |
| 4535 | |
| 4536 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4537 | * Similar to expand_smacro but used exclusively with macro identifiers |
| 4538 | * right before they are fetched in. The reason is that there can be |
| 4539 | * identifiers consisting of several subparts. We consider that if there |
| 4540 | * are more than one element forming the name, user wants a expansion, |
| 4541 | * otherwise it will be left as-is. Example: |
| 4542 | * |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4543 | * %define %$abc cde |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4544 | * |
| 4545 | * the identifier %$abc will be left as-is so that the handler for %define |
| 4546 | * will suck it and define the corresponding value. Other case: |
| 4547 | * |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4548 | * %define _%$abc cde |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4549 | * |
| 4550 | * In this case user wants name to be expanded *before* %define starts |
| 4551 | * working, so we'll expand %$abc into something (if it has a value; |
| 4552 | * otherwise it will be left as-is) then concatenate all successive |
| 4553 | * PP_IDs into one. |
| 4554 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4555 | static Token *expand_id(Token * tline) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4556 | { |
| 4557 | Token *cur, *oldnext = NULL; |
| 4558 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4559 | if (!tline || !tline->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4560 | return tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4561 | |
| 4562 | cur = tline; |
| 4563 | while (cur->next && |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4564 | (cur->next->type == TOK_ID || |
| 4565 | cur->next->type == TOK_PREPROC_ID |
| 4566 | || cur->next->type == TOK_NUMBER)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4567 | cur = cur->next; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4568 | |
| 4569 | /* If identifier consists of just one token, don't expand */ |
| 4570 | if (cur == tline) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4571 | return tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4572 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4573 | if (cur) { |
| 4574 | oldnext = cur->next; /* Detach the tail past identifier */ |
| 4575 | cur->next = NULL; /* so that expand_smacro stops here */ |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4576 | } |
| 4577 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4578 | tline = expand_smacro(tline); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4579 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4580 | if (cur) { |
| 4581 | /* expand_smacro possibly changhed tline; re-scan for EOL */ |
| 4582 | cur = tline; |
| 4583 | while (cur && cur->next) |
| 4584 | cur = cur->next; |
| 4585 | if (cur) |
| 4586 | cur->next = oldnext; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4587 | } |
| 4588 | |
| 4589 | return tline; |
| 4590 | } |
| 4591 | |
| 4592 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4593 | * Determine whether the given line constitutes a multi-line macro |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4594 | * 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] | 4595 | * to check for an initial label - that's taken care of in |
| 4596 | * expand_mmacro - but must check numbers of parameters. Guaranteed |
| 4597 | * to be called with tline->type == TOK_ID, so the putative macro |
| 4598 | * name is easy to find. |
| 4599 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4600 | static MMacro *is_mmacro(Token * tline, Token *** params_array) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4601 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4602 | MMacro *head, *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4603 | Token **params; |
| 4604 | int nparam; |
| 4605 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4606 | head = (MMacro *) hash_findix(&mmacros, tline->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4607 | |
| 4608 | /* |
| 4609 | * Efficiency: first we see if any macro exists with the given |
| 4610 | * name. If not, we can return NULL immediately. _Then_ we |
| 4611 | * count the parameters, and then we look further along the |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4612 | * list if necessary to find the proper MMacro. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4613 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4614 | list_for_each(m, head) |
| 4615 | if (!mstrcmp(m->name, tline->text, m->casesense)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4616 | break; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4617 | if (!m) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4618 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4619 | |
| 4620 | /* |
| 4621 | * OK, we have a potential macro. Count and demarcate the |
| 4622 | * parameters. |
| 4623 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4624 | count_mmac_params(tline->next, &nparam, ¶ms); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4625 | |
| 4626 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4627 | * 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] | 4628 | * structure that handles this number. |
| 4629 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4630 | while (m) { |
| 4631 | if (m->nparam_min <= nparam |
| 4632 | && (m->plus || nparam <= m->nparam_max)) { |
| 4633 | /* |
| 4634 | * This one is right. Just check if cycle removal |
| 4635 | * prohibits us using it before we actually celebrate... |
| 4636 | */ |
| 4637 | if (m->in_progress > m->max_depth) { |
| 4638 | if (m->max_depth > 0) { |
H. Peter Anvin (Intel) | 80c4f23 | 2018-12-14 13:33:24 -0800 | [diff] [blame] | 4639 | nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i", |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4640 | m->max_depth); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4641 | } |
| 4642 | nasm_free(params); |
| 4643 | return NULL; |
| 4644 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4645 | /* |
| 4646 | * It's right, and we can use it. Add its default |
| 4647 | * parameters to the end of our list if necessary. |
| 4648 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4649 | if (m->defaults && nparam < m->nparam_min + m->ndefs) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4650 | params = |
| 4651 | nasm_realloc(params, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4652 | ((m->nparam_min + m->ndefs + |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4653 | 1) * sizeof(*params))); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4654 | while (nparam < m->nparam_min + m->ndefs) { |
| 4655 | params[nparam] = m->defaults[nparam - m->nparam_min]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4656 | nparam++; |
| 4657 | } |
| 4658 | } |
| 4659 | /* |
| 4660 | * If we've gone over the maximum parameter count (and |
| 4661 | * we're in Plus mode), ignore parameters beyond |
| 4662 | * nparam_max. |
| 4663 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4664 | if (m->plus && nparam > m->nparam_max) |
| 4665 | nparam = m->nparam_max; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4666 | /* |
| 4667 | * Then terminate the parameter list, and leave. |
| 4668 | */ |
| 4669 | if (!params) { /* need this special case */ |
| 4670 | params = nasm_malloc(sizeof(*params)); |
| 4671 | nparam = 0; |
| 4672 | } |
| 4673 | params[nparam] = NULL; |
| 4674 | *params_array = params; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4675 | return m; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4676 | } |
| 4677 | /* |
| 4678 | * This one wasn't right: look for the next one with the |
| 4679 | * same name. |
| 4680 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4681 | list_for_each(m, m->next) |
| 4682 | if (!mstrcmp(m->name, tline->text, m->casesense)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4683 | break; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4684 | } |
| 4685 | |
| 4686 | /* |
| 4687 | * After all that, we didn't find one with the right number of |
| 4688 | * parameters. Issue a warning, and fail to expand the macro. |
| 4689 | */ |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 4690 | nasm_warn(WARN_MACRO_PARAMS, |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4691 | "macro `%s' exists, but not taking %d parameters", |
| 4692 | tline->text, nparam); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4693 | nasm_free(params); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4694 | return NULL; |
| 4695 | } |
| 4696 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4697 | |
| 4698 | /* |
| 4699 | * Save MMacro invocation specific fields in |
| 4700 | * preparation for a recursive macro expansion |
| 4701 | */ |
| 4702 | static void push_mmacro(MMacro *m) |
| 4703 | { |
| 4704 | MMacroInvocation *i; |
| 4705 | |
| 4706 | i = nasm_malloc(sizeof(MMacroInvocation)); |
| 4707 | i->prev = m->prev; |
| 4708 | i->params = m->params; |
| 4709 | i->iline = m->iline; |
| 4710 | i->nparam = m->nparam; |
| 4711 | i->rotate = m->rotate; |
| 4712 | i->paramlen = m->paramlen; |
| 4713 | i->unique = m->unique; |
| 4714 | i->condcnt = m->condcnt; |
| 4715 | m->prev = i; |
| 4716 | } |
| 4717 | |
| 4718 | |
| 4719 | /* |
| 4720 | * Restore MMacro invocation specific fields that were |
| 4721 | * saved during a previous recursive macro expansion |
| 4722 | */ |
| 4723 | static void pop_mmacro(MMacro *m) |
| 4724 | { |
| 4725 | MMacroInvocation *i; |
| 4726 | |
| 4727 | if (m->prev) { |
| 4728 | i = m->prev; |
| 4729 | m->prev = i->prev; |
| 4730 | m->params = i->params; |
| 4731 | m->iline = i->iline; |
| 4732 | m->nparam = i->nparam; |
| 4733 | m->rotate = i->rotate; |
| 4734 | m->paramlen = i->paramlen; |
| 4735 | m->unique = i->unique; |
| 4736 | m->condcnt = i->condcnt; |
| 4737 | nasm_free(i); |
| 4738 | } |
| 4739 | } |
| 4740 | |
| 4741 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4742 | /* |
| 4743 | * Expand the multi-line macro call made by the given line, if |
| 4744 | * 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] | 4745 | * istk->expansion and return 1. Otherwise return 0. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4746 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4747 | static int expand_mmacro(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4748 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4749 | Token *startline = tline; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4750 | Token *label = NULL; |
| 4751 | int dont_prepend = 0; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4752 | Token **params, *t, *tt; |
| 4753 | MMacro *m; |
| 4754 | Line *l, *ll; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4755 | int i, nparam, *paramlen; |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 4756 | const char *mname; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4757 | |
| 4758 | t = tline; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4759 | skip_white_(t); |
H. Peter Anvin | ce2233b | 2008-05-25 21:57:00 -0700 | [diff] [blame] | 4760 | /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */ |
H. Peter Anvin | dce1e2f | 2002-04-30 21:06:37 +0000 | [diff] [blame] | 4761 | 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] | 4762 | return 0; |
| 4763 | m = is_mmacro(t, ¶ms); |
| 4764 | if (m) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4765 | mname = t->text; |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 4766 | } else { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4767 | Token *last; |
| 4768 | /* |
| 4769 | * We have an id which isn't a macro call. We'll assume |
| 4770 | * it might be a label; we'll also check to see if a |
| 4771 | * colon follows it. Then, if there's another id after |
| 4772 | * that lot, we'll check it again for macro-hood. |
| 4773 | */ |
| 4774 | label = last = t; |
| 4775 | t = t->next; |
| 4776 | if (tok_type_(t, TOK_WHITESPACE)) |
| 4777 | last = t, t = t->next; |
| 4778 | if (tok_is_(t, ":")) { |
| 4779 | dont_prepend = 1; |
| 4780 | last = t, t = t->next; |
| 4781 | if (tok_type_(t, TOK_WHITESPACE)) |
| 4782 | last = t, t = t->next; |
| 4783 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4784 | if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, ¶ms))) |
| 4785 | return 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4786 | last->next = NULL; |
Keith Kanios | 891775e | 2009-07-11 06:08:54 -0500 | [diff] [blame] | 4787 | mname = t->text; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4788 | tline = t; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4789 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4790 | |
| 4791 | /* |
| 4792 | * Fix up the parameters: this involves stripping leading and |
| 4793 | * trailing whitespace, then stripping braces if they are |
| 4794 | * present. |
| 4795 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4796 | for (nparam = 0; params[nparam]; nparam++) ; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4797 | paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4798 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4799 | for (i = 0; params[i]; i++) { |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4800 | int brace = 0; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4801 | int comma = (!m->plus || i < nparam - 1); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4802 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4803 | t = params[i]; |
| 4804 | skip_white_(t); |
| 4805 | if (tok_is_(t, "{")) |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4806 | t = t->next, brace++, comma = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4807 | params[i] = t; |
| 4808 | paramlen[i] = 0; |
| 4809 | while (t) { |
| 4810 | if (comma && t->type == TOK_OTHER && !strcmp(t->text, ",")) |
| 4811 | break; /* ... because we have hit a comma */ |
| 4812 | if (comma && t->type == TOK_WHITESPACE |
| 4813 | && tok_is_(t->next, ",")) |
| 4814 | break; /* ... or a space then a comma */ |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4815 | if (brace && t->type == TOK_OTHER) { |
| 4816 | if (t->text[0] == '{') |
| 4817 | brace++; /* ... or a nested opening brace */ |
| 4818 | else if (t->text[0] == '}') |
| 4819 | if (!--brace) |
| 4820 | break; /* ... or a brace */ |
| 4821 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4822 | t = t->next; |
| 4823 | paramlen[i]++; |
| 4824 | } |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4825 | if (brace) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4826 | nasm_nonfatal("macro params should be enclosed in braces"); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4827 | } |
| 4828 | |
| 4829 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4830 | * OK, we have a MMacro structure together with a set of |
| 4831 | * parameters. We must now go through the expansion and push |
| 4832 | * copies of each Line on to istk->expansion. Substitution of |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4833 | * parameter tokens and macro-local tokens doesn't get done |
| 4834 | * until the single-line macro substitution process; this is |
| 4835 | * because delaying them allows us to change the semantics |
| 4836 | * later through %rotate. |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4837 | * |
| 4838 | * First, push an end marker on to istk->expansion, mark this |
| 4839 | * macro as in progress, and set up its invocation-specific |
| 4840 | * variables. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4841 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4842 | ll = nasm_malloc(sizeof(Line)); |
| 4843 | ll->next = istk->expansion; |
| 4844 | ll->finishes = m; |
| 4845 | ll->first = NULL; |
| 4846 | istk->expansion = ll; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4847 | |
| 4848 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4849 | * Save the previous MMacro expansion in the case of |
| 4850 | * macro recursion |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4851 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4852 | if (m->max_depth && m->in_progress) |
| 4853 | push_mmacro(m); |
| 4854 | |
| 4855 | m->in_progress ++; |
| 4856 | m->params = params; |
| 4857 | m->iline = tline; |
| 4858 | m->nparam = nparam; |
| 4859 | m->rotate = 0; |
| 4860 | m->paramlen = paramlen; |
| 4861 | m->unique = unique++; |
| 4862 | m->lineno = 0; |
| 4863 | m->condcnt = 0; |
| 4864 | |
| 4865 | m->next_active = istk->mstk; |
| 4866 | istk->mstk = m; |
| 4867 | |
| 4868 | list_for_each(l, m->expansion) { |
| 4869 | Token **tail; |
| 4870 | |
| 4871 | ll = nasm_malloc(sizeof(Line)); |
| 4872 | ll->finishes = NULL; |
| 4873 | ll->next = istk->expansion; |
| 4874 | istk->expansion = ll; |
| 4875 | tail = &ll->first; |
| 4876 | |
| 4877 | list_for_each(t, l->first) { |
| 4878 | Token *x = t; |
| 4879 | switch (t->type) { |
| 4880 | case TOK_PREPROC_Q: |
| 4881 | tt = *tail = new_Token(NULL, TOK_ID, mname, 0); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4882 | break; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4883 | case TOK_PREPROC_QQ: |
| 4884 | tt = *tail = new_Token(NULL, TOK_ID, m->name, 0); |
| 4885 | break; |
| 4886 | case TOK_PREPROC_ID: |
| 4887 | if (t->text[1] == '0' && t->text[2] == '0') { |
| 4888 | dont_prepend = -1; |
| 4889 | x = label; |
| 4890 | if (!x) |
| 4891 | continue; |
| 4892 | } |
| 4893 | /* fall through */ |
| 4894 | default: |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 4895 | tt = *tail = dup_Token(NULL, x); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4896 | break; |
| 4897 | } |
| 4898 | tail = &tt->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4899 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4900 | *tail = NULL; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4901 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4902 | |
| 4903 | /* |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4904 | * If we had a label, push it on as the first line of |
| 4905 | * the macro expansion. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4906 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4907 | if (label) { |
| 4908 | if (dont_prepend < 0) |
| 4909 | free_tlist(startline); |
| 4910 | else { |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 4911 | nasm_new(ll); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4912 | ll->finishes = NULL; |
| 4913 | ll->next = istk->expansion; |
| 4914 | istk->expansion = ll; |
| 4915 | ll->first = startline; |
| 4916 | if (!dont_prepend) { |
| 4917 | while (label->next) |
| 4918 | label = label->next; |
| 4919 | label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4920 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4921 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4922 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4923 | |
H. Peter Anvin | 0d4d431 | 2019-08-07 00:46:27 -0700 | [diff] [blame] | 4924 | lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4925 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4926 | return 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4927 | } |
| 4928 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4929 | /* |
| 4930 | * This function adds macro names to error messages, and suppresses |
| 4931 | * them if necessary. |
| 4932 | */ |
H. Peter Anvin (Intel) | 6bde2ed | 2018-12-13 19:39:41 -0800 | [diff] [blame] | 4933 | 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] | 4934 | { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4935 | char buff[BUFSIZ]; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4936 | MMacro *mmac = NULL; |
| 4937 | int delta = 0; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4938 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4939 | /* |
| 4940 | * If we're in a dead branch of IF or something like it, ignore the error. |
| 4941 | * However, because %else etc are evaluated in the state context |
| 4942 | * of the previous branch, errors might get lost: |
| 4943 | * %if 0 ... %else trailing garbage ... %endif |
| 4944 | * So %else etc should set the ERR_PP_PRECOND flag. |
| 4945 | */ |
| 4946 | if ((severity & ERR_MASK) < ERR_FATAL && |
| 4947 | istk && istk->conds && |
| 4948 | ((severity & ERR_PP_PRECOND) ? |
| 4949 | istk->conds->state == COND_NEVER : |
H. Peter Anvin | eb6653f | 2016-04-05 13:03:10 -0700 | [diff] [blame] | 4950 | !emitting(istk->conds->state))) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4951 | return; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4952 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4953 | /* get %macro name */ |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4954 | if (!(severity & ERR_NOFILE) && istk && istk->mstk) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4955 | mmac = istk->mstk; |
| 4956 | /* but %rep blocks should be skipped */ |
| 4957 | while (mmac && !mmac->name) |
| 4958 | mmac = mmac->next_active, delta++; |
Cyrill Gorcunov | 9900c6b | 2011-10-09 18:58:46 +0400 | [diff] [blame] | 4959 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4960 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4961 | if (mmac) { |
| 4962 | vsnprintf(buff, sizeof(buff), fmt, arg); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4963 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4964 | nasm_set_verror(real_verror); |
| 4965 | nasm_error(severity, "(%s:%d) %s", |
| 4966 | mmac->name, mmac->lineno - delta, buff); |
| 4967 | nasm_set_verror(pp_verror); |
| 4968 | } else { |
| 4969 | real_verror(severity, fmt, arg); |
| 4970 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4971 | } |
| 4972 | |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4973 | static Token *stdmac_file(const SMacro *s, Token **params, |
| 4974 | unsigned int nparams, bool *free_expansion) |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 4975 | { |
| 4976 | (void)s; |
| 4977 | (void)params; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4978 | (void)nparams; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 4979 | |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4980 | *free_expansion = true; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 4981 | return make_tok_qstr(src_get_fname()); |
| 4982 | } |
| 4983 | |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4984 | static Token *stdmac_line(const SMacro *s, Token **params, |
| 4985 | unsigned int nparams, bool *free_expansion) |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 4986 | { |
| 4987 | (void)s; |
| 4988 | (void)params; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4989 | (void)nparams; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 4990 | |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4991 | *free_expansion = true; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 4992 | return make_tok_num(src_get_linnum()); |
| 4993 | } |
| 4994 | |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 4995 | static Token *stdmac_bits(const SMacro *s, Token **params, |
| 4996 | unsigned int nparams, bool *free_expansion) |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 4997 | { |
| 4998 | (void)s; |
| 4999 | (void)params; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 5000 | (void)nparams; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5001 | |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 5002 | *free_expansion = true; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5003 | return make_tok_num(globalbits); |
| 5004 | } |
| 5005 | |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 5006 | static Token *stdmac_ptr(const SMacro *s, Token **params, |
| 5007 | unsigned int nparams, bool *free_expansion) |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5008 | { |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 5009 | const Token *t; |
Chang S. Bae | fea2269 | 2019-05-28 12:25:16 -0700 | [diff] [blame] | 5010 | static const Token ptr_word = { NULL, "word", 4, TOK_ID }; |
| 5011 | static const Token ptr_dword = { NULL, "dword", 5, TOK_ID }; |
| 5012 | static const Token ptr_qword = { NULL, "qword", 5, TOK_ID }; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5013 | |
| 5014 | (void)s; |
| 5015 | (void)params; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 5016 | (void)nparams; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5017 | |
| 5018 | switch (globalbits) { |
| 5019 | case 16: |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 5020 | t = &ptr_word; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5021 | break; |
| 5022 | case 32: |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 5023 | t = &ptr_dword; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5024 | break; |
| 5025 | case 64: |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 5026 | t = &ptr_qword; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5027 | break; |
| 5028 | default: |
| 5029 | panic(); |
| 5030 | } |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 5031 | |
| 5032 | *free_expansion = false; |
| 5033 | return (Token *)t; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5034 | } |
| 5035 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5036 | /* Add magic standard macros */ |
| 5037 | struct magic_macros { |
| 5038 | const char *name; |
| 5039 | int nparams; |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 5040 | MagicSMacro func; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5041 | }; |
| 5042 | static const struct magic_macros magic_macros[] = |
| 5043 | { |
| 5044 | { "__FILE__", 0, stdmac_file }, |
| 5045 | { "__LINE__", 0, stdmac_line }, |
| 5046 | { "__BITS__", 0, stdmac_bits }, |
| 5047 | { "__PTR__", 0, stdmac_ptr }, |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5048 | { NULL, 0, NULL } |
| 5049 | }; |
| 5050 | |
| 5051 | static void pp_add_magic_stdmac(void) |
| 5052 | { |
| 5053 | const struct magic_macros *m; |
| 5054 | SMacro *s; |
| 5055 | |
| 5056 | for (m = magic_macros; m->name; m++) { |
| 5057 | s = define_smacro(NULL, m->name, true, m->nparams, NULL); |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 5058 | s->magic = m->func; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5059 | } |
| 5060 | } |
| 5061 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5062 | static void |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5063 | 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] | 5064 | { |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5065 | int apass; |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 5066 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5067 | cstk = NULL; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5068 | nasm_new(istk); |
H. Peter Anvin | 3e83cec | 2016-05-25 04:28:46 -0700 | [diff] [blame] | 5069 | istk->fp = nasm_open_read(file, NF_TEXT); |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 5070 | src_set(0, file); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5071 | istk->lineinc = 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5072 | if (!istk->fp) |
Cyrill Gorcunov | c3527dd | 2018-11-24 22:17:47 +0300 | [diff] [blame] | 5073 | nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5074 | defining = NULL; |
Charles Crayne | d4200be | 2008-07-12 16:42:33 -0700 | [diff] [blame] | 5075 | nested_mac_count = 0; |
| 5076 | nested_rep_count = 0; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 5077 | init_macros(); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5078 | unique = 0; |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 5079 | deplist = dep_list; |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5080 | pp_mode = mode; |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 5081 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5082 | pp_add_magic_stdmac(); |
| 5083 | |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 5084 | if (tasm_compatible_mode) |
| 5085 | pp_add_stdmac(nasm_stdmac_tasm); |
| 5086 | |
| 5087 | pp_add_stdmac(nasm_stdmac_nasm); |
| 5088 | pp_add_stdmac(nasm_stdmac_version); |
| 5089 | |
Cyrill Gorcunov | 15ce78f | 2017-01-06 20:21:28 +0300 | [diff] [blame] | 5090 | if (extrastdmac) |
| 5091 | pp_add_stdmac(extrastdmac); |
| 5092 | |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 5093 | stdmacpos = stdmacros[0]; |
| 5094 | stdmacnext = &stdmacros[1]; |
| 5095 | |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 5096 | do_predef = true; |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 5097 | |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 5098 | strlist_add(deplist, file); |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 5099 | |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 5100 | /* |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 5101 | * Define the __PASS__ macro. This is defined here unlike all the |
| 5102 | * other builtins, because it is special -- it varies between |
| 5103 | * passes -- but there is really no particular reason to make it |
| 5104 | * magic. |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5105 | * |
| 5106 | * 0 = dependencies only |
| 5107 | * 1 = preparatory passes |
| 5108 | * 2 = final pass |
| 5109 | * 3 = preproces only |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 5110 | */ |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5111 | switch (mode) { |
| 5112 | case PP_NORMAL: |
| 5113 | apass = pass_final() ? 2 : 1; |
| 5114 | break; |
| 5115 | case PP_DEPS: |
| 5116 | apass = 0; |
| 5117 | break; |
| 5118 | case PP_PREPROC: |
| 5119 | apass = 3; |
| 5120 | break; |
| 5121 | default: |
| 5122 | panic(); |
| 5123 | } |
| 5124 | |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame] | 5125 | define_smacro(NULL, "__PASS__", true, 0, make_tok_num(apass)); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5126 | } |
| 5127 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 5128 | static void pp_init(void) |
| 5129 | { |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 5130 | } |
| 5131 | |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5132 | /* |
| 5133 | * Get a line of tokens. If we popped the macro expansion/include stack, |
| 5134 | * we return a pointer to the dummy token tok_pop; at that point if |
| 5135 | * istk is NULL then we have reached end of input; |
| 5136 | */ |
| 5137 | static Token tok_pop; /* Dummy token placeholder */ |
| 5138 | |
| 5139 | static Token *pp_tokline(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5140 | { |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5141 | Token *tline, *dtline; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5142 | |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5143 | while (true) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5144 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5145 | * Fetch a tokenized line, either from the macro-expansion |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5146 | * buffer or from the input file. |
| 5147 | */ |
| 5148 | tline = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5149 | while (istk->expansion && istk->expansion->finishes) { |
| 5150 | Line *l = istk->expansion; |
| 5151 | if (!l->finishes->name && l->finishes->in_progress > 1) { |
| 5152 | Line *ll; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5153 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5154 | /* |
| 5155 | * This is a macro-end marker for a macro with no |
| 5156 | * name, which means it's not really a macro at all |
| 5157 | * but a %rep block, and the `in_progress' field is |
| 5158 | * more than 1, meaning that we still need to |
| 5159 | * repeat. (1 means the natural last repetition; 0 |
| 5160 | * means termination by %exitrep.) We have |
| 5161 | * therefore expanded up to the %endrep, and must |
| 5162 | * push the whole block on to the expansion buffer |
| 5163 | * again. We don't bother to remove the macro-end |
| 5164 | * marker: we'd only have to generate another one |
| 5165 | * if we did. |
| 5166 | */ |
| 5167 | l->finishes->in_progress--; |
| 5168 | list_for_each(l, l->finishes->expansion) { |
| 5169 | Token *t, *tt, **tail; |
| 5170 | |
| 5171 | ll = nasm_malloc(sizeof(Line)); |
| 5172 | ll->next = istk->expansion; |
| 5173 | ll->finishes = NULL; |
| 5174 | ll->first = NULL; |
| 5175 | tail = &ll->first; |
| 5176 | |
| 5177 | list_for_each(t, l->first) { |
| 5178 | if (t->text || t->type == TOK_WHITESPACE) { |
| 5179 | tt = *tail = new_Token(NULL, t->type, t->text, 0); |
| 5180 | tail = &tt->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5181 | } |
| 5182 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5183 | istk->expansion = ll; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5184 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5185 | } else { |
| 5186 | /* |
| 5187 | * Check whether a `%rep' was started and not ended |
| 5188 | * within this macro expansion. This can happen and |
| 5189 | * should be detected. It's a fatal error because |
| 5190 | * I'm too confused to work out how to recover |
| 5191 | * sensibly from it. |
| 5192 | */ |
| 5193 | if (defining) { |
| 5194 | if (defining->name) |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 5195 | nasm_panic("defining with name in expansion"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5196 | else if (istk->mstk->name) |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 5197 | nasm_fatal("`%%rep' without `%%endrep' within" |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5198 | " expansion of macro `%s'", |
| 5199 | istk->mstk->name); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5200 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5201 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5202 | /* |
| 5203 | * FIXME: investigate the relationship at this point between |
| 5204 | * istk->mstk and l->finishes |
| 5205 | */ |
| 5206 | { |
| 5207 | MMacro *m = istk->mstk; |
| 5208 | istk->mstk = m->next_active; |
| 5209 | if (m->name) { |
| 5210 | /* |
| 5211 | * This was a real macro call, not a %rep, and |
| 5212 | * therefore the parameter information needs to |
| 5213 | * be freed. |
| 5214 | */ |
| 5215 | if (m->prev) { |
| 5216 | pop_mmacro(m); |
| 5217 | l->finishes->in_progress --; |
| 5218 | } else { |
| 5219 | nasm_free(m->params); |
| 5220 | free_tlist(m->iline); |
| 5221 | nasm_free(m->paramlen); |
| 5222 | l->finishes->in_progress = 0; |
| 5223 | } |
Adam Majer | 91e7240 | 2017-07-25 10:42:01 +0200 | [diff] [blame] | 5224 | } |
| 5225 | |
| 5226 | /* |
| 5227 | * FIXME It is incorrect to always free_mmacro here. |
| 5228 | * It leads to usage-after-free. |
| 5229 | * |
| 5230 | * https://bugzilla.nasm.us/show_bug.cgi?id=3392414 |
| 5231 | */ |
| 5232 | #if 0 |
| 5233 | else |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5234 | free_mmacro(m); |
Adam Majer | 91e7240 | 2017-07-25 10:42:01 +0200 | [diff] [blame] | 5235 | #endif |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5236 | } |
| 5237 | istk->expansion = l->next; |
| 5238 | nasm_free(l); |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 5239 | lfmt->downlevel(LIST_MACRO); |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5240 | return &tok_pop; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5241 | } |
| 5242 | } |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5243 | do { /* until we get a line we can use */ |
| 5244 | char *line; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5245 | |
| 5246 | if (istk->expansion) { /* from a macro expansion */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5247 | Line *l = istk->expansion; |
| 5248 | if (istk->mstk) |
| 5249 | istk->mstk->lineno++; |
| 5250 | tline = l->first; |
| 5251 | istk->expansion = l->next; |
| 5252 | nasm_free(l); |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5253 | line = detoken(tline, false); |
| 5254 | lfmt->line(LIST_MACRO, line); |
| 5255 | nasm_free(line); |
| 5256 | } else if ((line = read_line())) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5257 | line = prepreproc(line); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5258 | tline = tokenize(line); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5259 | nasm_free(line); |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5260 | } else { |
| 5261 | /* |
| 5262 | * The current file has ended; work down the istk |
| 5263 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5264 | Include *i = istk; |
| 5265 | fclose(i->fp); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5266 | if (i->conds) { |
| 5267 | /* nasm_error can't be conditionally suppressed */ |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 5268 | nasm_fatal("expected `%%endif' before end of file"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5269 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5270 | /* 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] | 5271 | if (i->next) |
| 5272 | src_set(i->lineno, i->fname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5273 | istk = i->next; |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 5274 | lfmt->downlevel(LIST_INCLUDE); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5275 | nasm_free(i); |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5276 | return &tok_pop; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5277 | } |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5278 | } while (0); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5279 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5280 | /* |
| 5281 | * We must expand MMacro parameters and MMacro-local labels |
| 5282 | * _before_ we plunge into directive processing, to cope |
| 5283 | * with things like `%define something %1' such as STRUC |
| 5284 | * uses. Unless we're _defining_ a MMacro, in which case |
| 5285 | * those tokens should be left alone to go into the |
| 5286 | * definition; and unless we're in a non-emitting |
| 5287 | * condition, in which case we don't want to meddle with |
| 5288 | * anything. |
| 5289 | */ |
| 5290 | if (!defining && !(istk->conds && !emitting(istk->conds->state)) |
| 5291 | && !(istk->mstk && !istk->mstk->in_progress)) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5292 | tline = expand_mmac_params(tline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5293 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5294 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5295 | /* |
| 5296 | * Check the line to see if it's a preprocessor directive. |
| 5297 | */ |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5298 | if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) { |
| 5299 | if (dtline) |
| 5300 | return dtline; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5301 | } else if (defining) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5302 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5303 | * We're defining a multi-line macro. We emit nothing |
| 5304 | * at all, and just |
| 5305 | * shove the tokenized line on to the macro definition. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5306 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5307 | Line *l = nasm_malloc(sizeof(Line)); |
| 5308 | l->next = defining->expansion; |
| 5309 | l->first = tline; |
| 5310 | l->finishes = NULL; |
| 5311 | defining->expansion = l; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5312 | } else if (istk->conds && !emitting(istk->conds->state)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5313 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5314 | * We're in a non-emitting branch of a condition block. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5315 | * Emit nothing at all, not even a blank line: when we |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5316 | * emerge from the condition we'll give a line-number |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5317 | * directive so we keep our place correctly. |
| 5318 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5319 | free_tlist(tline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5320 | } else if (istk->mstk && !istk->mstk->in_progress) { |
| 5321 | /* |
| 5322 | * We're in a %rep block which has been terminated, so |
| 5323 | * we're walking through to the %endrep without |
| 5324 | * emitting anything. Emit nothing at all, not even a |
| 5325 | * blank line: when we emerge from the %rep block we'll |
| 5326 | * give a line-number directive so we keep our place |
| 5327 | * correctly. |
| 5328 | */ |
| 5329 | free_tlist(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5330 | } else { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5331 | tline = expand_smacro(tline); |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5332 | if (!expand_mmacro(tline)) |
| 5333 | return tline; |
| 5334 | } |
| 5335 | } |
| 5336 | } |
| 5337 | |
| 5338 | static char *pp_getline(void) |
| 5339 | { |
| 5340 | char *line = NULL; |
| 5341 | Token *tline; |
| 5342 | |
| 5343 | real_verror = nasm_set_verror(pp_verror); |
| 5344 | |
| 5345 | while (true) { |
| 5346 | tline = pp_tokline(); |
| 5347 | if (tline == &tok_pop) { |
| 5348 | /* |
| 5349 | * We popped the macro/include stack. If istk is empty, |
| 5350 | * we are at end of input, otherwise just loop back. |
| 5351 | */ |
| 5352 | if (!istk) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5353 | break; |
H. Peter Anvin (Intel) | a7afe27 | 2019-04-26 00:34:04 -0700 | [diff] [blame] | 5354 | } else { |
| 5355 | /* |
| 5356 | * De-tokenize the line and emit it. |
| 5357 | */ |
| 5358 | line = detoken(tline, true); |
| 5359 | free_tlist(tline); |
| 5360 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5361 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5362 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5363 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5364 | nasm_set_verror(real_verror); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5365 | return line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5366 | } |
| 5367 | |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5368 | static void pp_cleanup_pass(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5369 | { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5370 | real_verror = nasm_set_verror(pp_verror); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5371 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5372 | if (defining) { |
| 5373 | if (defining->name) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 5374 | nasm_nonfatal("end of file while still defining macro `%s'", |
| 5375 | defining->name); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5376 | } else { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 5377 | nasm_nonfatal("end of file while still in %%rep"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5378 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5379 | |
| 5380 | free_mmacro(defining); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5381 | defining = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5382 | } |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5383 | |
| 5384 | nasm_set_verror(real_verror); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5385 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5386 | while (cstk) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5387 | ctx_pop(); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 5388 | free_macros(); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5389 | while (istk) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5390 | Include *i = istk; |
| 5391 | istk = istk->next; |
| 5392 | fclose(i->fp); |
Cyrill Gorcunov | 8dcfd88 | 2011-03-03 09:18:56 +0300 | [diff] [blame] | 5393 | nasm_free(i); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5394 | } |
| 5395 | while (cstk) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5396 | ctx_pop(); |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 5397 | src_set_fname(NULL); |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5398 | } |
| 5399 | |
| 5400 | static void pp_cleanup_session(void) |
| 5401 | { |
| 5402 | free_llist(predef); |
| 5403 | predef = NULL; |
| 5404 | delete_Blocks(); |
| 5405 | freeTokens = NULL; |
| 5406 | ipath_list = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5407 | } |
| 5408 | |
Cyrill Gorcunov | 8c0666b | 2018-11-24 14:33:48 +0300 | [diff] [blame] | 5409 | static void pp_include_path(struct strlist *list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5410 | { |
Cyrill Gorcunov | 8c0666b | 2018-11-24 14:33:48 +0300 | [diff] [blame] | 5411 | ipath_list = list; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5412 | } |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5413 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5414 | static void pp_pre_include(char *fname) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5415 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5416 | Token *inc, *space, *name; |
| 5417 | Line *l; |
| 5418 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5419 | name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0); |
| 5420 | space = new_Token(name, TOK_WHITESPACE, NULL, 0); |
| 5421 | inc = new_Token(space, TOK_PREPROC_ID, "%include", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5422 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5423 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5424 | l->next = predef; |
| 5425 | l->first = inc; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5426 | l->finishes = NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5427 | predef = l; |
| 5428 | } |
| 5429 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5430 | static void pp_pre_define(char *definition) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5431 | { |
| 5432 | Token *def, *space; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5433 | Line *l; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5434 | char *equals; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5435 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5436 | real_verror = nasm_set_verror(pp_verror); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5437 | |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5438 | equals = strchr(definition, '='); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5439 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 5440 | def = new_Token(space, TOK_PREPROC_ID, "%define", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5441 | if (equals) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5442 | *equals = ' '; |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5443 | space->next = tokenize(definition); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5444 | if (equals) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5445 | *equals = '='; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5446 | |
Cyrill Gorcunov | 6d42e9b | 2015-02-08 11:07:17 +0300 | [diff] [blame] | 5447 | if (space->next->type != TOK_PREPROC_ID && |
| 5448 | space->next->type != TOK_ID) |
H. Peter Anvin (Intel) | 80c4f23 | 2018-12-14 13:33:24 -0800 | [diff] [blame] | 5449 | nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition); |
Cyrill Gorcunov | 6d42e9b | 2015-02-08 11:07:17 +0300 | [diff] [blame] | 5450 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5451 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5452 | l->next = predef; |
| 5453 | l->first = def; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5454 | l->finishes = NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5455 | predef = l; |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5456 | |
| 5457 | nasm_set_verror(real_verror); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5458 | } |
| 5459 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5460 | static void pp_pre_undefine(char *definition) |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5461 | { |
| 5462 | Token *def, *space; |
| 5463 | Line *l; |
| 5464 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5465 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 5466 | def = new_Token(space, TOK_PREPROC_ID, "%undef", 0); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5467 | space->next = tokenize(definition); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5468 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5469 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5470 | l->next = predef; |
| 5471 | l->first = def; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5472 | l->finishes = NULL; |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5473 | predef = l; |
| 5474 | } |
| 5475 | |
H. Peter Anvin | 0599034 | 2018-06-11 13:32:42 -0700 | [diff] [blame] | 5476 | /* Insert an early preprocessor command that doesn't need special handling */ |
| 5477 | static void pp_pre_command(const char *what, char *string) |
| 5478 | { |
| 5479 | char *cmd; |
| 5480 | Token *def, *space; |
| 5481 | Line *l; |
| 5482 | |
| 5483 | def = tokenize(string); |
| 5484 | if (what) { |
| 5485 | cmd = nasm_strcat(what[0] == '%' ? "" : "%", what); |
| 5486 | space = new_Token(def, TOK_WHITESPACE, NULL, 0); |
| 5487 | def = new_Token(space, TOK_PREPROC_ID, cmd, 0); |
| 5488 | } |
| 5489 | |
| 5490 | l = nasm_malloc(sizeof(Line)); |
| 5491 | l->next = predef; |
| 5492 | l->first = def; |
| 5493 | l->finishes = NULL; |
| 5494 | predef = l; |
| 5495 | } |
| 5496 | |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 5497 | static void pp_add_stdmac(macros_t *macros) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5498 | { |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 5499 | macros_t **mp; |
| 5500 | |
| 5501 | /* Find the end of the list and avoid duplicates */ |
| 5502 | for (mp = stdmacros; *mp; mp++) { |
| 5503 | if (*mp == macros) |
| 5504 | return; /* Nothing to do */ |
| 5505 | } |
| 5506 | |
| 5507 | nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]); |
| 5508 | |
| 5509 | *mp = macros; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 5510 | } |
| 5511 | |
Cyrill Gorcunov | 15ce78f | 2017-01-06 20:21:28 +0300 | [diff] [blame] | 5512 | static void pp_extra_stdmac(macros_t *macros) |
| 5513 | { |
| 5514 | extrastdmac = macros; |
| 5515 | } |
| 5516 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5517 | static Token *make_tok_num(int64_t val) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5518 | { |
Cyrill Gorcunov | ce65274 | 2013-05-06 23:43:43 +0400 | [diff] [blame] | 5519 | char numbuf[32]; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5520 | int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val); |
| 5521 | return new_Token(NULL, TOK_NUMBER, numbuf, len); |
| 5522 | } |
| 5523 | |
| 5524 | static Token *make_tok_qstr(const char *str) |
| 5525 | { |
| 5526 | Token *t = new_Token(NULL, TOK_STRING, NULL, 0); |
H. Peter Anvin (Intel) | 41e9682 | 2019-04-25 18:00:32 -0700 | [diff] [blame] | 5527 | t->text = nasm_quote_cstr(str, &t->len); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5528 | return t; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5529 | } |
| 5530 | |
H. Peter Anvin (Intel) | 6bde2ed | 2018-12-13 19:39:41 -0800 | [diff] [blame] | 5531 | static void pp_list_one_macro(MMacro *m, errflags severity) |
H. Peter Anvin | 3736895 | 2016-05-09 14:10:32 -0700 | [diff] [blame] | 5532 | { |
| 5533 | if (!m) |
| 5534 | return; |
| 5535 | |
| 5536 | /* We need to print the next_active list in reverse order */ |
| 5537 | pp_list_one_macro(m->next_active, severity); |
| 5538 | |
| 5539 | if (m->name && !m->nolist) { |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 5540 | src_set(m->xline + m->lineno, m->fname); |
H. Peter Anvin | ddb2906 | 2018-12-11 00:06:29 -0800 | [diff] [blame] | 5541 | nasm_error(severity, "... from macro `%s' defined", m->name); |
H. Peter Anvin | 3736895 | 2016-05-09 14:10:32 -0700 | [diff] [blame] | 5542 | } |
| 5543 | } |
| 5544 | |
H. Peter Anvin (Intel) | 6bde2ed | 2018-12-13 19:39:41 -0800 | [diff] [blame] | 5545 | static void pp_error_list_macros(errflags severity) |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5546 | { |
H. Peter Anvin | ddb2906 | 2018-12-11 00:06:29 -0800 | [diff] [blame] | 5547 | struct src_location saved; |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5548 | |
H. Peter Anvin | ddb2906 | 2018-12-11 00:06:29 -0800 | [diff] [blame] | 5549 | severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE; |
| 5550 | saved = src_where(); |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5551 | |
Cyrill Gorcunov | 771d04e | 2016-05-10 23:27:03 +0300 | [diff] [blame] | 5552 | if (istk) |
| 5553 | pp_list_one_macro(istk->mstk, severity); |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5554 | |
H. Peter Anvin | ddb2906 | 2018-12-11 00:06:29 -0800 | [diff] [blame] | 5555 | src_update(saved); |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5556 | } |
| 5557 | |
H. Peter Anvin | e746971 | 2016-02-18 02:20:59 -0800 | [diff] [blame] | 5558 | const struct preproc_ops nasmpp = { |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 5559 | pp_init, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5560 | pp_reset, |
| 5561 | pp_getline, |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5562 | pp_cleanup_pass, |
| 5563 | pp_cleanup_session, |
Cyrill Gorcunov | 15ce78f | 2017-01-06 20:21:28 +0300 | [diff] [blame] | 5564 | pp_extra_stdmac, |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5565 | pp_pre_define, |
| 5566 | pp_pre_undefine, |
| 5567 | pp_pre_include, |
H. Peter Anvin | 0599034 | 2018-06-11 13:32:42 -0700 | [diff] [blame] | 5568 | pp_pre_command, |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5569 | pp_include_path, |
| 5570 | pp_error_list_macros, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5571 | }; |