H. Peter Anvin | 9e6747c | 2009-06-28 17:13:04 -0700 | [diff] [blame] | 1 | /* ----------------------------------------------------------------------- * |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2 | * |
H. Peter Anvin | 0599034 | 2018-06-11 13:32:42 -0700 | [diff] [blame] | 3 | * Copyright 1996-2018 The NASM Authors - All Rights Reserved |
H. Peter Anvin | 9e6747c | 2009-06-28 17:13:04 -0700 | [diff] [blame] | 4 | * See the file AUTHORS included with the NASM distribution for |
| 5 | * the specific copyright holders. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 6 | * |
H. Peter Anvin | 9e6747c | 2009-06-28 17:13:04 -0700 | [diff] [blame] | 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following |
| 9 | * conditions are met: |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 10 | * |
H. Peter Anvin | 9e6747c | 2009-06-28 17:13:04 -0700 | [diff] [blame] | 11 | * * Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * * Redistributions in binary form must reproduce the above |
| 14 | * copyright notice, this list of conditions and the following |
| 15 | * disclaimer in the documentation and/or other materials provided |
| 16 | * with the distribution. |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 17 | * |
H. Peter Anvin | 9e6747c | 2009-06-28 17:13:04 -0700 | [diff] [blame] | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 19 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 20 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 25 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 29 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 30 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | * ----------------------------------------------------------------------- */ |
| 33 | |
| 34 | /* |
| 35 | * preproc.c macro preprocessor for the Netwide Assembler |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 36 | */ |
| 37 | |
H. Peter Anvin | 4836e33 | 2002-04-30 20:56:43 +0000 | [diff] [blame] | 38 | /* Typical flow of text through preproc |
| 39 | * |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 40 | * pp_getline gets tokenized lines, either |
H. Peter Anvin | 4836e33 | 2002-04-30 20:56:43 +0000 | [diff] [blame] | 41 | * |
| 42 | * from a macro expansion |
| 43 | * |
| 44 | * or |
| 45 | * { |
| 46 | * read_line gets raw text from stdmacpos, or predef, or current input file |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 47 | * tokenize converts to tokens |
H. Peter Anvin | 4836e33 | 2002-04-30 20:56:43 +0000 | [diff] [blame] | 48 | * } |
| 49 | * |
| 50 | * expand_mmac_params is used to expand %1 etc., unless a macro is being |
| 51 | * defined or a false conditional is being processed |
| 52 | * (%0, %1, %+1, %-1, %%foo |
| 53 | * |
| 54 | * do_directive checks for directives |
| 55 | * |
| 56 | * expand_smacro is used to expand single line macros |
| 57 | * |
| 58 | * expand_mmacro is used to expand multi-line macros |
| 59 | * |
| 60 | * detoken is used to convert the line back to text |
| 61 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 62 | |
H. Peter Anvin | fe50195 | 2007-10-02 21:53:51 -0700 | [diff] [blame] | 63 | #include "compiler.h" |
| 64 | |
H. Peter Anvin | 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 | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 99 | * Store the definition of a single-line macro. |
| 100 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 101 | struct SMacro { |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 102 | SMacro *next; /* MUST BE FIRST - see free_smacro() */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 103 | char *name; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 104 | union { |
| 105 | Token *expansion; |
| 106 | Token *(*magic)(const SMacro *s, Token **params, int *paramsize); |
| 107 | } e; |
| 108 | bool *eval_param; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 109 | unsigned int nparam; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 110 | bool casesense; |
| 111 | bool magic; |
| 112 | bool in_progress; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 116 | * Store the definition of a multi-line macro. This is also used to |
| 117 | * store the interiors of `%rep...%endrep' blocks, which are |
| 118 | * effectively self-re-invoking multi-line macros which simply |
| 119 | * don't have a name or bother to appear in the hash tables. %rep |
| 120 | * blocks are signified by having a NULL `name' field. |
| 121 | * |
| 122 | * In a MMacro describing a `%rep' block, the `in_progress' field |
| 123 | * isn't merely boolean, but gives the number of repeats left to |
| 124 | * run. |
| 125 | * |
| 126 | * The `next' field is used for storing MMacros in hash tables; the |
| 127 | * `next_active' field is for stacking them on istk entries. |
| 128 | * |
| 129 | * When a MMacro is being expanded, `params', `iline', `nparam', |
| 130 | * `paramlen', `rotate' and `unique' are local to the invocation. |
| 131 | */ |
| 132 | struct MMacro { |
| 133 | MMacro *next; |
| 134 | MMacroInvocation *prev; /* previous invocation */ |
| 135 | char *name; |
| 136 | int nparam_min, nparam_max; |
| 137 | bool casesense; |
| 138 | bool plus; /* is the last parameter greedy? */ |
| 139 | bool nolist; /* is this macro listing-inhibited? */ |
| 140 | int64_t in_progress; /* is this macro currently being expanded? */ |
| 141 | int32_t max_depth; /* maximum number of recursive expansions allowed */ |
| 142 | Token *dlist; /* All defaults as one list */ |
| 143 | Token **defaults; /* Parameter default pointers */ |
| 144 | int ndefs; /* number of default parameters */ |
| 145 | Line *expansion; |
| 146 | |
| 147 | MMacro *next_active; |
| 148 | MMacro *rep_nest; /* used for nesting %rep */ |
| 149 | Token **params; /* actual parameters */ |
| 150 | Token *iline; /* invocation line */ |
| 151 | unsigned int nparam, rotate; |
| 152 | int *paramlen; |
| 153 | uint64_t unique; |
| 154 | int lineno; /* Current line number on expansion */ |
| 155 | uint64_t condcnt; /* number of if blocks... */ |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 156 | |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 157 | const char *fname; /* File where defined */ |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 158 | int32_t xline; /* First line in macro */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | |
| 162 | /* Store the definition of a multi-line macro, as defined in a |
| 163 | * previous recursive macro expansion. |
| 164 | */ |
| 165 | struct MMacroInvocation { |
| 166 | MMacroInvocation *prev; /* previous invocation */ |
| 167 | Token **params; /* actual parameters */ |
| 168 | Token *iline; /* invocation line */ |
| 169 | unsigned int nparam, rotate; |
| 170 | int *paramlen; |
| 171 | uint64_t unique; |
| 172 | uint64_t condcnt; |
| 173 | }; |
| 174 | |
| 175 | |
| 176 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 177 | * The context stack is composed of a linked list of these. |
| 178 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 179 | struct Context { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 180 | Context *next; |
| 181 | char *name; |
| 182 | struct hash_table localmac; |
| 183 | uint32_t number; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 184 | }; |
| 185 | |
| 186 | /* |
| 187 | * This is the internal form which we break input lines up into. |
| 188 | * Typically stored in linked lists. |
| 189 | * |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 190 | * Note that `type' serves a double meaning: TOK_SMAC_START_PARAMS is |
| 191 | * not necessarily used as-is, but is also used to encode the number |
| 192 | * and expansion type of substituted parameter. So in the definition |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 193 | * |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 194 | * %define a(x,=y) ( (x) & ~(y) ) |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 195 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 196 | * the token representing `x' will have its type changed to |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 197 | * tok_smac_param(0) but the one representing `y' will be |
| 198 | * tok_smac_param(1); see the accessor functions below. |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 199 | * |
| 200 | * TOK_INTERNAL_STRING is a dirty hack: it's a single string token |
| 201 | * which doesn't need quotes around it. Used in the pre-include |
| 202 | * mechanism as an alternative to trying to find a sensible type of |
| 203 | * quote to use on the filename we were passed. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 204 | */ |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 205 | enum pp_token_type { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 206 | TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID, |
| 207 | TOK_PREPROC_ID, TOK_STRING, |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 208 | TOK_NUMBER, TOK_FLOAT, TOK_OTHER, |
H. Peter Anvin | 6c81f0a | 2008-05-25 21:46:17 -0700 | [diff] [blame] | 209 | TOK_INTERNAL_STRING, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 210 | TOK_PREPROC_Q, TOK_PREPROC_QQ, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 211 | TOK_PASTE, /* %+ */ |
| 212 | TOK_INDIRECT, /* %[...] */ |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 213 | TOK_SMAC_END, /* Marker for the end of smacro expansion */ |
| 214 | TOK_SMAC_START_PARAMS, /* MUST BE LAST IN THE LIST!!! */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 215 | TOK_MAX = INT_MAX /* Keep compiler from reducing the range */ |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 216 | }; |
| 217 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 218 | static inline enum pp_token_type tok_smac_param(int param) |
| 219 | { |
| 220 | return TOK_SMAC_START_PARAMS + param; |
| 221 | } |
| 222 | static int smac_nparam(enum pp_token_type toktype) |
| 223 | { |
| 224 | return toktype - TOK_SMAC_START_PARAMS; |
| 225 | } |
| 226 | static bool is_smac_param(enum pp_token_type toktype) |
| 227 | { |
| 228 | return toktype >= TOK_SMAC_START_PARAMS; |
| 229 | } |
| 230 | |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 231 | #define PP_CONCAT_MASK(x) (1 << (x)) |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 232 | #define PP_CONCAT_MATCH(t, mask) (PP_CONCAT_MASK((t)->type) & mask) |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 233 | |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 234 | struct tokseq_match { |
| 235 | int mask_head; |
| 236 | int mask_tail; |
| 237 | }; |
| 238 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 239 | struct Token { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 240 | Token *next; |
| 241 | char *text; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 242 | union { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 243 | SMacro *mac; /* associated macro for TOK_SMAC_END */ |
| 244 | size_t len; /* scratch length field */ |
| 245 | } a; /* Auxiliary data */ |
| 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, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 473 | const char *text, int txtlen); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 474 | static Token *delete_Token(Token * t); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 475 | |
| 476 | /* |
| 477 | * Macros for safe checking of token pointers, avoid *(NULL) |
| 478 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 479 | #define tok_type_(x,t) ((x) && (x)->type == (t)) |
| 480 | #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next |
| 481 | #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v))) |
| 482 | #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] | 483 | |
Cyrill Gorcunov | 194ba89 | 2011-06-30 01:16:35 +0400 | [diff] [blame] | 484 | /* |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 485 | * nasm_unquote with error if the string contains NUL characters. |
| 486 | * If the string contains NUL characters, issue an error and return |
| 487 | * the C len, i.e. truncate at the NUL. |
| 488 | */ |
| 489 | static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive) |
| 490 | { |
| 491 | size_t len = nasm_unquote(qstr, NULL); |
| 492 | size_t clen = strlen(qstr); |
| 493 | |
| 494 | if (len != clen) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 495 | nasm_nonfatal("NUL character in `%s' directive", |
| 496 | pp_directives[directive]); |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 497 | return clen; |
| 498 | } |
| 499 | |
| 500 | /* |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 501 | * In-place reverse a list of tokens. |
| 502 | */ |
| 503 | static Token *reverse_tokens(Token *t) |
| 504 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 505 | Token *prev = NULL; |
| 506 | Token *next; |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 507 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 508 | while (t) { |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 509 | next = t->next; |
| 510 | t->next = prev; |
| 511 | prev = t; |
| 512 | t = next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 513 | } |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 514 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 515 | return prev; |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | /* |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 519 | * Handle TASM specific directives, which do not contain a % in |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 520 | * front of them. We do it here because I could not find any other |
| 521 | * place to do it for the moment, and it is a hack (ideally it would |
| 522 | * be nice to be able to use the NASM pre-processor to do it). |
| 523 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 524 | static char *check_tasm_directive(char *line) |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 525 | { |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 526 | int32_t i, j, k, m, len; |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 527 | char *p, *q, *oldline, oldchar; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 528 | |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 529 | p = nasm_skip_spaces(line); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 530 | |
| 531 | /* Binary search for the directive name */ |
| 532 | i = -1; |
Cyrill Gorcunov | a731924 | 2010-06-03 22:04:36 +0400 | [diff] [blame] | 533 | j = ARRAY_SIZE(tasm_directives); |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 534 | q = nasm_skip_word(p); |
| 535 | len = q - p; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 536 | if (len) { |
| 537 | oldchar = p[len]; |
| 538 | p[len] = 0; |
| 539 | while (j - i > 1) { |
| 540 | k = (j + i) / 2; |
| 541 | m = nasm_stricmp(p, tasm_directives[k]); |
| 542 | if (m == 0) { |
| 543 | /* We have found a directive, so jam a % in front of it |
| 544 | * so that NASM will then recognise it as one if it's own. |
| 545 | */ |
| 546 | p[len] = oldchar; |
| 547 | len = strlen(p); |
| 548 | oldline = line; |
| 549 | line = nasm_malloc(len + 2); |
| 550 | line[0] = '%'; |
| 551 | if (k == TM_IFDIFI) { |
H. Peter Anvin | 18f4879 | 2009-06-27 15:56:27 -0700 | [diff] [blame] | 552 | /* |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 553 | * NASM does not recognise IFDIFI, so we convert |
| 554 | * it to %if 0. This is not used in NASM |
| 555 | * compatible code, but does need to parse for the |
| 556 | * TASM macro package. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 557 | */ |
H. Peter Anvin | 18f4879 | 2009-06-27 15:56:27 -0700 | [diff] [blame] | 558 | strcpy(line + 1, "if 0"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 559 | } else { |
| 560 | memcpy(line + 1, p, len + 1); |
| 561 | } |
| 562 | nasm_free(oldline); |
| 563 | return line; |
| 564 | } else if (m < 0) { |
| 565 | j = k; |
| 566 | } else |
| 567 | i = k; |
| 568 | } |
| 569 | p[len] = oldchar; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 570 | } |
| 571 | return line; |
| 572 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 573 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 574 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 575 | * The pre-preprocessing stage... This function translates line |
| 576 | * number indications as they emerge from GNU cpp (`# lineno "file" |
| 577 | * flags') into NASM preprocessor line number indications (`%line |
| 578 | * lineno file'). |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 579 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 580 | static char *prepreproc(char *line) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 581 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 582 | int lineno, fnlen; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 583 | char *fname, *oldline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 584 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 585 | if (line[0] == '#' && line[1] == ' ') { |
| 586 | oldline = line; |
| 587 | fname = oldline + 2; |
| 588 | lineno = atoi(fname); |
| 589 | fname += strspn(fname, "0123456789 "); |
| 590 | if (*fname == '"') |
| 591 | fname++; |
| 592 | fnlen = strcspn(fname, "\""); |
| 593 | line = nasm_malloc(20 + fnlen); |
| 594 | snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname); |
| 595 | nasm_free(oldline); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 596 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 597 | if (tasm_compatible_mode) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 598 | return check_tasm_directive(line); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 599 | return line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 603 | * Free a linked list of tokens. |
| 604 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 605 | static void free_tlist(Token * list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 606 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 607 | while (list) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 608 | list = delete_Token(list); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | /* |
| 612 | * Free a linked list of lines. |
| 613 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 614 | static void free_llist(Line * list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 615 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 616 | Line *l, *tmp; |
| 617 | list_for_each_safe(l, tmp, list) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 618 | free_tlist(l->first); |
| 619 | nasm_free(l); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 620 | } |
| 621 | } |
| 622 | |
| 623 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 624 | * Free an MMacro |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 625 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 626 | static void free_mmacro(MMacro * m) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 627 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 628 | nasm_free(m->name); |
| 629 | free_tlist(m->dlist); |
| 630 | nasm_free(m->defaults); |
| 631 | free_llist(m->expansion); |
| 632 | nasm_free(m); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | /* |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 636 | * Free an SMacro |
| 637 | */ |
| 638 | static void free_smacro(SMacro *s, bool really) |
| 639 | { |
| 640 | nasm_free(s->name); |
| 641 | if (!s->magic) |
| 642 | free_tlist(s->e.expansion); |
| 643 | nasm_free(s->eval_param); |
| 644 | if (really) { |
| 645 | nasm_free(s); |
| 646 | } else { |
| 647 | /* Wipe everything except the next pointer */ |
| 648 | memset(&s->next + 1, 0, sizeof *s - sizeof s->next); |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | /* |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 653 | * Free all currently defined macros, and free the hash tables |
| 654 | */ |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 655 | static void free_smacro_table(struct hash_table *smt) |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 656 | { |
H. Peter Anvin (Intel) | ebb05a0 | 2018-12-11 12:30:25 -0800 | [diff] [blame] | 657 | struct hash_iterator it; |
| 658 | const struct hash_node *np; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 659 | |
H. Peter Anvin (Intel) | ebb05a0 | 2018-12-11 12:30:25 -0800 | [diff] [blame] | 660 | hash_for_each(smt, it, np) { |
| 661 | SMacro *tmp; |
| 662 | SMacro *s = np->data; |
| 663 | nasm_free((void *)np->key); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 664 | list_for_each_safe(s, tmp, s) |
| 665 | free_smacro(s, true); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 666 | } |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 667 | hash_free(smt); |
| 668 | } |
| 669 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 670 | static void free_mmacro_table(struct hash_table *mmt) |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 671 | { |
H. Peter Anvin (Intel) | ebb05a0 | 2018-12-11 12:30:25 -0800 | [diff] [blame] | 672 | struct hash_iterator it; |
| 673 | const struct hash_node *np; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 674 | |
H. Peter Anvin (Intel) | ebb05a0 | 2018-12-11 12:30:25 -0800 | [diff] [blame] | 675 | hash_for_each(mmt, it, np) { |
| 676 | MMacro *tmp; |
| 677 | MMacro *m = np->data; |
| 678 | nasm_free((void *)np->key); |
| 679 | list_for_each_safe(m, tmp, m) |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 680 | free_mmacro(m); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 681 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 682 | hash_free(mmt); |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | static void free_macros(void) |
| 686 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 687 | free_smacro_table(&smacros); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 688 | free_mmacro_table(&mmacros); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | /* |
| 692 | * Initialize the hash tables |
| 693 | */ |
| 694 | static void init_macros(void) |
| 695 | { |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 699 | * Pop the context stack. |
| 700 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 701 | static void ctx_pop(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 702 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 703 | Context *c = cstk; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 704 | |
| 705 | cstk = cstk->next; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 706 | free_smacro_table(&c->localmac); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 707 | nasm_free(c->name); |
| 708 | nasm_free(c); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 709 | } |
| 710 | |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 711 | /* |
| 712 | * Search for a key in the hash index; adding it if necessary |
| 713 | * (in which case we initialize the data pointer to NULL.) |
| 714 | */ |
| 715 | static void ** |
| 716 | hash_findi_add(struct hash_table *hash, const char *str) |
| 717 | { |
| 718 | struct hash_insert hi; |
| 719 | void **r; |
| 720 | char *strx; |
H. Peter Anvin (Intel) | ebb05a0 | 2018-12-11 12:30:25 -0800 | [diff] [blame] | 721 | size_t l = strlen(str) + 1; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 722 | |
H. Peter Anvin (Intel) | ebb05a0 | 2018-12-11 12:30:25 -0800 | [diff] [blame] | 723 | r = hash_findib(hash, str, l, &hi); |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 724 | if (r) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 725 | return r; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 726 | |
H. Peter Anvin (Intel) | ebb05a0 | 2018-12-11 12:30:25 -0800 | [diff] [blame] | 727 | strx = nasm_malloc(l); /* Use a more efficient allocator here? */ |
| 728 | memcpy(strx, str, l); |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 729 | return hash_add(&hi, strx, NULL); |
| 730 | } |
| 731 | |
| 732 | /* |
| 733 | * Like hash_findi, but returns the data element rather than a pointer |
| 734 | * to it. Used only when not adding a new element, hence no third |
| 735 | * argument. |
| 736 | */ |
| 737 | static void * |
| 738 | hash_findix(struct hash_table *hash, const char *str) |
| 739 | { |
| 740 | void **p; |
| 741 | |
| 742 | p = hash_findi(hash, str, NULL); |
| 743 | return p ? *p : NULL; |
| 744 | } |
| 745 | |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 746 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 747 | * read line from standart macros set, |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 748 | * if there no more left -- return NULL |
| 749 | */ |
| 750 | static char *line_from_stdmac(void) |
| 751 | { |
| 752 | unsigned char c; |
| 753 | const unsigned char *p = stdmacpos; |
| 754 | char *line, *q; |
| 755 | size_t len = 0; |
| 756 | |
| 757 | if (!stdmacpos) |
| 758 | return NULL; |
| 759 | |
| 760 | while ((c = *p++)) { |
| 761 | if (c >= 0x80) |
| 762 | len += pp_directives_len[c - 0x80] + 1; |
| 763 | else |
| 764 | len++; |
| 765 | } |
| 766 | |
| 767 | line = nasm_malloc(len + 1); |
| 768 | q = line; |
| 769 | while ((c = *stdmacpos++)) { |
| 770 | if (c >= 0x80) { |
| 771 | memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]); |
| 772 | q += pp_directives_len[c - 0x80]; |
| 773 | *q++ = ' '; |
| 774 | } else { |
| 775 | *q++ = c; |
| 776 | } |
| 777 | } |
| 778 | stdmacpos = p; |
| 779 | *q = '\0'; |
| 780 | |
| 781 | if (!*stdmacpos) { |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 782 | /* This was the last of this particular macro set */ |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 783 | stdmacpos = NULL; |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 784 | if (*stdmacnext) { |
| 785 | stdmacpos = *stdmacnext++; |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 786 | } else if (do_predef) { |
| 787 | Line *pd, *l; |
| 788 | Token *head, **tail, *t; |
| 789 | |
| 790 | /* |
| 791 | * Nasty hack: here we push the contents of |
| 792 | * `predef' on to the top-level expansion stack, |
| 793 | * since this is the most convenient way to |
| 794 | * implement the pre-include and pre-define |
| 795 | * features. |
| 796 | */ |
| 797 | list_for_each(pd, predef) { |
| 798 | head = NULL; |
| 799 | tail = &head; |
| 800 | list_for_each(t, pd->first) { |
| 801 | *tail = new_Token(NULL, t->type, t->text, 0); |
| 802 | tail = &(*tail)->next; |
| 803 | } |
| 804 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 805 | l = nasm_malloc(sizeof(Line)); |
| 806 | l->next = istk->expansion; |
| 807 | l->first = head; |
| 808 | l->finishes = NULL; |
| 809 | |
| 810 | istk->expansion = l; |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 811 | } |
| 812 | do_predef = false; |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | return line; |
| 817 | } |
| 818 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 819 | static char *read_line(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 820 | { |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 821 | unsigned int size, c, next; |
| 822 | const unsigned int delta = 512; |
| 823 | const unsigned int pad = 8; |
| 824 | unsigned int nr_cont = 0; |
| 825 | bool cont = false; |
| 826 | char *buffer, *p; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 827 | |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 828 | /* Standart macros set (predefined) goes first */ |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 829 | p = line_from_stdmac(); |
| 830 | if (p) |
| 831 | return p; |
H. Peter Anvin | 72edbb8 | 2008-06-19 16:00:04 -0700 | [diff] [blame] | 832 | |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 833 | size = delta; |
| 834 | p = buffer = nasm_malloc(size); |
| 835 | |
| 836 | for (;;) { |
| 837 | c = fgetc(istk->fp); |
| 838 | if ((int)(c) == EOF) { |
| 839 | p[0] = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 840 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 841 | } |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 842 | |
| 843 | switch (c) { |
| 844 | case '\r': |
| 845 | next = fgetc(istk->fp); |
| 846 | if (next != '\n') |
| 847 | ungetc(next, istk->fp); |
| 848 | if (cont) { |
| 849 | cont = false; |
| 850 | continue; |
| 851 | } |
| 852 | break; |
| 853 | |
| 854 | case '\n': |
| 855 | if (cont) { |
| 856 | cont = false; |
| 857 | continue; |
| 858 | } |
| 859 | break; |
| 860 | |
| 861 | case '\\': |
| 862 | next = fgetc(istk->fp); |
| 863 | ungetc(next, istk->fp); |
Cyrill Gorcunov | 490f85e | 2012-12-27 20:02:17 +0400 | [diff] [blame] | 864 | if (next == '\r' || next == '\n') { |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 865 | cont = true; |
| 866 | nr_cont++; |
| 867 | continue; |
| 868 | } |
| 869 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 870 | } |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 871 | |
| 872 | if (c == '\r' || c == '\n') { |
| 873 | *p++ = 0; |
| 874 | break; |
| 875 | } |
| 876 | |
| 877 | if (p >= (buffer + size - pad)) { |
| 878 | buffer = nasm_realloc(buffer, size + delta); |
| 879 | p = buffer + size - pad; |
| 880 | size += delta; |
| 881 | } |
| 882 | |
| 883 | *p++ = (unsigned char)c; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 884 | } |
| 885 | |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 886 | if (p == buffer) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 887 | nasm_free(buffer); |
| 888 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 889 | } |
| 890 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 891 | src_set_linnum(src_get_linnum() + istk->lineinc + |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 892 | (nr_cont * istk->lineinc)); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 893 | |
| 894 | /* |
| 895 | * Handle spurious ^Z, which may be inserted into source files |
| 896 | * by some file transfer utilities. |
| 897 | */ |
| 898 | buffer[strcspn(buffer, "\032")] = '\0'; |
| 899 | |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 900 | lfmt->line(LIST_READ, buffer); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 901 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 902 | return buffer; |
| 903 | } |
| 904 | |
| 905 | /* |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 906 | * 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] | 907 | * don't need to parse the value out of e.g. numeric tokens: we |
| 908 | * simply split one string into many. |
| 909 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 910 | static Token *tokenize(char *line) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 911 | { |
H. Peter Anvin | ca544db | 2008-10-19 19:30:11 -0700 | [diff] [blame] | 912 | char c, *p = line; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 913 | enum pp_token_type type; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 914 | Token *list = NULL; |
| 915 | Token *t, **tail = &list; |
| 916 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 917 | while (*line) { |
| 918 | p = line; |
| 919 | if (*p == '%') { |
| 920 | p++; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 921 | if (*p == '+' && !nasm_isdigit(p[1])) { |
| 922 | p++; |
| 923 | type = TOK_PASTE; |
| 924 | } else if (nasm_isdigit(*p) || |
| 925 | ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 926 | do { |
| 927 | p++; |
| 928 | } |
H. Peter Anvin | bda7a6e | 2008-06-21 10:23:17 -0700 | [diff] [blame] | 929 | while (nasm_isdigit(*p)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 930 | type = TOK_PREPROC_ID; |
| 931 | } else if (*p == '{') { |
| 932 | p++; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 933 | while (*p) { |
| 934 | if (*p == '}') |
| 935 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 936 | p[-1] = *p; |
| 937 | p++; |
| 938 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 939 | if (*p != '}') |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 940 | nasm_warn(WARN_OTHER, "unterminated %%{ construct"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 941 | p[-1] = '\0'; |
| 942 | if (*p) |
| 943 | p++; |
| 944 | type = TOK_PREPROC_ID; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 945 | } else if (*p == '[') { |
| 946 | int lvl = 1; |
| 947 | line += 2; /* Skip the leading %[ */ |
| 948 | p++; |
| 949 | while (lvl && (c = *p++)) { |
| 950 | switch (c) { |
| 951 | case ']': |
| 952 | lvl--; |
| 953 | break; |
| 954 | case '%': |
| 955 | if (*p == '[') |
| 956 | lvl++; |
| 957 | break; |
| 958 | case '\'': |
| 959 | case '\"': |
| 960 | case '`': |
Cyrill Gorcunov | 3144e84 | 2017-10-22 10:50:55 +0300 | [diff] [blame] | 961 | p = nasm_skip_string(p - 1); |
| 962 | if (*p) |
| 963 | p++; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 964 | break; |
| 965 | default: |
| 966 | break; |
| 967 | } |
| 968 | } |
| 969 | p--; |
| 970 | if (*p) |
| 971 | *p++ = '\0'; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 972 | if (lvl) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 973 | nasm_nonfatalf(ERR_PASS1, "unterminated %%[ construct"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 974 | type = TOK_INDIRECT; |
| 975 | } else if (*p == '?') { |
| 976 | type = TOK_PREPROC_Q; /* %? */ |
| 977 | p++; |
| 978 | if (*p == '?') { |
| 979 | type = TOK_PREPROC_QQ; /* %?? */ |
| 980 | p++; |
| 981 | } |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 982 | } else if (*p == '!') { |
| 983 | type = TOK_PREPROC_ID; |
| 984 | p++; |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 985 | if (nasm_isidchar(*p)) { |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 986 | do { |
| 987 | p++; |
| 988 | } |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 989 | while (nasm_isidchar(*p)); |
H. Peter Anvin | 53e2e4c | 2018-11-28 15:01:40 -0800 | [diff] [blame] | 990 | } else if (nasm_isquote(*p)) { |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 991 | p = nasm_skip_string(p); |
| 992 | if (*p) |
| 993 | p++; |
| 994 | else |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 995 | nasm_nonfatalf(ERR_PASS1, "unterminated %%! string"); |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 996 | } else { |
| 997 | /* %! without string or identifier */ |
| 998 | type = TOK_OTHER; /* Legacy behavior... */ |
| 999 | } |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 1000 | } else if (nasm_isidchar(*p) || |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1001 | ((*p == '!' || *p == '%' || *p == '$') && |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 1002 | nasm_isidchar(p[1]))) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1003 | do { |
| 1004 | p++; |
| 1005 | } |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 1006 | while (nasm_isidchar(*p)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1007 | type = TOK_PREPROC_ID; |
| 1008 | } else { |
| 1009 | type = TOK_OTHER; |
| 1010 | if (*p == '%') |
| 1011 | p++; |
| 1012 | } |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 1013 | } else if (nasm_isidstart(*p) || (*p == '$' && nasm_isidstart(p[1]))) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1014 | type = TOK_ID; |
| 1015 | p++; |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 1016 | while (*p && nasm_isidchar(*p)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1017 | p++; |
H. Peter Anvin | 53e2e4c | 2018-11-28 15:01:40 -0800 | [diff] [blame] | 1018 | } else if (nasm_isquote(*p)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1019 | /* |
| 1020 | * A string token. |
| 1021 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1022 | type = TOK_STRING; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1023 | p = nasm_skip_string(p); |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1024 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1025 | if (*p) { |
| 1026 | p++; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1027 | } else { |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 1028 | nasm_warn(WARN_OTHER, "unterminated string"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1029 | /* Handling unterminated strings by UNV */ |
| 1030 | /* type = -1; */ |
| 1031 | } |
Victor van den Elzen | fb5f251 | 2009-04-17 16:17:59 +0200 | [diff] [blame] | 1032 | } else if (p[0] == '$' && p[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1033 | type = TOK_OTHER; /* TOKEN_BASE */ |
Victor van den Elzen | fb5f251 | 2009-04-17 16:17:59 +0200 | [diff] [blame] | 1034 | p += 2; |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 1035 | } else if (nasm_isnumstart(*p)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1036 | bool is_hex = false; |
| 1037 | bool is_float = false; |
| 1038 | bool has_e = false; |
| 1039 | char c, *r; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1040 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1041 | /* |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1042 | * A numeric token. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1043 | */ |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1044 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1045 | if (*p == '$') { |
| 1046 | p++; |
| 1047 | is_hex = true; |
| 1048 | } |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1049 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1050 | for (;;) { |
| 1051 | c = *p++; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1052 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1053 | if (!is_hex && (c == 'e' || c == 'E')) { |
| 1054 | has_e = true; |
| 1055 | if (*p == '+' || *p == '-') { |
| 1056 | /* |
| 1057 | * e can only be followed by +/- if it is either a |
| 1058 | * prefixed hex number or a floating-point number |
| 1059 | */ |
| 1060 | p++; |
| 1061 | is_float = true; |
| 1062 | } |
| 1063 | } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') { |
| 1064 | is_hex = true; |
| 1065 | } else if (c == 'P' || c == 'p') { |
| 1066 | is_float = true; |
| 1067 | if (*p == '+' || *p == '-') |
| 1068 | p++; |
H. Peter Anvin | 1350620 | 2018-11-28 14:55:58 -0800 | [diff] [blame] | 1069 | } else if (nasm_isnumchar(c)) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1070 | ; /* just advance */ |
| 1071 | else if (c == '.') { |
| 1072 | /* |
| 1073 | * we need to deal with consequences of the legacy |
| 1074 | * parser, like "1.nolist" being two tokens |
| 1075 | * (TOK_NUMBER, TOK_ID) here; at least give it |
| 1076 | * a shot for now. In the future, we probably need |
| 1077 | * a flex-based scanner with proper pattern matching |
| 1078 | * to do it as well as it can be done. Nothing in |
| 1079 | * the world is going to help the person who wants |
| 1080 | * 0x123.p16 interpreted as two tokens, though. |
| 1081 | */ |
| 1082 | r = p; |
| 1083 | while (*r == '_') |
| 1084 | r++; |
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 (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) || |
| 1087 | (!is_hex && (*r == 'e' || *r == 'E')) || |
| 1088 | (*r == 'p' || *r == 'P')) { |
| 1089 | p = r; |
| 1090 | is_float = true; |
| 1091 | } else |
| 1092 | break; /* Terminate the token */ |
| 1093 | } else |
| 1094 | break; |
| 1095 | } |
| 1096 | p--; /* Point to first character beyond number */ |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1097 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1098 | if (p == line+1 && *line == '$') { |
| 1099 | type = TOK_OTHER; /* TOKEN_HERE */ |
| 1100 | } else { |
| 1101 | if (has_e && !is_hex) { |
| 1102 | /* 1e13 is floating-point, but 1e13h is not */ |
| 1103 | is_float = true; |
| 1104 | } |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 1105 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1106 | type = is_float ? TOK_FLOAT : TOK_NUMBER; |
| 1107 | } |
H. Peter Anvin | bda7a6e | 2008-06-21 10:23:17 -0700 | [diff] [blame] | 1108 | } else if (nasm_isspace(*p)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1109 | type = TOK_WHITESPACE; |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 1110 | p = nasm_skip_spaces(p); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1111 | /* |
| 1112 | * Whitespace just before end-of-line is discarded by |
| 1113 | * pretending it's a comment; whitespace just before a |
| 1114 | * comment gets lumped into the comment. |
| 1115 | */ |
| 1116 | if (!*p || *p == ';') { |
| 1117 | type = TOK_COMMENT; |
| 1118 | while (*p) |
| 1119 | p++; |
| 1120 | } |
| 1121 | } else if (*p == ';') { |
| 1122 | type = TOK_COMMENT; |
| 1123 | while (*p) |
| 1124 | p++; |
| 1125 | } else { |
| 1126 | /* |
| 1127 | * Anything else is an operator of some kind. We check |
| 1128 | * for all the double-character operators (>>, <<, //, |
| 1129 | * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1130 | * else is a single-character operator. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1131 | */ |
| 1132 | type = TOK_OTHER; |
| 1133 | if ((p[0] == '>' && p[1] == '>') || |
| 1134 | (p[0] == '<' && p[1] == '<') || |
| 1135 | (p[0] == '/' && p[1] == '/') || |
| 1136 | (p[0] == '<' && p[1] == '=') || |
| 1137 | (p[0] == '>' && p[1] == '=') || |
| 1138 | (p[0] == '=' && p[1] == '=') || |
| 1139 | (p[0] == '!' && p[1] == '=') || |
| 1140 | (p[0] == '<' && p[1] == '>') || |
| 1141 | (p[0] == '&' && p[1] == '&') || |
| 1142 | (p[0] == '|' && p[1] == '|') || |
| 1143 | (p[0] == '^' && p[1] == '^')) { |
| 1144 | p++; |
| 1145 | } |
| 1146 | p++; |
| 1147 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1148 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1149 | /* Handling unterminated string by UNV */ |
| 1150 | /*if (type == -1) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1151 | { |
| 1152 | *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1); |
| 1153 | t->text[p-line] = *line; |
| 1154 | tail = &t->next; |
| 1155 | } |
| 1156 | else */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1157 | if (type != TOK_COMMENT) { |
| 1158 | *tail = t = new_Token(NULL, type, line, p - line); |
| 1159 | tail = &t->next; |
| 1160 | } |
| 1161 | line = p; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1162 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1163 | return list; |
| 1164 | } |
| 1165 | |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1166 | /* |
| 1167 | * this function allocates a new managed block of memory and |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1168 | * returns a pointer to the block. The managed blocks are |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1169 | * deleted only all at once by the delete_Blocks function. |
| 1170 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1171 | static void *new_Block(size_t size) |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1172 | { |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1173 | Blocks *b = &blocks; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1174 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1175 | /* first, get to the end of the linked list */ |
| 1176 | while (b->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1177 | b = b->next; |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1178 | /* now allocate the requested chunk */ |
| 1179 | b->chunk = nasm_malloc(size); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1180 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1181 | /* now allocate a new block for the next request */ |
Cyrill Gorcunov | c31767c | 2014-06-28 02:22:17 +0400 | [diff] [blame] | 1182 | b->next = nasm_zalloc(sizeof(Blocks)); |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1183 | return b->chunk; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | /* |
| 1187 | * this function deletes all managed blocks of memory |
| 1188 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1189 | static void delete_Blocks(void) |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1190 | { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1191 | Blocks *a, *b = &blocks; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1192 | |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1193 | /* |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1194 | * keep in mind that the first block, pointed to by blocks |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1195 | * is a static and not dynamically allocated, so we don't |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1196 | * free it. |
| 1197 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1198 | while (b) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1199 | if (b->chunk) |
| 1200 | nasm_free(b->chunk); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1201 | a = b; |
| 1202 | b = b->next; |
| 1203 | if (a != &blocks) |
| 1204 | nasm_free(a); |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1205 | } |
Cyrill Gorcunov | dae24d7 | 2014-06-28 10:17:39 +0400 | [diff] [blame] | 1206 | memset(&blocks, 0, sizeof(blocks)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1207 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1208 | |
| 1209 | /* |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1210 | * this function creates a new Token and passes a pointer to it |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1211 | * back to the caller. It sets the type and text elements, and |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 1212 | * also the a.mac and next elements to NULL. |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1213 | */ |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1214 | static Token *new_Token(Token * next, enum pp_token_type type, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1215 | const char *text, int txtlen) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1216 | { |
| 1217 | Token *t; |
| 1218 | int i; |
| 1219 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1220 | if (!freeTokens) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1221 | freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token)); |
| 1222 | for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++) |
| 1223 | freeTokens[i].next = &freeTokens[i + 1]; |
| 1224 | freeTokens[i].next = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1225 | } |
| 1226 | t = freeTokens; |
| 1227 | freeTokens = t->next; |
| 1228 | t->next = next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 1229 | t->a.mac = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1230 | t->type = type; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1231 | if (type == TOK_WHITESPACE || !text) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1232 | t->text = NULL; |
| 1233 | } else { |
| 1234 | if (txtlen == 0) |
| 1235 | txtlen = strlen(text); |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1236 | t->text = nasm_malloc(txtlen+1); |
| 1237 | memcpy(t->text, text, txtlen); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1238 | t->text[txtlen] = '\0'; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1239 | } |
| 1240 | return t; |
| 1241 | } |
| 1242 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1243 | static Token *delete_Token(Token * t) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1244 | { |
| 1245 | Token *next = t->next; |
| 1246 | nasm_free(t->text); |
H. Peter Anvin | 788e6c1 | 2002-04-30 21:02:01 +0000 | [diff] [blame] | 1247 | t->next = freeTokens; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1248 | freeTokens = t; |
| 1249 | return next; |
| 1250 | } |
| 1251 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1252 | /* |
| 1253 | * Convert a line of tokens back into text. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1254 | * If expand_locals is not zero, identifiers of the form "%$*xxx" |
| 1255 | * will be transformed into ..@ctxnum.xxx |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1256 | */ |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 1257 | static char *detoken(Token * tlist, bool expand_locals) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1258 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1259 | Token *t; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1260 | char *line, *p; |
H. Peter Anvin | b4daadc | 2008-01-21 16:31:57 -0800 | [diff] [blame] | 1261 | const char *q; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1262 | int len = 0; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1263 | |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1264 | list_for_each(t, tlist) { |
Cyrill Gorcunov | 9b7ee09 | 2017-10-22 21:42:59 +0300 | [diff] [blame] | 1265 | if (t->type == TOK_PREPROC_ID && t->text && |
| 1266 | t->text[0] && t->text[1] == '!') { |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1267 | char *v; |
| 1268 | char *q = t->text; |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1269 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1270 | v = t->text + 2; |
H. Peter Anvin | 53e2e4c | 2018-11-28 15:01:40 -0800 | [diff] [blame] | 1271 | if (nasm_isquote(*v)) { |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1272 | size_t len = nasm_unquote(v, NULL); |
| 1273 | size_t clen = strlen(v); |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1274 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1275 | if (len != clen) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1276 | nasm_nonfatalf(ERR_PASS1, "NUL character in %%! string"); |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1277 | v = NULL; |
| 1278 | } |
| 1279 | } |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1280 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1281 | if (v) { |
| 1282 | char *p = getenv(v); |
| 1283 | if (!p) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1284 | nasm_nonfatalf(ERR_PASS1, "nonexistent environment variable `%s'", v); |
Cyrill Gorcunov | bbb7a1a | 2016-06-19 12:15:24 +0300 | [diff] [blame] | 1285 | /* |
| 1286 | * FIXME We better should investigate if accessing |
| 1287 | * ->text[1] without ->text[0] is safe enough. |
| 1288 | */ |
| 1289 | t->text = nasm_zalloc(2); |
| 1290 | } else |
| 1291 | t->text = nasm_strdup(p); |
Cyrill Gorcunov | 7500487 | 2017-07-26 01:21:16 +0300 | [diff] [blame] | 1292 | nasm_free(q); |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1293 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1294 | } |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1295 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1296 | /* Expand local macros here and not during preprocessing */ |
| 1297 | if (expand_locals && |
| 1298 | t->type == TOK_PREPROC_ID && t->text && |
| 1299 | t->text[0] == '%' && t->text[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1300 | const char *q; |
| 1301 | char *p; |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1302 | Context *ctx = get_ctx(t->text, &q); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1303 | if (ctx) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1304 | char buffer[40]; |
Keith Kanios | 93f2e9a | 2007-04-14 00:10:59 +0000 | [diff] [blame] | 1305 | snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1306 | p = nasm_strcat(buffer, q); |
| 1307 | nasm_free(t->text); |
| 1308 | t->text = p; |
| 1309 | } |
| 1310 | } |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1311 | if (t->type == TOK_WHITESPACE) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1312 | len++; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1313 | else if (t->text) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1314 | len += strlen(t->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1315 | } |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1316 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1317 | p = line = nasm_malloc(len + 1); |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1318 | |
| 1319 | list_for_each(t, tlist) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1320 | if (t->type == TOK_WHITESPACE) { |
H. Peter Anvin | b4daadc | 2008-01-21 16:31:57 -0800 | [diff] [blame] | 1321 | *p++ = ' '; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1322 | } else if (t->text) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1323 | q = t->text; |
| 1324 | while (*q) |
| 1325 | *p++ = *q++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1326 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1327 | } |
| 1328 | *p = '\0'; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1329 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1330 | return line; |
| 1331 | } |
| 1332 | |
| 1333 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1334 | * A scanner, suitable for use by the expression evaluator, which |
| 1335 | * operates on a line of Tokens. Expects a pointer to a pointer to |
| 1336 | * the first token in the line to be passed in as its private_data |
| 1337 | * field. |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1338 | * |
| 1339 | * FIX: This really needs to be unified with stdscan. |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1340 | */ |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 1341 | struct ppscan { |
| 1342 | Token *tptr; |
| 1343 | int ntokens; |
| 1344 | }; |
| 1345 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1346 | static int ppscan(void *private_data, struct tokenval *tokval) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1347 | { |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 1348 | struct ppscan *pps = private_data; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1349 | Token *tline; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1350 | char ourcopy[MAX_KEYWORD+1], *p, *r, *s; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1351 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1352 | do { |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 1353 | if (pps->ntokens && (tline = pps->tptr)) { |
| 1354 | pps->ntokens--; |
| 1355 | pps->tptr = tline->next; |
| 1356 | } else { |
| 1357 | pps->tptr = NULL; |
| 1358 | pps->ntokens = 0; |
| 1359 | return tokval->t_type = TOKEN_EOS; |
| 1360 | } |
| 1361 | } while (tline->type == TOK_WHITESPACE || tline->type == TOK_COMMENT); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1362 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1363 | tokval->t_charptr = tline->text; |
| 1364 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1365 | if (tline->text[0] == '$' && !tline->text[1]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1366 | return tokval->t_type = TOKEN_HERE; |
H. Peter Anvin | 7cf897e | 2002-05-30 21:30:33 +0000 | [diff] [blame] | 1367 | if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1368 | return tokval->t_type = TOKEN_BASE; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1369 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1370 | if (tline->type == TOK_ID) { |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1371 | p = tokval->t_charptr = tline->text; |
| 1372 | if (p[0] == '$') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1373 | tokval->t_charptr++; |
| 1374 | return tokval->t_type = TOKEN_ID; |
| 1375 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1376 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1377 | for (r = p, s = ourcopy; *r; r++) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1378 | if (r >= p+MAX_KEYWORD) |
| 1379 | return tokval->t_type = TOKEN_ID; /* Not a keyword */ |
H. Peter Anvin | ac8f8fc | 2008-06-11 15:49:41 -0700 | [diff] [blame] | 1380 | *s++ = nasm_tolower(*r); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1381 | } |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1382 | *s = '\0'; |
| 1383 | /* right, so we have an identifier sitting in temp storage. now, |
| 1384 | * is it actually a register or instruction name, or what? */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1385 | return nasm_token_hash(ourcopy, tokval); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1388 | if (tline->type == TOK_NUMBER) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1389 | bool rn_error; |
| 1390 | tokval->t_integer = readnum(tline->text, &rn_error); |
| 1391 | tokval->t_charptr = tline->text; |
| 1392 | if (rn_error) |
| 1393 | return tokval->t_type = TOKEN_ERRNUM; |
| 1394 | else |
| 1395 | return tokval->t_type = TOKEN_NUM; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1396 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1397 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1398 | if (tline->type == TOK_FLOAT) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1399 | return tokval->t_type = TOKEN_FLOAT; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1402 | if (tline->type == TOK_STRING) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1403 | char bq, *ep; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1404 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1405 | bq = tline->text[0]; |
H. Peter Anvin | 1162704 | 2008-06-09 20:45:19 -0700 | [diff] [blame] | 1406 | tokval->t_charptr = tline->text; |
| 1407 | tokval->t_inttwo = nasm_unquote(tline->text, &ep); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 1408 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1409 | if (ep[0] != bq || ep[1] != '\0') |
| 1410 | return tokval->t_type = TOKEN_ERRSTR; |
| 1411 | else |
| 1412 | return tokval->t_type = TOKEN_STR; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1415 | if (tline->type == TOK_OTHER) { |
| 1416 | if (!strcmp(tline->text, "<<")) |
| 1417 | return tokval->t_type = TOKEN_SHL; |
| 1418 | if (!strcmp(tline->text, ">>")) |
| 1419 | return tokval->t_type = TOKEN_SHR; |
| 1420 | if (!strcmp(tline->text, "//")) |
| 1421 | return tokval->t_type = TOKEN_SDIV; |
| 1422 | if (!strcmp(tline->text, "%%")) |
| 1423 | return tokval->t_type = TOKEN_SMOD; |
| 1424 | if (!strcmp(tline->text, "==")) |
| 1425 | return tokval->t_type = TOKEN_EQ; |
| 1426 | if (!strcmp(tline->text, "<>")) |
| 1427 | return tokval->t_type = TOKEN_NE; |
| 1428 | if (!strcmp(tline->text, "!=")) |
| 1429 | return tokval->t_type = TOKEN_NE; |
| 1430 | if (!strcmp(tline->text, "<=")) |
| 1431 | return tokval->t_type = TOKEN_LE; |
| 1432 | if (!strcmp(tline->text, ">=")) |
| 1433 | return tokval->t_type = TOKEN_GE; |
| 1434 | if (!strcmp(tline->text, "&&")) |
| 1435 | return tokval->t_type = TOKEN_DBL_AND; |
| 1436 | if (!strcmp(tline->text, "^^")) |
| 1437 | return tokval->t_type = TOKEN_DBL_XOR; |
| 1438 | if (!strcmp(tline->text, "||")) |
| 1439 | return tokval->t_type = TOKEN_DBL_OR; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | /* |
| 1443 | * We have no other options: just return the first character of |
| 1444 | * the token text. |
| 1445 | */ |
| 1446 | return tokval->t_type = tline->text[0]; |
| 1447 | } |
| 1448 | |
| 1449 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1450 | * Compare a string to the name of an existing macro; this is a |
| 1451 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 1452 | * depending on the value of the `casesense' parameter. |
| 1453 | */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 1454 | static int mstrcmp(const char *p, const char *q, bool casesense) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1455 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1456 | return casesense ? strcmp(p, q) : nasm_stricmp(p, q); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
| 1459 | /* |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1460 | * Compare a string to the name of an existing macro; this is a |
| 1461 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 1462 | * depending on the value of the `casesense' parameter. |
| 1463 | */ |
| 1464 | static int mmemcmp(const char *p, const char *q, size_t l, bool casesense) |
| 1465 | { |
| 1466 | return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l); |
| 1467 | } |
| 1468 | |
| 1469 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1470 | * Return the Context structure associated with a %$ token. Return |
| 1471 | * NULL, having _already_ reported an error condition, if the |
| 1472 | * context stack isn't deep enough for the supplied number of $ |
| 1473 | * signs. |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1474 | * |
| 1475 | * If "namep" is non-NULL, set it to the pointer to the macro name |
| 1476 | * tail, i.e. the part beyond %$... |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1477 | */ |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1478 | static Context *get_ctx(const char *name, const char **namep) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1479 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1480 | Context *ctx; |
| 1481 | int i; |
| 1482 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1483 | if (namep) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1484 | *namep = name; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1485 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1486 | if (!name || name[0] != '%' || name[1] != '$') |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1487 | return NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1488 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1489 | if (!cstk) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1490 | nasm_nonfatal("`%s': context stack is empty", name); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1491 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1492 | } |
| 1493 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1494 | name += 2; |
| 1495 | ctx = cstk; |
| 1496 | i = 0; |
| 1497 | while (ctx && *name == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1498 | name++; |
| 1499 | i++; |
| 1500 | ctx = ctx->next; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1501 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1502 | if (!ctx) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1503 | nasm_nonfatal("`%s': context stack is only" |
| 1504 | " %d level%s deep", name, i, (i == 1 ? "" : "s")); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1505 | return NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1506 | } |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1507 | |
| 1508 | if (namep) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1509 | *namep = name; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1510 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1511 | return ctx; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1512 | } |
| 1513 | |
| 1514 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1515 | * Open an include file. This routine must always return a valid |
| 1516 | * file pointer if it returns - it's responsible for throwing an |
| 1517 | * ERR_FATAL and bombing out completely if not. It should also try |
| 1518 | * the include path one by one until it finds the file or reaches |
| 1519 | * the end of the path. |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1520 | * |
| 1521 | * Note: for INC_PROBE the function returns NULL at all times; |
| 1522 | * instead look for the |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1523 | */ |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1524 | enum incopen_mode { |
| 1525 | INC_NEEDED, /* File must exist */ |
| 1526 | INC_OPTIONAL, /* Missing is OK */ |
| 1527 | INC_PROBE /* Only an existence probe */ |
| 1528 | }; |
| 1529 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1530 | /* This is conducts a full pathname search */ |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1531 | static FILE *inc_fopen_search(const char *file, char **slpath, |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1532 | enum incopen_mode omode, enum file_flags fmode) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1533 | { |
H. Peter Anvin (Intel) | 6447109 | 2018-12-11 13:06:14 -0800 | [diff] [blame] | 1534 | const struct strlist_entry *ip = strlist_head(ipath_list); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1535 | FILE *fp; |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1536 | const char *prefix = ""; |
night199uk | fdb1a1b | 2018-10-18 23:19:47 +0200 | [diff] [blame] | 1537 | char *sp; |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1538 | bool found; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1539 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1540 | while (1) { |
night199uk | fdb1a1b | 2018-10-18 23:19:47 +0200 | [diff] [blame] | 1541 | sp = nasm_catfile(prefix, file); |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1542 | if (omode == INC_PROBE) { |
| 1543 | fp = NULL; |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1544 | found = nasm_file_exists(sp); |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1545 | } else { |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1546 | fp = nasm_open_read(sp, fmode); |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1547 | found = (fp != NULL); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1548 | } |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1549 | if (found) { |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1550 | *slpath = sp; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1551 | return fp; |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1552 | } |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 1553 | |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1554 | nasm_free(sp); |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 1555 | |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1556 | if (!ip) { |
| 1557 | *slpath = NULL; |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1558 | return NULL; |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1559 | } |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1560 | |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1561 | prefix = ip->str; |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1562 | ip = ip->next; |
| 1563 | } |
| 1564 | } |
| 1565 | |
| 1566 | /* |
| 1567 | * Open a file, or test for the presence of one (depending on omode), |
| 1568 | * considering the include path. |
| 1569 | */ |
| 1570 | static FILE *inc_fopen(const char *file, |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 1571 | struct strlist *dhead, |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 1572 | const char **found_path, |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1573 | enum incopen_mode omode, |
| 1574 | enum file_flags fmode) |
| 1575 | { |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1576 | struct hash_insert hi; |
| 1577 | void **hp; |
| 1578 | char *path; |
| 1579 | FILE *fp = NULL; |
| 1580 | |
| 1581 | hp = hash_find(&FileHash, file, &hi); |
| 1582 | if (hp) { |
| 1583 | path = *hp; |
Martin Storsjö | f283c8f | 2017-08-13 17:28:46 +0300 | [diff] [blame] | 1584 | if (path || omode != INC_NEEDED) { |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 1585 | strlist_add(dhead, path ? path : file); |
Martin Storsjö | f283c8f | 2017-08-13 17:28:46 +0300 | [diff] [blame] | 1586 | } |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1587 | } else { |
| 1588 | /* Need to do the actual path search */ |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1589 | fp = inc_fopen_search(file, &path, omode, fmode); |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1590 | |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1591 | /* Positive or negative result */ |
| 1592 | hash_add(&hi, nasm_strdup(file), path); |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1593 | |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 1594 | /* |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1595 | * Add file to dependency path. |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 1596 | */ |
| 1597 | if (path || omode != INC_NEEDED) |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 1598 | strlist_add(dhead, file); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1599 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1600 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1601 | if (!path) { |
| 1602 | if (omode == INC_NEEDED) |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 1603 | nasm_fatal("unable to open include file `%s'", file); |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1604 | } else { |
| 1605 | if (!fp && omode != INC_PROBE) |
| 1606 | fp = nasm_open_read(path, fmode); |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1607 | } |
| 1608 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1609 | if (found_path) |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 1610 | *found_path = path; |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1611 | |
| 1612 | return fp; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1613 | } |
| 1614 | |
| 1615 | /* |
Fabian Giesen | 0bbc38d | 2016-04-28 13:48:14 -0700 | [diff] [blame] | 1616 | * Opens an include or input file. Public version, for use by modules |
| 1617 | * that get a file:lineno pair and need to look at the file again |
| 1618 | * (e.g. the CodeView debug backend). Returns NULL on failure. |
| 1619 | */ |
H. Peter Anvin | 3e83cec | 2016-05-25 04:28:46 -0700 | [diff] [blame] | 1620 | FILE *pp_input_fopen(const char *filename, enum file_flags mode) |
Fabian Giesen | 0bbc38d | 2016-04-28 13:48:14 -0700 | [diff] [blame] | 1621 | { |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 1622 | return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode); |
Fabian Giesen | 0bbc38d | 2016-04-28 13:48:14 -0700 | [diff] [blame] | 1623 | } |
| 1624 | |
| 1625 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1626 | * 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] | 1627 | * 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] | 1628 | * return true if _any_ single-line macro of that name is defined. |
| 1629 | * Otherwise, will return true if a single-line macro with either |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1630 | * `nparam' or no parameters is defined. |
| 1631 | * |
| 1632 | * If a macro with precisely the right number of parameters is |
H. Peter Anvin | ef7468f | 2002-04-30 20:57:59 +0000 | [diff] [blame] | 1633 | * defined, or nparam is -1, the address of the definition structure |
| 1634 | * 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] | 1635 | * is NULL, no action will be taken regarding its contents, and no |
| 1636 | * error will occur. |
| 1637 | * |
| 1638 | * Note that this is also called with nparam zero to resolve |
| 1639 | * `ifdef'. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1640 | * |
| 1641 | * If you already know which context macro belongs to, you can pass |
| 1642 | * the context pointer as first parameter; if you won't but name begins |
| 1643 | * with %$ the context will be automatically computed. If all_contexts |
| 1644 | * is true, macro will be searched in outer contexts as well. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1645 | */ |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1646 | static bool |
H. Peter Anvin | b2a5fda | 2008-06-19 21:42:42 -0700 | [diff] [blame] | 1647 | smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn, |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1648 | bool nocase) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1649 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1650 | struct hash_table *smtbl; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1651 | SMacro *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1652 | |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1653 | if (ctx) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1654 | smtbl = &ctx->localmac; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1655 | } else if (name[0] == '%' && name[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1656 | if (cstk) |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1657 | ctx = get_ctx(name, &name); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1658 | if (!ctx) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1659 | return false; /* got to return _something_ */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1660 | smtbl = &ctx->localmac; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1661 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1662 | smtbl = &smacros; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1663 | } |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1664 | m = (SMacro *) hash_findix(smtbl, name); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1665 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1666 | while (m) { |
| 1667 | if (!mstrcmp(m->name, name, m->casesense && nocase) && |
Charles Crayne | 192d5b5 | 2007-10-18 19:02:42 -0700 | [diff] [blame] | 1668 | (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1669 | if (defn) { |
Charles Crayne | 192d5b5 | 2007-10-18 19:02:42 -0700 | [diff] [blame] | 1670 | if (nparam == (int) m->nparam || nparam == -1) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1671 | *defn = m; |
| 1672 | else |
| 1673 | *defn = NULL; |
| 1674 | } |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1675 | return true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1676 | } |
| 1677 | m = m->next; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1678 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1679 | |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1680 | return false; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1681 | } |
| 1682 | |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 1683 | /* param should be a natural number [0; INT_MAX] */ |
| 1684 | static int read_param_count(const char *str) |
| 1685 | { |
| 1686 | int result; |
| 1687 | bool err; |
| 1688 | |
| 1689 | result = readnum(str, &err); |
| 1690 | if (result < 0 || result > INT_MAX) { |
| 1691 | result = 0; |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1692 | nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]", |
| 1693 | str, 0, INT_MAX); |
| 1694 | } else if (err) |
| 1695 | nasm_nonfatal("unable to parse parameter count `%s'", str); |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 1696 | return result; |
| 1697 | } |
| 1698 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1699 | /* |
| 1700 | * Count and mark off the parameters in a multi-line macro call. |
| 1701 | * This is called both from within the multi-line macro expansion |
| 1702 | * code, and also to mark off the default parameters when provided |
| 1703 | * in a %macro definition line. |
| 1704 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1705 | static void count_mmac_params(Token * t, int *nparam, Token *** params) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1706 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1707 | int paramsize, brace; |
| 1708 | |
| 1709 | *nparam = paramsize = 0; |
| 1710 | *params = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1711 | while (t) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1712 | /* +1: we need space for the final NULL */ |
H. Peter Anvin | 91fb6f1 | 2008-09-01 10:56:33 -0700 | [diff] [blame] | 1713 | if (*nparam+1 >= paramsize) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1714 | paramsize += PARAM_DELTA; |
| 1715 | *params = nasm_realloc(*params, sizeof(**params) * paramsize); |
| 1716 | } |
| 1717 | skip_white_(t); |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1718 | brace = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1719 | if (tok_is_(t, "{")) |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1720 | brace++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1721 | (*params)[(*nparam)++] = t; |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1722 | if (brace) { |
| 1723 | while (brace && (t = t->next) != NULL) { |
| 1724 | if (tok_is_(t, "{")) |
| 1725 | brace++; |
| 1726 | else if (tok_is_(t, "}")) |
| 1727 | brace--; |
| 1728 | } |
| 1729 | |
| 1730 | if (t) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1731 | /* |
| 1732 | * Now we've found the closing brace, look further |
| 1733 | * for the comma. |
| 1734 | */ |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1735 | t = t->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1736 | skip_white_(t); |
| 1737 | if (tok_isnt_(t, ",")) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1738 | nasm_nonfatal("braces do not enclose all of macro parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1739 | while (tok_isnt_(t, ",")) |
| 1740 | t = t->next; |
| 1741 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1742 | } |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1743 | } else { |
| 1744 | while (tok_isnt_(t, ",")) |
| 1745 | t = t->next; |
| 1746 | } |
| 1747 | if (t) { /* got a comma/brace */ |
| 1748 | t = t->next; /* eat the comma */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1749 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1750 | } |
| 1751 | } |
| 1752 | |
| 1753 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1754 | * Determine whether one of the various `if' conditions is true or |
| 1755 | * not. |
| 1756 | * |
| 1757 | * We must free the tline we get passed. |
| 1758 | */ |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1759 | static bool if_condition(Token * tline, enum preproc_token ct) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1760 | { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1761 | enum pp_conditional i = PP_COND(ct); |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1762 | bool j; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 1763 | Token *t, *tt, *origline; |
| 1764 | struct ppscan pps; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1765 | struct tokenval tokval; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1766 | expr *evalresult; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1767 | enum pp_token_type needtype; |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1768 | char *p; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1769 | |
| 1770 | origline = tline; |
| 1771 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1772 | switch (i) { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1773 | case PPC_IFCTX: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1774 | j = false; /* have we matched yet? */ |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1775 | while (true) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1776 | skip_white_(tline); |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1777 | if (!tline) |
| 1778 | break; |
| 1779 | if (tline->type != TOK_ID) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1780 | nasm_nonfatal("`%s' expects context identifiers", |
| 1781 | pp_directives[ct]); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1782 | free_tlist(origline); |
| 1783 | return -1; |
| 1784 | } |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1785 | if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1786 | j = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1787 | tline = tline->next; |
| 1788 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1789 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1790 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1791 | case PPC_IFDEF: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1792 | j = false; /* have we matched yet? */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1793 | while (tline) { |
| 1794 | skip_white_(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1795 | if (!tline || (tline->type != TOK_ID && |
| 1796 | (tline->type != TOK_PREPROC_ID || |
| 1797 | tline->text[1] != '$'))) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1798 | nasm_nonfatal("`%s' expects macro identifiers", |
| 1799 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1800 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1801 | } |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1802 | if (smacro_defined(NULL, tline->text, 0, NULL, true)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1803 | j = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1804 | tline = tline->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1805 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1806 | break; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1807 | |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1808 | case PPC_IFENV: |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1809 | tline = expand_smacro(tline); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1810 | j = false; /* have we matched yet? */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1811 | while (tline) { |
| 1812 | skip_white_(tline); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1813 | if (!tline || (tline->type != TOK_ID && |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1814 | tline->type != TOK_STRING && |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1815 | (tline->type != TOK_PREPROC_ID || |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1816 | tline->text[1] != '!'))) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1817 | nasm_nonfatal("`%s' expects environment variable names", |
| 1818 | pp_directives[ct]); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1819 | goto fail; |
| 1820 | } |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1821 | p = tline->text; |
| 1822 | if (tline->type == TOK_PREPROC_ID) |
| 1823 | p += 2; /* Skip leading %! */ |
H. Peter Anvin | 53e2e4c | 2018-11-28 15:01:40 -0800 | [diff] [blame] | 1824 | if (nasm_isquote(*p)) |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1825 | nasm_unquote_cstr(p, ct); |
| 1826 | if (getenv(p)) |
| 1827 | j = true; |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1828 | tline = tline->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1829 | } |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1830 | break; |
| 1831 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1832 | case PPC_IFIDN: |
| 1833 | case PPC_IFIDNI: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1834 | tline = expand_smacro(tline); |
| 1835 | t = tt = tline; |
| 1836 | while (tok_isnt_(tt, ",")) |
| 1837 | tt = tt->next; |
| 1838 | if (!tt) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1839 | nasm_nonfatal("`%s' expects two comma-separated arguments", |
| 1840 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1841 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1842 | } |
| 1843 | tt = tt->next; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1844 | j = true; /* assume equality unless proved not */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1845 | while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) { |
| 1846 | if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1847 | nasm_nonfatal("`%s': more than one comma on line", |
| 1848 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1849 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1850 | } |
| 1851 | if (t->type == TOK_WHITESPACE) { |
| 1852 | t = t->next; |
| 1853 | continue; |
| 1854 | } |
| 1855 | if (tt->type == TOK_WHITESPACE) { |
| 1856 | tt = tt->next; |
| 1857 | continue; |
| 1858 | } |
| 1859 | if (tt->type != t->type) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1860 | j = false; /* found mismatching tokens */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1861 | break; |
| 1862 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1863 | /* When comparing strings, need to unquote them first */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1864 | if (t->type == TOK_STRING) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1865 | size_t l1 = nasm_unquote(t->text, NULL); |
| 1866 | size_t l2 = nasm_unquote(tt->text, NULL); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 1867 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1868 | if (l1 != l2) { |
| 1869 | j = false; |
| 1870 | break; |
| 1871 | } |
| 1872 | if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) { |
| 1873 | j = false; |
| 1874 | break; |
| 1875 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1876 | } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1877 | j = false; /* found mismatching tokens */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1878 | break; |
| 1879 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1880 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1881 | t = t->next; |
| 1882 | tt = tt->next; |
| 1883 | } |
| 1884 | if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1885 | j = false; /* trailing gunk on one end or other */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1886 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1887 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1888 | case PPC_IFMACRO: |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1889 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1890 | bool found = false; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1891 | MMacro searching, *mmac; |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 1892 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1893 | skip_white_(tline); |
| 1894 | tline = expand_id(tline); |
| 1895 | if (!tok_type_(tline, TOK_ID)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1896 | nasm_nonfatal("`%s' expects a macro name", pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1897 | goto fail; |
| 1898 | } |
| 1899 | searching.name = nasm_strdup(tline->text); |
| 1900 | searching.casesense = true; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1901 | searching.plus = false; |
| 1902 | searching.nolist = false; |
| 1903 | searching.in_progress = 0; |
| 1904 | searching.max_depth = 0; |
| 1905 | searching.rep_nest = NULL; |
| 1906 | searching.nparam_min = 0; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1907 | searching.nparam_max = INT_MAX; |
| 1908 | tline = expand_smacro(tline->next); |
| 1909 | skip_white_(tline); |
| 1910 | if (!tline) { |
| 1911 | } else if (!tok_type_(tline, TOK_NUMBER)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1912 | nasm_nonfatal("`%s' expects a parameter count or nothing", |
| 1913 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1914 | } else { |
| 1915 | searching.nparam_min = searching.nparam_max = |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 1916 | read_param_count(tline->text); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1917 | } |
| 1918 | if (tline && tok_is_(tline->next, "-")) { |
| 1919 | tline = tline->next->next; |
| 1920 | if (tok_is_(tline, "*")) |
| 1921 | searching.nparam_max = INT_MAX; |
| 1922 | else if (!tok_type_(tline, TOK_NUMBER)) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1923 | nasm_nonfatal("`%s' expects a parameter count after `-'", |
| 1924 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1925 | else { |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 1926 | searching.nparam_max = read_param_count(tline->text); |
Cyrill Gorcunov | c9244ea | 2017-10-22 15:25:48 +0300 | [diff] [blame] | 1927 | if (searching.nparam_min > searching.nparam_max) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 1928 | nasm_nonfatal("minimum parameter count exceeds maximum"); |
Cyrill Gorcunov | c9244ea | 2017-10-22 15:25:48 +0300 | [diff] [blame] | 1929 | searching.nparam_max = searching.nparam_min; |
| 1930 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1931 | } |
| 1932 | } |
| 1933 | if (tline && tok_is_(tline->next, "+")) { |
| 1934 | tline = tline->next; |
| 1935 | searching.plus = true; |
| 1936 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1937 | mmac = (MMacro *) hash_findix(&mmacros, searching.name); |
| 1938 | while (mmac) { |
| 1939 | if (!strcmp(mmac->name, searching.name) && |
| 1940 | (mmac->nparam_min <= searching.nparam_max |
| 1941 | || searching.plus) |
| 1942 | && (searching.nparam_min <= mmac->nparam_max |
| 1943 | || mmac->plus)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1944 | found = true; |
| 1945 | break; |
| 1946 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1947 | mmac = mmac->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1948 | } |
| 1949 | if (tline && tline->next) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 1950 | nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1951 | nasm_free(searching.name); |
| 1952 | j = found; |
| 1953 | break; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1954 | } |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 1955 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1956 | case PPC_IFID: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1957 | needtype = TOK_ID; |
| 1958 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1959 | case PPC_IFNUM: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1960 | needtype = TOK_NUMBER; |
| 1961 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1962 | case PPC_IFSTR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1963 | needtype = TOK_STRING; |
| 1964 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1965 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1966 | iftype: |
| 1967 | t = tline = expand_smacro(tline); |
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 | while (tok_type_(t, TOK_WHITESPACE) || |
| 1970 | (needtype == TOK_NUMBER && |
| 1971 | tok_type_(t, TOK_OTHER) && |
| 1972 | (t->text[0] == '-' || t->text[0] == '+') && |
| 1973 | !t->text[1])) |
| 1974 | t = t->next; |
H. Peter Anvin | d85d250 | 2008-05-04 17:53:31 -0700 | [diff] [blame] | 1975 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1976 | j = tok_type_(t, needtype); |
| 1977 | break; |
H. Peter Anvin | cbf768d | 2008-02-16 16:41:25 -0800 | [diff] [blame] | 1978 | |
| 1979 | case PPC_IFTOKEN: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1980 | t = tline = expand_smacro(tline); |
| 1981 | while (tok_type_(t, TOK_WHITESPACE)) |
| 1982 | t = t->next; |
H. Peter Anvin | cbf768d | 2008-02-16 16:41:25 -0800 | [diff] [blame] | 1983 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1984 | j = false; |
| 1985 | if (t) { |
| 1986 | t = t->next; /* Skip the actual token */ |
| 1987 | while (tok_type_(t, TOK_WHITESPACE)) |
| 1988 | t = t->next; |
| 1989 | j = !t; /* Should be nothing left */ |
| 1990 | } |
| 1991 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1992 | |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 1993 | case PPC_IFEMPTY: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1994 | t = tline = expand_smacro(tline); |
| 1995 | while (tok_type_(t, TOK_WHITESPACE)) |
| 1996 | t = t->next; |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 1997 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1998 | j = !t; /* Should be empty */ |
| 1999 | break; |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 2000 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2001 | case PPC_IF: |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2002 | pps.tptr = tline = expand_smacro(tline); |
| 2003 | pps.ntokens = -1; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2004 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 2005 | evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2006 | if (!evalresult) |
| 2007 | return -1; |
| 2008 | if (tokval.t_type) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2009 | nasm_warn(WARN_OTHER, "trailing garbage after expression ignored"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2010 | if (!is_simple(evalresult)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2011 | nasm_nonfatal("non-constant value given to `%s'", |
| 2012 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2013 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2014 | } |
Chuck Crayne | 60ae75d | 2007-05-02 01:59:16 +0000 | [diff] [blame] | 2015 | j = reloc_value(evalresult) != 0; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2016 | break; |
H. Peter Anvin | 95e2882 | 2007-09-12 04:20:08 +0000 | [diff] [blame] | 2017 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2018 | default: |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2019 | nasm_fatal("preprocessor directive `%s' not yet implemented", |
| 2020 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2021 | goto fail; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2022 | } |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2023 | |
| 2024 | free_tlist(origline); |
| 2025 | return j ^ PP_NEGATIVE(ct); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2026 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2027 | fail: |
| 2028 | free_tlist(origline); |
| 2029 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2030 | } |
| 2031 | |
| 2032 | /* |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2033 | * Common code for defining an smacro |
| 2034 | */ |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2035 | static SMacro *define_smacro(Context *ctx, const char *mname, |
| 2036 | bool casesense, int nparam, Token *expansion) |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2037 | { |
| 2038 | SMacro *smac, **smhead; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2039 | struct hash_table *smtbl; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2040 | |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2041 | if (smacro_defined(ctx, mname, nparam, &smac, casesense)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2042 | if (!smac) { |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2043 | nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and" |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2044 | " without parameters", mname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2045 | /* |
| 2046 | * Some instances of the old code considered this a failure, |
| 2047 | * some others didn't. What is the right thing to do here? |
| 2048 | */ |
| 2049 | free_tlist(expansion); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2050 | return NULL; /* Failure */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2051 | } else { |
| 2052 | /* |
| 2053 | * We're redefining, so we have to take over an |
| 2054 | * existing SMacro structure. This means freeing |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2055 | * what was already in it, but not the structure itself. |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2056 | */ |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2057 | free_smacro(smac, false); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2058 | } |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2059 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2060 | smtbl = ctx ? &ctx->localmac : &smacros; |
| 2061 | smhead = (SMacro **) hash_findi_add(smtbl, mname); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2062 | nasm_new(smac); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2063 | smac->next = *smhead; |
| 2064 | *smhead = smac; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2065 | } |
| 2066 | smac->name = nasm_strdup(mname); |
| 2067 | smac->casesense = casesense; |
| 2068 | smac->nparam = nparam; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2069 | smac->e.expansion = expansion; |
| 2070 | return smac; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2071 | } |
| 2072 | |
| 2073 | /* |
| 2074 | * Undefine an smacro |
| 2075 | */ |
| 2076 | static void undef_smacro(Context *ctx, const char *mname) |
| 2077 | { |
| 2078 | SMacro **smhead, *s, **sp; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2079 | struct hash_table *smtbl; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2080 | |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2081 | smtbl = ctx ? &ctx->localmac : &smacros; |
| 2082 | smhead = (SMacro **)hash_findi(smtbl, mname, NULL); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2083 | |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2084 | if (smhead) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2085 | /* |
| 2086 | * We now have a macro name... go hunt for it. |
| 2087 | */ |
| 2088 | sp = smhead; |
| 2089 | while ((s = *sp) != NULL) { |
| 2090 | if (!mstrcmp(s->name, mname, s->casesense)) { |
| 2091 | *sp = s->next; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2092 | free_smacro(s, true); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2093 | } else { |
| 2094 | sp = &s->next; |
| 2095 | } |
| 2096 | } |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2097 | } |
| 2098 | } |
| 2099 | |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2100 | /* |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2101 | * Parse a mmacro specification. |
| 2102 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2103 | 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] | 2104 | { |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2105 | tline = tline->next; |
| 2106 | skip_white_(tline); |
| 2107 | tline = expand_id(tline); |
| 2108 | if (!tok_type_(tline, TOK_ID)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2109 | nasm_nonfatal("`%s' expects a macro name", directive); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2110 | return false; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2111 | } |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2112 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2113 | def->prev = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2114 | def->name = nasm_strdup(tline->text); |
| 2115 | def->plus = false; |
| 2116 | def->nolist = false; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2117 | def->in_progress = 0; |
| 2118 | def->rep_nest = NULL; |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2119 | def->nparam_min = 0; |
| 2120 | def->nparam_max = 0; |
| 2121 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2122 | tline = expand_smacro(tline->next); |
| 2123 | skip_white_(tline); |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2124 | if (!tok_type_(tline, TOK_NUMBER)) |
| 2125 | nasm_nonfatal("`%s' expects a parameter count", directive); |
| 2126 | else |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 2127 | def->nparam_min = def->nparam_max = read_param_count(tline->text); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2128 | if (tline && tok_is_(tline->next, "-")) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2129 | tline = tline->next->next; |
| 2130 | if (tok_is_(tline, "*")) { |
| 2131 | def->nparam_max = INT_MAX; |
| 2132 | } else if (!tok_type_(tline, TOK_NUMBER)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2133 | nasm_nonfatal("`%s' expects a parameter count after `-'", directive); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2134 | } else { |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 2135 | def->nparam_max = read_param_count(tline->text); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2136 | if (def->nparam_min > def->nparam_max) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2137 | nasm_nonfatal("minimum parameter count exceeds maximum"); |
Cyrill Gorcunov | c9244ea | 2017-10-22 15:25:48 +0300 | [diff] [blame] | 2138 | def->nparam_max = def->nparam_min; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2139 | } |
| 2140 | } |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2141 | } |
| 2142 | if (tline && tok_is_(tline->next, "+")) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2143 | tline = tline->next; |
| 2144 | def->plus = true; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2145 | } |
| 2146 | if (tline && tok_type_(tline->next, TOK_ID) && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2147 | !nasm_stricmp(tline->next->text, ".nolist")) { |
| 2148 | tline = tline->next; |
| 2149 | def->nolist = true; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2150 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2151 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2152 | /* |
| 2153 | * Handle default parameters. |
| 2154 | */ |
| 2155 | if (tline && tline->next) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2156 | def->dlist = tline->next; |
| 2157 | tline->next = NULL; |
| 2158 | count_mmac_params(def->dlist, &def->ndefs, &def->defaults); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2159 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2160 | def->dlist = NULL; |
| 2161 | def->defaults = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2162 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2163 | def->expansion = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2164 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2165 | 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] | 2166 | !def->plus) { |
| 2167 | /* |
| 2168 | *!macro-defaults [on] macros with more default than optional parameters |
| 2169 | *! warns when a macro has more default parameters than optional parameters. |
| 2170 | *! See \k{mlmacdef} for why might want to disable this warning. |
| 2171 | */ |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2172 | nasm_warn(WARN_MACRO_DEFAULTS, |
H. Peter Anvin (Intel) | 723ab48 | 2018-12-13 21:53:31 -0800 | [diff] [blame] | 2173 | "too many default macro parameters in macro `%s'", def->name); |
| 2174 | } |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2175 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2176 | return true; |
| 2177 | } |
| 2178 | |
| 2179 | |
| 2180 | /* |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2181 | * Decode a size directive |
| 2182 | */ |
| 2183 | static int parse_size(const char *str) { |
| 2184 | static const char *size_names[] = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2185 | { "byte", "dword", "oword", "qword", "tword", "word", "yword" }; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2186 | static const int sizes[] = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2187 | { 0, 1, 4, 16, 8, 10, 2, 32 }; |
Cyrill Gorcunov | c713b5f | 2018-09-29 14:30:14 +0300 | [diff] [blame] | 2188 | 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] | 2189 | } |
| 2190 | |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2191 | /* |
| 2192 | * Process a preprocessor %pragma directive. Currently there are none. |
| 2193 | * Gets passed the token list starting with the "preproc" token from |
| 2194 | * "%pragma preproc". |
| 2195 | */ |
| 2196 | static void do_pragma_preproc(Token *tline) |
| 2197 | { |
| 2198 | /* Skip to the real stuff */ |
| 2199 | tline = tline->next; |
| 2200 | skip_white_(tline); |
| 2201 | if (!tline) |
| 2202 | return; |
| 2203 | |
| 2204 | (void)tline; /* Nothing else to do at present */ |
| 2205 | } |
| 2206 | |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2207 | /** |
| 2208 | * find and process preprocessor directive in passed line |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2209 | * Find out if a line contains a preprocessor directive, and deal |
| 2210 | * with it if so. |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2211 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2212 | * If a directive _is_ found, it is the responsibility of this routine |
| 2213 | * (and not the caller) to free_tlist() the line. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2214 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2215 | * @param tline a pointer to the current tokeninzed line linked list |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2216 | * @param output if this directive generated output |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2217 | * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2218 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2219 | */ |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2220 | static int do_directive(Token *tline, char **output) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2221 | { |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2222 | enum preproc_token i; |
| 2223 | int j; |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 2224 | bool err; |
| 2225 | int nparam; |
| 2226 | bool nolist; |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 2227 | bool casesense; |
H. Peter Anvin | 8cfdb9d | 2007-09-14 18:36:01 -0700 | [diff] [blame] | 2228 | int k, m; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2229 | int offset; |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 2230 | char *p, *pp; |
| 2231 | const char *found_path; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 2232 | const char *mname; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2233 | struct ppscan pps; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2234 | Include *inc; |
| 2235 | Context *ctx; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2236 | Cond *cond; |
| 2237 | MMacro *mmac, **mmhead; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2238 | Token *t = NULL, *tt, *param_start, *macro_start, *last, *origline; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2239 | Line *l; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2240 | struct tokenval tokval; |
| 2241 | expr *evalresult; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2242 | MMacro *tmp_defining; /* Used when manipulating rep_nest */ |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 2243 | int64_t count; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 2244 | size_t len; |
H. Peter Anvin (Intel) | 6bde2ed | 2018-12-13 19:39:41 -0800 | [diff] [blame] | 2245 | errflags severity; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2246 | const char *dname; /* Name of directive, for messages */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2247 | |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2248 | *output = NULL; /* No output generated */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2249 | origline = tline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2250 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2251 | skip_white_(tline); |
H. Peter Anvin | f2936d7 | 2008-06-04 15:11:23 -0700 | [diff] [blame] | 2252 | if (!tline || !tok_type_(tline, TOK_PREPROC_ID) || |
Cyrill Gorcunov | 4b5b737 | 2018-10-29 22:54:08 +0300 | [diff] [blame] | 2253 | (tline->text[0] && (tline->text[1] == '%' || |
| 2254 | tline->text[1] == '$' || |
| 2255 | tline->text[1] == '!'))) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2256 | return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2257 | |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2258 | i = pp_token_hash(tline->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2259 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2260 | /* |
| 2261 | * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO |
| 2262 | * since they are known to be buggy at moment, we need to fix them |
| 2263 | * in future release (2.09-2.10) |
| 2264 | */ |
Philipp Kloke | b432f57 | 2013-03-31 12:01:23 +0200 | [diff] [blame] | 2265 | if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2266 | nasm_nonfatal("unknown preprocessor directive `%s'", tline->text); |
| 2267 | return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2268 | } |
| 2269 | |
| 2270 | /* |
| 2271 | * If we're in a non-emitting branch of a condition construct, |
| 2272 | * or walking to the end of an already terminated %rep block, |
| 2273 | * we should ignore all directives except for condition |
| 2274 | * directives. |
| 2275 | */ |
| 2276 | if (((istk->conds && !emitting(istk->conds->state)) || |
| 2277 | (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) { |
| 2278 | return NO_DIRECTIVE_FOUND; |
| 2279 | } |
| 2280 | |
| 2281 | /* |
| 2282 | * If we're defining a macro or reading a %rep block, we should |
| 2283 | * ignore all directives except for %macro/%imacro (which nest), |
| 2284 | * %endm/%endmacro, and (only if we're in a %rep block) %endrep. |
| 2285 | * If we're in a %rep block, another %rep nests, so should be let through. |
| 2286 | */ |
| 2287 | if (defining && i != PP_MACRO && i != PP_IMACRO && |
| 2288 | i != PP_RMACRO && i != PP_IRMACRO && |
| 2289 | i != PP_ENDMACRO && i != PP_ENDM && |
| 2290 | (defining->name || (i != PP_ENDREP && i != PP_REP))) { |
| 2291 | return NO_DIRECTIVE_FOUND; |
| 2292 | } |
| 2293 | |
| 2294 | if (defining) { |
| 2295 | if (i == PP_MACRO || i == PP_IMACRO || |
| 2296 | i == PP_RMACRO || i == PP_IRMACRO) { |
| 2297 | nested_mac_count++; |
| 2298 | return NO_DIRECTIVE_FOUND; |
| 2299 | } else if (nested_mac_count > 0) { |
| 2300 | if (i == PP_ENDMACRO) { |
| 2301 | nested_mac_count--; |
| 2302 | return NO_DIRECTIVE_FOUND; |
| 2303 | } |
| 2304 | } |
| 2305 | if (!defining->name) { |
| 2306 | if (i == PP_REP) { |
| 2307 | nested_rep_count++; |
| 2308 | return NO_DIRECTIVE_FOUND; |
| 2309 | } else if (nested_rep_count > 0) { |
| 2310 | if (i == PP_ENDREP) { |
| 2311 | nested_rep_count--; |
| 2312 | return NO_DIRECTIVE_FOUND; |
| 2313 | } |
| 2314 | } |
| 2315 | } |
| 2316 | } |
| 2317 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2318 | dname = pp_directives[i]; /* Directive name, for error messages */ |
| 2319 | casesense = true; /* Default to case sensitive */ |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2320 | switch (i) { |
| 2321 | case PP_INVALID: |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2322 | nasm_nonfatal("unknown preprocessor directive `%s'", tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2323 | return NO_DIRECTIVE_FOUND; /* didn't get it */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2324 | |
H. Peter Anvin | 3f87a2a | 2016-10-04 14:07:19 -0700 | [diff] [blame] | 2325 | case PP_PRAGMA: |
| 2326 | /* |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2327 | * %pragma namespace options... |
| 2328 | * |
| 2329 | * The namespace "preproc" is reserved for the preprocessor; |
| 2330 | * all other namespaces generate a [pragma] assembly directive. |
| 2331 | * |
| 2332 | * Invalid %pragmas are ignored and may have different |
| 2333 | * meaning in future versions of NASM. |
H. Peter Anvin | 3f87a2a | 2016-10-04 14:07:19 -0700 | [diff] [blame] | 2334 | */ |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2335 | tline = tline->next; |
| 2336 | skip_white_(tline); |
| 2337 | tline = expand_smacro(tline); |
| 2338 | if (tok_type_(tline, TOK_ID)) { |
| 2339 | if (!nasm_stricmp(tline->text, "preproc")) { |
| 2340 | /* Preprocessor pragma */ |
| 2341 | do_pragma_preproc(tline); |
| 2342 | } else { |
| 2343 | /* Build the assembler directive */ |
| 2344 | t = new_Token(NULL, TOK_OTHER, "[", 1); |
| 2345 | t->next = new_Token(NULL, TOK_ID, "pragma", 6); |
| 2346 | t->next->next = new_Token(tline, TOK_WHITESPACE, NULL, 0); |
| 2347 | tline = t; |
| 2348 | for (t = tline; t->next; t = t->next) |
| 2349 | ; |
| 2350 | t->next = new_Token(NULL, TOK_OTHER, "]", 1); |
| 2351 | /* true here can be revisited in the future */ |
| 2352 | *output = detoken(tline, true); |
| 2353 | } |
| 2354 | } |
H. Peter Anvin | 3f87a2a | 2016-10-04 14:07:19 -0700 | [diff] [blame] | 2355 | free_tlist(origline); |
| 2356 | return DIRECTIVE_FOUND; |
| 2357 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2358 | case PP_STACKSIZE: |
| 2359 | /* Directive to tell NASM what the default stack size is. The |
| 2360 | * default is for a 16-bit stack, and this can be overriden with |
| 2361 | * %stacksize large. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2362 | */ |
| 2363 | tline = tline->next; |
| 2364 | if (tline && tline->type == TOK_WHITESPACE) |
| 2365 | tline = tline->next; |
| 2366 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 2367 | nasm_nonfatal("`%s' missing size parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2368 | free_tlist(origline); |
| 2369 | return DIRECTIVE_FOUND; |
| 2370 | } |
| 2371 | if (nasm_stricmp(tline->text, "flat") == 0) { |
| 2372 | /* All subsequent ARG directives are for a 32-bit stack */ |
| 2373 | StackSize = 4; |
| 2374 | StackPointer = "ebp"; |
| 2375 | ArgOffset = 8; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2376 | LocalOffset = 0; |
Charles Crayne | 7eaf919 | 2007-11-08 22:11:14 -0800 | [diff] [blame] | 2377 | } else if (nasm_stricmp(tline->text, "flat64") == 0) { |
| 2378 | /* All subsequent ARG directives are for a 64-bit stack */ |
| 2379 | StackSize = 8; |
| 2380 | StackPointer = "rbp"; |
Per Jessen | 53252e0 | 2010-02-11 00:16:59 +0300 | [diff] [blame] | 2381 | ArgOffset = 16; |
Charles Crayne | 7eaf919 | 2007-11-08 22:11:14 -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, "large") == 0) { |
| 2384 | /* All subsequent ARG directives are for a 16-bit stack, |
| 2385 | * far function call. |
| 2386 | */ |
| 2387 | StackSize = 2; |
| 2388 | StackPointer = "bp"; |
| 2389 | ArgOffset = 4; |
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 if (nasm_stricmp(tline->text, "small") == 0) { |
| 2392 | /* All subsequent ARG directives are for a 16-bit stack, |
| 2393 | * far function call. We don't support near functions. |
| 2394 | */ |
| 2395 | StackSize = 2; |
| 2396 | StackPointer = "bp"; |
| 2397 | ArgOffset = 6; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2398 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2399 | } else { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 2400 | nasm_nonfatal("`%s' invalid size type", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2401 | free_tlist(origline); |
| 2402 | return DIRECTIVE_FOUND; |
| 2403 | } |
| 2404 | free_tlist(origline); |
| 2405 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2406 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2407 | case PP_ARG: |
| 2408 | /* TASM like ARG directive to define arguments to functions, in |
| 2409 | * the following form: |
| 2410 | * |
| 2411 | * ARG arg1:WORD, arg2:DWORD, arg4:QWORD |
| 2412 | */ |
| 2413 | offset = ArgOffset; |
| 2414 | do { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 2415 | char *arg, directive[256]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2416 | int size = StackSize; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2417 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2418 | /* Find the argument name */ |
| 2419 | tline = tline->next; |
| 2420 | if (tline && tline->type == TOK_WHITESPACE) |
| 2421 | tline = tline->next; |
| 2422 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 2423 | nasm_nonfatal("`%s' missing argument parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2424 | free_tlist(origline); |
| 2425 | return DIRECTIVE_FOUND; |
| 2426 | } |
| 2427 | arg = tline->text; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2428 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2429 | /* Find the argument size type */ |
| 2430 | tline = tline->next; |
| 2431 | if (!tline || tline->type != TOK_OTHER |
| 2432 | || tline->text[0] != ':') { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 2433 | nasm_nonfatal("syntax error processing `%s' directive", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2434 | free_tlist(origline); |
| 2435 | return DIRECTIVE_FOUND; |
| 2436 | } |
| 2437 | tline = tline->next; |
| 2438 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 2439 | nasm_nonfatal("`%s' missing size type parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2440 | free_tlist(origline); |
| 2441 | return DIRECTIVE_FOUND; |
| 2442 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2443 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2444 | /* Allow macro expansion of type parameter */ |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2445 | tt = tokenize(tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2446 | tt = expand_smacro(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2447 | size = parse_size(tt->text); |
| 2448 | if (!size) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 2449 | nasm_nonfatal("invalid size type for `%s' missing directive", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2450 | free_tlist(tt); |
| 2451 | free_tlist(origline); |
| 2452 | return DIRECTIVE_FOUND; |
| 2453 | } |
| 2454 | free_tlist(tt); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2455 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2456 | /* Round up to even stack slots */ |
| 2457 | size = ALIGN(size, StackSize); |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2458 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2459 | /* Now define the macro for the argument */ |
| 2460 | snprintf(directive, sizeof(directive), "%%define %s (%s+%d)", |
| 2461 | arg, StackPointer, offset); |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2462 | do_directive(tokenize(directive), output); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2463 | offset += size; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2464 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2465 | /* Move to the next argument in the list */ |
| 2466 | tline = tline->next; |
| 2467 | if (tline && tline->type == TOK_WHITESPACE) |
| 2468 | tline = tline->next; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2469 | } while (tline && tline->type == TOK_OTHER && tline->text[0] == ','); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2470 | ArgOffset = offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2471 | free_tlist(origline); |
| 2472 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2473 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2474 | case PP_LOCAL: |
| 2475 | /* TASM like LOCAL directive to define local variables for a |
| 2476 | * function, in the following form: |
| 2477 | * |
| 2478 | * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize |
| 2479 | * |
| 2480 | * The '= LocalSize' at the end is ignored by NASM, but is |
| 2481 | * required by TASM to define the local parameter size (and used |
| 2482 | * by the TASM macro package). |
| 2483 | */ |
| 2484 | offset = LocalOffset; |
| 2485 | do { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 2486 | char *local, directive[256]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2487 | int size = StackSize; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2488 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2489 | /* Find the argument name */ |
| 2490 | tline = tline->next; |
| 2491 | if (tline && tline->type == TOK_WHITESPACE) |
| 2492 | tline = tline->next; |
| 2493 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 2494 | nasm_nonfatal("`%s' missing argument parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2495 | free_tlist(origline); |
| 2496 | return DIRECTIVE_FOUND; |
| 2497 | } |
| 2498 | local = tline->text; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2499 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2500 | /* Find the argument size type */ |
| 2501 | tline = tline->next; |
| 2502 | if (!tline || tline->type != TOK_OTHER |
| 2503 | || tline->text[0] != ':') { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 2504 | nasm_nonfatal("syntax error processing `%s' directive", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2505 | free_tlist(origline); |
| 2506 | return DIRECTIVE_FOUND; |
| 2507 | } |
| 2508 | tline = tline->next; |
| 2509 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 2510 | nasm_nonfatal("`%s' missing size type parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2511 | free_tlist(origline); |
| 2512 | return DIRECTIVE_FOUND; |
| 2513 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2514 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2515 | /* Allow macro expansion of type parameter */ |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2516 | tt = tokenize(tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2517 | tt = expand_smacro(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2518 | size = parse_size(tt->text); |
| 2519 | if (!size) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 2520 | nasm_nonfatal("invalid size type for `%s' missing directive", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2521 | free_tlist(tt); |
| 2522 | free_tlist(origline); |
| 2523 | return DIRECTIVE_FOUND; |
| 2524 | } |
| 2525 | free_tlist(tt); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2526 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2527 | /* Round up to even stack slots */ |
| 2528 | size = ALIGN(size, StackSize); |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2529 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2530 | offset += size; /* Negative offset, increment before */ |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2531 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2532 | /* Now define the macro for the argument */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2533 | snprintf(directive, sizeof(directive), "%%define %s (%s-%d)", |
| 2534 | local, StackPointer, offset); |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2535 | do_directive(tokenize(directive), output); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2536 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2537 | /* Now define the assign to setup the enter_c macro correctly */ |
| 2538 | snprintf(directive, sizeof(directive), |
| 2539 | "%%assign %%$localsize %%$localsize+%d", size); |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2540 | do_directive(tokenize(directive), output); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2541 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2542 | /* Move to the next argument in the list */ |
| 2543 | tline = tline->next; |
| 2544 | if (tline && tline->type == TOK_WHITESPACE) |
| 2545 | tline = tline->next; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2546 | } while (tline && tline->type == TOK_OTHER && tline->text[0] == ','); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2547 | LocalOffset = offset; |
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 | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2551 | case PP_CLEAR: |
| 2552 | if (tline->next) |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 2553 | nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2554 | free_macros(); |
| 2555 | init_macros(); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2556 | free_tlist(origline); |
| 2557 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2558 | |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2559 | case PP_DEPEND: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2560 | t = tline->next = expand_smacro(tline->next); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2561 | skip_white_(t); |
| 2562 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2563 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 2564 | nasm_nonfatal("`%s' expects a file name", dname); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2565 | free_tlist(origline); |
| 2566 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2567 | } |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2568 | if (t->next) |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 2569 | nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2570 | p = t->text; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2571 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2572 | nasm_unquote_cstr(p, i); |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 2573 | strlist_add(deplist, p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2574 | free_tlist(origline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2575 | return DIRECTIVE_FOUND; |
| 2576 | |
| 2577 | case PP_INCLUDE: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2578 | t = tline->next = expand_smacro(tline->next); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2579 | skip_white_(t); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2580 | |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2581 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2582 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 2583 | nasm_nonfatal("`%s' expects a file name", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2584 | free_tlist(origline); |
| 2585 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2586 | } |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2587 | if (t->next) |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 2588 | nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2589 | p = t->text; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2590 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2591 | nasm_unquote_cstr(p, i); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2592 | inc = nasm_malloc(sizeof(Include)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2593 | inc->next = istk; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2594 | inc->conds = NULL; |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 2595 | found_path = NULL; |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 2596 | inc->fp = inc_fopen(p, deplist, &found_path, |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 2597 | (pp_mode == PP_DEPS) |
| 2598 | ? INC_OPTIONAL : INC_NEEDED, NF_TEXT); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2599 | if (!inc->fp) { |
| 2600 | /* -MG given but file not found */ |
| 2601 | nasm_free(inc); |
| 2602 | } else { |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 2603 | inc->fname = src_set_fname(found_path ? found_path : p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2604 | inc->lineno = src_set_linnum(0); |
| 2605 | inc->lineinc = 1; |
| 2606 | inc->expansion = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2607 | inc->mstk = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2608 | istk = inc; |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 2609 | lfmt->uplevel(LIST_INCLUDE); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2610 | } |
| 2611 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2612 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2613 | |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2614 | case PP_USE: |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2615 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2616 | static macros_t *use_pkg; |
| 2617 | const char *pkg_macro = NULL; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2618 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2619 | tline = tline->next; |
| 2620 | skip_white_(tline); |
| 2621 | tline = expand_id(tline); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2622 | |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2623 | if (!tline || (tline->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2624 | tline->type != TOK_INTERNAL_STRING && |
| 2625 | tline->type != TOK_ID)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 2626 | nasm_nonfatal("`%s' expects a package name", dname); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2627 | free_tlist(origline); |
| 2628 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2629 | } |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2630 | if (tline->next) |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 2631 | nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2632 | if (tline->type == TOK_STRING) |
| 2633 | nasm_unquote_cstr(tline->text, i); |
| 2634 | use_pkg = nasm_stdmac_find_package(tline->text); |
| 2635 | if (!use_pkg) |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 2636 | nasm_nonfatal("unknown `%s' package: %s", dname, tline->text); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2637 | else |
| 2638 | 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] | 2639 | if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2640 | /* Not already included, go ahead and include it */ |
| 2641 | stdmacpos = use_pkg; |
| 2642 | } |
| 2643 | free_tlist(origline); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2644 | return DIRECTIVE_FOUND; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2645 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2646 | case PP_PUSH: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2647 | case PP_REPL: |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2648 | case PP_POP: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2649 | tline = tline->next; |
| 2650 | skip_white_(tline); |
| 2651 | tline = expand_id(tline); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2652 | if (tline) { |
| 2653 | if (!tok_type_(tline, TOK_ID)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2654 | nasm_nonfatal("`%s' expects a context identifier", |
| 2655 | pp_directives[i]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2656 | free_tlist(origline); |
| 2657 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2658 | } |
| 2659 | if (tline->next) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2660 | nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2661 | pp_directives[i]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2662 | p = nasm_strdup(tline->text); |
| 2663 | } else { |
| 2664 | p = NULL; /* Anonymous */ |
| 2665 | } |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2666 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2667 | if (i == PP_PUSH) { |
H. Peter Anvin (Intel) | ebb05a0 | 2018-12-11 12:30:25 -0800 | [diff] [blame] | 2668 | nasm_new(ctx); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2669 | ctx->next = cstk; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2670 | ctx->name = p; |
| 2671 | ctx->number = unique++; |
| 2672 | cstk = ctx; |
| 2673 | } else { |
| 2674 | /* %pop or %repl */ |
| 2675 | if (!cstk) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2676 | nasm_nonfatal("`%s': context stack is empty", |
| 2677 | pp_directives[i]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2678 | } else if (i == PP_POP) { |
| 2679 | if (p && (!cstk->name || nasm_stricmp(p, cstk->name))) |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 2680 | nasm_nonfatal("`%s' in wrong context: %s, " |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2681 | "expected %s", |
| 2682 | dname, cstk->name ? cstk->name : "anonymous", p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2683 | else |
| 2684 | ctx_pop(); |
| 2685 | } else { |
| 2686 | /* i == PP_REPL */ |
| 2687 | nasm_free(cstk->name); |
| 2688 | cstk->name = p; |
| 2689 | p = NULL; |
| 2690 | } |
| 2691 | nasm_free(p); |
| 2692 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2693 | free_tlist(origline); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2694 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2695 | case PP_FATAL: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2696 | severity = ERR_FATAL; |
| 2697 | goto issue_error; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2698 | case PP_ERROR: |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2699 | severity = ERR_NONFATAL|ERR_PASS2; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2700 | goto issue_error; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2701 | case PP_WARNING: |
H. Peter Anvin (Intel) | 723ab48 | 2018-12-13 21:53:31 -0800 | [diff] [blame] | 2702 | /*! |
| 2703 | *!user [on] %warning directives |
| 2704 | *! controls output of \c{%warning} directives (see \k{pperror}). |
| 2705 | */ |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2706 | severity = ERR_WARNING|WARN_USER|ERR_PASS2; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2707 | goto issue_error; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2708 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2709 | issue_error: |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2710 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2711 | /* Only error out if this is the final pass */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2712 | tline->next = expand_smacro(tline->next); |
| 2713 | tline = tline->next; |
| 2714 | skip_white_(tline); |
| 2715 | t = tline ? tline->next : NULL; |
| 2716 | skip_white_(t); |
| 2717 | if (tok_type_(tline, TOK_STRING) && !t) { |
| 2718 | /* The line contains only a quoted string */ |
| 2719 | p = tline->text; |
| 2720 | nasm_unquote(p, NULL); /* Ignore NUL character truncation */ |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2721 | nasm_error(severity, "%s", p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2722 | } else { |
| 2723 | /* Not a quoted string, or more than a quoted string */ |
| 2724 | p = detoken(tline, false); |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2725 | nasm_error(severity, "%s", p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2726 | nasm_free(p); |
| 2727 | } |
| 2728 | free_tlist(origline); |
| 2729 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2730 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2731 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2732 | CASE_PP_IF: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2733 | if (istk->conds && !emitting(istk->conds->state)) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2734 | j = COND_NEVER; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2735 | else { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2736 | j = if_condition(tline->next, i); |
| 2737 | tline->next = NULL; /* it got freed */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2738 | j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2739 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2740 | cond = nasm_malloc(sizeof(Cond)); |
| 2741 | cond->next = istk->conds; |
| 2742 | cond->state = j; |
| 2743 | istk->conds = cond; |
| 2744 | if(istk->mstk) |
| 2745 | istk->mstk->condcnt ++; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2746 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2747 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2748 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2749 | CASE_PP_ELIF: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2750 | if (!istk->conds) |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 2751 | nasm_fatal("`%s': no matching `%%if'", dname); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2752 | switch(istk->conds->state) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2753 | case COND_IF_TRUE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2754 | istk->conds->state = COND_DONE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2755 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2756 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2757 | case COND_DONE: |
| 2758 | case COND_NEVER: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2759 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2760 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2761 | case COND_ELSE_TRUE: |
| 2762 | case COND_ELSE_FALSE: |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2763 | nasm_warn(WARN_OTHER|ERR_PP_PRECOND, |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2764 | "`%%elif' after `%%else' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2765 | istk->conds->state = COND_NEVER; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2766 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2767 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2768 | case COND_IF_FALSE: |
| 2769 | /* |
| 2770 | * IMPORTANT: In the case of %if, we will already have |
| 2771 | * called expand_mmac_params(); however, if we're |
| 2772 | * processing an %elif we must have been in a |
| 2773 | * non-emitting mode, which would have inhibited |
| 2774 | * the normal invocation of expand_mmac_params(). |
| 2775 | * Therefore, we have to do it explicitly here. |
| 2776 | */ |
| 2777 | j = if_condition(expand_mmac_params(tline->next), i); |
| 2778 | tline->next = NULL; /* it got freed */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2779 | istk->conds->state = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2780 | j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; |
| 2781 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2782 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2783 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2784 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2785 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2786 | case PP_ELSE: |
| 2787 | if (tline->next) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2788 | nasm_warn(WARN_OTHER|ERR_PP_PRECOND, |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2789 | "trailing garbage after `%%else' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2790 | if (!istk->conds) |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 2791 | nasm_fatal("`%%else: no matching `%%if'"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2792 | switch(istk->conds->state) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2793 | case COND_IF_TRUE: |
| 2794 | case COND_DONE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2795 | istk->conds->state = COND_ELSE_FALSE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2796 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2797 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2798 | case COND_NEVER: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2799 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2800 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2801 | case COND_IF_FALSE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2802 | istk->conds->state = COND_ELSE_TRUE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2803 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2804 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2805 | case COND_ELSE_TRUE: |
| 2806 | case COND_ELSE_FALSE: |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2807 | nasm_warn(WARN_OTHER|ERR_PP_PRECOND, |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2808 | "`%%else' after `%%else' ignored."); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2809 | istk->conds->state = COND_NEVER; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2810 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2811 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2812 | free_tlist(origline); |
| 2813 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2814 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2815 | case PP_ENDIF: |
| 2816 | if (tline->next) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2817 | nasm_warn(WARN_OTHER|ERR_PP_PRECOND, |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2818 | "trailing garbage after `%%endif' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2819 | if (!istk->conds) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2820 | nasm_fatal("`%%endif': no matching `%%if'"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2821 | cond = istk->conds; |
| 2822 | istk->conds = cond->next; |
| 2823 | nasm_free(cond); |
| 2824 | if(istk->mstk) |
| 2825 | istk->mstk->condcnt --; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2826 | free_tlist(origline); |
| 2827 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2828 | |
H. Peter Anvin | db8f96e | 2009-07-15 09:07:29 -0400 | [diff] [blame] | 2829 | case PP_IRMACRO: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2830 | case PP_IMACRO: |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2831 | casesense = false; |
| 2832 | /* fall through */ |
| 2833 | case PP_RMACRO: |
| 2834 | case PP_MACRO: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2835 | if (defining) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 2836 | nasm_fatal("`%s': already defining a macro", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2837 | return DIRECTIVE_FOUND; |
| 2838 | } |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 2839 | defining = nasm_zalloc(sizeof(MMacro)); |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 2840 | defining->max_depth = ((i == PP_RMACRO) || (i == PP_IRMACRO)) |
| 2841 | ? nasm_limit[LIMIT_MACROS] : 0; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2842 | defining->casesense = casesense; |
| 2843 | if (!parse_mmacro_spec(tline, defining, dname)) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2844 | nasm_free(defining); |
| 2845 | defining = NULL; |
| 2846 | return DIRECTIVE_FOUND; |
| 2847 | } |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2848 | |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 2849 | src_get(&defining->xline, &defining->fname); |
| 2850 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2851 | mmac = (MMacro *) hash_findix(&mmacros, defining->name); |
| 2852 | while (mmac) { |
| 2853 | if (!strcmp(mmac->name, defining->name) && |
| 2854 | (mmac->nparam_min <= defining->nparam_max |
| 2855 | || defining->plus) |
| 2856 | && (defining->nparam_min <= mmac->nparam_max |
| 2857 | || mmac->plus)) { |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2858 | nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'", |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2859 | defining->name); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2860 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2861 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2862 | mmac = mmac->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2863 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2864 | free_tlist(origline); |
| 2865 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2866 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2867 | case PP_ENDM: |
| 2868 | case PP_ENDMACRO: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2869 | if (! (defining && defining->name)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2870 | nasm_nonfatal("`%s': not defining a macro", tline->text); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2871 | return DIRECTIVE_FOUND; |
| 2872 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2873 | mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name); |
| 2874 | defining->next = *mmhead; |
| 2875 | *mmhead = defining; |
| 2876 | defining = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2877 | free_tlist(origline); |
| 2878 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2879 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2880 | case PP_EXITMACRO: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2881 | /* |
| 2882 | * We must search along istk->expansion until we hit a |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2883 | * macro-end marker for a macro with a name. Then we |
| 2884 | * bypass all lines between exitmacro and endmacro. |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2885 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2886 | list_for_each(l, istk->expansion) |
| 2887 | if (l->finishes && l->finishes->name) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2888 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2889 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2890 | if (l) { |
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 | * Remove all conditional entries relative to this |
| 2893 | * macro invocation. (safe to do in this context) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2894 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2895 | for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) { |
| 2896 | cond = istk->conds; |
| 2897 | istk->conds = cond->next; |
| 2898 | nasm_free(cond); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2899 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2900 | istk->expansion = l; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2901 | } else { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2902 | nasm_nonfatal("`%%exitmacro' not within `%%macro' block"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2903 | } |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 2904 | free_tlist(origline); |
| 2905 | return DIRECTIVE_FOUND; |
| 2906 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2907 | case PP_UNIMACRO: |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2908 | casesense = false; |
| 2909 | /* fall through */ |
| 2910 | case PP_UNMACRO: |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2911 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2912 | MMacro **mmac_p; |
| 2913 | MMacro spec; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2914 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2915 | spec.casesense = casesense; |
| 2916 | if (!parse_mmacro_spec(tline, &spec, dname)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2917 | return DIRECTIVE_FOUND; |
| 2918 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2919 | mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL); |
| 2920 | while (mmac_p && *mmac_p) { |
| 2921 | mmac = *mmac_p; |
| 2922 | if (mmac->casesense == spec.casesense && |
| 2923 | !mstrcmp(mmac->name, spec.name, spec.casesense) && |
| 2924 | mmac->nparam_min == spec.nparam_min && |
| 2925 | mmac->nparam_max == spec.nparam_max && |
| 2926 | mmac->plus == spec.plus) { |
| 2927 | *mmac_p = mmac->next; |
| 2928 | free_mmacro(mmac); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2929 | } else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2930 | mmac_p = &mmac->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2931 | } |
| 2932 | } |
| 2933 | free_tlist(origline); |
| 2934 | free_tlist(spec.dlist); |
| 2935 | return DIRECTIVE_FOUND; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2936 | } |
| 2937 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2938 | case PP_ROTATE: |
| 2939 | if (tline->next && tline->next->type == TOK_WHITESPACE) |
| 2940 | tline = tline->next; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2941 | if (!tline->next) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2942 | free_tlist(origline); |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2943 | nasm_nonfatal("`%%rotate' missing rotate count"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2944 | return DIRECTIVE_FOUND; |
| 2945 | } |
| 2946 | t = expand_smacro(tline->next); |
| 2947 | tline->next = NULL; |
| 2948 | free_tlist(origline); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2949 | pps.tptr = tline = t; |
| 2950 | pps.ntokens = -1; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2951 | tokval.t_type = TOKEN_INVALID; |
| 2952 | evalresult = |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 2953 | evaluate(ppscan, &pps, &tokval, NULL, true, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2954 | free_tlist(tline); |
| 2955 | if (!evalresult) |
| 2956 | return DIRECTIVE_FOUND; |
| 2957 | if (tokval.t_type) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 2958 | nasm_warn(WARN_OTHER, "trailing garbage after expression ignored"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2959 | if (!is_simple(evalresult)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2960 | nasm_nonfatal("non-constant value given to `%%rotate'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2961 | return DIRECTIVE_FOUND; |
| 2962 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2963 | mmac = istk->mstk; |
| 2964 | while (mmac && !mmac->name) /* avoid mistaking %reps for macros */ |
| 2965 | mmac = mmac->next_active; |
| 2966 | if (!mmac) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2967 | nasm_nonfatal("`%%rotate' invoked outside a macro call"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2968 | } else if (mmac->nparam == 0) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 2969 | nasm_nonfatal("`%%rotate' invoked within macro without parameters"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2970 | } else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2971 | int rotate = mmac->rotate + reloc_value(evalresult); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2972 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2973 | rotate %= (int)mmac->nparam; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2974 | if (rotate < 0) |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2975 | rotate += mmac->nparam; |
| 2976 | |
| 2977 | mmac->rotate = rotate; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2978 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2979 | return DIRECTIVE_FOUND; |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 2980 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2981 | case PP_REP: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 2982 | nolist = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2983 | do { |
| 2984 | tline = tline->next; |
| 2985 | } while (tok_type_(tline, TOK_WHITESPACE)); |
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 (tok_type_(tline, TOK_ID) && |
| 2988 | nasm_stricmp(tline->text, ".nolist") == 0) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 2989 | nolist = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2990 | do { |
| 2991 | tline = tline->next; |
| 2992 | } while (tok_type_(tline, TOK_WHITESPACE)); |
| 2993 | } |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 2994 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2995 | if (tline) { |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 2996 | pps.tptr = expand_smacro(tline); |
| 2997 | pps.ntokens = -1; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2998 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 2999 | /* XXX: really critical?! */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3000 | evalresult = |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 3001 | evaluate(ppscan, &pps, &tokval, NULL, true, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3002 | if (!evalresult) { |
| 3003 | free_tlist(origline); |
| 3004 | return DIRECTIVE_FOUND; |
| 3005 | } |
| 3006 | if (tokval.t_type) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 3007 | nasm_warn(WARN_OTHER, "trailing garbage after expression ignored"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3008 | if (!is_simple(evalresult)) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3009 | nasm_nonfatal("non-constant value given to `%%rep'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3010 | return DIRECTIVE_FOUND; |
| 3011 | } |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 3012 | count = reloc_value(evalresult); |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 3013 | if (count > nasm_limit[LIMIT_REP]) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3014 | nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")", |
| 3015 | count, nasm_limit[LIMIT_REP]); |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 3016 | count = 0; |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 3017 | } else if (count < 0) { |
H. Peter Anvin (Intel) | 723ab48 | 2018-12-13 21:53:31 -0800 | [diff] [blame] | 3018 | /*! |
| 3019 | *!negative-rep [on] regative %rep count |
| 3020 | *! warns about negative counts given to the \c{%rep} |
| 3021 | *! preprocessor directive. |
| 3022 | */ |
H. Peter Anvin (Intel) | 80c4f23 | 2018-12-14 13:33:24 -0800 | [diff] [blame] | 3023 | nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP, |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 3024 | "negative `%%rep' count: %"PRId64, count); |
| 3025 | count = 0; |
| 3026 | } else { |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 3027 | count++; |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 3028 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3029 | } else { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3030 | nasm_nonfatal("`%%rep' expects a repeat count"); |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 3031 | count = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3032 | } |
| 3033 | free_tlist(origline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3034 | |
| 3035 | tmp_defining = defining; |
| 3036 | defining = nasm_malloc(sizeof(MMacro)); |
| 3037 | defining->prev = NULL; |
| 3038 | defining->name = NULL; /* flags this macro as a %rep block */ |
| 3039 | defining->casesense = false; |
| 3040 | defining->plus = false; |
| 3041 | defining->nolist = nolist; |
| 3042 | defining->in_progress = count; |
| 3043 | defining->max_depth = 0; |
| 3044 | defining->nparam_min = defining->nparam_max = 0; |
| 3045 | defining->defaults = NULL; |
| 3046 | defining->dlist = NULL; |
| 3047 | defining->expansion = NULL; |
| 3048 | defining->next_active = istk->mstk; |
| 3049 | defining->rep_nest = tmp_defining; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3050 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3051 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3052 | case PP_ENDREP: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3053 | if (!defining || defining->name) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3054 | nasm_nonfatal("`%%endrep': no matching `%%rep'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3055 | return DIRECTIVE_FOUND; |
| 3056 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3057 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3058 | /* |
| 3059 | * Now we have a "macro" defined - although it has no name |
| 3060 | * and we won't be entering it in the hash tables - we must |
| 3061 | * push a macro-end marker for it on to istk->expansion. |
| 3062 | * After that, it will take care of propagating itself (a |
| 3063 | * macro-end marker line for a macro which is really a %rep |
| 3064 | * block will cause the macro to be re-expanded, complete |
| 3065 | * with another macro-end marker to ensure the process |
| 3066 | * continues) until the whole expansion is forcibly removed |
| 3067 | * from istk->expansion by a %exitrep. |
| 3068 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3069 | l = nasm_malloc(sizeof(Line)); |
| 3070 | l->next = istk->expansion; |
| 3071 | l->finishes = defining; |
| 3072 | l->first = NULL; |
| 3073 | istk->expansion = l; |
| 3074 | |
| 3075 | istk->mstk = defining; |
| 3076 | |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 3077 | lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3078 | tmp_defining = defining; |
| 3079 | defining = defining->rep_nest; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3080 | free_tlist(origline); |
| 3081 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3082 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3083 | case PP_EXITREP: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3084 | /* |
| 3085 | * We must search along istk->expansion until we hit a |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3086 | * macro-end marker for a macro with no name. Then we set |
| 3087 | * its `in_progress' flag to 0. |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3088 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3089 | list_for_each(l, istk->expansion) |
| 3090 | if (l->finishes && !l->finishes->name) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3091 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3092 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3093 | if (l) |
| 3094 | l->finishes->in_progress = 1; |
| 3095 | else |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3096 | nasm_nonfatal("`%%exitrep' not within `%%rep' block"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3097 | free_tlist(origline); |
| 3098 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3099 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3100 | case PP_IDEFINE: |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3101 | case PP_IXDEFINE: |
| 3102 | casesense = false; |
| 3103 | /* fall through */ |
| 3104 | case PP_DEFINE: |
| 3105 | case PP_XDEFINE: |
| 3106 | { |
| 3107 | SMacro *s; |
| 3108 | bool have_eval_params = false; |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3109 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3110 | tline = tline->next; |
| 3111 | skip_white_(tline); |
| 3112 | tline = expand_id(tline); |
| 3113 | if (!tline || (tline->type != TOK_ID && |
| 3114 | (tline->type != TOK_PREPROC_ID || |
| 3115 | tline->text[1] != '$'))) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 3116 | nasm_nonfatal("`%s' expects a macro identifier", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3117 | free_tlist(origline); |
| 3118 | return DIRECTIVE_FOUND; |
| 3119 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3120 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3121 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3122 | last = tline; |
| 3123 | param_start = tline = tline->next; |
| 3124 | nparam = 0; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3125 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3126 | if (tok_is_(tline, "(")) { |
| 3127 | /* |
| 3128 | * This macro has parameters. |
| 3129 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3130 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3131 | tline = tline->next; |
| 3132 | while (1) { |
| 3133 | skip_white_(tline); |
| 3134 | if (!tline) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3135 | nasm_nonfatal("parameter identifier expected"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3136 | free_tlist(origline); |
| 3137 | return DIRECTIVE_FOUND; |
| 3138 | } |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3139 | if (tok_is_(tline, "=")) { |
| 3140 | have_eval_params = true; |
| 3141 | tline = tline->next; |
| 3142 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3143 | if (tline->type != TOK_ID) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3144 | nasm_nonfatal("`%s': parameter identifier expected", |
| 3145 | tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3146 | free_tlist(origline); |
| 3147 | return DIRECTIVE_FOUND; |
| 3148 | } |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3149 | tline->type = tok_smac_param(nparam++); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3150 | tline = tline->next; |
| 3151 | skip_white_(tline); |
| 3152 | if (tok_is_(tline, ",")) { |
| 3153 | tline = tline->next; |
H. Peter Anvin | ca348b6 | 2008-07-23 10:49:26 -0400 | [diff] [blame] | 3154 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3155 | if (!tok_is_(tline, ")")) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3156 | nasm_nonfatal("`)' expected to terminate macro template"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3157 | free_tlist(origline); |
| 3158 | return DIRECTIVE_FOUND; |
| 3159 | } |
| 3160 | break; |
| 3161 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3162 | } |
| 3163 | last = tline; |
| 3164 | tline = tline->next; |
| 3165 | } |
| 3166 | if (tok_type_(tline, TOK_WHITESPACE)) |
| 3167 | last = tline, tline = tline->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3168 | last->next = NULL; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3169 | |
| 3170 | /* Expand the macro definition now for %xdefine and %ixdefine */ |
| 3171 | if ((i == PP_XDEFINE) || (i == PP_IXDEFINE)) |
| 3172 | tline = expand_smacro(tline); |
| 3173 | |
| 3174 | macro_start = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3175 | t = tline; |
| 3176 | while (t) { |
| 3177 | if (t->type == TOK_ID) { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3178 | list_for_each(tt, param_start) |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3179 | if (is_smac_param(tt->type) && !strcmp(tt->text, t->text)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3180 | t->type = tt->type; |
| 3181 | } |
| 3182 | tt = t->next; |
| 3183 | t->next = macro_start; |
| 3184 | macro_start = t; |
| 3185 | t = tt; |
| 3186 | } |
| 3187 | /* |
| 3188 | * Good. We now have a macro name, a parameter count, and a |
| 3189 | * token list (in reverse order) for an expansion. We ought |
| 3190 | * to be OK just to create an SMacro, store it, and let |
| 3191 | * free_tlist have the rest of the line (which we have |
| 3192 | * carefully re-terminated after chopping off the expansion |
| 3193 | * from the end). |
| 3194 | */ |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3195 | s = define_smacro(ctx, mname, casesense, nparam, macro_start); |
| 3196 | |
| 3197 | if (have_eval_params) { |
| 3198 | /* Create evaluated parameters table */ |
| 3199 | bool is_eval = false; |
| 3200 | |
| 3201 | nasm_newn(s->eval_param, nparam); |
| 3202 | list_for_each(tt, param_start) { |
| 3203 | if (is_smac_param(tt->type)) |
| 3204 | s->eval_param[smac_nparam(tt->type)] = is_eval; |
| 3205 | is_eval = tok_is_(tt, "="); |
| 3206 | } |
| 3207 | } |
| 3208 | |
| 3209 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3210 | free_tlist(origline); |
| 3211 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3212 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3213 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3214 | case PP_UNDEF: |
| 3215 | tline = tline->next; |
| 3216 | skip_white_(tline); |
| 3217 | tline = expand_id(tline); |
| 3218 | if (!tline || (tline->type != TOK_ID && |
| 3219 | (tline->type != TOK_PREPROC_ID || |
| 3220 | tline->text[1] != '$'))) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3221 | nasm_nonfatal("`%%undef' expects a macro identifier"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3222 | free_tlist(origline); |
| 3223 | return DIRECTIVE_FOUND; |
| 3224 | } |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3225 | if (tline->next) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 3226 | nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored"); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3227 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3228 | /* Find the context that symbol belongs to */ |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3229 | ctx = get_ctx(tline->text, &mname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3230 | undef_smacro(ctx, mname); |
| 3231 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3232 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3233 | |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3234 | case PP_IDEFSTR: |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3235 | casesense = false; |
| 3236 | /* fall through */ |
| 3237 | case PP_DEFSTR: |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3238 | tline = tline->next; |
| 3239 | skip_white_(tline); |
| 3240 | tline = expand_id(tline); |
| 3241 | if (!tline || (tline->type != TOK_ID && |
| 3242 | (tline->type != TOK_PREPROC_ID || |
| 3243 | tline->text[1] != '$'))) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 3244 | nasm_nonfatal("`%s' expects a macro identifier", dname); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3245 | free_tlist(origline); |
| 3246 | return DIRECTIVE_FOUND; |
| 3247 | } |
| 3248 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3249 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3250 | last = tline; |
| 3251 | tline = expand_smacro(tline->next); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3252 | last->next = NULL; |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3253 | |
| 3254 | while (tok_type_(tline, TOK_WHITESPACE)) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3255 | tline = delete_Token(tline); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3256 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3257 | p = detoken(tline, false); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3258 | macro_start = make_tok_qstr(p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3259 | nasm_free(p); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3260 | |
| 3261 | /* |
| 3262 | * We now have a macro name, an implicit parameter count of |
| 3263 | * zero, and a string token to use as an expansion. Create |
| 3264 | * and store an SMacro. |
| 3265 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3266 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3267 | free_tlist(origline); |
| 3268 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3269 | |
H. Peter Anvin | 2f55bda | 2009-07-14 15:04:04 -0400 | [diff] [blame] | 3270 | case PP_IDEFTOK: |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3271 | casesense = false; |
| 3272 | /* fall through */ |
| 3273 | case PP_DEFTOK: |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3274 | tline = tline->next; |
| 3275 | skip_white_(tline); |
| 3276 | tline = expand_id(tline); |
| 3277 | if (!tline || (tline->type != TOK_ID && |
| 3278 | (tline->type != TOK_PREPROC_ID || |
| 3279 | tline->text[1] != '$'))) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 3280 | nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3281 | free_tlist(origline); |
| 3282 | return DIRECTIVE_FOUND; |
| 3283 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3284 | ctx = get_ctx(tline->text, &mname); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3285 | last = tline; |
| 3286 | tline = expand_smacro(tline->next); |
| 3287 | last->next = NULL; |
| 3288 | |
| 3289 | t = tline; |
| 3290 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3291 | t = t->next; |
| 3292 | /* t should now point to the string */ |
Cyrill Gorcunov | 6908e58 | 2010-09-06 19:36:15 +0400 | [diff] [blame] | 3293 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 3294 | nasm_nonfatal("`%s` requires string as second parameter", dname); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3295 | free_tlist(tline); |
| 3296 | free_tlist(origline); |
| 3297 | return DIRECTIVE_FOUND; |
| 3298 | } |
| 3299 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 3300 | /* |
| 3301 | * Convert the string to a token stream. Note that smacros |
| 3302 | * are stored with the token stream reversed, so we have to |
| 3303 | * reverse the output of tokenize(). |
| 3304 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3305 | nasm_unquote_cstr(t->text, i); |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 3306 | macro_start = reverse_tokens(tokenize(t->text)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3307 | |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3308 | /* |
| 3309 | * We now have a macro name, an implicit parameter count of |
| 3310 | * zero, and a numeric token to use as an expansion. Create |
| 3311 | * and store an SMacro. |
| 3312 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3313 | define_smacro(ctx, mname, casesense, 0, macro_start); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3314 | free_tlist(tline); |
| 3315 | free_tlist(origline); |
| 3316 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3317 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3318 | case PP_IPATHSEARCH: |
| 3319 | casesense = false; |
| 3320 | /* fall through */ |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3321 | case PP_PATHSEARCH: |
| 3322 | { |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 3323 | const char *found_path; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3324 | |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3325 | tline = tline->next; |
| 3326 | skip_white_(tline); |
| 3327 | tline = expand_id(tline); |
| 3328 | if (!tline || (tline->type != TOK_ID && |
| 3329 | (tline->type != TOK_PREPROC_ID || |
| 3330 | tline->text[1] != '$'))) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 3331 | nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3332 | free_tlist(origline); |
| 3333 | return DIRECTIVE_FOUND; |
| 3334 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3335 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3336 | last = tline; |
| 3337 | tline = expand_smacro(tline->next); |
| 3338 | last->next = NULL; |
| 3339 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3340 | t = tline; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3341 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3342 | t = t->next; |
| 3343 | |
| 3344 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3345 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 3346 | nasm_nonfatal("`%s' expects a file name", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3347 | free_tlist(tline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3348 | free_tlist(origline); |
| 3349 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 3350 | } |
| 3351 | if (t->next) |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 3352 | nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3353 | p = t->text; |
H. Peter Anvin | 427cc91 | 2008-06-01 21:43:03 -0700 | [diff] [blame] | 3354 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3355 | nasm_unquote(p, NULL); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3356 | |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 3357 | inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY); |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 3358 | if (!found_path) |
| 3359 | found_path = p; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3360 | macro_start = make_tok_qstr(found_path); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3361 | |
| 3362 | /* |
| 3363 | * We now have a macro name, an implicit parameter count of |
| 3364 | * zero, and a string token to use as an expansion. Create |
| 3365 | * and store an SMacro. |
| 3366 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3367 | define_smacro(ctx, mname, casesense, 0, macro_start); |
| 3368 | free_tlist(tline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3369 | free_tlist(origline); |
| 3370 | return DIRECTIVE_FOUND; |
| 3371 | } |
| 3372 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3373 | case PP_ISTRLEN: |
| 3374 | casesense = false; |
| 3375 | /* fall through */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3376 | case PP_STRLEN: |
| 3377 | tline = tline->next; |
| 3378 | skip_white_(tline); |
| 3379 | tline = expand_id(tline); |
| 3380 | if (!tline || (tline->type != TOK_ID && |
| 3381 | (tline->type != TOK_PREPROC_ID || |
| 3382 | tline->text[1] != '$'))) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 3383 | nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3384 | free_tlist(origline); |
| 3385 | return DIRECTIVE_FOUND; |
| 3386 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3387 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3388 | last = tline; |
| 3389 | tline = expand_smacro(tline->next); |
| 3390 | last->next = NULL; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3391 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3392 | t = tline; |
| 3393 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3394 | t = t->next; |
| 3395 | /* t should now point to the string */ |
Cyrill Gorcunov | 4e1d5ab | 2010-07-23 18:51:51 +0400 | [diff] [blame] | 3396 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 3397 | nasm_nonfatal("`%s' requires string as second parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3398 | free_tlist(tline); |
| 3399 | free_tlist(origline); |
| 3400 | return DIRECTIVE_FOUND; |
| 3401 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3402 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3403 | macro_start = make_tok_num(nasm_unquote(t->text, NULL)); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3404 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3405 | /* |
| 3406 | * We now have a macro name, an implicit parameter count of |
| 3407 | * zero, and a numeric token to use as an expansion. Create |
| 3408 | * and store an SMacro. |
| 3409 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3410 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3411 | free_tlist(tline); |
| 3412 | free_tlist(origline); |
| 3413 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3414 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3415 | case PP_ISTRCAT: |
| 3416 | casesense = false; |
| 3417 | /* fall through */ |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3418 | case PP_STRCAT: |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3419 | tline = tline->next; |
| 3420 | skip_white_(tline); |
| 3421 | tline = expand_id(tline); |
| 3422 | if (!tline || (tline->type != TOK_ID && |
| 3423 | (tline->type != TOK_PREPROC_ID || |
| 3424 | tline->text[1] != '$'))) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 3425 | nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3426 | free_tlist(origline); |
| 3427 | return DIRECTIVE_FOUND; |
| 3428 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3429 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3430 | last = tline; |
| 3431 | tline = expand_smacro(tline->next); |
| 3432 | last->next = NULL; |
| 3433 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3434 | len = 0; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3435 | list_for_each(t, tline) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3436 | switch (t->type) { |
| 3437 | case TOK_WHITESPACE: |
| 3438 | break; |
| 3439 | case TOK_STRING: |
| 3440 | len += t->a.len = nasm_unquote(t->text, NULL); |
| 3441 | break; |
| 3442 | case TOK_OTHER: |
| 3443 | if (!strcmp(t->text, ",")) /* permit comma separators */ |
| 3444 | break; |
| 3445 | /* else fall through */ |
| 3446 | default: |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 3447 | nasm_nonfatal("non-string passed to `%s': %s", dname, t->text); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3448 | free_tlist(tline); |
| 3449 | free_tlist(origline); |
| 3450 | return DIRECTIVE_FOUND; |
| 3451 | } |
| 3452 | } |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3453 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3454 | p = pp = nasm_malloc(len); |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3455 | list_for_each(t, tline) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3456 | if (t->type == TOK_STRING) { |
| 3457 | memcpy(p, t->text, t->a.len); |
| 3458 | p += t->a.len; |
| 3459 | } |
| 3460 | } |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3461 | |
| 3462 | /* |
| 3463 | * We now have a macro name, an implicit parameter count of |
| 3464 | * zero, and a numeric token to use as an expansion. Create |
| 3465 | * and store an SMacro. |
| 3466 | */ |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3467 | macro_start = make_tok_qstr(pp); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3468 | nasm_free(pp); |
| 3469 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3470 | free_tlist(tline); |
| 3471 | free_tlist(origline); |
| 3472 | return DIRECTIVE_FOUND; |
| 3473 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3474 | case PP_ISUBSTR: |
| 3475 | casesense = false; |
| 3476 | /* fall through */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3477 | case PP_SUBSTR: |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3478 | { |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3479 | int64_t start, count; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3480 | size_t len; |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 3481 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3482 | tline = tline->next; |
| 3483 | skip_white_(tline); |
| 3484 | tline = expand_id(tline); |
| 3485 | if (!tline || (tline->type != TOK_ID && |
| 3486 | (tline->type != TOK_PREPROC_ID || |
| 3487 | tline->text[1] != '$'))) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 3488 | nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3489 | free_tlist(origline); |
| 3490 | return DIRECTIVE_FOUND; |
| 3491 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3492 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3493 | last = tline; |
| 3494 | tline = expand_smacro(tline->next); |
| 3495 | last->next = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3496 | |
Cyrill Gorcunov | 35519d6 | 2010-09-06 23:49:52 +0400 | [diff] [blame] | 3497 | if (tline) /* skip expanded id */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3498 | t = tline->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3499 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3500 | t = t->next; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3501 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3502 | /* t should now point to the string */ |
Cyrill Gorcunov | 35519d6 | 2010-09-06 23:49:52 +0400 | [diff] [blame] | 3503 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 3504 | nasm_nonfatal("`%s' requires string as second parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3505 | free_tlist(tline); |
| 3506 | free_tlist(origline); |
| 3507 | return DIRECTIVE_FOUND; |
| 3508 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3509 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3510 | pps.tptr = t->next; |
| 3511 | pps.ntokens = -1; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3512 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 3513 | evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3514 | if (!evalresult) { |
| 3515 | free_tlist(tline); |
| 3516 | free_tlist(origline); |
| 3517 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3518 | } else if (!is_simple(evalresult)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 3519 | nasm_nonfatal("non-constant value given to `%s'", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3520 | free_tlist(tline); |
| 3521 | free_tlist(origline); |
| 3522 | return DIRECTIVE_FOUND; |
| 3523 | } |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3524 | start = evalresult->value - 1; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3525 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3526 | while (tok_type_(pps.tptr, TOK_WHITESPACE)) |
| 3527 | pps.tptr = pps.tptr->next; |
| 3528 | if (!pps.tptr) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3529 | count = 1; /* Backwards compatibility: one character */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3530 | } else { |
| 3531 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 3532 | evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3533 | if (!evalresult) { |
| 3534 | free_tlist(tline); |
| 3535 | free_tlist(origline); |
| 3536 | return DIRECTIVE_FOUND; |
| 3537 | } else if (!is_simple(evalresult)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 3538 | nasm_nonfatal("non-constant value given to `%s'", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3539 | free_tlist(tline); |
| 3540 | free_tlist(origline); |
| 3541 | return DIRECTIVE_FOUND; |
| 3542 | } |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3543 | count = evalresult->value; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3544 | } |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3545 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3546 | len = nasm_unquote(t->text, NULL); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3547 | |
Cyrill Gorcunov | cff031e | 2010-09-07 20:31:11 +0400 | [diff] [blame] | 3548 | /* make start and count being in range */ |
| 3549 | if (start < 0) |
| 3550 | start = 0; |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3551 | if (count < 0) |
| 3552 | count = len + count + 1 - start; |
| 3553 | if (start + count > (int64_t)len) |
Cyrill Gorcunov | cff031e | 2010-09-07 20:31:11 +0400 | [diff] [blame] | 3554 | count = len - start; |
| 3555 | if (!len || count < 0 || start >=(int64_t)len) |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3556 | start = -1, count = 0; /* empty string */ |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3557 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3558 | macro_start = new_Token(NULL, TOK_STRING, NULL, 0); |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3559 | macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3560 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3561 | /* |
| 3562 | * We now have a macro name, an implicit parameter count of |
| 3563 | * zero, and a numeric token to use as an expansion. Create |
| 3564 | * and store an SMacro. |
| 3565 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3566 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3567 | free_tlist(tline); |
| 3568 | free_tlist(origline); |
| 3569 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3570 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3571 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3572 | case PP_IASSIGN: |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3573 | casesense = false; |
| 3574 | /* fall through */ |
| 3575 | case PP_ASSIGN: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3576 | tline = tline->next; |
| 3577 | skip_white_(tline); |
| 3578 | tline = expand_id(tline); |
| 3579 | if (!tline || (tline->type != TOK_ID && |
| 3580 | (tline->type != TOK_PREPROC_ID || |
| 3581 | tline->text[1] != '$'))) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 3582 | nasm_nonfatal("`%s' expects a macro identifier", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3583 | free_tlist(origline); |
| 3584 | return DIRECTIVE_FOUND; |
| 3585 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3586 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3587 | last = tline; |
| 3588 | tline = expand_smacro(tline->next); |
| 3589 | last->next = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3590 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3591 | pps.tptr = tline; |
| 3592 | pps.ntokens = -1; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3593 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 3594 | evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3595 | free_tlist(tline); |
| 3596 | if (!evalresult) { |
| 3597 | free_tlist(origline); |
| 3598 | return DIRECTIVE_FOUND; |
| 3599 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3600 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3601 | if (tokval.t_type) |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 3602 | nasm_warn(WARN_OTHER, "trailing garbage after expression ignored"); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3603 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3604 | if (!is_simple(evalresult)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 3605 | nasm_nonfatal("non-constant value given to `%s'", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3606 | free_tlist(origline); |
| 3607 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3608 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3609 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 3610 | macro_start = make_tok_num(reloc_value(evalresult)); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3611 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3612 | /* |
| 3613 | * We now have a macro name, an implicit parameter count of |
| 3614 | * zero, and a numeric token to use as an expansion. Create |
| 3615 | * and store an SMacro. |
| 3616 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3617 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3618 | free_tlist(origline); |
| 3619 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3620 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3621 | case PP_LINE: |
| 3622 | /* |
| 3623 | * Syntax is `%line nnn[+mmm] [filename]' |
| 3624 | */ |
H. Peter Anvin (Intel) | 800c168 | 2018-12-14 12:22:11 -0800 | [diff] [blame] | 3625 | if (unlikely(pp_noline)) { |
| 3626 | free_tlist(origline); |
| 3627 | return DIRECTIVE_FOUND; |
| 3628 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3629 | tline = tline->next; |
| 3630 | skip_white_(tline); |
| 3631 | if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 3632 | nasm_nonfatal("`%s' expects line number", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3633 | free_tlist(origline); |
| 3634 | return DIRECTIVE_FOUND; |
| 3635 | } |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 3636 | k = readnum(tline->text, &err); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3637 | m = 1; |
| 3638 | tline = tline->next; |
| 3639 | if (tok_is_(tline, "+")) { |
| 3640 | tline = tline->next; |
| 3641 | if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 3642 | nasm_nonfatal("`%s' expects line increment", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3643 | free_tlist(origline); |
| 3644 | return DIRECTIVE_FOUND; |
| 3645 | } |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 3646 | m = readnum(tline->text, &err); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3647 | tline = tline->next; |
| 3648 | } |
| 3649 | skip_white_(tline); |
| 3650 | src_set_linnum(k); |
| 3651 | istk->lineinc = m; |
| 3652 | if (tline) { |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 3653 | char *fname = detoken(tline, false); |
| 3654 | src_set_fname(fname); |
| 3655 | nasm_free(fname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3656 | } |
| 3657 | free_tlist(origline); |
| 3658 | return DIRECTIVE_FOUND; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3659 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3660 | default: |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 3661 | nasm_nonfatal("preprocessor directive `%s' not yet implemented", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3662 | return DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3663 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3664 | } |
| 3665 | |
| 3666 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3667 | * Ensure that a macro parameter contains a condition code and |
| 3668 | * nothing else. Return the condition code index if so, or -1 |
| 3669 | * otherwise. |
| 3670 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3671 | static int find_cc(Token * t) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3672 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3673 | Token *tt; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3674 | |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3675 | if (!t) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3676 | return -1; /* Probably a %+ without a space */ |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3677 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3678 | skip_white_(t); |
Cyrill Gorcunov | 7524cfd | 2017-10-22 19:01:16 +0300 | [diff] [blame] | 3679 | if (!t) |
| 3680 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3681 | if (t->type != TOK_ID) |
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 | tt = t->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3684 | skip_white_(tt); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3685 | if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ","))) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3686 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3687 | |
Cyrill Gorcunov | 1945639 | 2012-05-02 00:18:56 +0400 | [diff] [blame] | 3688 | return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3689 | } |
| 3690 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3691 | /* |
| 3692 | * This routines walks over tokens strem and hadnles tokens |
| 3693 | * pasting, if @handle_explicit passed then explicit pasting |
| 3694 | * term is handled, otherwise -- implicit pastings only. |
| 3695 | */ |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 3696 | static bool paste_tokens(Token **head, const struct tokseq_match *m, |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3697 | size_t mnum, bool handle_explicit) |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3698 | { |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3699 | Token *tok, *next, **prev_next, **prev_nonspace; |
| 3700 | bool pasted = false; |
| 3701 | char *buf, *p; |
| 3702 | size_t len, i; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3703 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3704 | /* |
| 3705 | * The last token before pasting. We need it |
| 3706 | * to be able to connect new handled tokens. |
| 3707 | * In other words if there were a tokens stream |
| 3708 | * |
| 3709 | * A -> B -> C -> D |
| 3710 | * |
| 3711 | * and we've joined tokens B and C, the resulting |
| 3712 | * stream should be |
| 3713 | * |
| 3714 | * A -> BC -> D |
| 3715 | */ |
| 3716 | tok = *head; |
| 3717 | prev_next = NULL; |
| 3718 | |
| 3719 | if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE)) |
| 3720 | prev_nonspace = head; |
| 3721 | else |
| 3722 | prev_nonspace = NULL; |
| 3723 | |
| 3724 | while (tok && (next = tok->next)) { |
| 3725 | |
| 3726 | switch (tok->type) { |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3727 | case TOK_WHITESPACE: |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3728 | /* Zap redundant whitespaces */ |
| 3729 | while (tok_type_(next, TOK_WHITESPACE)) |
| 3730 | next = delete_Token(next); |
| 3731 | tok->next = next; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3732 | break; |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3733 | |
| 3734 | case TOK_PASTE: |
| 3735 | /* Explicit pasting */ |
| 3736 | if (!handle_explicit) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3737 | break; |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3738 | next = delete_Token(tok); |
| 3739 | |
| 3740 | while (tok_type_(next, TOK_WHITESPACE)) |
| 3741 | next = delete_Token(next); |
| 3742 | |
| 3743 | if (!pasted) |
| 3744 | pasted = true; |
| 3745 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3746 | /* Left pasting token is start of line */ |
| 3747 | if (!prev_nonspace) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3748 | nasm_fatal("No lvalue found on pasting"); |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3749 | |
Cyrill Gorcunov | 8b5c9fb | 2013-02-04 01:24:54 +0400 | [diff] [blame] | 3750 | /* |
| 3751 | * No ending token, this might happen in two |
| 3752 | * cases |
| 3753 | * |
| 3754 | * 1) There indeed no right token at all |
| 3755 | * 2) There is a bare "%define ID" statement, |
| 3756 | * and @ID does expand to whitespace. |
| 3757 | * |
| 3758 | * So technically we need to do a grammar analysis |
| 3759 | * in another stage of parsing, but for now lets don't |
| 3760 | * change the behaviour people used to. Simply allow |
| 3761 | * whitespace after paste token. |
| 3762 | */ |
| 3763 | if (!next) { |
| 3764 | /* |
| 3765 | * Zap ending space tokens and that's all. |
| 3766 | */ |
| 3767 | tok = (*prev_nonspace)->next; |
| 3768 | while (tok_type_(tok, TOK_WHITESPACE)) |
| 3769 | tok = delete_Token(tok); |
| 3770 | tok = *prev_nonspace; |
| 3771 | tok->next = NULL; |
| 3772 | break; |
| 3773 | } |
| 3774 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3775 | tok = *prev_nonspace; |
| 3776 | while (tok_type_(tok, TOK_WHITESPACE)) |
| 3777 | tok = delete_Token(tok); |
| 3778 | len = strlen(tok->text); |
| 3779 | len += strlen(next->text); |
| 3780 | |
| 3781 | p = buf = nasm_malloc(len + 1); |
| 3782 | strcpy(p, tok->text); |
| 3783 | p = strchr(p, '\0'); |
| 3784 | strcpy(p, next->text); |
| 3785 | |
| 3786 | delete_Token(tok); |
| 3787 | |
| 3788 | tok = tokenize(buf); |
| 3789 | nasm_free(buf); |
| 3790 | |
| 3791 | *prev_nonspace = tok; |
| 3792 | while (tok && tok->next) |
| 3793 | tok = tok->next; |
| 3794 | |
| 3795 | tok->next = delete_Token(next); |
| 3796 | |
| 3797 | /* Restart from pasted tokens head */ |
| 3798 | tok = *prev_nonspace; |
| 3799 | break; |
| 3800 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3801 | default: |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3802 | /* implicit pasting */ |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 3803 | for (i = 0; i < mnum; i++) { |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3804 | if (!(PP_CONCAT_MATCH(tok, m[i].mask_head))) |
| 3805 | continue; |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 3806 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3807 | len = 0; |
| 3808 | while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) { |
| 3809 | len += strlen(next->text); |
| 3810 | next = next->next; |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 3811 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3812 | |
Cyrill Gorcunov | 6f8109e | 2017-10-22 21:26:36 +0300 | [diff] [blame] | 3813 | /* No match or no text to process */ |
| 3814 | if (tok == next || len == 0) |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3815 | break; |
| 3816 | |
| 3817 | len += strlen(tok->text); |
| 3818 | p = buf = nasm_malloc(len + 1); |
| 3819 | |
Adam Majer | 1a06943 | 2017-07-25 11:12:35 +0200 | [diff] [blame] | 3820 | strcpy(p, tok->text); |
| 3821 | p = strchr(p, '\0'); |
| 3822 | tok = delete_Token(tok); |
| 3823 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3824 | while (tok != next) { |
Adam Majer | 1a06943 | 2017-07-25 11:12:35 +0200 | [diff] [blame] | 3825 | if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) { |
| 3826 | strcpy(p, tok->text); |
| 3827 | p = strchr(p, '\0'); |
| 3828 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3829 | tok = delete_Token(tok); |
| 3830 | } |
| 3831 | |
| 3832 | tok = tokenize(buf); |
| 3833 | nasm_free(buf); |
| 3834 | |
| 3835 | if (prev_next) |
| 3836 | *prev_next = tok; |
| 3837 | else |
| 3838 | *head = tok; |
| 3839 | |
| 3840 | /* |
| 3841 | * Connect pasted into original stream, |
| 3842 | * ie A -> new-tokens -> B |
| 3843 | */ |
| 3844 | while (tok && tok->next) |
| 3845 | tok = tok->next; |
| 3846 | tok->next = next; |
| 3847 | |
| 3848 | if (!pasted) |
| 3849 | pasted = true; |
| 3850 | |
| 3851 | /* Restart from pasted tokens head */ |
| 3852 | tok = prev_next ? *prev_next : *head; |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 3853 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3854 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3855 | break; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3856 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3857 | |
| 3858 | prev_next = &tok->next; |
| 3859 | |
| 3860 | if (tok->next && |
| 3861 | !tok_type_(tok->next, TOK_WHITESPACE) && |
| 3862 | !tok_type_(tok->next, TOK_PASTE)) |
| 3863 | prev_nonspace = prev_next; |
| 3864 | |
| 3865 | tok = tok->next; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3866 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3867 | |
| 3868 | return pasted; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3869 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3870 | |
| 3871 | /* |
| 3872 | * expands to a list of tokens from %{x:y} |
| 3873 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3874 | static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3875 | { |
| 3876 | Token *t = tline, **tt, *tm, *head; |
| 3877 | char *pos; |
| 3878 | int fst, lst, j, i; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3879 | |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3880 | pos = strchr(tline->text, ':'); |
| 3881 | nasm_assert(pos); |
| 3882 | |
| 3883 | lst = atoi(pos + 1); |
| 3884 | fst = atoi(tline->text + 1); |
| 3885 | |
| 3886 | /* |
| 3887 | * only macros params are accounted so |
| 3888 | * if someone passes %0 -- we reject such |
| 3889 | * value(s) |
| 3890 | */ |
| 3891 | if (lst == 0 || fst == 0) |
| 3892 | goto err; |
| 3893 | |
| 3894 | /* the values should be sane */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3895 | if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) || |
| 3896 | (lst > (int)mac->nparam || lst < (-(int)mac->nparam))) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3897 | goto err; |
| 3898 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3899 | fst = fst < 0 ? fst + (int)mac->nparam + 1: fst; |
| 3900 | lst = lst < 0 ? lst + (int)mac->nparam + 1: lst; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3901 | |
| 3902 | /* counted from zero */ |
| 3903 | fst--, lst--; |
| 3904 | |
| 3905 | /* |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3906 | * It will be at least one token. Note we |
| 3907 | * need to scan params until separator, otherwise |
| 3908 | * only first token will be passed. |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3909 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3910 | tm = mac->params[(fst + mac->rotate) % mac->nparam]; |
Cyrill Gorcunov | 67f2ca2 | 2018-10-13 19:41:01 +0300 | [diff] [blame] | 3911 | if (!tm) |
| 3912 | goto err; |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3913 | head = new_Token(NULL, tm->type, tm->text, 0); |
| 3914 | tt = &head->next, tm = tm->next; |
| 3915 | while (tok_isnt_(tm, ",")) { |
| 3916 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 3917 | *tt = t, tt = &t->next, tm = tm->next; |
| 3918 | } |
| 3919 | |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3920 | if (fst < lst) { |
| 3921 | for (i = fst + 1; i <= lst; i++) { |
| 3922 | t = new_Token(NULL, TOK_OTHER, ",", 0); |
| 3923 | *tt = t, tt = &t->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3924 | j = (i + mac->rotate) % mac->nparam; |
| 3925 | tm = mac->params[j]; |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3926 | while (tok_isnt_(tm, ",")) { |
| 3927 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 3928 | *tt = t, tt = &t->next, tm = tm->next; |
| 3929 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3930 | } |
| 3931 | } else { |
| 3932 | for (i = fst - 1; i >= lst; i--) { |
| 3933 | t = new_Token(NULL, TOK_OTHER, ",", 0); |
| 3934 | *tt = t, tt = &t->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3935 | j = (i + mac->rotate) % mac->nparam; |
| 3936 | tm = mac->params[j]; |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3937 | while (tok_isnt_(tm, ",")) { |
| 3938 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 3939 | *tt = t, tt = &t->next, tm = tm->next; |
| 3940 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3941 | } |
| 3942 | } |
| 3943 | |
| 3944 | *last = tt; |
| 3945 | return head; |
| 3946 | |
| 3947 | err: |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3948 | nasm_nonfatal("`%%{%s}': macro parameters out of range", |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3949 | &tline->text[1]); |
| 3950 | return tline; |
| 3951 | } |
| 3952 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3953 | /* |
| 3954 | * Expand MMacro-local things: parameter references (%0, %n, %+n, |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 3955 | * %-n) and MMacro-local identifiers (%%foo) as well as |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3956 | * macro indirection (%[...]) and range (%{..:..}). |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3957 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3958 | static Token *expand_mmac_params(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3959 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3960 | Token *t, *tt, **tail, *thead; |
H. Peter Anvin | 6125b62 | 2009-04-08 14:02:25 -0700 | [diff] [blame] | 3961 | bool changed = false; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3962 | char *pos; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3963 | |
| 3964 | tail = &thead; |
| 3965 | thead = NULL; |
| 3966 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3967 | while (tline) { |
Cyrill Gorcunov | 661f723 | 2018-10-28 20:39:34 +0300 | [diff] [blame] | 3968 | if (tline->type == TOK_PREPROC_ID && tline->text && tline->text[0] && |
Cyrill Gorcunov | ca61119 | 2010-06-04 09:22:12 +0400 | [diff] [blame] | 3969 | (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) || |
| 3970 | (tline->text[1] >= '0' && tline->text[1] <= '9') || |
| 3971 | tline->text[1] == '%')) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 3972 | char *text = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3973 | int type = 0, cc; /* type = 0 to placate optimisers */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 3974 | char tmpbuf[30]; |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3975 | unsigned int n; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3976 | int i; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3977 | MMacro *mac; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3978 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3979 | t = tline; |
| 3980 | tline = tline->next; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3981 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3982 | mac = istk->mstk; |
| 3983 | while (mac && !mac->name) /* avoid mistaking %reps for macros */ |
| 3984 | mac = mac->next_active; |
| 3985 | if (!mac) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 3986 | nasm_nonfatal("`%s': not in a macro call", t->text); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3987 | } else { |
| 3988 | pos = strchr(t->text, ':'); |
| 3989 | if (!pos) { |
| 3990 | switch (t->text[1]) { |
| 3991 | /* |
| 3992 | * We have to make a substitution of one of the |
| 3993 | * forms %1, %-1, %+1, %%foo, %0. |
| 3994 | */ |
| 3995 | case '0': |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3996 | type = TOK_NUMBER; |
| 3997 | snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam); |
| 3998 | text = nasm_strdup(tmpbuf); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3999 | break; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4000 | case '%': |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4001 | type = TOK_ID; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4002 | snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".", |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4003 | mac->unique); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4004 | text = nasm_strcat(tmpbuf, t->text + 2); |
| 4005 | break; |
| 4006 | case '-': |
| 4007 | n = atoi(t->text + 2) - 1; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4008 | if (n >= mac->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4009 | tt = NULL; |
| 4010 | else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4011 | if (mac->nparam > 1) |
| 4012 | n = (n + mac->rotate) % mac->nparam; |
| 4013 | tt = mac->params[n]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4014 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4015 | cc = find_cc(tt); |
| 4016 | if (cc == -1) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4017 | nasm_nonfatal("macro parameter %d is not a condition code", |
| 4018 | n + 1); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4019 | text = NULL; |
| 4020 | } else { |
| 4021 | type = TOK_ID; |
| 4022 | if (inverse_ccs[cc] == -1) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4023 | nasm_nonfatal("condition code `%s' is not invertible", |
| 4024 | conditions[cc]); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4025 | text = NULL; |
| 4026 | } else |
| 4027 | text = nasm_strdup(conditions[inverse_ccs[cc]]); |
| 4028 | } |
| 4029 | break; |
| 4030 | case '+': |
| 4031 | n = atoi(t->text + 2) - 1; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4032 | if (n >= mac->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4033 | tt = NULL; |
| 4034 | else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4035 | if (mac->nparam > 1) |
| 4036 | n = (n + mac->rotate) % mac->nparam; |
| 4037 | tt = mac->params[n]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4038 | } |
| 4039 | cc = find_cc(tt); |
| 4040 | if (cc == -1) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4041 | nasm_nonfatal("macro parameter %d is not a condition code", |
| 4042 | n + 1); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4043 | text = NULL; |
| 4044 | } else { |
| 4045 | type = TOK_ID; |
| 4046 | text = nasm_strdup(conditions[cc]); |
| 4047 | } |
| 4048 | break; |
| 4049 | default: |
| 4050 | n = atoi(t->text + 1) - 1; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4051 | if (n >= mac->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4052 | tt = NULL; |
| 4053 | else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4054 | if (mac->nparam > 1) |
| 4055 | n = (n + mac->rotate) % mac->nparam; |
| 4056 | tt = mac->params[n]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4057 | } |
| 4058 | if (tt) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4059 | for (i = 0; i < mac->paramlen[n]; i++) { |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4060 | *tail = new_Token(NULL, tt->type, tt->text, 0); |
| 4061 | tail = &(*tail)->next; |
| 4062 | tt = tt->next; |
| 4063 | } |
| 4064 | } |
| 4065 | text = NULL; /* we've done it here */ |
| 4066 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4067 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4068 | } else { |
| 4069 | /* |
| 4070 | * seems we have a parameters range here |
| 4071 | */ |
| 4072 | Token *head, **last; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4073 | head = expand_mmac_params_range(mac, t, &last); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4074 | if (head != t) { |
| 4075 | *tail = head; |
| 4076 | *last = tline; |
| 4077 | tline = head; |
| 4078 | text = NULL; |
| 4079 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4080 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4081 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4082 | if (!text) { |
| 4083 | delete_Token(t); |
| 4084 | } else { |
| 4085 | *tail = t; |
| 4086 | tail = &t->next; |
| 4087 | t->type = type; |
| 4088 | nasm_free(t->text); |
| 4089 | t->text = text; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4090 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4091 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4092 | changed = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4093 | continue; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4094 | } else if (tline->type == TOK_INDIRECT) { |
| 4095 | t = tline; |
| 4096 | tline = tline->next; |
| 4097 | tt = tokenize(t->text); |
| 4098 | tt = expand_mmac_params(tt); |
| 4099 | tt = expand_smacro(tt); |
| 4100 | *tail = tt; |
| 4101 | while (tt) { |
| 4102 | tt->a.mac = NULL; /* Necessary? */ |
| 4103 | tail = &tt->next; |
| 4104 | tt = tt->next; |
| 4105 | } |
| 4106 | delete_Token(t); |
| 4107 | changed = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4108 | } else { |
| 4109 | t = *tail = tline; |
| 4110 | tline = tline->next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4111 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4112 | tail = &t->next; |
| 4113 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4114 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4115 | *tail = NULL; |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 4116 | |
Cyrill Gorcunov | c6a742c | 2011-06-27 01:23:09 +0400 | [diff] [blame] | 4117 | if (changed) { |
| 4118 | const struct tokseq_match t[] = { |
| 4119 | { |
| 4120 | PP_CONCAT_MASK(TOK_ID) | |
| 4121 | PP_CONCAT_MASK(TOK_FLOAT), /* head */ |
| 4122 | PP_CONCAT_MASK(TOK_ID) | |
| 4123 | PP_CONCAT_MASK(TOK_NUMBER) | |
| 4124 | PP_CONCAT_MASK(TOK_FLOAT) | |
| 4125 | PP_CONCAT_MASK(TOK_OTHER) /* tail */ |
| 4126 | }, |
| 4127 | { |
| 4128 | PP_CONCAT_MASK(TOK_NUMBER), /* head */ |
| 4129 | PP_CONCAT_MASK(TOK_NUMBER) /* tail */ |
| 4130 | } |
| 4131 | }; |
| 4132 | paste_tokens(&thead, t, ARRAY_SIZE(t), false); |
| 4133 | } |
H. Peter Anvin | 6125b62 | 2009-04-08 14:02:25 -0700 | [diff] [blame] | 4134 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4135 | return thead; |
| 4136 | } |
| 4137 | |
| 4138 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4139 | * Expand all single-line macro calls made in the given line. |
| 4140 | * Return the expanded version of the line. The original is deemed |
| 4141 | * to be destroyed in the process. (In reality we'll just move |
| 4142 | * Tokens from input to output a lot of the time, rather than |
| 4143 | * actually bothering to destroy and replicate.) |
| 4144 | */ |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4145 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4146 | static Token *expand_smacro(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4147 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4148 | Token *t, *tt, *mstart, **tail, *thead; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4149 | SMacro *head = NULL, *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4150 | Token **params; |
| 4151 | int *paramsize; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 4152 | Token *eparams; |
| 4153 | unsigned int nparam, sparam, i; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 4154 | int brackets; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4155 | Token *org_tline = tline; |
| 4156 | Context *ctx; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 4157 | const char *mname; |
H. Peter Anvin | a3d96d0 | 2018-06-15 17:51:39 -0700 | [diff] [blame] | 4158 | int64_t deadman = nasm_limit[LIMIT_MACROS]; |
H. Peter Anvin | 8287daf | 2009-07-07 16:00:58 -0700 | [diff] [blame] | 4159 | bool expanded; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4160 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4161 | /* |
| 4162 | * Trick: we should avoid changing the start token pointer since it can |
| 4163 | * be contained in "next" field of other token. Because of this |
| 4164 | * we allocate a copy of first token and work with it; at the end of |
| 4165 | * routine we copy it back |
| 4166 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4167 | if (org_tline) { |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4168 | tline = new_Token(org_tline->next, org_tline->type, |
| 4169 | org_tline->text, 0); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4170 | tline->a.mac = org_tline->a.mac; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4171 | nasm_free(org_tline->text); |
| 4172 | org_tline->text = NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4173 | } |
| 4174 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4175 | expanded = true; /* Always expand %+ at least once */ |
H. Peter Anvin | 8287daf | 2009-07-07 16:00:58 -0700 | [diff] [blame] | 4176 | |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4177 | again: |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4178 | thead = NULL; |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4179 | tail = &thead; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4180 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4181 | while (tline) { /* main token loop */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4182 | if (!--deadman) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4183 | nasm_nonfatal("interminable macro recursion"); |
Cyrill Gorcunov | bd38c8f | 2009-11-21 11:11:23 +0300 | [diff] [blame] | 4184 | goto err; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4185 | } |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4186 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4187 | if ((mname = tline->text)) { |
| 4188 | /* if this token is a local macro, look in local context */ |
Cyrill Gorcunov | c56d9ad | 2010-02-11 15:12:19 +0300 | [diff] [blame] | 4189 | if (tline->type == TOK_ID) { |
| 4190 | head = (SMacro *)hash_findix(&smacros, mname); |
| 4191 | } else if (tline->type == TOK_PREPROC_ID) { |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 4192 | ctx = get_ctx(mname, &mname); |
Cyrill Gorcunov | c56d9ad | 2010-02-11 15:12:19 +0300 | [diff] [blame] | 4193 | head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL; |
| 4194 | } else |
| 4195 | head = NULL; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 4196 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4197 | /* |
| 4198 | * We've hit an identifier. As in is_mmacro below, we first |
| 4199 | * check whether the identifier is a single-line macro at |
| 4200 | * all, then think about checking for parameters if |
| 4201 | * necessary. |
| 4202 | */ |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 4203 | list_for_each(m, head) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4204 | if (!mstrcmp(m->name, mname, m->casesense)) |
| 4205 | break; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 4206 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4207 | if (m) { |
| 4208 | mstart = tline; |
| 4209 | params = NULL; |
| 4210 | paramsize = NULL; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 4211 | eparams = NULL; |
| 4212 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4213 | if (m->nparam == 0) { |
| 4214 | /* |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 4215 | * Simple case: the macro is parameterless. |
| 4216 | * Nothing to parse; just drop the macro token itself. |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4217 | */ |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 4218 | tline = tline->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4219 | } else { |
| 4220 | /* |
| 4221 | * Complicated case: at least one macro with this name |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4222 | * exists and takes parameters. We must find the |
| 4223 | * parameters in the call, count them, find the SMacro |
| 4224 | * that corresponds to that form of the macro call, and |
| 4225 | * substitute for the parameters when we expand. What a |
| 4226 | * pain. |
| 4227 | */ |
| 4228 | /*tline = tline->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4229 | skip_white_(tline); */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4230 | do { |
| 4231 | t = tline->next; |
| 4232 | while (tok_type_(t, TOK_SMAC_END)) { |
Cyrill Gorcunov | 982186a | 2019-03-16 23:05:50 +0300 | [diff] [blame] | 4233 | if (t->a.mac) |
| 4234 | t->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4235 | t->text = NULL; |
| 4236 | t = tline->next = delete_Token(t); |
| 4237 | } |
| 4238 | tline = t; |
| 4239 | } while (tok_type_(tline, TOK_WHITESPACE)); |
| 4240 | if (!tok_is_(tline, "(")) { |
| 4241 | /* |
| 4242 | * This macro wasn't called with parameters: ignore |
| 4243 | * the call. (Behaviour borrowed from gnu cpp.) |
| 4244 | */ |
| 4245 | tline = mstart; |
| 4246 | m = NULL; |
| 4247 | } else { |
| 4248 | int paren = 0; |
| 4249 | int white = 0; |
| 4250 | brackets = 0; |
| 4251 | nparam = 0; |
| 4252 | sparam = PARAM_DELTA; |
| 4253 | params = nasm_malloc(sparam * sizeof(Token *)); |
| 4254 | params[0] = tline->next; |
| 4255 | paramsize = nasm_malloc(sparam * sizeof(int)); |
| 4256 | paramsize[0] = 0; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4257 | while (true) { /* parameter loop */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4258 | /* |
| 4259 | * For some unusual expansions |
| 4260 | * which concatenates function call |
| 4261 | */ |
| 4262 | t = tline->next; |
| 4263 | while (tok_type_(t, TOK_SMAC_END)) { |
Cyrill Gorcunov | 982186a | 2019-03-16 23:05:50 +0300 | [diff] [blame] | 4264 | if (t->a.mac) |
| 4265 | t->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4266 | t->text = NULL; |
| 4267 | t = tline->next = delete_Token(t); |
| 4268 | } |
| 4269 | tline = t; |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 4270 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4271 | if (!tline) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4272 | nasm_nonfatal("macro call expects terminating `)'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4273 | break; |
| 4274 | } |
| 4275 | if (tline->type == TOK_WHITESPACE |
| 4276 | && brackets <= 0) { |
| 4277 | if (paramsize[nparam]) |
| 4278 | white++; |
| 4279 | else |
| 4280 | params[nparam] = tline->next; |
| 4281 | continue; /* parameter loop */ |
| 4282 | } |
| 4283 | if (tline->type == TOK_OTHER |
| 4284 | && tline->text[1] == 0) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4285 | char ch = tline->text[0]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4286 | if (ch == ',' && !paren && brackets <= 0) { |
| 4287 | if (++nparam >= sparam) { |
| 4288 | sparam += PARAM_DELTA; |
| 4289 | params = nasm_realloc(params, |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4290 | sparam * sizeof(Token *)); |
| 4291 | paramsize = nasm_realloc(paramsize, |
| 4292 | sparam * sizeof(int)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4293 | } |
| 4294 | params[nparam] = tline->next; |
| 4295 | paramsize[nparam] = 0; |
| 4296 | white = 0; |
| 4297 | continue; /* parameter loop */ |
| 4298 | } |
| 4299 | if (ch == '{' && |
| 4300 | (brackets > 0 || (brackets == 0 && |
| 4301 | !paramsize[nparam]))) |
| 4302 | { |
| 4303 | if (!(brackets++)) { |
| 4304 | params[nparam] = tline->next; |
| 4305 | continue; /* parameter loop */ |
| 4306 | } |
| 4307 | } |
| 4308 | if (ch == '}' && brackets > 0) |
| 4309 | if (--brackets == 0) { |
| 4310 | brackets = -1; |
| 4311 | continue; /* parameter loop */ |
| 4312 | } |
| 4313 | if (ch == '(' && !brackets) |
| 4314 | paren++; |
| 4315 | if (ch == ')' && brackets <= 0) |
| 4316 | if (--paren < 0) |
| 4317 | break; |
| 4318 | } |
| 4319 | if (brackets < 0) { |
| 4320 | brackets = 0; |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4321 | nasm_nonfatal("braces do not " |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4322 | "enclose all of macro parameter"); |
| 4323 | } |
| 4324 | paramsize[nparam] += white + 1; |
| 4325 | white = 0; |
| 4326 | } /* parameter loop */ |
| 4327 | nparam++; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 4328 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4329 | while (m && (m->nparam != nparam || |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 4330 | mstrcmp(m->name, mname, m->casesense))) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4331 | m = m->next; |
H. Peter Anvin (Intel) | 723ab48 | 2018-12-13 21:53:31 -0800 | [diff] [blame] | 4332 | if (!m) { |
| 4333 | /*! |
| 4334 | *!macro-params [on] macro calls with wrong parameter count |
| 4335 | *! covers warnings about \i{multi-line macros} being invoked |
| 4336 | *! with the wrong number of parameters. See \k{mlmacover} for an |
| 4337 | *! example of why you might want to disable this warning. |
| 4338 | */ |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 4339 | nasm_warn(WARN_MACRO_PARAMS, |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4340 | "macro `%s' exists, " |
| 4341 | "but not taking %d parameters", |
| 4342 | mstart->text, nparam); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 4343 | } else if (m->eval_param) { |
| 4344 | struct ppscan pps; |
| 4345 | struct tokenval tokval; |
| 4346 | expr *evalresult; |
| 4347 | |
| 4348 | /* Evaluate parameters if applicable */ |
| 4349 | for (i = 0; i < nparam; i++) { |
| 4350 | if (!m->eval_param[i]) |
| 4351 | continue; |
| 4352 | |
| 4353 | pps.tptr = params[i]; |
| 4354 | pps.ntokens = paramsize[i]; |
| 4355 | tokval.t_type = TOKEN_INVALID; |
| 4356 | evalresult = evaluate(ppscan, &pps, &tokval, |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 4357 | NULL, true, NULL); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 4358 | if (!evalresult) |
| 4359 | continue; |
| 4360 | |
| 4361 | if (tokval.t_type) { |
| 4362 | nasm_error(ERR_NONFATAL, |
| 4363 | "invalid expression in parameter %d of macro `%s'", i, m->name); |
| 4364 | continue; |
| 4365 | } |
| 4366 | |
| 4367 | if (!is_simple(evalresult)) { |
| 4368 | nasm_error(ERR_NONFATAL, |
| 4369 | "non-constant expression in parameter %d of macro `%s'", i, m->name); |
| 4370 | continue; |
| 4371 | } |
| 4372 | params[i] = make_tok_num(reloc_value(evalresult)); |
| 4373 | params[i]->next = eparams; |
| 4374 | eparams = params[i]; |
| 4375 | paramsize[i] = 1; |
| 4376 | } |
| 4377 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4378 | } |
| 4379 | } |
| 4380 | if (m && m->in_progress) |
| 4381 | m = NULL; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 4382 | if (!m) { |
| 4383 | /* in progress or didn't find '(' or wrong nparam */ |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 4384 | /* |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4385 | * Design question: should we handle !tline, which |
| 4386 | * indicates missing ')' here, or expand those |
| 4387 | * macros anyway, which requires the (t) test a few |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 4388 | * lines down? |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4389 | */ |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 4390 | free_tlist(eparams); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4391 | nasm_free(params); |
| 4392 | nasm_free(paramsize); |
| 4393 | tline = mstart; |
| 4394 | } else { |
| 4395 | /* |
| 4396 | * Expand the macro: we are placed on the last token of the |
| 4397 | * call, so that we can easily split the call from the |
| 4398 | * following tokens. We also start by pushing an SMAC_END |
| 4399 | * token for the cycle removal. |
| 4400 | */ |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 4401 | Token *expansion; |
| 4402 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4403 | t = tline; |
| 4404 | if (t) { |
| 4405 | tline = t->next; |
| 4406 | t->next = NULL; |
| 4407 | } |
| 4408 | tt = new_Token(tline, TOK_SMAC_END, NULL, 0); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4409 | tt->a.mac = m; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4410 | m->in_progress = true; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 4411 | if (unlikely(m->magic)) |
| 4412 | expansion = m->e.magic(m, params, paramsize); |
| 4413 | else |
| 4414 | expansion = m->e.expansion; |
| 4415 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4416 | tline = tt; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 4417 | list_for_each(t, expansion) { |
| 4418 | if (is_smac_param(t->type)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4419 | Token *pcopy = tline, **ptail = &pcopy; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4420 | Token *ttt, *pt; |
| 4421 | int i; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4422 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 4423 | ttt = params[smac_nparam(t->type)]; |
| 4424 | i = paramsize[smac_nparam(t->type)]; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4425 | while (--i >= 0) { |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 4426 | nasm_assert(ttt); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4427 | pt = *ptail = new_Token(tline, ttt->type, |
| 4428 | ttt->text, 0); |
| 4429 | ptail = &pt->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4430 | ttt = ttt->next; |
Cyrill Gorcunov | 59ce1c6 | 2017-10-22 18:42:07 +0300 | [diff] [blame] | 4431 | if (!ttt && i > 0) { |
| 4432 | /* |
| 4433 | * FIXME: Need to handle more gracefully, |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 4434 | * exiting early on arguments analysis. |
Cyrill Gorcunov | 59ce1c6 | 2017-10-22 18:42:07 +0300 | [diff] [blame] | 4435 | */ |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4436 | nasm_fatal("macro `%s' expects %d args", |
Cyrill Gorcunov | 59ce1c6 | 2017-10-22 18:42:07 +0300 | [diff] [blame] | 4437 | mstart->text, |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 4438 | paramsize[t->type - TOK_SMAC_START_PARAMS]); |
Cyrill Gorcunov | 59ce1c6 | 2017-10-22 18:42:07 +0300 | [diff] [blame] | 4439 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4440 | } |
| 4441 | tline = pcopy; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4442 | } else if (t->type == TOK_PREPROC_Q) { |
| 4443 | tt = new_Token(tline, TOK_ID, mname, 0); |
| 4444 | tline = tt; |
| 4445 | } else if (t->type == TOK_PREPROC_QQ) { |
| 4446 | tt = new_Token(tline, TOK_ID, m->name, 0); |
| 4447 | tline = tt; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4448 | } else { |
| 4449 | tt = new_Token(tline, t->type, t->text, 0); |
| 4450 | tline = tt; |
| 4451 | } |
| 4452 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4453 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4454 | /* |
| 4455 | * Having done that, get rid of the macro call, and clean |
| 4456 | * up the parameters. |
| 4457 | */ |
| 4458 | nasm_free(params); |
| 4459 | nasm_free(paramsize); |
| 4460 | free_tlist(mstart); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 4461 | free_tlist(eparams); |
| 4462 | if (m->magic) |
| 4463 | free_tlist(expansion); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4464 | expanded = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4465 | continue; /* main token loop */ |
| 4466 | } |
| 4467 | } |
| 4468 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4469 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4470 | if (tline->type == TOK_SMAC_END) { |
Cyrill Gorcunov | 980dd65 | 2018-10-14 19:25:32 +0300 | [diff] [blame] | 4471 | /* On error path it might already be dropped */ |
| 4472 | if (tline->a.mac) |
| 4473 | tline->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4474 | tline = delete_Token(tline); |
| 4475 | } else { |
| 4476 | t = *tail = tline; |
| 4477 | tline = tline->next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4478 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4479 | t->next = NULL; |
| 4480 | tail = &t->next; |
| 4481 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4482 | } |
| 4483 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4484 | /* |
| 4485 | * 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] | 4486 | * after expansion (they can't be produced by tokenize()). The successive |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4487 | * TOK_IDs should be concatenated. |
| 4488 | * Also we look for %+ tokens and concatenate the tokens before and after |
| 4489 | * them (without white spaces in between). |
| 4490 | */ |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4491 | if (expanded) { |
Cyrill Gorcunov | c6a742c | 2011-06-27 01:23:09 +0400 | [diff] [blame] | 4492 | const struct tokseq_match t[] = { |
| 4493 | { |
| 4494 | PP_CONCAT_MASK(TOK_ID) | |
| 4495 | PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */ |
| 4496 | PP_CONCAT_MASK(TOK_ID) | |
| 4497 | PP_CONCAT_MASK(TOK_PREPROC_ID) | |
| 4498 | PP_CONCAT_MASK(TOK_NUMBER) /* tail */ |
| 4499 | } |
| 4500 | }; |
| 4501 | if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) { |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4502 | /* |
| 4503 | * If we concatenated something, *and* we had previously expanded |
| 4504 | * an actual macro, scan the lines again for macros... |
| 4505 | */ |
| 4506 | tline = thead; |
| 4507 | expanded = false; |
| 4508 | goto again; |
| 4509 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4510 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4511 | |
Cyrill Gorcunov | bd38c8f | 2009-11-21 11:11:23 +0300 | [diff] [blame] | 4512 | err: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4513 | if (org_tline) { |
| 4514 | if (thead) { |
| 4515 | *org_tline = *thead; |
| 4516 | /* since we just gave text to org_line, don't free it */ |
| 4517 | thead->text = NULL; |
| 4518 | delete_Token(thead); |
| 4519 | } else { |
| 4520 | /* the expression expanded to empty line; |
| 4521 | we can't return NULL for some reasons |
| 4522 | we just set the line to a single WHITESPACE token. */ |
| 4523 | memset(org_tline, 0, sizeof(*org_tline)); |
| 4524 | org_tline->text = NULL; |
| 4525 | org_tline->type = TOK_WHITESPACE; |
| 4526 | } |
| 4527 | thead = org_tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4528 | } |
| 4529 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4530 | return thead; |
| 4531 | } |
| 4532 | |
| 4533 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4534 | * Similar to expand_smacro but used exclusively with macro identifiers |
| 4535 | * right before they are fetched in. The reason is that there can be |
| 4536 | * identifiers consisting of several subparts. We consider that if there |
| 4537 | * are more than one element forming the name, user wants a expansion, |
| 4538 | * otherwise it will be left as-is. Example: |
| 4539 | * |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4540 | * %define %$abc cde |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4541 | * |
| 4542 | * the identifier %$abc will be left as-is so that the handler for %define |
| 4543 | * will suck it and define the corresponding value. Other case: |
| 4544 | * |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4545 | * %define _%$abc cde |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4546 | * |
| 4547 | * In this case user wants name to be expanded *before* %define starts |
| 4548 | * working, so we'll expand %$abc into something (if it has a value; |
| 4549 | * otherwise it will be left as-is) then concatenate all successive |
| 4550 | * PP_IDs into one. |
| 4551 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4552 | static Token *expand_id(Token * tline) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4553 | { |
| 4554 | Token *cur, *oldnext = NULL; |
| 4555 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4556 | if (!tline || !tline->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4557 | return tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4558 | |
| 4559 | cur = tline; |
| 4560 | while (cur->next && |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4561 | (cur->next->type == TOK_ID || |
| 4562 | cur->next->type == TOK_PREPROC_ID |
| 4563 | || cur->next->type == TOK_NUMBER)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4564 | cur = cur->next; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4565 | |
| 4566 | /* If identifier consists of just one token, don't expand */ |
| 4567 | if (cur == tline) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4568 | return tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4569 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4570 | if (cur) { |
| 4571 | oldnext = cur->next; /* Detach the tail past identifier */ |
| 4572 | cur->next = NULL; /* so that expand_smacro stops here */ |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4573 | } |
| 4574 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4575 | tline = expand_smacro(tline); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4576 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4577 | if (cur) { |
| 4578 | /* expand_smacro possibly changhed tline; re-scan for EOL */ |
| 4579 | cur = tline; |
| 4580 | while (cur && cur->next) |
| 4581 | cur = cur->next; |
| 4582 | if (cur) |
| 4583 | cur->next = oldnext; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4584 | } |
| 4585 | |
| 4586 | return tline; |
| 4587 | } |
| 4588 | |
| 4589 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4590 | * Determine whether the given line constitutes a multi-line macro |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4591 | * 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] | 4592 | * to check for an initial label - that's taken care of in |
| 4593 | * expand_mmacro - but must check numbers of parameters. Guaranteed |
| 4594 | * to be called with tline->type == TOK_ID, so the putative macro |
| 4595 | * name is easy to find. |
| 4596 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4597 | static MMacro *is_mmacro(Token * tline, Token *** params_array) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4598 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4599 | MMacro *head, *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4600 | Token **params; |
| 4601 | int nparam; |
| 4602 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4603 | head = (MMacro *) hash_findix(&mmacros, tline->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4604 | |
| 4605 | /* |
| 4606 | * Efficiency: first we see if any macro exists with the given |
| 4607 | * name. If not, we can return NULL immediately. _Then_ we |
| 4608 | * count the parameters, and then we look further along the |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4609 | * list if necessary to find the proper MMacro. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4610 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4611 | list_for_each(m, head) |
| 4612 | if (!mstrcmp(m->name, tline->text, m->casesense)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4613 | break; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4614 | if (!m) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4615 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4616 | |
| 4617 | /* |
| 4618 | * OK, we have a potential macro. Count and demarcate the |
| 4619 | * parameters. |
| 4620 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4621 | count_mmac_params(tline->next, &nparam, ¶ms); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4622 | |
| 4623 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4624 | * 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] | 4625 | * structure that handles this number. |
| 4626 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4627 | while (m) { |
| 4628 | if (m->nparam_min <= nparam |
| 4629 | && (m->plus || nparam <= m->nparam_max)) { |
| 4630 | /* |
| 4631 | * This one is right. Just check if cycle removal |
| 4632 | * prohibits us using it before we actually celebrate... |
| 4633 | */ |
| 4634 | if (m->in_progress > m->max_depth) { |
| 4635 | if (m->max_depth > 0) { |
H. Peter Anvin (Intel) | 80c4f23 | 2018-12-14 13:33:24 -0800 | [diff] [blame] | 4636 | nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i", |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4637 | m->max_depth); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4638 | } |
| 4639 | nasm_free(params); |
| 4640 | return NULL; |
| 4641 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4642 | /* |
| 4643 | * It's right, and we can use it. Add its default |
| 4644 | * parameters to the end of our list if necessary. |
| 4645 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4646 | if (m->defaults && nparam < m->nparam_min + m->ndefs) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4647 | params = |
| 4648 | nasm_realloc(params, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4649 | ((m->nparam_min + m->ndefs + |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4650 | 1) * sizeof(*params))); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4651 | while (nparam < m->nparam_min + m->ndefs) { |
| 4652 | params[nparam] = m->defaults[nparam - m->nparam_min]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4653 | nparam++; |
| 4654 | } |
| 4655 | } |
| 4656 | /* |
| 4657 | * If we've gone over the maximum parameter count (and |
| 4658 | * we're in Plus mode), ignore parameters beyond |
| 4659 | * nparam_max. |
| 4660 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4661 | if (m->plus && nparam > m->nparam_max) |
| 4662 | nparam = m->nparam_max; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4663 | /* |
| 4664 | * Then terminate the parameter list, and leave. |
| 4665 | */ |
| 4666 | if (!params) { /* need this special case */ |
| 4667 | params = nasm_malloc(sizeof(*params)); |
| 4668 | nparam = 0; |
| 4669 | } |
| 4670 | params[nparam] = NULL; |
| 4671 | *params_array = params; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4672 | return m; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4673 | } |
| 4674 | /* |
| 4675 | * This one wasn't right: look for the next one with the |
| 4676 | * same name. |
| 4677 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4678 | list_for_each(m, m->next) |
| 4679 | if (!mstrcmp(m->name, tline->text, m->casesense)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4680 | break; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4681 | } |
| 4682 | |
| 4683 | /* |
| 4684 | * After all that, we didn't find one with the right number of |
| 4685 | * parameters. Issue a warning, and fail to expand the macro. |
| 4686 | */ |
H. Peter Anvin (Intel) | 5df6ca7 | 2018-12-18 12:25:11 -0800 | [diff] [blame] | 4687 | nasm_warn(WARN_MACRO_PARAMS, |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4688 | "macro `%s' exists, but not taking %d parameters", |
| 4689 | tline->text, nparam); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4690 | nasm_free(params); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4691 | return NULL; |
| 4692 | } |
| 4693 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4694 | |
| 4695 | /* |
| 4696 | * Save MMacro invocation specific fields in |
| 4697 | * preparation for a recursive macro expansion |
| 4698 | */ |
| 4699 | static void push_mmacro(MMacro *m) |
| 4700 | { |
| 4701 | MMacroInvocation *i; |
| 4702 | |
| 4703 | i = nasm_malloc(sizeof(MMacroInvocation)); |
| 4704 | i->prev = m->prev; |
| 4705 | i->params = m->params; |
| 4706 | i->iline = m->iline; |
| 4707 | i->nparam = m->nparam; |
| 4708 | i->rotate = m->rotate; |
| 4709 | i->paramlen = m->paramlen; |
| 4710 | i->unique = m->unique; |
| 4711 | i->condcnt = m->condcnt; |
| 4712 | m->prev = i; |
| 4713 | } |
| 4714 | |
| 4715 | |
| 4716 | /* |
| 4717 | * Restore MMacro invocation specific fields that were |
| 4718 | * saved during a previous recursive macro expansion |
| 4719 | */ |
| 4720 | static void pop_mmacro(MMacro *m) |
| 4721 | { |
| 4722 | MMacroInvocation *i; |
| 4723 | |
| 4724 | if (m->prev) { |
| 4725 | i = m->prev; |
| 4726 | m->prev = i->prev; |
| 4727 | m->params = i->params; |
| 4728 | m->iline = i->iline; |
| 4729 | m->nparam = i->nparam; |
| 4730 | m->rotate = i->rotate; |
| 4731 | m->paramlen = i->paramlen; |
| 4732 | m->unique = i->unique; |
| 4733 | m->condcnt = i->condcnt; |
| 4734 | nasm_free(i); |
| 4735 | } |
| 4736 | } |
| 4737 | |
| 4738 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4739 | /* |
| 4740 | * Expand the multi-line macro call made by the given line, if |
| 4741 | * 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] | 4742 | * istk->expansion and return 1. Otherwise return 0. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4743 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4744 | static int expand_mmacro(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4745 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4746 | Token *startline = tline; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4747 | Token *label = NULL; |
| 4748 | int dont_prepend = 0; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4749 | Token **params, *t, *tt; |
| 4750 | MMacro *m; |
| 4751 | Line *l, *ll; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4752 | int i, nparam, *paramlen; |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 4753 | const char *mname; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4754 | |
| 4755 | t = tline; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4756 | skip_white_(t); |
H. Peter Anvin | ce2233b | 2008-05-25 21:57:00 -0700 | [diff] [blame] | 4757 | /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */ |
H. Peter Anvin | dce1e2f | 2002-04-30 21:06:37 +0000 | [diff] [blame] | 4758 | 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] | 4759 | return 0; |
| 4760 | m = is_mmacro(t, ¶ms); |
| 4761 | if (m) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4762 | mname = t->text; |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 4763 | } else { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4764 | Token *last; |
| 4765 | /* |
| 4766 | * We have an id which isn't a macro call. We'll assume |
| 4767 | * it might be a label; we'll also check to see if a |
| 4768 | * colon follows it. Then, if there's another id after |
| 4769 | * that lot, we'll check it again for macro-hood. |
| 4770 | */ |
| 4771 | label = last = t; |
| 4772 | t = t->next; |
| 4773 | if (tok_type_(t, TOK_WHITESPACE)) |
| 4774 | last = t, t = t->next; |
| 4775 | if (tok_is_(t, ":")) { |
| 4776 | dont_prepend = 1; |
| 4777 | last = t, t = t->next; |
| 4778 | if (tok_type_(t, TOK_WHITESPACE)) |
| 4779 | last = t, t = t->next; |
| 4780 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4781 | if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, ¶ms))) |
| 4782 | return 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4783 | last->next = NULL; |
Keith Kanios | 891775e | 2009-07-11 06:08:54 -0500 | [diff] [blame] | 4784 | mname = t->text; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4785 | tline = t; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4786 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4787 | |
| 4788 | /* |
| 4789 | * Fix up the parameters: this involves stripping leading and |
| 4790 | * trailing whitespace, then stripping braces if they are |
| 4791 | * present. |
| 4792 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4793 | for (nparam = 0; params[nparam]; nparam++) ; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4794 | paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4795 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4796 | for (i = 0; params[i]; i++) { |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4797 | int brace = 0; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4798 | int comma = (!m->plus || i < nparam - 1); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4799 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4800 | t = params[i]; |
| 4801 | skip_white_(t); |
| 4802 | if (tok_is_(t, "{")) |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4803 | t = t->next, brace++, comma = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4804 | params[i] = t; |
| 4805 | paramlen[i] = 0; |
| 4806 | while (t) { |
| 4807 | if (comma && t->type == TOK_OTHER && !strcmp(t->text, ",")) |
| 4808 | break; /* ... because we have hit a comma */ |
| 4809 | if (comma && t->type == TOK_WHITESPACE |
| 4810 | && tok_is_(t->next, ",")) |
| 4811 | break; /* ... or a space then a comma */ |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4812 | if (brace && t->type == TOK_OTHER) { |
| 4813 | if (t->text[0] == '{') |
| 4814 | brace++; /* ... or a nested opening brace */ |
| 4815 | else if (t->text[0] == '}') |
| 4816 | if (!--brace) |
| 4817 | break; /* ... or a brace */ |
| 4818 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4819 | t = t->next; |
| 4820 | paramlen[i]++; |
| 4821 | } |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4822 | if (brace) |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 4823 | nasm_nonfatal("macro params should be enclosed in braces"); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4824 | } |
| 4825 | |
| 4826 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4827 | * OK, we have a MMacro structure together with a set of |
| 4828 | * parameters. We must now go through the expansion and push |
| 4829 | * copies of each Line on to istk->expansion. Substitution of |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4830 | * parameter tokens and macro-local tokens doesn't get done |
| 4831 | * until the single-line macro substitution process; this is |
| 4832 | * because delaying them allows us to change the semantics |
| 4833 | * later through %rotate. |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4834 | * |
| 4835 | * First, push an end marker on to istk->expansion, mark this |
| 4836 | * macro as in progress, and set up its invocation-specific |
| 4837 | * variables. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4838 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4839 | ll = nasm_malloc(sizeof(Line)); |
| 4840 | ll->next = istk->expansion; |
| 4841 | ll->finishes = m; |
| 4842 | ll->first = NULL; |
| 4843 | istk->expansion = ll; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4844 | |
| 4845 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4846 | * Save the previous MMacro expansion in the case of |
| 4847 | * macro recursion |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4848 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4849 | if (m->max_depth && m->in_progress) |
| 4850 | push_mmacro(m); |
| 4851 | |
| 4852 | m->in_progress ++; |
| 4853 | m->params = params; |
| 4854 | m->iline = tline; |
| 4855 | m->nparam = nparam; |
| 4856 | m->rotate = 0; |
| 4857 | m->paramlen = paramlen; |
| 4858 | m->unique = unique++; |
| 4859 | m->lineno = 0; |
| 4860 | m->condcnt = 0; |
| 4861 | |
| 4862 | m->next_active = istk->mstk; |
| 4863 | istk->mstk = m; |
| 4864 | |
| 4865 | list_for_each(l, m->expansion) { |
| 4866 | Token **tail; |
| 4867 | |
| 4868 | ll = nasm_malloc(sizeof(Line)); |
| 4869 | ll->finishes = NULL; |
| 4870 | ll->next = istk->expansion; |
| 4871 | istk->expansion = ll; |
| 4872 | tail = &ll->first; |
| 4873 | |
| 4874 | list_for_each(t, l->first) { |
| 4875 | Token *x = t; |
| 4876 | switch (t->type) { |
| 4877 | case TOK_PREPROC_Q: |
| 4878 | tt = *tail = new_Token(NULL, TOK_ID, mname, 0); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4879 | break; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4880 | case TOK_PREPROC_QQ: |
| 4881 | tt = *tail = new_Token(NULL, TOK_ID, m->name, 0); |
| 4882 | break; |
| 4883 | case TOK_PREPROC_ID: |
| 4884 | if (t->text[1] == '0' && t->text[2] == '0') { |
| 4885 | dont_prepend = -1; |
| 4886 | x = label; |
| 4887 | if (!x) |
| 4888 | continue; |
| 4889 | } |
| 4890 | /* fall through */ |
| 4891 | default: |
| 4892 | tt = *tail = new_Token(NULL, x->type, x->text, 0); |
| 4893 | break; |
| 4894 | } |
| 4895 | tail = &tt->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4896 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4897 | *tail = NULL; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4898 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4899 | |
| 4900 | /* |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4901 | * If we had a label, push it on as the first line of |
| 4902 | * the macro expansion. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4903 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4904 | if (label) { |
| 4905 | if (dont_prepend < 0) |
| 4906 | free_tlist(startline); |
| 4907 | else { |
| 4908 | ll = nasm_malloc(sizeof(Line)); |
| 4909 | ll->finishes = NULL; |
| 4910 | ll->next = istk->expansion; |
| 4911 | istk->expansion = ll; |
| 4912 | ll->first = startline; |
| 4913 | if (!dont_prepend) { |
| 4914 | while (label->next) |
| 4915 | label = label->next; |
| 4916 | label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4917 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4918 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4919 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4920 | |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 4921 | lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4922 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4923 | return 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4924 | } |
| 4925 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4926 | /* |
| 4927 | * This function adds macro names to error messages, and suppresses |
| 4928 | * them if necessary. |
| 4929 | */ |
H. Peter Anvin (Intel) | 6bde2ed | 2018-12-13 19:39:41 -0800 | [diff] [blame] | 4930 | 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] | 4931 | { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4932 | char buff[BUFSIZ]; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4933 | MMacro *mmac = NULL; |
| 4934 | int delta = 0; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4935 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4936 | /* |
| 4937 | * If we're in a dead branch of IF or something like it, ignore the error. |
| 4938 | * However, because %else etc are evaluated in the state context |
| 4939 | * of the previous branch, errors might get lost: |
| 4940 | * %if 0 ... %else trailing garbage ... %endif |
| 4941 | * So %else etc should set the ERR_PP_PRECOND flag. |
| 4942 | */ |
| 4943 | if ((severity & ERR_MASK) < ERR_FATAL && |
| 4944 | istk && istk->conds && |
| 4945 | ((severity & ERR_PP_PRECOND) ? |
| 4946 | istk->conds->state == COND_NEVER : |
H. Peter Anvin | eb6653f | 2016-04-05 13:03:10 -0700 | [diff] [blame] | 4947 | !emitting(istk->conds->state))) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4948 | return; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4949 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4950 | /* get %macro name */ |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4951 | if (!(severity & ERR_NOFILE) && istk && istk->mstk) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4952 | mmac = istk->mstk; |
| 4953 | /* but %rep blocks should be skipped */ |
| 4954 | while (mmac && !mmac->name) |
| 4955 | mmac = mmac->next_active, delta++; |
Cyrill Gorcunov | 9900c6b | 2011-10-09 18:58:46 +0400 | [diff] [blame] | 4956 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4957 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4958 | if (mmac) { |
| 4959 | vsnprintf(buff, sizeof(buff), fmt, arg); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4960 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4961 | nasm_set_verror(real_verror); |
| 4962 | nasm_error(severity, "(%s:%d) %s", |
| 4963 | mmac->name, mmac->lineno - delta, buff); |
| 4964 | nasm_set_verror(pp_verror); |
| 4965 | } else { |
| 4966 | real_verror(severity, fmt, arg); |
| 4967 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4968 | } |
| 4969 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 4970 | static Token *stdmac_file(const SMacro *s, Token **params, int *paramsize) |
| 4971 | { |
| 4972 | (void)s; |
| 4973 | (void)params; |
| 4974 | (void)paramsize; |
| 4975 | |
| 4976 | return make_tok_qstr(src_get_fname()); |
| 4977 | } |
| 4978 | |
| 4979 | static Token *stdmac_line(const SMacro *s, Token **params, int *paramsize) |
| 4980 | { |
| 4981 | (void)s; |
| 4982 | (void)params; |
| 4983 | (void)paramsize; |
| 4984 | |
| 4985 | return make_tok_num(src_get_linnum()); |
| 4986 | } |
| 4987 | |
| 4988 | static Token *stdmac_bits(const SMacro *s, Token **params, int *paramsize) |
| 4989 | { |
| 4990 | (void)s; |
| 4991 | (void)params; |
| 4992 | (void)paramsize; |
| 4993 | |
| 4994 | return make_tok_num(globalbits); |
| 4995 | } |
| 4996 | |
| 4997 | static Token *stdmac_ptr(const SMacro *s, Token **params, int *paramsize) |
| 4998 | { |
| 4999 | const char *name; |
| 5000 | |
| 5001 | (void)s; |
| 5002 | (void)params; |
| 5003 | (void)paramsize; |
| 5004 | |
| 5005 | switch (globalbits) { |
| 5006 | case 16: |
| 5007 | name = "word"; |
| 5008 | break; |
| 5009 | case 32: |
| 5010 | name = "dword"; |
| 5011 | break; |
| 5012 | case 64: |
| 5013 | name = "qword"; |
| 5014 | break; |
| 5015 | default: |
| 5016 | panic(); |
| 5017 | } |
| 5018 | return new_Token(NULL, TOK_ID, name, 0); |
| 5019 | } |
| 5020 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5021 | /* Add magic standard macros */ |
| 5022 | struct magic_macros { |
| 5023 | const char *name; |
| 5024 | int nparams; |
| 5025 | Token *(*func)(const SMacro *s, Token **params, int *paramsize); |
| 5026 | }; |
| 5027 | static const struct magic_macros magic_macros[] = |
| 5028 | { |
| 5029 | { "__FILE__", 0, stdmac_file }, |
| 5030 | { "__LINE__", 0, stdmac_line }, |
| 5031 | { "__BITS__", 0, stdmac_bits }, |
| 5032 | { "__PTR__", 0, stdmac_ptr }, |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5033 | { NULL, 0, NULL } |
| 5034 | }; |
| 5035 | |
| 5036 | static void pp_add_magic_stdmac(void) |
| 5037 | { |
| 5038 | const struct magic_macros *m; |
| 5039 | SMacro *s; |
| 5040 | |
| 5041 | for (m = magic_macros; m->name; m++) { |
| 5042 | s = define_smacro(NULL, m->name, true, m->nparams, NULL); |
| 5043 | s->magic = true; |
| 5044 | s->e.magic = m->func; |
| 5045 | } |
| 5046 | } |
| 5047 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5048 | static void |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5049 | 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] | 5050 | { |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5051 | int apass; |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 5052 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5053 | cstk = NULL; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5054 | nasm_new(istk); |
H. Peter Anvin | 3e83cec | 2016-05-25 04:28:46 -0700 | [diff] [blame] | 5055 | istk->fp = nasm_open_read(file, NF_TEXT); |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 5056 | src_set(0, file); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5057 | istk->lineinc = 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5058 | if (!istk->fp) |
Cyrill Gorcunov | c3527dd | 2018-11-24 22:17:47 +0300 | [diff] [blame] | 5059 | nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5060 | defining = NULL; |
Charles Crayne | d4200be | 2008-07-12 16:42:33 -0700 | [diff] [blame] | 5061 | nested_mac_count = 0; |
| 5062 | nested_rep_count = 0; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 5063 | init_macros(); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5064 | unique = 0; |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 5065 | deplist = dep_list; |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5066 | pp_mode = mode; |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 5067 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5068 | pp_add_magic_stdmac(); |
| 5069 | |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 5070 | if (tasm_compatible_mode) |
| 5071 | pp_add_stdmac(nasm_stdmac_tasm); |
| 5072 | |
| 5073 | pp_add_stdmac(nasm_stdmac_nasm); |
| 5074 | pp_add_stdmac(nasm_stdmac_version); |
| 5075 | |
Cyrill Gorcunov | 15ce78f | 2017-01-06 20:21:28 +0300 | [diff] [blame] | 5076 | if (extrastdmac) |
| 5077 | pp_add_stdmac(extrastdmac); |
| 5078 | |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 5079 | stdmacpos = stdmacros[0]; |
| 5080 | stdmacnext = &stdmacros[1]; |
| 5081 | |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 5082 | do_predef = true; |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 5083 | |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 5084 | strlist_add(deplist, file); |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 5085 | |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 5086 | /* |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 5087 | * Define the __PASS__ macro. This is defined here unlike all the |
| 5088 | * other builtins, because it is special -- it varies between |
| 5089 | * passes -- but there is really no particular reason to make it |
| 5090 | * magic. |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5091 | * |
| 5092 | * 0 = dependencies only |
| 5093 | * 1 = preparatory passes |
| 5094 | * 2 = final pass |
| 5095 | * 3 = preproces only |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 5096 | */ |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5097 | switch (mode) { |
| 5098 | case PP_NORMAL: |
| 5099 | apass = pass_final() ? 2 : 1; |
| 5100 | break; |
| 5101 | case PP_DEPS: |
| 5102 | apass = 0; |
| 5103 | break; |
| 5104 | case PP_PREPROC: |
| 5105 | apass = 3; |
| 5106 | break; |
| 5107 | default: |
| 5108 | panic(); |
| 5109 | } |
| 5110 | |
H. Peter Anvin (Intel) | 9bb55bd | 2019-04-24 11:14:43 -0700 | [diff] [blame^] | 5111 | define_smacro(NULL, "__PASS__", true, 0, make_tok_num(apass)); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5112 | } |
| 5113 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 5114 | static void pp_init(void) |
| 5115 | { |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 5116 | } |
| 5117 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5118 | static char *pp_getline(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5119 | { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5120 | char *line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5121 | Token *tline; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5122 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5123 | real_verror = nasm_set_verror(pp_verror); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5124 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5125 | while (1) { |
| 5126 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5127 | * Fetch a tokenized line, either from the macro-expansion |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5128 | * buffer or from the input file. |
| 5129 | */ |
| 5130 | tline = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5131 | while (istk->expansion && istk->expansion->finishes) { |
| 5132 | Line *l = istk->expansion; |
| 5133 | if (!l->finishes->name && l->finishes->in_progress > 1) { |
| 5134 | Line *ll; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5135 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5136 | /* |
| 5137 | * This is a macro-end marker for a macro with no |
| 5138 | * name, which means it's not really a macro at all |
| 5139 | * but a %rep block, and the `in_progress' field is |
| 5140 | * more than 1, meaning that we still need to |
| 5141 | * repeat. (1 means the natural last repetition; 0 |
| 5142 | * means termination by %exitrep.) We have |
| 5143 | * therefore expanded up to the %endrep, and must |
| 5144 | * push the whole block on to the expansion buffer |
| 5145 | * again. We don't bother to remove the macro-end |
| 5146 | * marker: we'd only have to generate another one |
| 5147 | * if we did. |
| 5148 | */ |
| 5149 | l->finishes->in_progress--; |
| 5150 | list_for_each(l, l->finishes->expansion) { |
| 5151 | Token *t, *tt, **tail; |
| 5152 | |
| 5153 | ll = nasm_malloc(sizeof(Line)); |
| 5154 | ll->next = istk->expansion; |
| 5155 | ll->finishes = NULL; |
| 5156 | ll->first = NULL; |
| 5157 | tail = &ll->first; |
| 5158 | |
| 5159 | list_for_each(t, l->first) { |
| 5160 | if (t->text || t->type == TOK_WHITESPACE) { |
| 5161 | tt = *tail = new_Token(NULL, t->type, t->text, 0); |
| 5162 | tail = &tt->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5163 | } |
| 5164 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5165 | |
| 5166 | istk->expansion = ll; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5167 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5168 | } else { |
| 5169 | /* |
| 5170 | * Check whether a `%rep' was started and not ended |
| 5171 | * within this macro expansion. This can happen and |
| 5172 | * should be detected. It's a fatal error because |
| 5173 | * I'm too confused to work out how to recover |
| 5174 | * sensibly from it. |
| 5175 | */ |
| 5176 | if (defining) { |
| 5177 | if (defining->name) |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 5178 | nasm_panic("defining with name in expansion"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5179 | else if (istk->mstk->name) |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 5180 | nasm_fatal("`%%rep' without `%%endrep' within" |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5181 | " expansion of macro `%s'", |
| 5182 | istk->mstk->name); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5183 | } |
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 | /* |
| 5186 | * FIXME: investigate the relationship at this point between |
| 5187 | * istk->mstk and l->finishes |
| 5188 | */ |
| 5189 | { |
| 5190 | MMacro *m = istk->mstk; |
| 5191 | istk->mstk = m->next_active; |
| 5192 | if (m->name) { |
| 5193 | /* |
| 5194 | * This was a real macro call, not a %rep, and |
| 5195 | * therefore the parameter information needs to |
| 5196 | * be freed. |
| 5197 | */ |
| 5198 | if (m->prev) { |
| 5199 | pop_mmacro(m); |
| 5200 | l->finishes->in_progress --; |
| 5201 | } else { |
| 5202 | nasm_free(m->params); |
| 5203 | free_tlist(m->iline); |
| 5204 | nasm_free(m->paramlen); |
| 5205 | l->finishes->in_progress = 0; |
| 5206 | } |
Adam Majer | 91e7240 | 2017-07-25 10:42:01 +0200 | [diff] [blame] | 5207 | } |
| 5208 | |
| 5209 | /* |
| 5210 | * FIXME It is incorrect to always free_mmacro here. |
| 5211 | * It leads to usage-after-free. |
| 5212 | * |
| 5213 | * https://bugzilla.nasm.us/show_bug.cgi?id=3392414 |
| 5214 | */ |
| 5215 | #if 0 |
| 5216 | else |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5217 | free_mmacro(m); |
Adam Majer | 91e7240 | 2017-07-25 10:42:01 +0200 | [diff] [blame] | 5218 | #endif |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5219 | } |
| 5220 | istk->expansion = l->next; |
| 5221 | nasm_free(l); |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 5222 | lfmt->downlevel(LIST_MACRO); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5223 | } |
| 5224 | } |
| 5225 | while (1) { /* until we get a line we can use */ |
| 5226 | |
| 5227 | if (istk->expansion) { /* from a macro expansion */ |
| 5228 | char *p; |
| 5229 | Line *l = istk->expansion; |
| 5230 | if (istk->mstk) |
| 5231 | istk->mstk->lineno++; |
| 5232 | tline = l->first; |
| 5233 | istk->expansion = l->next; |
| 5234 | nasm_free(l); |
| 5235 | p = detoken(tline, false); |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 5236 | lfmt->line(LIST_MACRO, p); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5237 | nasm_free(p); |
| 5238 | break; |
| 5239 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5240 | line = read_line(); |
| 5241 | if (line) { /* from the current input file */ |
| 5242 | line = prepreproc(line); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5243 | tline = tokenize(line); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5244 | nasm_free(line); |
| 5245 | break; |
| 5246 | } |
| 5247 | /* |
| 5248 | * The current file has ended; work down the istk |
| 5249 | */ |
| 5250 | { |
| 5251 | Include *i = istk; |
| 5252 | fclose(i->fp); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5253 | if (i->conds) { |
| 5254 | /* nasm_error can't be conditionally suppressed */ |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 5255 | nasm_fatal("expected `%%endif' before end of file"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5256 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5257 | /* 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] | 5258 | if (i->next) |
| 5259 | src_set(i->lineno, i->fname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5260 | istk = i->next; |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 5261 | lfmt->downlevel(LIST_INCLUDE); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5262 | nasm_free(i); |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5263 | if (!istk) { |
| 5264 | line = NULL; |
| 5265 | goto done; |
| 5266 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5267 | if (istk->expansion && istk->expansion->finishes) |
| 5268 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5269 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5270 | } |
| 5271 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5272 | /* |
| 5273 | * We must expand MMacro parameters and MMacro-local labels |
| 5274 | * _before_ we plunge into directive processing, to cope |
| 5275 | * with things like `%define something %1' such as STRUC |
| 5276 | * uses. Unless we're _defining_ a MMacro, in which case |
| 5277 | * those tokens should be left alone to go into the |
| 5278 | * definition; and unless we're in a non-emitting |
| 5279 | * condition, in which case we don't want to meddle with |
| 5280 | * anything. |
| 5281 | */ |
| 5282 | if (!defining && !(istk->conds && !emitting(istk->conds->state)) |
| 5283 | && !(istk->mstk && !istk->mstk->in_progress)) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5284 | tline = expand_mmac_params(tline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5285 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5286 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5287 | /* |
| 5288 | * Check the line to see if it's a preprocessor directive. |
| 5289 | */ |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 5290 | if (do_directive(tline, &line) == DIRECTIVE_FOUND) { |
| 5291 | if (line) |
| 5292 | break; /* Directive generated output */ |
| 5293 | else |
| 5294 | continue; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5295 | } else if (defining) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5296 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5297 | * We're defining a multi-line macro. We emit nothing |
| 5298 | * at all, and just |
| 5299 | * shove the tokenized line on to the macro definition. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5300 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5301 | Line *l = nasm_malloc(sizeof(Line)); |
| 5302 | l->next = defining->expansion; |
| 5303 | l->first = tline; |
| 5304 | l->finishes = NULL; |
| 5305 | defining->expansion = l; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5306 | continue; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5307 | } else if (istk->conds && !emitting(istk->conds->state)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5308 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5309 | * We're in a non-emitting branch of a condition block. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5310 | * Emit nothing at all, not even a blank line: when we |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5311 | * emerge from the condition we'll give a line-number |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5312 | * directive so we keep our place correctly. |
| 5313 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5314 | free_tlist(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5315 | continue; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5316 | } else if (istk->mstk && !istk->mstk->in_progress) { |
| 5317 | /* |
| 5318 | * We're in a %rep block which has been terminated, so |
| 5319 | * we're walking through to the %endrep without |
| 5320 | * emitting anything. Emit nothing at all, not even a |
| 5321 | * blank line: when we emerge from the %rep block we'll |
| 5322 | * give a line-number directive so we keep our place |
| 5323 | * correctly. |
| 5324 | */ |
| 5325 | free_tlist(tline); |
| 5326 | continue; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5327 | } else { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5328 | tline = expand_smacro(tline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5329 | if (!expand_mmacro(tline)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5330 | /* |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5331 | * De-tokenize the line again, and emit it. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5332 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5333 | line = detoken(tline, true); |
| 5334 | free_tlist(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5335 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5336 | } else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5337 | continue; /* expand_mmacro calls free_tlist */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5338 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5339 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5340 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5341 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5342 | done: |
| 5343 | nasm_set_verror(real_verror); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5344 | return line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5345 | } |
| 5346 | |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5347 | static void pp_cleanup_pass(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5348 | { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5349 | real_verror = nasm_set_verror(pp_verror); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5350 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5351 | if (defining) { |
| 5352 | if (defining->name) { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 5353 | nasm_nonfatal("end of file while still defining macro `%s'", |
| 5354 | defining->name); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5355 | } else { |
Cyrill Gorcunov | 295b795 | 2018-11-25 12:55:48 +0300 | [diff] [blame] | 5356 | nasm_nonfatal("end of file while still in %%rep"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5357 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5358 | |
| 5359 | free_mmacro(defining); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5360 | defining = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5361 | } |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5362 | |
| 5363 | nasm_set_verror(real_verror); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5364 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5365 | while (cstk) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5366 | ctx_pop(); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 5367 | free_macros(); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5368 | while (istk) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5369 | Include *i = istk; |
| 5370 | istk = istk->next; |
| 5371 | fclose(i->fp); |
Cyrill Gorcunov | 8dcfd88 | 2011-03-03 09:18:56 +0300 | [diff] [blame] | 5372 | nasm_free(i); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5373 | } |
| 5374 | while (cstk) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5375 | ctx_pop(); |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 5376 | src_set_fname(NULL); |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5377 | } |
| 5378 | |
| 5379 | static void pp_cleanup_session(void) |
| 5380 | { |
| 5381 | free_llist(predef); |
| 5382 | predef = NULL; |
| 5383 | delete_Blocks(); |
| 5384 | freeTokens = NULL; |
| 5385 | ipath_list = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5386 | } |
| 5387 | |
Cyrill Gorcunov | 8c0666b | 2018-11-24 14:33:48 +0300 | [diff] [blame] | 5388 | static void pp_include_path(struct strlist *list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5389 | { |
Cyrill Gorcunov | 8c0666b | 2018-11-24 14:33:48 +0300 | [diff] [blame] | 5390 | ipath_list = list; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5391 | } |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5392 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5393 | static void pp_pre_include(char *fname) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5394 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5395 | Token *inc, *space, *name; |
| 5396 | Line *l; |
| 5397 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5398 | name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0); |
| 5399 | space = new_Token(name, TOK_WHITESPACE, NULL, 0); |
| 5400 | inc = new_Token(space, TOK_PREPROC_ID, "%include", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5401 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5402 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5403 | l->next = predef; |
| 5404 | l->first = inc; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5405 | l->finishes = NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5406 | predef = l; |
| 5407 | } |
| 5408 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5409 | static void pp_pre_define(char *definition) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5410 | { |
| 5411 | Token *def, *space; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5412 | Line *l; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5413 | char *equals; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5414 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5415 | real_verror = nasm_set_verror(pp_verror); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5416 | |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5417 | equals = strchr(definition, '='); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5418 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 5419 | def = new_Token(space, TOK_PREPROC_ID, "%define", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5420 | if (equals) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5421 | *equals = ' '; |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5422 | space->next = tokenize(definition); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5423 | if (equals) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5424 | *equals = '='; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5425 | |
Cyrill Gorcunov | 6d42e9b | 2015-02-08 11:07:17 +0300 | [diff] [blame] | 5426 | if (space->next->type != TOK_PREPROC_ID && |
| 5427 | space->next->type != TOK_ID) |
H. Peter Anvin (Intel) | 80c4f23 | 2018-12-14 13:33:24 -0800 | [diff] [blame] | 5428 | nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition); |
Cyrill Gorcunov | 6d42e9b | 2015-02-08 11:07:17 +0300 | [diff] [blame] | 5429 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5430 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5431 | l->next = predef; |
| 5432 | l->first = def; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5433 | l->finishes = NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5434 | predef = l; |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5435 | |
| 5436 | nasm_set_verror(real_verror); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5437 | } |
| 5438 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5439 | static void pp_pre_undefine(char *definition) |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5440 | { |
| 5441 | Token *def, *space; |
| 5442 | Line *l; |
| 5443 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5444 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 5445 | def = new_Token(space, TOK_PREPROC_ID, "%undef", 0); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5446 | space->next = tokenize(definition); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5447 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5448 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5449 | l->next = predef; |
| 5450 | l->first = def; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5451 | l->finishes = NULL; |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5452 | predef = l; |
| 5453 | } |
| 5454 | |
H. Peter Anvin | 0599034 | 2018-06-11 13:32:42 -0700 | [diff] [blame] | 5455 | /* Insert an early preprocessor command that doesn't need special handling */ |
| 5456 | static void pp_pre_command(const char *what, char *string) |
| 5457 | { |
| 5458 | char *cmd; |
| 5459 | Token *def, *space; |
| 5460 | Line *l; |
| 5461 | |
| 5462 | def = tokenize(string); |
| 5463 | if (what) { |
| 5464 | cmd = nasm_strcat(what[0] == '%' ? "" : "%", what); |
| 5465 | space = new_Token(def, TOK_WHITESPACE, NULL, 0); |
| 5466 | def = new_Token(space, TOK_PREPROC_ID, cmd, 0); |
| 5467 | } |
| 5468 | |
| 5469 | l = nasm_malloc(sizeof(Line)); |
| 5470 | l->next = predef; |
| 5471 | l->first = def; |
| 5472 | l->finishes = NULL; |
| 5473 | predef = l; |
| 5474 | } |
| 5475 | |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 5476 | static void pp_add_stdmac(macros_t *macros) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5477 | { |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 5478 | macros_t **mp; |
| 5479 | |
| 5480 | /* Find the end of the list and avoid duplicates */ |
| 5481 | for (mp = stdmacros; *mp; mp++) { |
| 5482 | if (*mp == macros) |
| 5483 | return; /* Nothing to do */ |
| 5484 | } |
| 5485 | |
| 5486 | nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]); |
| 5487 | |
| 5488 | *mp = macros; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 5489 | } |
| 5490 | |
Cyrill Gorcunov | 15ce78f | 2017-01-06 20:21:28 +0300 | [diff] [blame] | 5491 | static void pp_extra_stdmac(macros_t *macros) |
| 5492 | { |
| 5493 | extrastdmac = macros; |
| 5494 | } |
| 5495 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5496 | static Token *make_tok_num(int64_t val) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5497 | { |
Cyrill Gorcunov | ce65274 | 2013-05-06 23:43:43 +0400 | [diff] [blame] | 5498 | char numbuf[32]; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame] | 5499 | int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val); |
| 5500 | return new_Token(NULL, TOK_NUMBER, numbuf, len); |
| 5501 | } |
| 5502 | |
| 5503 | static Token *make_tok_qstr(const char *str) |
| 5504 | { |
| 5505 | Token *t = new_Token(NULL, TOK_STRING, NULL, 0); |
| 5506 | t->text = nasm_quote_cstr(str); |
| 5507 | return t; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5508 | } |
| 5509 | |
H. Peter Anvin (Intel) | 6bde2ed | 2018-12-13 19:39:41 -0800 | [diff] [blame] | 5510 | static void pp_list_one_macro(MMacro *m, errflags severity) |
H. Peter Anvin | 3736895 | 2016-05-09 14:10:32 -0700 | [diff] [blame] | 5511 | { |
| 5512 | if (!m) |
| 5513 | return; |
| 5514 | |
| 5515 | /* We need to print the next_active list in reverse order */ |
| 5516 | pp_list_one_macro(m->next_active, severity); |
| 5517 | |
| 5518 | if (m->name && !m->nolist) { |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 5519 | src_set(m->xline + m->lineno, m->fname); |
H. Peter Anvin | ddb2906 | 2018-12-11 00:06:29 -0800 | [diff] [blame] | 5520 | nasm_error(severity, "... from macro `%s' defined", m->name); |
H. Peter Anvin | 3736895 | 2016-05-09 14:10:32 -0700 | [diff] [blame] | 5521 | } |
| 5522 | } |
| 5523 | |
H. Peter Anvin (Intel) | 6bde2ed | 2018-12-13 19:39:41 -0800 | [diff] [blame] | 5524 | static void pp_error_list_macros(errflags severity) |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5525 | { |
H. Peter Anvin | ddb2906 | 2018-12-11 00:06:29 -0800 | [diff] [blame] | 5526 | struct src_location saved; |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5527 | |
H. Peter Anvin | ddb2906 | 2018-12-11 00:06:29 -0800 | [diff] [blame] | 5528 | severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE; |
| 5529 | saved = src_where(); |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5530 | |
Cyrill Gorcunov | 771d04e | 2016-05-10 23:27:03 +0300 | [diff] [blame] | 5531 | if (istk) |
| 5532 | pp_list_one_macro(istk->mstk, severity); |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5533 | |
H. Peter Anvin | ddb2906 | 2018-12-11 00:06:29 -0800 | [diff] [blame] | 5534 | src_update(saved); |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5535 | } |
| 5536 | |
H. Peter Anvin | e746971 | 2016-02-18 02:20:59 -0800 | [diff] [blame] | 5537 | const struct preproc_ops nasmpp = { |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 5538 | pp_init, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5539 | pp_reset, |
| 5540 | pp_getline, |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 5541 | pp_cleanup_pass, |
| 5542 | pp_cleanup_session, |
Cyrill Gorcunov | 15ce78f | 2017-01-06 20:21:28 +0300 | [diff] [blame] | 5543 | pp_extra_stdmac, |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5544 | pp_pre_define, |
| 5545 | pp_pre_undefine, |
| 5546 | pp_pre_include, |
H. Peter Anvin | 0599034 | 2018-06-11 13:32:42 -0700 | [diff] [blame] | 5547 | pp_pre_command, |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5548 | pp_include_path, |
| 5549 | pp_error_list_macros, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5550 | }; |