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 | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 65 | #include <stdio.h> |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 66 | #include <stdarg.h> |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 67 | #include <stdlib.h> |
| 68 | #include <stddef.h> |
| 69 | #include <string.h> |
| 70 | #include <ctype.h> |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 71 | #include <limits.h> |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 72 | |
| 73 | #include "nasm.h" |
| 74 | #include "nasmlib.h" |
H. Peter Anvin | b20bc73 | 2017-03-07 19:23:03 -0800 | [diff] [blame] | 75 | #include "error.h" |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 76 | #include "preproc.h" |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 77 | #include "hashtbl.h" |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 78 | #include "quote.h" |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 79 | #include "stdscan.h" |
H. Peter Anvin | dbb640b | 2009-07-18 18:57:16 -0700 | [diff] [blame] | 80 | #include "eval.h" |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 81 | #include "tokens.h" |
H. Peter Anvin | a4835d4 | 2008-05-20 14:21:29 -0700 | [diff] [blame] | 82 | #include "tables.h" |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 83 | #include "listing.h" |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 84 | |
| 85 | typedef struct SMacro SMacro; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 86 | typedef struct MMacro MMacro; |
| 87 | typedef struct MMacroInvocation MMacroInvocation; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 88 | typedef struct Context Context; |
| 89 | typedef struct Token Token; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 90 | typedef struct Blocks Blocks; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 91 | typedef struct Line Line; |
| 92 | typedef struct Include Include; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 93 | typedef struct Cond Cond; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 94 | |
| 95 | /* |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 96 | * Note on the storage of both SMacro and MMacros: the hash table |
| 97 | * indexes them case-insensitively, and we then have to go through a |
| 98 | * linked list of potential case aliases (and, for MMacros, parameter |
| 99 | * ranges); this is to preserve the matching semantics of the earlier |
| 100 | * code. If the number of case aliases for a specific macro is a |
| 101 | * performance issue, you may want to reconsider your coding style. |
| 102 | */ |
| 103 | |
| 104 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 105 | * Store the definition of a single-line macro. |
| 106 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 107 | struct SMacro { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 108 | SMacro *next; |
| 109 | char *name; |
| 110 | bool casesense; |
| 111 | bool in_progress; |
| 112 | unsigned int nparam; |
| 113 | Token *expansion; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 117 | * Store the definition of a multi-line macro. This is also used to |
| 118 | * store the interiors of `%rep...%endrep' blocks, which are |
| 119 | * effectively self-re-invoking multi-line macros which simply |
| 120 | * don't have a name or bother to appear in the hash tables. %rep |
| 121 | * blocks are signified by having a NULL `name' field. |
| 122 | * |
| 123 | * In a MMacro describing a `%rep' block, the `in_progress' field |
| 124 | * isn't merely boolean, but gives the number of repeats left to |
| 125 | * run. |
| 126 | * |
| 127 | * The `next' field is used for storing MMacros in hash tables; the |
| 128 | * `next_active' field is for stacking them on istk entries. |
| 129 | * |
| 130 | * When a MMacro is being expanded, `params', `iline', `nparam', |
| 131 | * `paramlen', `rotate' and `unique' are local to the invocation. |
| 132 | */ |
| 133 | struct MMacro { |
| 134 | MMacro *next; |
| 135 | MMacroInvocation *prev; /* previous invocation */ |
| 136 | char *name; |
| 137 | int nparam_min, nparam_max; |
| 138 | bool casesense; |
| 139 | bool plus; /* is the last parameter greedy? */ |
| 140 | bool nolist; /* is this macro listing-inhibited? */ |
| 141 | int64_t in_progress; /* is this macro currently being expanded? */ |
| 142 | int32_t max_depth; /* maximum number of recursive expansions allowed */ |
| 143 | Token *dlist; /* All defaults as one list */ |
| 144 | Token **defaults; /* Parameter default pointers */ |
| 145 | int ndefs; /* number of default parameters */ |
| 146 | Line *expansion; |
| 147 | |
| 148 | MMacro *next_active; |
| 149 | MMacro *rep_nest; /* used for nesting %rep */ |
| 150 | Token **params; /* actual parameters */ |
| 151 | Token *iline; /* invocation line */ |
| 152 | unsigned int nparam, rotate; |
| 153 | int *paramlen; |
| 154 | uint64_t unique; |
| 155 | int lineno; /* Current line number on expansion */ |
| 156 | uint64_t condcnt; /* number of if blocks... */ |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 157 | |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 158 | const char *fname; /* File where defined */ |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 159 | int32_t xline; /* First line in macro */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | |
| 163 | /* Store the definition of a multi-line macro, as defined in a |
| 164 | * previous recursive macro expansion. |
| 165 | */ |
| 166 | struct MMacroInvocation { |
| 167 | MMacroInvocation *prev; /* previous invocation */ |
| 168 | Token **params; /* actual parameters */ |
| 169 | Token *iline; /* invocation line */ |
| 170 | unsigned int nparam, rotate; |
| 171 | int *paramlen; |
| 172 | uint64_t unique; |
| 173 | uint64_t condcnt; |
| 174 | }; |
| 175 | |
| 176 | |
| 177 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 178 | * The context stack is composed of a linked list of these. |
| 179 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 180 | struct Context { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 181 | Context *next; |
| 182 | char *name; |
| 183 | struct hash_table localmac; |
| 184 | uint32_t number; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | /* |
| 188 | * This is the internal form which we break input lines up into. |
| 189 | * Typically stored in linked lists. |
| 190 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 191 | * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not |
| 192 | * necessarily used as-is, but is intended to denote the number of |
| 193 | * the substituted parameter. So in the definition |
| 194 | * |
| 195 | * %define a(x,y) ( (x) & ~(y) ) |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 196 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 197 | * the token representing `x' will have its type changed to |
| 198 | * TOK_SMAC_PARAM, but the one representing `y' will be |
| 199 | * TOK_SMAC_PARAM+1. |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 200 | * |
| 201 | * TOK_INTERNAL_STRING is a dirty hack: it's a single string token |
| 202 | * which doesn't need quotes around it. Used in the pre-include |
| 203 | * mechanism as an alternative to trying to find a sensible type of |
| 204 | * quote to use on the filename we were passed. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 205 | */ |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 206 | enum pp_token_type { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 207 | TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID, |
| 208 | TOK_PREPROC_ID, TOK_STRING, |
| 209 | TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER, |
H. Peter Anvin | 6c81f0a | 2008-05-25 21:46:17 -0700 | [diff] [blame] | 210 | TOK_INTERNAL_STRING, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 211 | TOK_PREPROC_Q, TOK_PREPROC_QQ, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 212 | TOK_PASTE, /* %+ */ |
| 213 | TOK_INDIRECT, /* %[...] */ |
| 214 | TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */ |
| 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 | |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 218 | #define PP_CONCAT_MASK(x) (1 << (x)) |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 219 | #define PP_CONCAT_MATCH(t, mask) (PP_CONCAT_MASK((t)->type) & mask) |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 220 | |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 221 | struct tokseq_match { |
| 222 | int mask_head; |
| 223 | int mask_tail; |
| 224 | }; |
| 225 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 226 | struct Token { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 227 | Token *next; |
| 228 | char *text; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 229 | union { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 230 | SMacro *mac; /* associated macro for TOK_SMAC_END */ |
| 231 | size_t len; /* scratch length field */ |
| 232 | } a; /* Auxiliary data */ |
| 233 | enum pp_token_type type; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 234 | }; |
| 235 | |
| 236 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 237 | * Multi-line macro definitions are stored as a linked list of |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 238 | * these, which is essentially a container to allow several linked |
| 239 | * lists of Tokens. |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 240 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 241 | * Note that in this module, linked lists are treated as stacks |
| 242 | * wherever possible. For this reason, Lines are _pushed_ on to the |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 243 | * `expansion' field in MMacro structures, so that the linked list, |
| 244 | * if walked, would give the macro lines in reverse order; this |
| 245 | * means that we can walk the list when expanding a macro, and thus |
| 246 | * push the lines on to the `expansion' field in _istk_ in reverse |
| 247 | * order (so that when popped back off they are in the right |
| 248 | * order). It may seem cockeyed, and it relies on my design having |
| 249 | * an even number of steps in, but it works... |
| 250 | * |
| 251 | * Some of these structures, rather than being actual lines, are |
| 252 | * markers delimiting the end of the expansion of a given macro. |
| 253 | * This is for use in the cycle-tracking and %rep-handling code. |
| 254 | * Such structures have `finishes' non-NULL, and `first' NULL. All |
| 255 | * others have `finishes' NULL, but `first' may still be NULL if |
| 256 | * the line is blank. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 257 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 258 | struct Line { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 259 | Line *next; |
| 260 | MMacro *finishes; |
| 261 | Token *first; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 262 | }; |
| 263 | |
| 264 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 265 | * To handle an arbitrary level of file inclusion, we maintain a |
| 266 | * stack (ie linked list) of these things. |
| 267 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 268 | struct Include { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 269 | Include *next; |
| 270 | FILE *fp; |
| 271 | Cond *conds; |
| 272 | Line *expansion; |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 273 | const char *fname; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 274 | int lineno, lineinc; |
| 275 | MMacro *mstk; /* stack of active macros/reps */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 276 | }; |
| 277 | |
| 278 | /* |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 279 | * File real name hash, so we don't have to re-search the include |
| 280 | * path for every pass (and potentially more than that if a file |
| 281 | * is used more than once.) |
| 282 | */ |
| 283 | struct hash_table FileHash; |
| 284 | |
| 285 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 286 | * Conditional assembly: we maintain a separate stack of these for |
| 287 | * each level of file inclusion. (The only reason we keep the |
| 288 | * stacks separate is to ensure that a stray `%endif' in a file |
| 289 | * included from within the true branch of a `%if' won't terminate |
| 290 | * it and cause confusion: instead, rightly, it'll cause an error.) |
| 291 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 292 | struct Cond { |
| 293 | Cond *next; |
| 294 | int state; |
| 295 | }; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 296 | enum { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 297 | /* |
| 298 | * These states are for use just after %if or %elif: IF_TRUE |
| 299 | * means the condition has evaluated to truth so we are |
| 300 | * currently emitting, whereas IF_FALSE means we are not |
| 301 | * currently emitting but will start doing so if a %else comes |
| 302 | * up. In these states, all directives are admissible: %elif, |
| 303 | * %else and %endif. (And of course %if.) |
| 304 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 305 | COND_IF_TRUE, COND_IF_FALSE, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 306 | /* |
| 307 | * These states come up after a %else: ELSE_TRUE means we're |
| 308 | * emitting, and ELSE_FALSE means we're not. In ELSE_* states, |
| 309 | * any %elif or %else will cause an error. |
| 310 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 311 | COND_ELSE_TRUE, COND_ELSE_FALSE, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 312 | /* |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 313 | * These states mean that we're not emitting now, and also that |
| 314 | * nothing until %endif will be emitted at all. COND_DONE is |
| 315 | * used when we've had our moment of emission |
| 316 | * and have now started seeing %elifs. COND_NEVER is used when |
| 317 | * the condition construct in question is contained within a |
| 318 | * non-emitting branch of a larger condition construct, |
| 319 | * or if there is an error. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 320 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 321 | COND_DONE, COND_NEVER |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 322 | }; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 323 | #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE ) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 324 | |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 325 | /* |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 326 | * These defines are used as the possible return values for do_directive |
| 327 | */ |
| 328 | #define NO_DIRECTIVE_FOUND 0 |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 329 | #define DIRECTIVE_FOUND 1 |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 330 | |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 331 | /* max reps */ |
| 332 | #define REP_LIMIT ((INT64_C(1) << 62)) |
| 333 | |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 334 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 335 | * Condition codes. Note that we use c_ prefix not C_ because C_ is |
| 336 | * used in nasm.h for the "real" condition codes. At _this_ level, |
| 337 | * we treat CXZ and ECXZ as condition codes, albeit non-invertible |
| 338 | * ones, so we need a different enum... |
| 339 | */ |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 340 | static const char * const conditions[] = { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 341 | "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le", |
| 342 | "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no", |
H. Peter Anvin | ce9be34 | 2007-09-12 00:22:29 +0000 | [diff] [blame] | 343 | "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z" |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 344 | }; |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 345 | enum pp_conds { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 346 | c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE, |
| 347 | 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] | 348 | c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z, |
| 349 | c_none = -1 |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 350 | }; |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 351 | static const enum pp_conds inverse_ccs[] = { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 352 | c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE, |
| 353 | 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] | 354 | 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] | 355 | }; |
| 356 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 357 | /* |
| 358 | * Directive names. |
| 359 | */ |
| 360 | /* If this is a an IF, ELIF, ELSE or ENDIF keyword */ |
| 361 | static int is_condition(enum preproc_token arg) |
| 362 | { |
| 363 | return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF); |
| 364 | } |
| 365 | |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 366 | /* For TASM compatibility we need to be able to recognise TASM compatible |
| 367 | * conditional compilation directives. Using the NASM pre-processor does |
| 368 | * not work, so we look for them specifically from the following list and |
| 369 | * then jam in the equivalent NASM directive into the input stream. |
| 370 | */ |
| 371 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 372 | enum { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 373 | TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI, |
| 374 | TM_IFNDEF, TM_INCLUDE, TM_LOCAL |
| 375 | }; |
| 376 | |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 377 | static const char * const tasm_directives[] = { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 378 | "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi", |
| 379 | "ifndef", "include", "local" |
| 380 | }; |
| 381 | |
| 382 | static int StackSize = 4; |
H. Peter Anvin | 6c8b2be | 2016-05-24 23:46:50 -0700 | [diff] [blame] | 383 | static const char *StackPointer = "ebp"; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 384 | static int ArgOffset = 8; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 385 | static int LocalOffset = 0; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 386 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 387 | static Context *cstk; |
| 388 | static Include *istk; |
Cyrill Gorcunov | 8c0666b | 2018-11-24 14:33:48 +0300 | [diff] [blame] | 389 | static const struct strlist *ipath_list; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 390 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 391 | static int pass; /* HACK: pass 0 = generate dependencies only */ |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 392 | static struct strlist *deplist; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 393 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 394 | static uint64_t unique; /* unique identifier numbers */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 395 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 396 | static Line *predef = NULL; |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 397 | static bool do_predef; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 398 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 399 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 400 | * The current set of multi-line macros we have defined. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 401 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 402 | static struct hash_table mmacros; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 403 | |
| 404 | /* |
| 405 | * The current set of single-line macros we have defined. |
| 406 | */ |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 407 | static struct hash_table smacros; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 408 | |
| 409 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 410 | * The multi-line macro we are currently defining, or the %rep |
| 411 | * block we are currently reading, if any. |
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 | static MMacro *defining; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 414 | |
Charles Crayne | d4200be | 2008-07-12 16:42:33 -0700 | [diff] [blame] | 415 | static uint64_t nested_mac_count; |
| 416 | static uint64_t nested_rep_count; |
| 417 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 418 | /* |
| 419 | * The number of macro parameters to allocate space for at a time. |
| 420 | */ |
| 421 | #define PARAM_DELTA 16 |
| 422 | |
| 423 | /* |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 424 | * The standard macro set: defined in macros.c in a set of arrays. |
| 425 | * This gives our position in any macro set, while we are processing it. |
| 426 | * The stdmacset is an array of such macro sets. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 427 | */ |
H. Peter Anvin | a70547f | 2008-07-19 21:44:26 -0700 | [diff] [blame] | 428 | static macros_t *stdmacpos; |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 429 | static macros_t **stdmacnext; |
| 430 | static macros_t *stdmacros[8]; |
Cyrill Gorcunov | 15ce78f | 2017-01-06 20:21:28 +0300 | [diff] [blame] | 431 | static macros_t *extrastdmac; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 432 | |
| 433 | /* |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 434 | * Tokens are allocated in blocks to improve speed |
| 435 | */ |
| 436 | #define TOKEN_BLOCKSIZE 4096 |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 437 | static Token *freeTokens = NULL; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 438 | struct Blocks { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 439 | Blocks *next; |
| 440 | void *chunk; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 441 | }; |
| 442 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 443 | static Blocks blocks = { NULL, NULL }; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 444 | |
| 445 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 446 | * Forward declarations. |
| 447 | */ |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 448 | static void pp_add_stdmac(macros_t *macros); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 449 | static Token *expand_mmac_params(Token * tline); |
| 450 | static Token *expand_smacro(Token * tline); |
| 451 | static Token *expand_id(Token * tline); |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 452 | static Context *get_ctx(const char *name, const char **namep); |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 453 | static void make_tok_num(Token * tok, int64_t val); |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 454 | static void pp_verror(int severity, const char *fmt, va_list ap); |
| 455 | static vefunc real_verror; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 456 | static void *new_Block(size_t size); |
| 457 | static void delete_Blocks(void); |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 458 | static Token *new_Token(Token * next, enum pp_token_type type, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 459 | const char *text, int txtlen); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 460 | static Token *delete_Token(Token * t); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 461 | |
| 462 | /* |
| 463 | * Macros for safe checking of token pointers, avoid *(NULL) |
| 464 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 465 | #define tok_type_(x,t) ((x) && (x)->type == (t)) |
| 466 | #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next |
| 467 | #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v))) |
| 468 | #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] | 469 | |
Cyrill Gorcunov | 194ba89 | 2011-06-30 01:16:35 +0400 | [diff] [blame] | 470 | /* |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 471 | * nasm_unquote with error if the string contains NUL characters. |
| 472 | * If the string contains NUL characters, issue an error and return |
| 473 | * the C len, i.e. truncate at the NUL. |
| 474 | */ |
| 475 | static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive) |
| 476 | { |
| 477 | size_t len = nasm_unquote(qstr, NULL); |
| 478 | size_t clen = strlen(qstr); |
| 479 | |
| 480 | if (len != clen) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 481 | nasm_error(ERR_NONFATAL, "NUL character in `%s' directive", |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 482 | pp_directives[directive]); |
| 483 | |
| 484 | return clen; |
| 485 | } |
| 486 | |
| 487 | /* |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 488 | * In-place reverse a list of tokens. |
| 489 | */ |
| 490 | static Token *reverse_tokens(Token *t) |
| 491 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 492 | Token *prev = NULL; |
| 493 | Token *next; |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 494 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 495 | while (t) { |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 496 | next = t->next; |
| 497 | t->next = prev; |
| 498 | prev = t; |
| 499 | t = next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 500 | } |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 501 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 502 | return prev; |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | /* |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 506 | * Handle TASM specific directives, which do not contain a % in |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 507 | * front of them. We do it here because I could not find any other |
| 508 | * place to do it for the moment, and it is a hack (ideally it would |
| 509 | * be nice to be able to use the NASM pre-processor to do it). |
| 510 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 511 | static char *check_tasm_directive(char *line) |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 512 | { |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 513 | int32_t i, j, k, m, len; |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 514 | char *p, *q, *oldline, oldchar; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 515 | |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 516 | p = nasm_skip_spaces(line); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 517 | |
| 518 | /* Binary search for the directive name */ |
| 519 | i = -1; |
Cyrill Gorcunov | a731924 | 2010-06-03 22:04:36 +0400 | [diff] [blame] | 520 | j = ARRAY_SIZE(tasm_directives); |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 521 | q = nasm_skip_word(p); |
| 522 | len = q - p; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 523 | if (len) { |
| 524 | oldchar = p[len]; |
| 525 | p[len] = 0; |
| 526 | while (j - i > 1) { |
| 527 | k = (j + i) / 2; |
| 528 | m = nasm_stricmp(p, tasm_directives[k]); |
| 529 | if (m == 0) { |
| 530 | /* We have found a directive, so jam a % in front of it |
| 531 | * so that NASM will then recognise it as one if it's own. |
| 532 | */ |
| 533 | p[len] = oldchar; |
| 534 | len = strlen(p); |
| 535 | oldline = line; |
| 536 | line = nasm_malloc(len + 2); |
| 537 | line[0] = '%'; |
| 538 | if (k == TM_IFDIFI) { |
H. Peter Anvin | 18f4879 | 2009-06-27 15:56:27 -0700 | [diff] [blame] | 539 | /* |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 540 | * NASM does not recognise IFDIFI, so we convert |
| 541 | * it to %if 0. This is not used in NASM |
| 542 | * compatible code, but does need to parse for the |
| 543 | * TASM macro package. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 544 | */ |
H. Peter Anvin | 18f4879 | 2009-06-27 15:56:27 -0700 | [diff] [blame] | 545 | strcpy(line + 1, "if 0"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 546 | } else { |
| 547 | memcpy(line + 1, p, len + 1); |
| 548 | } |
| 549 | nasm_free(oldline); |
| 550 | return line; |
| 551 | } else if (m < 0) { |
| 552 | j = k; |
| 553 | } else |
| 554 | i = k; |
| 555 | } |
| 556 | p[len] = oldchar; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 557 | } |
| 558 | return line; |
| 559 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 560 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 561 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 562 | * The pre-preprocessing stage... This function translates line |
| 563 | * number indications as they emerge from GNU cpp (`# lineno "file" |
| 564 | * flags') into NASM preprocessor line number indications (`%line |
| 565 | * lineno file'). |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 566 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 567 | static char *prepreproc(char *line) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 568 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 569 | int lineno, fnlen; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 570 | char *fname, *oldline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 571 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 572 | if (line[0] == '#' && line[1] == ' ') { |
| 573 | oldline = line; |
| 574 | fname = oldline + 2; |
| 575 | lineno = atoi(fname); |
| 576 | fname += strspn(fname, "0123456789 "); |
| 577 | if (*fname == '"') |
| 578 | fname++; |
| 579 | fnlen = strcspn(fname, "\""); |
| 580 | line = nasm_malloc(20 + fnlen); |
| 581 | snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname); |
| 582 | nasm_free(oldline); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 583 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 584 | if (tasm_compatible_mode) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 585 | return check_tasm_directive(line); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 586 | return line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 590 | * Free a linked list of tokens. |
| 591 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 592 | static void free_tlist(Token * list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 593 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 594 | while (list) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 595 | list = delete_Token(list); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | /* |
| 599 | * Free a linked list of lines. |
| 600 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 601 | static void free_llist(Line * list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 602 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 603 | Line *l, *tmp; |
| 604 | list_for_each_safe(l, tmp, list) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 605 | free_tlist(l->first); |
| 606 | nasm_free(l); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | |
| 610 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 611 | * Free an MMacro |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 612 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 613 | static void free_mmacro(MMacro * m) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 614 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 615 | nasm_free(m->name); |
| 616 | free_tlist(m->dlist); |
| 617 | nasm_free(m->defaults); |
| 618 | free_llist(m->expansion); |
| 619 | nasm_free(m); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | /* |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 623 | * Free all currently defined macros, and free the hash tables |
| 624 | */ |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 625 | static void free_smacro_table(struct hash_table *smt) |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 626 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 627 | SMacro *s, *tmp; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 628 | const char *key; |
| 629 | struct hash_tbl_node *it = NULL; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 630 | |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 631 | while ((s = hash_iterate(smt, &it, &key)) != NULL) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 632 | nasm_free((void *)key); |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 633 | list_for_each_safe(s, tmp, s) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 634 | nasm_free(s->name); |
| 635 | free_tlist(s->expansion); |
| 636 | nasm_free(s); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 637 | } |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 638 | } |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 639 | hash_free(smt); |
| 640 | } |
| 641 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 642 | static void free_mmacro_table(struct hash_table *mmt) |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 643 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 644 | MMacro *m, *tmp; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 645 | const char *key; |
| 646 | struct hash_tbl_node *it = NULL; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 647 | |
| 648 | it = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 649 | while ((m = hash_iterate(mmt, &it, &key)) != NULL) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 650 | nasm_free((void *)key); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 651 | list_for_each_safe(m ,tmp, m) |
| 652 | free_mmacro(m); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 653 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 654 | hash_free(mmt); |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | static void free_macros(void) |
| 658 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 659 | free_smacro_table(&smacros); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 660 | free_mmacro_table(&mmacros); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | /* |
| 664 | * Initialize the hash tables |
| 665 | */ |
| 666 | static void init_macros(void) |
| 667 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 668 | hash_init(&smacros, HASH_LARGE); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 669 | hash_init(&mmacros, HASH_LARGE); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 673 | * Pop the context stack. |
| 674 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 675 | static void ctx_pop(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 676 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 677 | Context *c = cstk; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 678 | |
| 679 | cstk = cstk->next; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 680 | free_smacro_table(&c->localmac); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 681 | nasm_free(c->name); |
| 682 | nasm_free(c); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 683 | } |
| 684 | |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 685 | /* |
| 686 | * Search for a key in the hash index; adding it if necessary |
| 687 | * (in which case we initialize the data pointer to NULL.) |
| 688 | */ |
| 689 | static void ** |
| 690 | hash_findi_add(struct hash_table *hash, const char *str) |
| 691 | { |
| 692 | struct hash_insert hi; |
| 693 | void **r; |
| 694 | char *strx; |
| 695 | |
| 696 | r = hash_findi(hash, str, &hi); |
| 697 | if (r) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 698 | return r; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 699 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 700 | strx = nasm_strdup(str); /* Use a more efficient allocator here? */ |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 701 | return hash_add(&hi, strx, NULL); |
| 702 | } |
| 703 | |
| 704 | /* |
| 705 | * Like hash_findi, but returns the data element rather than a pointer |
| 706 | * to it. Used only when not adding a new element, hence no third |
| 707 | * argument. |
| 708 | */ |
| 709 | static void * |
| 710 | hash_findix(struct hash_table *hash, const char *str) |
| 711 | { |
| 712 | void **p; |
| 713 | |
| 714 | p = hash_findi(hash, str, NULL); |
| 715 | return p ? *p : NULL; |
| 716 | } |
| 717 | |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 718 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 719 | * read line from standart macros set, |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 720 | * if there no more left -- return NULL |
| 721 | */ |
| 722 | static char *line_from_stdmac(void) |
| 723 | { |
| 724 | unsigned char c; |
| 725 | const unsigned char *p = stdmacpos; |
| 726 | char *line, *q; |
| 727 | size_t len = 0; |
| 728 | |
| 729 | if (!stdmacpos) |
| 730 | return NULL; |
| 731 | |
| 732 | while ((c = *p++)) { |
| 733 | if (c >= 0x80) |
| 734 | len += pp_directives_len[c - 0x80] + 1; |
| 735 | else |
| 736 | len++; |
| 737 | } |
| 738 | |
| 739 | line = nasm_malloc(len + 1); |
| 740 | q = line; |
| 741 | while ((c = *stdmacpos++)) { |
| 742 | if (c >= 0x80) { |
| 743 | memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]); |
| 744 | q += pp_directives_len[c - 0x80]; |
| 745 | *q++ = ' '; |
| 746 | } else { |
| 747 | *q++ = c; |
| 748 | } |
| 749 | } |
| 750 | stdmacpos = p; |
| 751 | *q = '\0'; |
| 752 | |
| 753 | if (!*stdmacpos) { |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 754 | /* This was the last of this particular macro set */ |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 755 | stdmacpos = NULL; |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 756 | if (*stdmacnext) { |
| 757 | stdmacpos = *stdmacnext++; |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 758 | } else if (do_predef) { |
| 759 | Line *pd, *l; |
| 760 | Token *head, **tail, *t; |
| 761 | |
| 762 | /* |
| 763 | * Nasty hack: here we push the contents of |
| 764 | * `predef' on to the top-level expansion stack, |
| 765 | * since this is the most convenient way to |
| 766 | * implement the pre-include and pre-define |
| 767 | * features. |
| 768 | */ |
| 769 | list_for_each(pd, predef) { |
| 770 | head = NULL; |
| 771 | tail = &head; |
| 772 | list_for_each(t, pd->first) { |
| 773 | *tail = new_Token(NULL, t->type, t->text, 0); |
| 774 | tail = &(*tail)->next; |
| 775 | } |
| 776 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 777 | l = nasm_malloc(sizeof(Line)); |
| 778 | l->next = istk->expansion; |
| 779 | l->first = head; |
| 780 | l->finishes = NULL; |
| 781 | |
| 782 | istk->expansion = l; |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 783 | } |
| 784 | do_predef = false; |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | return line; |
| 789 | } |
| 790 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 791 | static char *read_line(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 792 | { |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 793 | unsigned int size, c, next; |
| 794 | const unsigned int delta = 512; |
| 795 | const unsigned int pad = 8; |
| 796 | unsigned int nr_cont = 0; |
| 797 | bool cont = false; |
| 798 | char *buffer, *p; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 799 | |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 800 | /* Standart macros set (predefined) goes first */ |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 801 | p = line_from_stdmac(); |
| 802 | if (p) |
| 803 | return p; |
H. Peter Anvin | 72edbb8 | 2008-06-19 16:00:04 -0700 | [diff] [blame] | 804 | |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 805 | size = delta; |
| 806 | p = buffer = nasm_malloc(size); |
| 807 | |
| 808 | for (;;) { |
| 809 | c = fgetc(istk->fp); |
| 810 | if ((int)(c) == EOF) { |
| 811 | p[0] = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 812 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 813 | } |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 814 | |
| 815 | switch (c) { |
| 816 | case '\r': |
| 817 | next = fgetc(istk->fp); |
| 818 | if (next != '\n') |
| 819 | ungetc(next, istk->fp); |
| 820 | if (cont) { |
| 821 | cont = false; |
| 822 | continue; |
| 823 | } |
| 824 | break; |
| 825 | |
| 826 | case '\n': |
| 827 | if (cont) { |
| 828 | cont = false; |
| 829 | continue; |
| 830 | } |
| 831 | break; |
| 832 | |
| 833 | case '\\': |
| 834 | next = fgetc(istk->fp); |
| 835 | ungetc(next, istk->fp); |
Cyrill Gorcunov | 490f85e | 2012-12-27 20:02:17 +0400 | [diff] [blame] | 836 | if (next == '\r' || next == '\n') { |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 837 | cont = true; |
| 838 | nr_cont++; |
| 839 | continue; |
| 840 | } |
| 841 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 842 | } |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 843 | |
| 844 | if (c == '\r' || c == '\n') { |
| 845 | *p++ = 0; |
| 846 | break; |
| 847 | } |
| 848 | |
| 849 | if (p >= (buffer + size - pad)) { |
| 850 | buffer = nasm_realloc(buffer, size + delta); |
| 851 | p = buffer + size - pad; |
| 852 | size += delta; |
| 853 | } |
| 854 | |
| 855 | *p++ = (unsigned char)c; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 856 | } |
| 857 | |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 858 | if (p == buffer) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 859 | nasm_free(buffer); |
| 860 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 861 | } |
| 862 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 863 | src_set_linnum(src_get_linnum() + istk->lineinc + |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 864 | (nr_cont * istk->lineinc)); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 865 | |
| 866 | /* |
| 867 | * Handle spurious ^Z, which may be inserted into source files |
| 868 | * by some file transfer utilities. |
| 869 | */ |
| 870 | buffer[strcspn(buffer, "\032")] = '\0'; |
| 871 | |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 872 | lfmt->line(LIST_READ, buffer); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 873 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 874 | return buffer; |
| 875 | } |
| 876 | |
| 877 | /* |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 878 | * 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] | 879 | * don't need to parse the value out of e.g. numeric tokens: we |
| 880 | * simply split one string into many. |
| 881 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 882 | static Token *tokenize(char *line) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 883 | { |
H. Peter Anvin | ca544db | 2008-10-19 19:30:11 -0700 | [diff] [blame] | 884 | char c, *p = line; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 885 | enum pp_token_type type; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 886 | Token *list = NULL; |
| 887 | Token *t, **tail = &list; |
| 888 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 889 | while (*line) { |
| 890 | p = line; |
| 891 | if (*p == '%') { |
| 892 | p++; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 893 | if (*p == '+' && !nasm_isdigit(p[1])) { |
| 894 | p++; |
| 895 | type = TOK_PASTE; |
| 896 | } else if (nasm_isdigit(*p) || |
| 897 | ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 898 | do { |
| 899 | p++; |
| 900 | } |
H. Peter Anvin | bda7a6e | 2008-06-21 10:23:17 -0700 | [diff] [blame] | 901 | while (nasm_isdigit(*p)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 902 | type = TOK_PREPROC_ID; |
| 903 | } else if (*p == '{') { |
| 904 | p++; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 905 | while (*p) { |
| 906 | if (*p == '}') |
| 907 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 908 | p[-1] = *p; |
| 909 | p++; |
| 910 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 911 | if (*p != '}') |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 912 | nasm_error(ERR_WARNING | ERR_PASS1, |
| 913 | "unterminated %%{ construct"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 914 | p[-1] = '\0'; |
| 915 | if (*p) |
| 916 | p++; |
| 917 | type = TOK_PREPROC_ID; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 918 | } else if (*p == '[') { |
| 919 | int lvl = 1; |
| 920 | line += 2; /* Skip the leading %[ */ |
| 921 | p++; |
| 922 | while (lvl && (c = *p++)) { |
| 923 | switch (c) { |
| 924 | case ']': |
| 925 | lvl--; |
| 926 | break; |
| 927 | case '%': |
| 928 | if (*p == '[') |
| 929 | lvl++; |
| 930 | break; |
| 931 | case '\'': |
| 932 | case '\"': |
| 933 | case '`': |
Cyrill Gorcunov | 3144e84 | 2017-10-22 10:50:55 +0300 | [diff] [blame] | 934 | p = nasm_skip_string(p - 1); |
| 935 | if (*p) |
| 936 | p++; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 937 | break; |
| 938 | default: |
| 939 | break; |
| 940 | } |
| 941 | } |
| 942 | p--; |
| 943 | if (*p) |
| 944 | *p++ = '\0'; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 945 | if (lvl) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 946 | nasm_error(ERR_NONFATAL|ERR_PASS1, |
| 947 | "unterminated %%[ construct"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 948 | type = TOK_INDIRECT; |
| 949 | } else if (*p == '?') { |
| 950 | type = TOK_PREPROC_Q; /* %? */ |
| 951 | p++; |
| 952 | if (*p == '?') { |
| 953 | type = TOK_PREPROC_QQ; /* %?? */ |
| 954 | p++; |
| 955 | } |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 956 | } else if (*p == '!') { |
| 957 | type = TOK_PREPROC_ID; |
| 958 | p++; |
| 959 | if (isidchar(*p)) { |
| 960 | do { |
| 961 | p++; |
| 962 | } |
| 963 | while (isidchar(*p)); |
| 964 | } else if (*p == '\'' || *p == '\"' || *p == '`') { |
| 965 | p = nasm_skip_string(p); |
| 966 | if (*p) |
| 967 | p++; |
| 968 | else |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 969 | nasm_error(ERR_NONFATAL|ERR_PASS1, |
| 970 | "unterminated %%! string"); |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 971 | } else { |
| 972 | /* %! without string or identifier */ |
| 973 | type = TOK_OTHER; /* Legacy behavior... */ |
| 974 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 975 | } else if (isidchar(*p) || |
| 976 | ((*p == '!' || *p == '%' || *p == '$') && |
| 977 | isidchar(p[1]))) { |
| 978 | do { |
| 979 | p++; |
| 980 | } |
| 981 | while (isidchar(*p)); |
| 982 | type = TOK_PREPROC_ID; |
| 983 | } else { |
| 984 | type = TOK_OTHER; |
| 985 | if (*p == '%') |
| 986 | p++; |
| 987 | } |
| 988 | } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) { |
| 989 | type = TOK_ID; |
| 990 | p++; |
| 991 | while (*p && isidchar(*p)) |
| 992 | p++; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 993 | } else if (*p == '\'' || *p == '"' || *p == '`') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 994 | /* |
| 995 | * A string token. |
| 996 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 997 | type = TOK_STRING; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 998 | p = nasm_skip_string(p); |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 999 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1000 | if (*p) { |
| 1001 | p++; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1002 | } else { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1003 | nasm_error(ERR_WARNING|ERR_PASS1, "unterminated string"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1004 | /* Handling unterminated strings by UNV */ |
| 1005 | /* type = -1; */ |
| 1006 | } |
Victor van den Elzen | fb5f251 | 2009-04-17 16:17:59 +0200 | [diff] [blame] | 1007 | } else if (p[0] == '$' && p[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1008 | type = TOK_OTHER; /* TOKEN_BASE */ |
Victor van den Elzen | fb5f251 | 2009-04-17 16:17:59 +0200 | [diff] [blame] | 1009 | p += 2; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1010 | } else if (isnumstart(*p)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1011 | bool is_hex = false; |
| 1012 | bool is_float = false; |
| 1013 | bool has_e = false; |
| 1014 | char c, *r; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1015 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1016 | /* |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1017 | * A numeric token. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1018 | */ |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1019 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1020 | if (*p == '$') { |
| 1021 | p++; |
| 1022 | is_hex = true; |
| 1023 | } |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1024 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1025 | for (;;) { |
| 1026 | c = *p++; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1027 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1028 | if (!is_hex && (c == 'e' || c == 'E')) { |
| 1029 | has_e = true; |
| 1030 | if (*p == '+' || *p == '-') { |
| 1031 | /* |
| 1032 | * e can only be followed by +/- if it is either a |
| 1033 | * prefixed hex number or a floating-point number |
| 1034 | */ |
| 1035 | p++; |
| 1036 | is_float = true; |
| 1037 | } |
| 1038 | } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') { |
| 1039 | is_hex = true; |
| 1040 | } else if (c == 'P' || c == 'p') { |
| 1041 | is_float = true; |
| 1042 | if (*p == '+' || *p == '-') |
| 1043 | p++; |
Martin Lindhe | b150e38 | 2016-11-16 15:36:53 +0100 | [diff] [blame] | 1044 | } else if (isnumchar(c)) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1045 | ; /* just advance */ |
| 1046 | else if (c == '.') { |
| 1047 | /* |
| 1048 | * we need to deal with consequences of the legacy |
| 1049 | * parser, like "1.nolist" being two tokens |
| 1050 | * (TOK_NUMBER, TOK_ID) here; at least give it |
| 1051 | * a shot for now. In the future, we probably need |
| 1052 | * a flex-based scanner with proper pattern matching |
| 1053 | * to do it as well as it can be done. Nothing in |
| 1054 | * the world is going to help the person who wants |
| 1055 | * 0x123.p16 interpreted as two tokens, though. |
| 1056 | */ |
| 1057 | r = p; |
| 1058 | while (*r == '_') |
| 1059 | r++; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1060 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1061 | if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) || |
| 1062 | (!is_hex && (*r == 'e' || *r == 'E')) || |
| 1063 | (*r == 'p' || *r == 'P')) { |
| 1064 | p = r; |
| 1065 | is_float = true; |
| 1066 | } else |
| 1067 | break; /* Terminate the token */ |
| 1068 | } else |
| 1069 | break; |
| 1070 | } |
| 1071 | p--; /* Point to first character beyond number */ |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1072 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1073 | if (p == line+1 && *line == '$') { |
| 1074 | type = TOK_OTHER; /* TOKEN_HERE */ |
| 1075 | } else { |
| 1076 | if (has_e && !is_hex) { |
| 1077 | /* 1e13 is floating-point, but 1e13h is not */ |
| 1078 | is_float = true; |
| 1079 | } |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 1080 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1081 | type = is_float ? TOK_FLOAT : TOK_NUMBER; |
| 1082 | } |
H. Peter Anvin | bda7a6e | 2008-06-21 10:23:17 -0700 | [diff] [blame] | 1083 | } else if (nasm_isspace(*p)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1084 | type = TOK_WHITESPACE; |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 1085 | p = nasm_skip_spaces(p); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1086 | /* |
| 1087 | * Whitespace just before end-of-line is discarded by |
| 1088 | * pretending it's a comment; whitespace just before a |
| 1089 | * comment gets lumped into the comment. |
| 1090 | */ |
| 1091 | if (!*p || *p == ';') { |
| 1092 | type = TOK_COMMENT; |
| 1093 | while (*p) |
| 1094 | p++; |
| 1095 | } |
| 1096 | } else if (*p == ';') { |
| 1097 | type = TOK_COMMENT; |
| 1098 | while (*p) |
| 1099 | p++; |
| 1100 | } else { |
| 1101 | /* |
| 1102 | * Anything else is an operator of some kind. We check |
| 1103 | * for all the double-character operators (>>, <<, //, |
| 1104 | * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1105 | * else is a single-character operator. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1106 | */ |
| 1107 | type = TOK_OTHER; |
| 1108 | if ((p[0] == '>' && p[1] == '>') || |
| 1109 | (p[0] == '<' && p[1] == '<') || |
| 1110 | (p[0] == '/' && p[1] == '/') || |
| 1111 | (p[0] == '<' && p[1] == '=') || |
| 1112 | (p[0] == '>' && p[1] == '=') || |
| 1113 | (p[0] == '=' && p[1] == '=') || |
| 1114 | (p[0] == '!' && p[1] == '=') || |
| 1115 | (p[0] == '<' && p[1] == '>') || |
| 1116 | (p[0] == '&' && p[1] == '&') || |
| 1117 | (p[0] == '|' && p[1] == '|') || |
| 1118 | (p[0] == '^' && p[1] == '^')) { |
| 1119 | p++; |
| 1120 | } |
| 1121 | p++; |
| 1122 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1123 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1124 | /* Handling unterminated string by UNV */ |
| 1125 | /*if (type == -1) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1126 | { |
| 1127 | *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1); |
| 1128 | t->text[p-line] = *line; |
| 1129 | tail = &t->next; |
| 1130 | } |
| 1131 | else */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1132 | if (type != TOK_COMMENT) { |
| 1133 | *tail = t = new_Token(NULL, type, line, p - line); |
| 1134 | tail = &t->next; |
| 1135 | } |
| 1136 | line = p; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1137 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1138 | return list; |
| 1139 | } |
| 1140 | |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1141 | /* |
| 1142 | * this function allocates a new managed block of memory and |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1143 | * returns a pointer to the block. The managed blocks are |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1144 | * deleted only all at once by the delete_Blocks function. |
| 1145 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1146 | static void *new_Block(size_t size) |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1147 | { |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1148 | Blocks *b = &blocks; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1149 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1150 | /* first, get to the end of the linked list */ |
| 1151 | while (b->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1152 | b = b->next; |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1153 | /* now allocate the requested chunk */ |
| 1154 | b->chunk = nasm_malloc(size); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1155 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1156 | /* now allocate a new block for the next request */ |
Cyrill Gorcunov | c31767c | 2014-06-28 02:22:17 +0400 | [diff] [blame] | 1157 | b->next = nasm_zalloc(sizeof(Blocks)); |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1158 | return b->chunk; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | /* |
| 1162 | * this function deletes all managed blocks of memory |
| 1163 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1164 | static void delete_Blocks(void) |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1165 | { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1166 | Blocks *a, *b = &blocks; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1167 | |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1168 | /* |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1169 | * keep in mind that the first block, pointed to by blocks |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1170 | * is a static and not dynamically allocated, so we don't |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1171 | * free it. |
| 1172 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1173 | while (b) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1174 | if (b->chunk) |
| 1175 | nasm_free(b->chunk); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1176 | a = b; |
| 1177 | b = b->next; |
| 1178 | if (a != &blocks) |
| 1179 | nasm_free(a); |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1180 | } |
Cyrill Gorcunov | dae24d7 | 2014-06-28 10:17:39 +0400 | [diff] [blame] | 1181 | memset(&blocks, 0, sizeof(blocks)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1182 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1183 | |
| 1184 | /* |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1185 | * 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] | 1186 | * 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] | 1187 | * also the a.mac and next elements to NULL. |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1188 | */ |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1189 | static Token *new_Token(Token * next, enum pp_token_type type, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1190 | const char *text, int txtlen) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1191 | { |
| 1192 | Token *t; |
| 1193 | int i; |
| 1194 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1195 | if (!freeTokens) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1196 | freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token)); |
| 1197 | for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++) |
| 1198 | freeTokens[i].next = &freeTokens[i + 1]; |
| 1199 | freeTokens[i].next = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1200 | } |
| 1201 | t = freeTokens; |
| 1202 | freeTokens = t->next; |
| 1203 | t->next = next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 1204 | t->a.mac = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1205 | t->type = type; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1206 | if (type == TOK_WHITESPACE || !text) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1207 | t->text = NULL; |
| 1208 | } else { |
| 1209 | if (txtlen == 0) |
| 1210 | txtlen = strlen(text); |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1211 | t->text = nasm_malloc(txtlen+1); |
| 1212 | memcpy(t->text, text, txtlen); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1213 | t->text[txtlen] = '\0'; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1214 | } |
| 1215 | return t; |
| 1216 | } |
| 1217 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1218 | static Token *delete_Token(Token * t) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1219 | { |
| 1220 | Token *next = t->next; |
| 1221 | nasm_free(t->text); |
H. Peter Anvin | 788e6c1 | 2002-04-30 21:02:01 +0000 | [diff] [blame] | 1222 | t->next = freeTokens; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1223 | freeTokens = t; |
| 1224 | return next; |
| 1225 | } |
| 1226 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1227 | /* |
| 1228 | * Convert a line of tokens back into text. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1229 | * If expand_locals is not zero, identifiers of the form "%$*xxx" |
| 1230 | * will be transformed into ..@ctxnum.xxx |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1231 | */ |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 1232 | static char *detoken(Token * tlist, bool expand_locals) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1233 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1234 | Token *t; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1235 | char *line, *p; |
H. Peter Anvin | b4daadc | 2008-01-21 16:31:57 -0800 | [diff] [blame] | 1236 | const char *q; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1237 | int len = 0; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1238 | |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1239 | list_for_each(t, tlist) { |
Cyrill Gorcunov | 9b7ee09 | 2017-10-22 21:42:59 +0300 | [diff] [blame] | 1240 | if (t->type == TOK_PREPROC_ID && t->text && |
| 1241 | t->text[0] && t->text[1] == '!') { |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1242 | char *v; |
| 1243 | char *q = t->text; |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1244 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1245 | v = t->text + 2; |
| 1246 | if (*v == '\'' || *v == '\"' || *v == '`') { |
| 1247 | size_t len = nasm_unquote(v, NULL); |
| 1248 | size_t clen = strlen(v); |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1249 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1250 | if (len != clen) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1251 | nasm_error(ERR_NONFATAL | ERR_PASS1, |
| 1252 | "NUL character in %%! string"); |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1253 | v = NULL; |
| 1254 | } |
| 1255 | } |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1256 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1257 | if (v) { |
| 1258 | char *p = getenv(v); |
| 1259 | if (!p) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1260 | nasm_error(ERR_NONFATAL | ERR_PASS1, |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1261 | "nonexistent environment variable `%s'", v); |
Cyrill Gorcunov | bbb7a1a | 2016-06-19 12:15:24 +0300 | [diff] [blame] | 1262 | /* |
| 1263 | * FIXME We better should investigate if accessing |
| 1264 | * ->text[1] without ->text[0] is safe enough. |
| 1265 | */ |
| 1266 | t->text = nasm_zalloc(2); |
| 1267 | } else |
| 1268 | t->text = nasm_strdup(p); |
Cyrill Gorcunov | 7500487 | 2017-07-26 01:21:16 +0300 | [diff] [blame] | 1269 | nasm_free(q); |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1270 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1271 | } |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1272 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1273 | /* Expand local macros here and not during preprocessing */ |
| 1274 | if (expand_locals && |
| 1275 | t->type == TOK_PREPROC_ID && t->text && |
| 1276 | t->text[0] == '%' && t->text[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1277 | const char *q; |
| 1278 | char *p; |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1279 | Context *ctx = get_ctx(t->text, &q); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1280 | if (ctx) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1281 | char buffer[40]; |
Keith Kanios | 93f2e9a | 2007-04-14 00:10:59 +0000 | [diff] [blame] | 1282 | snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1283 | p = nasm_strcat(buffer, q); |
| 1284 | nasm_free(t->text); |
| 1285 | t->text = p; |
| 1286 | } |
| 1287 | } |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1288 | if (t->type == TOK_WHITESPACE) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1289 | len++; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1290 | else if (t->text) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1291 | len += strlen(t->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1292 | } |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1293 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1294 | p = line = nasm_malloc(len + 1); |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1295 | |
| 1296 | list_for_each(t, tlist) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1297 | if (t->type == TOK_WHITESPACE) { |
H. Peter Anvin | b4daadc | 2008-01-21 16:31:57 -0800 | [diff] [blame] | 1298 | *p++ = ' '; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1299 | } else if (t->text) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1300 | q = t->text; |
| 1301 | while (*q) |
| 1302 | *p++ = *q++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1303 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1304 | } |
| 1305 | *p = '\0'; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1306 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1307 | return line; |
| 1308 | } |
| 1309 | |
| 1310 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1311 | * A scanner, suitable for use by the expression evaluator, which |
| 1312 | * operates on a line of Tokens. Expects a pointer to a pointer to |
| 1313 | * the first token in the line to be passed in as its private_data |
| 1314 | * field. |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1315 | * |
| 1316 | * FIX: This really needs to be unified with stdscan. |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1317 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1318 | static int ppscan(void *private_data, struct tokenval *tokval) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1319 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1320 | Token **tlineptr = private_data; |
| 1321 | Token *tline; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1322 | char ourcopy[MAX_KEYWORD+1], *p, *r, *s; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1323 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1324 | do { |
| 1325 | tline = *tlineptr; |
| 1326 | *tlineptr = tline ? tline->next : NULL; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 1327 | } while (tline && (tline->type == TOK_WHITESPACE || |
| 1328 | tline->type == TOK_COMMENT)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1329 | |
| 1330 | if (!tline) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1331 | return tokval->t_type = TOKEN_EOS; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1332 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1333 | tokval->t_charptr = tline->text; |
| 1334 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1335 | if (tline->text[0] == '$' && !tline->text[1]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1336 | return tokval->t_type = TOKEN_HERE; |
H. Peter Anvin | 7cf897e | 2002-05-30 21:30:33 +0000 | [diff] [blame] | 1337 | if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1338 | return tokval->t_type = TOKEN_BASE; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1339 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1340 | if (tline->type == TOK_ID) { |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1341 | p = tokval->t_charptr = tline->text; |
| 1342 | if (p[0] == '$') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1343 | tokval->t_charptr++; |
| 1344 | return tokval->t_type = TOKEN_ID; |
| 1345 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1346 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1347 | for (r = p, s = ourcopy; *r; r++) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1348 | if (r >= p+MAX_KEYWORD) |
| 1349 | return tokval->t_type = TOKEN_ID; /* Not a keyword */ |
H. Peter Anvin | ac8f8fc | 2008-06-11 15:49:41 -0700 | [diff] [blame] | 1350 | *s++ = nasm_tolower(*r); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1351 | } |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1352 | *s = '\0'; |
| 1353 | /* right, so we have an identifier sitting in temp storage. now, |
| 1354 | * is it actually a register or instruction name, or what? */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1355 | return nasm_token_hash(ourcopy, tokval); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1358 | if (tline->type == TOK_NUMBER) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1359 | bool rn_error; |
| 1360 | tokval->t_integer = readnum(tline->text, &rn_error); |
| 1361 | tokval->t_charptr = tline->text; |
| 1362 | if (rn_error) |
| 1363 | return tokval->t_type = TOKEN_ERRNUM; |
| 1364 | else |
| 1365 | return tokval->t_type = TOKEN_NUM; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1366 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1367 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1368 | if (tline->type == TOK_FLOAT) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1369 | return tokval->t_type = TOKEN_FLOAT; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1370 | } |
| 1371 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1372 | if (tline->type == TOK_STRING) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1373 | char bq, *ep; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1374 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1375 | bq = tline->text[0]; |
H. Peter Anvin | 1162704 | 2008-06-09 20:45:19 -0700 | [diff] [blame] | 1376 | tokval->t_charptr = tline->text; |
| 1377 | tokval->t_inttwo = nasm_unquote(tline->text, &ep); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 1378 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1379 | if (ep[0] != bq || ep[1] != '\0') |
| 1380 | return tokval->t_type = TOKEN_ERRSTR; |
| 1381 | else |
| 1382 | return tokval->t_type = TOKEN_STR; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1385 | if (tline->type == TOK_OTHER) { |
| 1386 | if (!strcmp(tline->text, "<<")) |
| 1387 | return tokval->t_type = TOKEN_SHL; |
| 1388 | if (!strcmp(tline->text, ">>")) |
| 1389 | return tokval->t_type = TOKEN_SHR; |
| 1390 | if (!strcmp(tline->text, "//")) |
| 1391 | return tokval->t_type = TOKEN_SDIV; |
| 1392 | if (!strcmp(tline->text, "%%")) |
| 1393 | return tokval->t_type = TOKEN_SMOD; |
| 1394 | if (!strcmp(tline->text, "==")) |
| 1395 | return tokval->t_type = TOKEN_EQ; |
| 1396 | if (!strcmp(tline->text, "<>")) |
| 1397 | return tokval->t_type = TOKEN_NE; |
| 1398 | if (!strcmp(tline->text, "!=")) |
| 1399 | return tokval->t_type = TOKEN_NE; |
| 1400 | if (!strcmp(tline->text, "<=")) |
| 1401 | return tokval->t_type = TOKEN_LE; |
| 1402 | if (!strcmp(tline->text, ">=")) |
| 1403 | return tokval->t_type = TOKEN_GE; |
| 1404 | if (!strcmp(tline->text, "&&")) |
| 1405 | return tokval->t_type = TOKEN_DBL_AND; |
| 1406 | if (!strcmp(tline->text, "^^")) |
| 1407 | return tokval->t_type = TOKEN_DBL_XOR; |
| 1408 | if (!strcmp(tline->text, "||")) |
| 1409 | return tokval->t_type = TOKEN_DBL_OR; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
| 1412 | /* |
| 1413 | * We have no other options: just return the first character of |
| 1414 | * the token text. |
| 1415 | */ |
| 1416 | return tokval->t_type = tline->text[0]; |
| 1417 | } |
| 1418 | |
| 1419 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1420 | * Compare a string to the name of an existing macro; this is a |
| 1421 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 1422 | * depending on the value of the `casesense' parameter. |
| 1423 | */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 1424 | static int mstrcmp(const char *p, const char *q, bool casesense) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1425 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1426 | return casesense ? strcmp(p, q) : nasm_stricmp(p, q); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
| 1429 | /* |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1430 | * Compare a string to the name of an existing macro; this is a |
| 1431 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 1432 | * depending on the value of the `casesense' parameter. |
| 1433 | */ |
| 1434 | static int mmemcmp(const char *p, const char *q, size_t l, bool casesense) |
| 1435 | { |
| 1436 | return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l); |
| 1437 | } |
| 1438 | |
| 1439 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1440 | * Return the Context structure associated with a %$ token. Return |
| 1441 | * NULL, having _already_ reported an error condition, if the |
| 1442 | * context stack isn't deep enough for the supplied number of $ |
| 1443 | * signs. |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1444 | * |
| 1445 | * If "namep" is non-NULL, set it to the pointer to the macro name |
| 1446 | * tail, i.e. the part beyond %$... |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1447 | */ |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1448 | static Context *get_ctx(const char *name, const char **namep) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1449 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1450 | Context *ctx; |
| 1451 | int i; |
| 1452 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1453 | if (namep) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1454 | *namep = name; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1455 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1456 | if (!name || name[0] != '%' || name[1] != '$') |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1457 | return NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1458 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1459 | if (!cstk) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1460 | nasm_error(ERR_NONFATAL, "`%s': context stack is empty", name); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1461 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1464 | name += 2; |
| 1465 | ctx = cstk; |
| 1466 | i = 0; |
| 1467 | while (ctx && *name == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1468 | name++; |
| 1469 | i++; |
| 1470 | ctx = ctx->next; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1471 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1472 | if (!ctx) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1473 | nasm_error(ERR_NONFATAL, "`%s': context stack is only" |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1474 | " %d level%s deep", name, i, (i == 1 ? "" : "s")); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1475 | return NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1476 | } |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1477 | |
| 1478 | if (namep) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1479 | *namep = name; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1480 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1481 | return ctx; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1482 | } |
| 1483 | |
| 1484 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1485 | * Open an include file. This routine must always return a valid |
| 1486 | * file pointer if it returns - it's responsible for throwing an |
| 1487 | * ERR_FATAL and bombing out completely if not. It should also try |
| 1488 | * the include path one by one until it finds the file or reaches |
| 1489 | * the end of the path. |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1490 | * |
| 1491 | * Note: for INC_PROBE the function returns NULL at all times; |
| 1492 | * instead look for the |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1493 | */ |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1494 | enum incopen_mode { |
| 1495 | INC_NEEDED, /* File must exist */ |
| 1496 | INC_OPTIONAL, /* Missing is OK */ |
| 1497 | INC_PROBE /* Only an existence probe */ |
| 1498 | }; |
| 1499 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1500 | /* This is conducts a full pathname search */ |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1501 | static FILE *inc_fopen_search(const char *file, char **slpath, |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1502 | enum incopen_mode omode, enum file_flags fmode) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1503 | { |
Cyrill Gorcunov | 8c0666b | 2018-11-24 14:33:48 +0300 | [diff] [blame] | 1504 | const struct strlist_entry *ip = NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1505 | FILE *fp; |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1506 | const char *prefix = ""; |
night199uk | fdb1a1b | 2018-10-18 23:19:47 +0200 | [diff] [blame] | 1507 | char *sp; |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1508 | bool found; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1509 | |
Cyrill Gorcunov | 8c0666b | 2018-11-24 14:33:48 +0300 | [diff] [blame] | 1510 | if (ipath_list) |
| 1511 | ip = ipath_list->head; |
| 1512 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1513 | while (1) { |
night199uk | fdb1a1b | 2018-10-18 23:19:47 +0200 | [diff] [blame] | 1514 | sp = nasm_catfile(prefix, file); |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1515 | if (omode == INC_PROBE) { |
| 1516 | fp = NULL; |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1517 | found = nasm_file_exists(sp); |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1518 | } else { |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1519 | fp = nasm_open_read(sp, fmode); |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1520 | found = (fp != NULL); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1521 | } |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1522 | if (found) { |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1523 | *slpath = sp; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1524 | return fp; |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1525 | } |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 1526 | |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1527 | nasm_free(sp); |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 1528 | |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1529 | if (!ip) { |
| 1530 | *slpath = NULL; |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1531 | return NULL; |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1532 | } |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1533 | |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1534 | prefix = ip->str; |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1535 | ip = ip->next; |
| 1536 | } |
| 1537 | } |
| 1538 | |
| 1539 | /* |
| 1540 | * Open a file, or test for the presence of one (depending on omode), |
| 1541 | * considering the include path. |
| 1542 | */ |
| 1543 | static FILE *inc_fopen(const char *file, |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 1544 | struct strlist *dhead, |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 1545 | const char **found_path, |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1546 | enum incopen_mode omode, |
| 1547 | enum file_flags fmode) |
| 1548 | { |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1549 | struct hash_insert hi; |
| 1550 | void **hp; |
| 1551 | char *path; |
| 1552 | FILE *fp = NULL; |
| 1553 | |
| 1554 | hp = hash_find(&FileHash, file, &hi); |
| 1555 | if (hp) { |
| 1556 | path = *hp; |
Martin Storsjö | f283c8f | 2017-08-13 17:28:46 +0300 | [diff] [blame] | 1557 | if (path || omode != INC_NEEDED) { |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 1558 | strlist_add(dhead, path ? path : file); |
Martin Storsjö | f283c8f | 2017-08-13 17:28:46 +0300 | [diff] [blame] | 1559 | } |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1560 | } else { |
| 1561 | /* Need to do the actual path search */ |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1562 | fp = inc_fopen_search(file, &path, omode, fmode); |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1563 | |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1564 | /* Positive or negative result */ |
| 1565 | hash_add(&hi, nasm_strdup(file), path); |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1566 | |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 1567 | /* |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1568 | * Add file to dependency path. |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 1569 | */ |
| 1570 | if (path || omode != INC_NEEDED) |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 1571 | strlist_add(dhead, file); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1572 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1573 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1574 | if (!path) { |
| 1575 | if (omode == INC_NEEDED) |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 1576 | nasm_fatal("unable to open include file `%s'", file); |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 1577 | } else { |
| 1578 | if (!fp && omode != INC_PROBE) |
| 1579 | fp = nasm_open_read(path, fmode); |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1580 | } |
| 1581 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1582 | if (found_path) |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 1583 | *found_path = path; |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1584 | |
| 1585 | return fp; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
| 1588 | /* |
Fabian Giesen | 0bbc38d | 2016-04-28 13:48:14 -0700 | [diff] [blame] | 1589 | * Opens an include or input file. Public version, for use by modules |
| 1590 | * that get a file:lineno pair and need to look at the file again |
| 1591 | * (e.g. the CodeView debug backend). Returns NULL on failure. |
| 1592 | */ |
H. Peter Anvin | 3e83cec | 2016-05-25 04:28:46 -0700 | [diff] [blame] | 1593 | FILE *pp_input_fopen(const char *filename, enum file_flags mode) |
Fabian Giesen | 0bbc38d | 2016-04-28 13:48:14 -0700 | [diff] [blame] | 1594 | { |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 1595 | return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode); |
Fabian Giesen | 0bbc38d | 2016-04-28 13:48:14 -0700 | [diff] [blame] | 1596 | } |
| 1597 | |
| 1598 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1599 | * 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] | 1600 | * 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] | 1601 | * return true if _any_ single-line macro of that name is defined. |
| 1602 | * Otherwise, will return true if a single-line macro with either |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1603 | * `nparam' or no parameters is defined. |
| 1604 | * |
| 1605 | * If a macro with precisely the right number of parameters is |
H. Peter Anvin | ef7468f | 2002-04-30 20:57:59 +0000 | [diff] [blame] | 1606 | * defined, or nparam is -1, the address of the definition structure |
| 1607 | * 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] | 1608 | * is NULL, no action will be taken regarding its contents, and no |
| 1609 | * error will occur. |
| 1610 | * |
| 1611 | * Note that this is also called with nparam zero to resolve |
| 1612 | * `ifdef'. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1613 | * |
| 1614 | * If you already know which context macro belongs to, you can pass |
| 1615 | * the context pointer as first parameter; if you won't but name begins |
| 1616 | * with %$ the context will be automatically computed. If all_contexts |
| 1617 | * is true, macro will be searched in outer contexts as well. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1618 | */ |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1619 | static bool |
H. Peter Anvin | b2a5fda | 2008-06-19 21:42:42 -0700 | [diff] [blame] | 1620 | smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn, |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1621 | bool nocase) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1622 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1623 | struct hash_table *smtbl; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1624 | SMacro *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1625 | |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1626 | if (ctx) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1627 | smtbl = &ctx->localmac; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1628 | } else if (name[0] == '%' && name[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1629 | if (cstk) |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1630 | ctx = get_ctx(name, &name); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1631 | if (!ctx) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1632 | return false; /* got to return _something_ */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1633 | smtbl = &ctx->localmac; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1634 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1635 | smtbl = &smacros; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1636 | } |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1637 | m = (SMacro *) hash_findix(smtbl, name); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1638 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1639 | while (m) { |
| 1640 | if (!mstrcmp(m->name, name, m->casesense && nocase) && |
Charles Crayne | 192d5b5 | 2007-10-18 19:02:42 -0700 | [diff] [blame] | 1641 | (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1642 | if (defn) { |
Charles Crayne | 192d5b5 | 2007-10-18 19:02:42 -0700 | [diff] [blame] | 1643 | if (nparam == (int) m->nparam || nparam == -1) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1644 | *defn = m; |
| 1645 | else |
| 1646 | *defn = NULL; |
| 1647 | } |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1648 | return true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1649 | } |
| 1650 | m = m->next; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1651 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1652 | |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1653 | return false; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1654 | } |
| 1655 | |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 1656 | /* param should be a natural number [0; INT_MAX] */ |
| 1657 | static int read_param_count(const char *str) |
| 1658 | { |
| 1659 | int result; |
| 1660 | bool err; |
| 1661 | |
| 1662 | result = readnum(str, &err); |
| 1663 | if (result < 0 || result > INT_MAX) { |
| 1664 | result = 0; |
| 1665 | nasm_error(ERR_NONFATAL, "parameter count `%s' is out of bounds [%d; %d]", |
| 1666 | str, 0, INT_MAX); |
| 1667 | } else if (err) { |
| 1668 | nasm_error(ERR_NONFATAL, "unable to parse parameter count `%s'", str); |
| 1669 | } |
| 1670 | return result; |
| 1671 | } |
| 1672 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1673 | /* |
| 1674 | * Count and mark off the parameters in a multi-line macro call. |
| 1675 | * This is called both from within the multi-line macro expansion |
| 1676 | * code, and also to mark off the default parameters when provided |
| 1677 | * in a %macro definition line. |
| 1678 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1679 | static void count_mmac_params(Token * t, int *nparam, Token *** params) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1680 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1681 | int paramsize, brace; |
| 1682 | |
| 1683 | *nparam = paramsize = 0; |
| 1684 | *params = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1685 | while (t) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1686 | /* +1: we need space for the final NULL */ |
H. Peter Anvin | 91fb6f1 | 2008-09-01 10:56:33 -0700 | [diff] [blame] | 1687 | if (*nparam+1 >= paramsize) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1688 | paramsize += PARAM_DELTA; |
| 1689 | *params = nasm_realloc(*params, sizeof(**params) * paramsize); |
| 1690 | } |
| 1691 | skip_white_(t); |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1692 | brace = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1693 | if (tok_is_(t, "{")) |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1694 | brace++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1695 | (*params)[(*nparam)++] = t; |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1696 | if (brace) { |
| 1697 | while (brace && (t = t->next) != NULL) { |
| 1698 | if (tok_is_(t, "{")) |
| 1699 | brace++; |
| 1700 | else if (tok_is_(t, "}")) |
| 1701 | brace--; |
| 1702 | } |
| 1703 | |
| 1704 | if (t) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1705 | /* |
| 1706 | * Now we've found the closing brace, look further |
| 1707 | * for the comma. |
| 1708 | */ |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1709 | t = t->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1710 | skip_white_(t); |
| 1711 | if (tok_isnt_(t, ",")) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1712 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1713 | "braces do not enclose all of macro parameter"); |
| 1714 | while (tok_isnt_(t, ",")) |
| 1715 | t = t->next; |
| 1716 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1717 | } |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1718 | } else { |
| 1719 | while (tok_isnt_(t, ",")) |
| 1720 | t = t->next; |
| 1721 | } |
| 1722 | if (t) { /* got a comma/brace */ |
| 1723 | t = t->next; /* eat the comma */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1724 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1725 | } |
| 1726 | } |
| 1727 | |
| 1728 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1729 | * Determine whether one of the various `if' conditions is true or |
| 1730 | * not. |
| 1731 | * |
| 1732 | * We must free the tline we get passed. |
| 1733 | */ |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1734 | static bool if_condition(Token * tline, enum preproc_token ct) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1735 | { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1736 | enum pp_conditional i = PP_COND(ct); |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1737 | bool j; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1738 | Token *t, *tt, **tptr, *origline; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1739 | struct tokenval tokval; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1740 | expr *evalresult; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1741 | enum pp_token_type needtype; |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1742 | char *p; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1743 | |
| 1744 | origline = tline; |
| 1745 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1746 | switch (i) { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1747 | case PPC_IFCTX: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1748 | j = false; /* have we matched yet? */ |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1749 | while (true) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1750 | skip_white_(tline); |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1751 | if (!tline) |
| 1752 | break; |
| 1753 | if (tline->type != TOK_ID) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1754 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1755 | "`%s' expects context identifiers", pp_directives[ct]); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1756 | free_tlist(origline); |
| 1757 | return -1; |
| 1758 | } |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1759 | if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1760 | j = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1761 | tline = tline->next; |
| 1762 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1763 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1764 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1765 | case PPC_IFDEF: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1766 | j = false; /* have we matched yet? */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1767 | while (tline) { |
| 1768 | skip_white_(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1769 | if (!tline || (tline->type != TOK_ID && |
| 1770 | (tline->type != TOK_PREPROC_ID || |
| 1771 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1772 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1773 | "`%s' expects macro identifiers", pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1774 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1775 | } |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1776 | if (smacro_defined(NULL, tline->text, 0, NULL, true)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1777 | j = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1778 | tline = tline->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1779 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1780 | break; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1781 | |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1782 | case PPC_IFENV: |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1783 | tline = expand_smacro(tline); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1784 | j = false; /* have we matched yet? */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1785 | while (tline) { |
| 1786 | skip_white_(tline); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1787 | if (!tline || (tline->type != TOK_ID && |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1788 | tline->type != TOK_STRING && |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1789 | (tline->type != TOK_PREPROC_ID || |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1790 | tline->text[1] != '!'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1791 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1792 | "`%s' expects environment variable names", |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1793 | pp_directives[ct]); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1794 | goto fail; |
| 1795 | } |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1796 | p = tline->text; |
| 1797 | if (tline->type == TOK_PREPROC_ID) |
| 1798 | p += 2; /* Skip leading %! */ |
| 1799 | if (*p == '\'' || *p == '\"' || *p == '`') |
| 1800 | nasm_unquote_cstr(p, ct); |
| 1801 | if (getenv(p)) |
| 1802 | j = true; |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1803 | tline = tline->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1804 | } |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1805 | break; |
| 1806 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1807 | case PPC_IFIDN: |
| 1808 | case PPC_IFIDNI: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1809 | tline = expand_smacro(tline); |
| 1810 | t = tt = tline; |
| 1811 | while (tok_isnt_(tt, ",")) |
| 1812 | tt = tt->next; |
| 1813 | if (!tt) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1814 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1815 | "`%s' expects two comma-separated arguments", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1816 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1817 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1818 | } |
| 1819 | tt = tt->next; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1820 | j = true; /* assume equality unless proved not */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1821 | while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) { |
| 1822 | if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1823 | nasm_error(ERR_NONFATAL, "`%s': more than one comma on line", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1824 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1825 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1826 | } |
| 1827 | if (t->type == TOK_WHITESPACE) { |
| 1828 | t = t->next; |
| 1829 | continue; |
| 1830 | } |
| 1831 | if (tt->type == TOK_WHITESPACE) { |
| 1832 | tt = tt->next; |
| 1833 | continue; |
| 1834 | } |
| 1835 | if (tt->type != t->type) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1836 | j = false; /* found mismatching tokens */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1837 | break; |
| 1838 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1839 | /* When comparing strings, need to unquote them first */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1840 | if (t->type == TOK_STRING) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1841 | size_t l1 = nasm_unquote(t->text, NULL); |
| 1842 | size_t l2 = nasm_unquote(tt->text, NULL); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 1843 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1844 | if (l1 != l2) { |
| 1845 | j = false; |
| 1846 | break; |
| 1847 | } |
| 1848 | if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) { |
| 1849 | j = false; |
| 1850 | break; |
| 1851 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1852 | } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1853 | j = false; /* found mismatching tokens */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1854 | break; |
| 1855 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1856 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1857 | t = t->next; |
| 1858 | tt = tt->next; |
| 1859 | } |
| 1860 | if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1861 | j = false; /* trailing gunk on one end or other */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1862 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1863 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1864 | case PPC_IFMACRO: |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1865 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1866 | bool found = false; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1867 | MMacro searching, *mmac; |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 1868 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1869 | skip_white_(tline); |
| 1870 | tline = expand_id(tline); |
| 1871 | if (!tok_type_(tline, TOK_ID)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1872 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1873 | "`%s' expects a macro name", pp_directives[ct]); |
| 1874 | goto fail; |
| 1875 | } |
| 1876 | searching.name = nasm_strdup(tline->text); |
| 1877 | searching.casesense = true; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1878 | searching.plus = false; |
| 1879 | searching.nolist = false; |
| 1880 | searching.in_progress = 0; |
| 1881 | searching.max_depth = 0; |
| 1882 | searching.rep_nest = NULL; |
| 1883 | searching.nparam_min = 0; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1884 | searching.nparam_max = INT_MAX; |
| 1885 | tline = expand_smacro(tline->next); |
| 1886 | skip_white_(tline); |
| 1887 | if (!tline) { |
| 1888 | } else if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1889 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1890 | "`%s' expects a parameter count or nothing", |
| 1891 | pp_directives[ct]); |
| 1892 | } else { |
| 1893 | searching.nparam_min = searching.nparam_max = |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 1894 | read_param_count(tline->text); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1895 | } |
| 1896 | if (tline && tok_is_(tline->next, "-")) { |
| 1897 | tline = tline->next->next; |
| 1898 | if (tok_is_(tline, "*")) |
| 1899 | searching.nparam_max = INT_MAX; |
| 1900 | else if (!tok_type_(tline, TOK_NUMBER)) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1901 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1902 | "`%s' expects a parameter count after `-'", |
| 1903 | pp_directives[ct]); |
| 1904 | else { |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 1905 | searching.nparam_max = read_param_count(tline->text); |
Cyrill Gorcunov | c9244ea | 2017-10-22 15:25:48 +0300 | [diff] [blame] | 1906 | if (searching.nparam_min > searching.nparam_max) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1907 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1908 | "minimum parameter count exceeds maximum"); |
Cyrill Gorcunov | c9244ea | 2017-10-22 15:25:48 +0300 | [diff] [blame] | 1909 | searching.nparam_max = searching.nparam_min; |
| 1910 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1911 | } |
| 1912 | } |
| 1913 | if (tline && tok_is_(tline->next, "+")) { |
| 1914 | tline = tline->next; |
| 1915 | searching.plus = true; |
| 1916 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1917 | mmac = (MMacro *) hash_findix(&mmacros, searching.name); |
| 1918 | while (mmac) { |
| 1919 | if (!strcmp(mmac->name, searching.name) && |
| 1920 | (mmac->nparam_min <= searching.nparam_max |
| 1921 | || searching.plus) |
| 1922 | && (searching.nparam_min <= mmac->nparam_max |
| 1923 | || mmac->plus)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1924 | found = true; |
| 1925 | break; |
| 1926 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1927 | mmac = mmac->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1928 | } |
| 1929 | if (tline && tline->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1930 | nasm_error(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1931 | "trailing garbage after %%ifmacro ignored"); |
| 1932 | nasm_free(searching.name); |
| 1933 | j = found; |
| 1934 | break; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1935 | } |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 1936 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1937 | case PPC_IFID: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1938 | needtype = TOK_ID; |
| 1939 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1940 | case PPC_IFNUM: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1941 | needtype = TOK_NUMBER; |
| 1942 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1943 | case PPC_IFSTR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1944 | needtype = TOK_STRING; |
| 1945 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1946 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1947 | iftype: |
| 1948 | t = tline = expand_smacro(tline); |
H. Peter Anvin | d85d250 | 2008-05-04 17:53:31 -0700 | [diff] [blame] | 1949 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1950 | while (tok_type_(t, TOK_WHITESPACE) || |
| 1951 | (needtype == TOK_NUMBER && |
| 1952 | tok_type_(t, TOK_OTHER) && |
| 1953 | (t->text[0] == '-' || t->text[0] == '+') && |
| 1954 | !t->text[1])) |
| 1955 | t = t->next; |
H. Peter Anvin | d85d250 | 2008-05-04 17:53:31 -0700 | [diff] [blame] | 1956 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1957 | j = tok_type_(t, needtype); |
| 1958 | break; |
H. Peter Anvin | cbf768d | 2008-02-16 16:41:25 -0800 | [diff] [blame] | 1959 | |
| 1960 | case PPC_IFTOKEN: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1961 | t = tline = expand_smacro(tline); |
| 1962 | while (tok_type_(t, TOK_WHITESPACE)) |
| 1963 | t = t->next; |
H. Peter Anvin | cbf768d | 2008-02-16 16:41:25 -0800 | [diff] [blame] | 1964 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1965 | j = false; |
| 1966 | if (t) { |
| 1967 | t = t->next; /* Skip the actual token */ |
| 1968 | while (tok_type_(t, TOK_WHITESPACE)) |
| 1969 | t = t->next; |
| 1970 | j = !t; /* Should be nothing left */ |
| 1971 | } |
| 1972 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1973 | |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 1974 | case PPC_IFEMPTY: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1975 | t = tline = expand_smacro(tline); |
| 1976 | while (tok_type_(t, TOK_WHITESPACE)) |
| 1977 | t = t->next; |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 1978 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1979 | j = !t; /* Should be empty */ |
| 1980 | break; |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 1981 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1982 | case PPC_IF: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1983 | t = tline = expand_smacro(tline); |
| 1984 | tptr = &t; |
| 1985 | tokval.t_type = TOKEN_INVALID; |
| 1986 | evalresult = evaluate(ppscan, tptr, &tokval, |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1987 | NULL, pass | CRITICAL, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1988 | if (!evalresult) |
| 1989 | return -1; |
| 1990 | if (tokval.t_type) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1991 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1992 | "trailing garbage after expression ignored"); |
| 1993 | if (!is_simple(evalresult)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1994 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1995 | "non-constant value given to `%s'", pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1996 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1997 | } |
Chuck Crayne | 60ae75d | 2007-05-02 01:59:16 +0000 | [diff] [blame] | 1998 | j = reloc_value(evalresult) != 0; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1999 | break; |
H. Peter Anvin | 95e2882 | 2007-09-12 04:20:08 +0000 | [diff] [blame] | 2000 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2001 | default: |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2002 | nasm_error(ERR_FATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2003 | "preprocessor directive `%s' not yet implemented", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2004 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2005 | goto fail; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2006 | } |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2007 | |
| 2008 | free_tlist(origline); |
| 2009 | return j ^ PP_NEGATIVE(ct); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2010 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2011 | fail: |
| 2012 | free_tlist(origline); |
| 2013 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2014 | } |
| 2015 | |
| 2016 | /* |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2017 | * Common code for defining an smacro |
| 2018 | */ |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 2019 | static bool define_smacro(Context *ctx, const char *mname, bool casesense, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2020 | int nparam, Token *expansion) |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2021 | { |
| 2022 | SMacro *smac, **smhead; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2023 | struct hash_table *smtbl; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2024 | |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2025 | if (smacro_defined(ctx, mname, nparam, &smac, casesense)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2026 | if (!smac) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2027 | nasm_error(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2028 | "single-line macro `%s' defined both with and" |
| 2029 | " without parameters", mname); |
| 2030 | /* |
| 2031 | * Some instances of the old code considered this a failure, |
| 2032 | * some others didn't. What is the right thing to do here? |
| 2033 | */ |
| 2034 | free_tlist(expansion); |
| 2035 | return false; /* Failure */ |
| 2036 | } else { |
| 2037 | /* |
| 2038 | * We're redefining, so we have to take over an |
| 2039 | * existing SMacro structure. This means freeing |
| 2040 | * what was already in it. |
| 2041 | */ |
| 2042 | nasm_free(smac->name); |
| 2043 | free_tlist(smac->expansion); |
| 2044 | } |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2045 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2046 | smtbl = ctx ? &ctx->localmac : &smacros; |
| 2047 | smhead = (SMacro **) hash_findi_add(smtbl, mname); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2048 | smac = nasm_malloc(sizeof(SMacro)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2049 | smac->next = *smhead; |
| 2050 | *smhead = smac; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2051 | } |
| 2052 | smac->name = nasm_strdup(mname); |
| 2053 | smac->casesense = casesense; |
| 2054 | smac->nparam = nparam; |
| 2055 | smac->expansion = expansion; |
| 2056 | smac->in_progress = false; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2057 | return true; /* Success */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2058 | } |
| 2059 | |
| 2060 | /* |
| 2061 | * Undefine an smacro |
| 2062 | */ |
| 2063 | static void undef_smacro(Context *ctx, const char *mname) |
| 2064 | { |
| 2065 | SMacro **smhead, *s, **sp; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2066 | struct hash_table *smtbl; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2067 | |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2068 | smtbl = ctx ? &ctx->localmac : &smacros; |
| 2069 | smhead = (SMacro **)hash_findi(smtbl, mname, NULL); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2070 | |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2071 | if (smhead) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2072 | /* |
| 2073 | * We now have a macro name... go hunt for it. |
| 2074 | */ |
| 2075 | sp = smhead; |
| 2076 | while ((s = *sp) != NULL) { |
| 2077 | if (!mstrcmp(s->name, mname, s->casesense)) { |
| 2078 | *sp = s->next; |
| 2079 | nasm_free(s->name); |
| 2080 | free_tlist(s->expansion); |
| 2081 | nasm_free(s); |
| 2082 | } else { |
| 2083 | sp = &s->next; |
| 2084 | } |
| 2085 | } |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2086 | } |
| 2087 | } |
| 2088 | |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2089 | /* |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2090 | * Parse a mmacro specification. |
| 2091 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2092 | 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] | 2093 | { |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2094 | tline = tline->next; |
| 2095 | skip_white_(tline); |
| 2096 | tline = expand_id(tline); |
| 2097 | if (!tok_type_(tline, TOK_ID)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2098 | nasm_error(ERR_NONFATAL, "`%s' expects a macro name", directive); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2099 | return false; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2100 | } |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2101 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2102 | def->prev = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2103 | def->name = nasm_strdup(tline->text); |
| 2104 | def->plus = false; |
| 2105 | def->nolist = false; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2106 | def->in_progress = 0; |
| 2107 | def->rep_nest = NULL; |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2108 | def->nparam_min = 0; |
| 2109 | def->nparam_max = 0; |
| 2110 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2111 | tline = expand_smacro(tline->next); |
| 2112 | skip_white_(tline); |
| 2113 | if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2114 | nasm_error(ERR_NONFATAL, "`%s' expects a parameter count", directive); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2115 | } else { |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 2116 | def->nparam_min = def->nparam_max = read_param_count(tline->text); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2117 | } |
| 2118 | if (tline && tok_is_(tline->next, "-")) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2119 | tline = tline->next->next; |
| 2120 | if (tok_is_(tline, "*")) { |
| 2121 | def->nparam_max = INT_MAX; |
| 2122 | } else if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2123 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2124 | "`%s' expects a parameter count after `-'", directive); |
| 2125 | } else { |
Cyrill Gorcunov | 3079f79 | 2018-11-14 10:03:42 +0300 | [diff] [blame] | 2126 | def->nparam_max = read_param_count(tline->text); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2127 | if (def->nparam_min > def->nparam_max) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2128 | nasm_error(ERR_NONFATAL, "minimum parameter count exceeds maximum"); |
Cyrill Gorcunov | c9244ea | 2017-10-22 15:25:48 +0300 | [diff] [blame] | 2129 | def->nparam_max = def->nparam_min; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2130 | } |
| 2131 | } |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2132 | } |
| 2133 | if (tline && tok_is_(tline->next, "+")) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2134 | tline = tline->next; |
| 2135 | def->plus = true; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2136 | } |
| 2137 | if (tline && tok_type_(tline->next, TOK_ID) && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2138 | !nasm_stricmp(tline->next->text, ".nolist")) { |
| 2139 | tline = tline->next; |
| 2140 | def->nolist = true; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2141 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2142 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2143 | /* |
| 2144 | * Handle default parameters. |
| 2145 | */ |
| 2146 | if (tline && tline->next) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2147 | def->dlist = tline->next; |
| 2148 | tline->next = NULL; |
| 2149 | count_mmac_params(def->dlist, &def->ndefs, &def->defaults); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2150 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2151 | def->dlist = NULL; |
| 2152 | def->defaults = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2153 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2154 | def->expansion = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2155 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2156 | if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2157 | !def->plus) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2158 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2159 | "too many default macro parameters"); |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2160 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2161 | return true; |
| 2162 | } |
| 2163 | |
| 2164 | |
| 2165 | /* |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2166 | * Decode a size directive |
| 2167 | */ |
| 2168 | static int parse_size(const char *str) { |
| 2169 | static const char *size_names[] = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2170 | { "byte", "dword", "oword", "qword", "tword", "word", "yword" }; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2171 | static const int sizes[] = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2172 | { 0, 1, 4, 16, 8, 10, 2, 32 }; |
Cyrill Gorcunov | c713b5f | 2018-09-29 14:30:14 +0300 | [diff] [blame] | 2173 | 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] | 2174 | } |
| 2175 | |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2176 | /* |
| 2177 | * Process a preprocessor %pragma directive. Currently there are none. |
| 2178 | * Gets passed the token list starting with the "preproc" token from |
| 2179 | * "%pragma preproc". |
| 2180 | */ |
| 2181 | static void do_pragma_preproc(Token *tline) |
| 2182 | { |
| 2183 | /* Skip to the real stuff */ |
| 2184 | tline = tline->next; |
| 2185 | skip_white_(tline); |
| 2186 | if (!tline) |
| 2187 | return; |
| 2188 | |
| 2189 | (void)tline; /* Nothing else to do at present */ |
| 2190 | } |
| 2191 | |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2192 | /** |
| 2193 | * find and process preprocessor directive in passed line |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2194 | * Find out if a line contains a preprocessor directive, and deal |
| 2195 | * with it if so. |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2196 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2197 | * If a directive _is_ found, it is the responsibility of this routine |
| 2198 | * (and not the caller) to free_tlist() the line. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2199 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2200 | * @param tline a pointer to the current tokeninzed line linked list |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2201 | * @param output if this directive generated output |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2202 | * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2203 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2204 | */ |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2205 | static int do_directive(Token *tline, char **output) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2206 | { |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2207 | enum preproc_token i; |
| 2208 | int j; |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 2209 | bool err; |
| 2210 | int nparam; |
| 2211 | bool nolist; |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 2212 | bool casesense; |
H. Peter Anvin | 8cfdb9d | 2007-09-14 18:36:01 -0700 | [diff] [blame] | 2213 | int k, m; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2214 | int offset; |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 2215 | char *p, *pp; |
| 2216 | const char *found_path; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 2217 | const char *mname; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2218 | Include *inc; |
| 2219 | Context *ctx; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2220 | Cond *cond; |
| 2221 | MMacro *mmac, **mmhead; |
Jin Kyu Song | c9486b9 | 2013-10-28 17:07:57 -0700 | [diff] [blame] | 2222 | Token *t = NULL, *tt, *param_start, *macro_start, *last, **tptr, *origline; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2223 | Line *l; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2224 | struct tokenval tokval; |
| 2225 | expr *evalresult; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2226 | MMacro *tmp_defining; /* Used when manipulating rep_nest */ |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 2227 | int64_t count; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 2228 | size_t len; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2229 | int severity; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2230 | |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2231 | *output = NULL; /* No output generated */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2232 | origline = tline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2233 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2234 | skip_white_(tline); |
H. Peter Anvin | f2936d7 | 2008-06-04 15:11:23 -0700 | [diff] [blame] | 2235 | if (!tline || !tok_type_(tline, TOK_PREPROC_ID) || |
Cyrill Gorcunov | 4b5b737 | 2018-10-29 22:54:08 +0300 | [diff] [blame] | 2236 | (tline->text[0] && (tline->text[1] == '%' || |
| 2237 | tline->text[1] == '$' || |
| 2238 | tline->text[1] == '!'))) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2239 | return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2240 | |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2241 | i = pp_token_hash(tline->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2242 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2243 | /* |
| 2244 | * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO |
| 2245 | * since they are known to be buggy at moment, we need to fix them |
| 2246 | * in future release (2.09-2.10) |
| 2247 | */ |
Philipp Kloke | b432f57 | 2013-03-31 12:01:23 +0200 | [diff] [blame] | 2248 | if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2249 | nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'", |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2250 | tline->text); |
| 2251 | return NO_DIRECTIVE_FOUND; |
| 2252 | } |
| 2253 | |
| 2254 | /* |
| 2255 | * If we're in a non-emitting branch of a condition construct, |
| 2256 | * or walking to the end of an already terminated %rep block, |
| 2257 | * we should ignore all directives except for condition |
| 2258 | * directives. |
| 2259 | */ |
| 2260 | if (((istk->conds && !emitting(istk->conds->state)) || |
| 2261 | (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) { |
| 2262 | return NO_DIRECTIVE_FOUND; |
| 2263 | } |
| 2264 | |
| 2265 | /* |
| 2266 | * If we're defining a macro or reading a %rep block, we should |
| 2267 | * ignore all directives except for %macro/%imacro (which nest), |
| 2268 | * %endm/%endmacro, and (only if we're in a %rep block) %endrep. |
| 2269 | * If we're in a %rep block, another %rep nests, so should be let through. |
| 2270 | */ |
| 2271 | if (defining && i != PP_MACRO && i != PP_IMACRO && |
| 2272 | i != PP_RMACRO && i != PP_IRMACRO && |
| 2273 | i != PP_ENDMACRO && i != PP_ENDM && |
| 2274 | (defining->name || (i != PP_ENDREP && i != PP_REP))) { |
| 2275 | return NO_DIRECTIVE_FOUND; |
| 2276 | } |
| 2277 | |
| 2278 | if (defining) { |
| 2279 | if (i == PP_MACRO || i == PP_IMACRO || |
| 2280 | i == PP_RMACRO || i == PP_IRMACRO) { |
| 2281 | nested_mac_count++; |
| 2282 | return NO_DIRECTIVE_FOUND; |
| 2283 | } else if (nested_mac_count > 0) { |
| 2284 | if (i == PP_ENDMACRO) { |
| 2285 | nested_mac_count--; |
| 2286 | return NO_DIRECTIVE_FOUND; |
| 2287 | } |
| 2288 | } |
| 2289 | if (!defining->name) { |
| 2290 | if (i == PP_REP) { |
| 2291 | nested_rep_count++; |
| 2292 | return NO_DIRECTIVE_FOUND; |
| 2293 | } else if (nested_rep_count > 0) { |
| 2294 | if (i == PP_ENDREP) { |
| 2295 | nested_rep_count--; |
| 2296 | return NO_DIRECTIVE_FOUND; |
| 2297 | } |
| 2298 | } |
| 2299 | } |
| 2300 | } |
| 2301 | |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2302 | switch (i) { |
| 2303 | case PP_INVALID: |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2304 | nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'", |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2305 | tline->text); |
| 2306 | return NO_DIRECTIVE_FOUND; /* didn't get it */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2307 | |
H. Peter Anvin | 3f87a2a | 2016-10-04 14:07:19 -0700 | [diff] [blame] | 2308 | case PP_PRAGMA: |
| 2309 | /* |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2310 | * %pragma namespace options... |
| 2311 | * |
| 2312 | * The namespace "preproc" is reserved for the preprocessor; |
| 2313 | * all other namespaces generate a [pragma] assembly directive. |
| 2314 | * |
| 2315 | * Invalid %pragmas are ignored and may have different |
| 2316 | * meaning in future versions of NASM. |
H. Peter Anvin | 3f87a2a | 2016-10-04 14:07:19 -0700 | [diff] [blame] | 2317 | */ |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2318 | tline = tline->next; |
| 2319 | skip_white_(tline); |
| 2320 | tline = expand_smacro(tline); |
| 2321 | if (tok_type_(tline, TOK_ID)) { |
| 2322 | if (!nasm_stricmp(tline->text, "preproc")) { |
| 2323 | /* Preprocessor pragma */ |
| 2324 | do_pragma_preproc(tline); |
| 2325 | } else { |
| 2326 | /* Build the assembler directive */ |
| 2327 | t = new_Token(NULL, TOK_OTHER, "[", 1); |
| 2328 | t->next = new_Token(NULL, TOK_ID, "pragma", 6); |
| 2329 | t->next->next = new_Token(tline, TOK_WHITESPACE, NULL, 0); |
| 2330 | tline = t; |
| 2331 | for (t = tline; t->next; t = t->next) |
| 2332 | ; |
| 2333 | t->next = new_Token(NULL, TOK_OTHER, "]", 1); |
| 2334 | /* true here can be revisited in the future */ |
| 2335 | *output = detoken(tline, true); |
| 2336 | } |
| 2337 | } |
H. Peter Anvin | 3f87a2a | 2016-10-04 14:07:19 -0700 | [diff] [blame] | 2338 | free_tlist(origline); |
| 2339 | return DIRECTIVE_FOUND; |
| 2340 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2341 | case PP_STACKSIZE: |
| 2342 | /* Directive to tell NASM what the default stack size is. The |
| 2343 | * default is for a 16-bit stack, and this can be overriden with |
| 2344 | * %stacksize large. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2345 | */ |
| 2346 | tline = tline->next; |
| 2347 | if (tline && tline->type == TOK_WHITESPACE) |
| 2348 | tline = tline->next; |
| 2349 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2350 | nasm_error(ERR_NONFATAL, "`%%stacksize' missing size parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2351 | free_tlist(origline); |
| 2352 | return DIRECTIVE_FOUND; |
| 2353 | } |
| 2354 | if (nasm_stricmp(tline->text, "flat") == 0) { |
| 2355 | /* All subsequent ARG directives are for a 32-bit stack */ |
| 2356 | StackSize = 4; |
| 2357 | StackPointer = "ebp"; |
| 2358 | ArgOffset = 8; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2359 | LocalOffset = 0; |
Charles Crayne | 7eaf919 | 2007-11-08 22:11:14 -0800 | [diff] [blame] | 2360 | } else if (nasm_stricmp(tline->text, "flat64") == 0) { |
| 2361 | /* All subsequent ARG directives are for a 64-bit stack */ |
| 2362 | StackSize = 8; |
| 2363 | StackPointer = "rbp"; |
Per Jessen | 53252e0 | 2010-02-11 00:16:59 +0300 | [diff] [blame] | 2364 | ArgOffset = 16; |
Charles Crayne | 7eaf919 | 2007-11-08 22:11:14 -0800 | [diff] [blame] | 2365 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2366 | } else if (nasm_stricmp(tline->text, "large") == 0) { |
| 2367 | /* All subsequent ARG directives are for a 16-bit stack, |
| 2368 | * far function call. |
| 2369 | */ |
| 2370 | StackSize = 2; |
| 2371 | StackPointer = "bp"; |
| 2372 | ArgOffset = 4; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2373 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2374 | } else if (nasm_stricmp(tline->text, "small") == 0) { |
| 2375 | /* All subsequent ARG directives are for a 16-bit stack, |
| 2376 | * far function call. We don't support near functions. |
| 2377 | */ |
| 2378 | StackSize = 2; |
| 2379 | StackPointer = "bp"; |
| 2380 | ArgOffset = 6; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2381 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2382 | } else { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2383 | nasm_error(ERR_NONFATAL, "`%%stacksize' invalid size type"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2384 | free_tlist(origline); |
| 2385 | return DIRECTIVE_FOUND; |
| 2386 | } |
| 2387 | free_tlist(origline); |
| 2388 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2389 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2390 | case PP_ARG: |
| 2391 | /* TASM like ARG directive to define arguments to functions, in |
| 2392 | * the following form: |
| 2393 | * |
| 2394 | * ARG arg1:WORD, arg2:DWORD, arg4:QWORD |
| 2395 | */ |
| 2396 | offset = ArgOffset; |
| 2397 | do { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 2398 | char *arg, directive[256]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2399 | int size = StackSize; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2400 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2401 | /* Find the argument name */ |
| 2402 | tline = tline->next; |
| 2403 | if (tline && tline->type == TOK_WHITESPACE) |
| 2404 | tline = tline->next; |
| 2405 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2406 | nasm_error(ERR_NONFATAL, "`%%arg' missing argument parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2407 | free_tlist(origline); |
| 2408 | return DIRECTIVE_FOUND; |
| 2409 | } |
| 2410 | arg = tline->text; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2411 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2412 | /* Find the argument size type */ |
| 2413 | tline = tline->next; |
| 2414 | if (!tline || tline->type != TOK_OTHER |
| 2415 | || tline->text[0] != ':') { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2416 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2417 | "Syntax error processing `%%arg' directive"); |
| 2418 | free_tlist(origline); |
| 2419 | return DIRECTIVE_FOUND; |
| 2420 | } |
| 2421 | tline = tline->next; |
| 2422 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2423 | nasm_error(ERR_NONFATAL, "`%%arg' missing size type parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2424 | free_tlist(origline); |
| 2425 | return DIRECTIVE_FOUND; |
| 2426 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2427 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2428 | /* Allow macro expansion of type parameter */ |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2429 | tt = tokenize(tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2430 | tt = expand_smacro(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2431 | size = parse_size(tt->text); |
| 2432 | if (!size) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2433 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2434 | "Invalid size type for `%%arg' missing directive"); |
| 2435 | free_tlist(tt); |
| 2436 | free_tlist(origline); |
| 2437 | return DIRECTIVE_FOUND; |
| 2438 | } |
| 2439 | free_tlist(tt); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2440 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2441 | /* Round up to even stack slots */ |
| 2442 | size = ALIGN(size, StackSize); |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2443 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2444 | /* Now define the macro for the argument */ |
| 2445 | snprintf(directive, sizeof(directive), "%%define %s (%s+%d)", |
| 2446 | arg, StackPointer, offset); |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2447 | do_directive(tokenize(directive), output); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2448 | offset += size; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2449 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2450 | /* Move to the next argument in the list */ |
| 2451 | tline = tline->next; |
| 2452 | if (tline && tline->type == TOK_WHITESPACE) |
| 2453 | tline = tline->next; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2454 | } while (tline && tline->type == TOK_OTHER && tline->text[0] == ','); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2455 | ArgOffset = offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2456 | free_tlist(origline); |
| 2457 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2458 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2459 | case PP_LOCAL: |
| 2460 | /* TASM like LOCAL directive to define local variables for a |
| 2461 | * function, in the following form: |
| 2462 | * |
| 2463 | * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize |
| 2464 | * |
| 2465 | * The '= LocalSize' at the end is ignored by NASM, but is |
| 2466 | * required by TASM to define the local parameter size (and used |
| 2467 | * by the TASM macro package). |
| 2468 | */ |
| 2469 | offset = LocalOffset; |
| 2470 | do { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 2471 | char *local, directive[256]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2472 | int size = StackSize; |
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 | /* Find the argument name */ |
| 2475 | tline = tline->next; |
| 2476 | if (tline && tline->type == TOK_WHITESPACE) |
| 2477 | tline = tline->next; |
| 2478 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2479 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2480 | "`%%local' missing argument parameter"); |
| 2481 | free_tlist(origline); |
| 2482 | return DIRECTIVE_FOUND; |
| 2483 | } |
| 2484 | local = tline->text; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2485 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2486 | /* Find the argument size type */ |
| 2487 | tline = tline->next; |
| 2488 | if (!tline || tline->type != TOK_OTHER |
| 2489 | || tline->text[0] != ':') { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2490 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2491 | "Syntax error processing `%%local' directive"); |
| 2492 | free_tlist(origline); |
| 2493 | return DIRECTIVE_FOUND; |
| 2494 | } |
| 2495 | tline = tline->next; |
| 2496 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2497 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2498 | "`%%local' missing size type parameter"); |
| 2499 | free_tlist(origline); |
| 2500 | return DIRECTIVE_FOUND; |
| 2501 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2502 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2503 | /* Allow macro expansion of type parameter */ |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2504 | tt = tokenize(tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2505 | tt = expand_smacro(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2506 | size = parse_size(tt->text); |
| 2507 | if (!size) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2508 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2509 | "Invalid size type for `%%local' missing directive"); |
| 2510 | free_tlist(tt); |
| 2511 | free_tlist(origline); |
| 2512 | return DIRECTIVE_FOUND; |
| 2513 | } |
| 2514 | free_tlist(tt); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2515 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2516 | /* Round up to even stack slots */ |
| 2517 | size = ALIGN(size, StackSize); |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2518 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2519 | offset += size; /* Negative offset, increment before */ |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2520 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2521 | /* Now define the macro for the argument */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2522 | snprintf(directive, sizeof(directive), "%%define %s (%s-%d)", |
| 2523 | local, StackPointer, offset); |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2524 | do_directive(tokenize(directive), output); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2525 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2526 | /* Now define the assign to setup the enter_c macro correctly */ |
| 2527 | snprintf(directive, sizeof(directive), |
| 2528 | "%%assign %%$localsize %%$localsize+%d", size); |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2529 | do_directive(tokenize(directive), output); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2530 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2531 | /* Move to the next argument in the list */ |
| 2532 | tline = tline->next; |
| 2533 | if (tline && tline->type == TOK_WHITESPACE) |
| 2534 | tline = tline->next; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2535 | } while (tline && tline->type == TOK_OTHER && tline->text[0] == ','); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2536 | LocalOffset = offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2537 | free_tlist(origline); |
| 2538 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2539 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2540 | case PP_CLEAR: |
| 2541 | if (tline->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2542 | nasm_error(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2543 | "trailing garbage after `%%clear' ignored"); |
| 2544 | free_macros(); |
| 2545 | init_macros(); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2546 | free_tlist(origline); |
| 2547 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2548 | |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2549 | case PP_DEPEND: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2550 | t = tline->next = expand_smacro(tline->next); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2551 | skip_white_(t); |
| 2552 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2553 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2554 | nasm_error(ERR_NONFATAL, "`%%depend' expects a file name"); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2555 | free_tlist(origline); |
| 2556 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2557 | } |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2558 | if (t->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2559 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2560 | "trailing garbage after `%%depend' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2561 | p = t->text; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2562 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2563 | nasm_unquote_cstr(p, i); |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 2564 | strlist_add(deplist, p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2565 | free_tlist(origline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2566 | return DIRECTIVE_FOUND; |
| 2567 | |
| 2568 | case PP_INCLUDE: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2569 | t = tline->next = expand_smacro(tline->next); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2570 | skip_white_(t); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2571 | |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2572 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2573 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2574 | nasm_error(ERR_NONFATAL, "`%%include' expects a file name"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2575 | free_tlist(origline); |
| 2576 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2577 | } |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2578 | if (t->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2579 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2580 | "trailing garbage after `%%include' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2581 | p = t->text; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2582 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2583 | nasm_unquote_cstr(p, i); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2584 | inc = nasm_malloc(sizeof(Include)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2585 | inc->next = istk; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2586 | inc->conds = NULL; |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 2587 | found_path = NULL; |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 2588 | inc->fp = inc_fopen(p, deplist, &found_path, |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 2589 | pass == 0 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2590 | if (!inc->fp) { |
| 2591 | /* -MG given but file not found */ |
| 2592 | nasm_free(inc); |
| 2593 | } else { |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 2594 | inc->fname = src_set_fname(found_path ? found_path : p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2595 | inc->lineno = src_set_linnum(0); |
| 2596 | inc->lineinc = 1; |
| 2597 | inc->expansion = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2598 | inc->mstk = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2599 | istk = inc; |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 2600 | lfmt->uplevel(LIST_INCLUDE); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2601 | } |
| 2602 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2603 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2604 | |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2605 | case PP_USE: |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2606 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2607 | static macros_t *use_pkg; |
| 2608 | const char *pkg_macro = NULL; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2609 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2610 | tline = tline->next; |
| 2611 | skip_white_(tline); |
| 2612 | tline = expand_id(tline); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2613 | |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2614 | if (!tline || (tline->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2615 | tline->type != TOK_INTERNAL_STRING && |
| 2616 | tline->type != TOK_ID)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2617 | nasm_error(ERR_NONFATAL, "`%%use' expects a package name"); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2618 | free_tlist(origline); |
| 2619 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2620 | } |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2621 | if (tline->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2622 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2623 | "trailing garbage after `%%use' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2624 | if (tline->type == TOK_STRING) |
| 2625 | nasm_unquote_cstr(tline->text, i); |
| 2626 | use_pkg = nasm_stdmac_find_package(tline->text); |
| 2627 | if (!use_pkg) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2628 | nasm_error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2629 | else |
| 2630 | pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */ |
Victor van den Elzen | 35eb2ea | 2010-03-10 22:33:48 +0100 | [diff] [blame] | 2631 | if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2632 | /* Not already included, go ahead and include it */ |
| 2633 | stdmacpos = use_pkg; |
| 2634 | } |
| 2635 | free_tlist(origline); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2636 | return DIRECTIVE_FOUND; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2637 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2638 | case PP_PUSH: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2639 | case PP_REPL: |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2640 | case PP_POP: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2641 | tline = tline->next; |
| 2642 | skip_white_(tline); |
| 2643 | tline = expand_id(tline); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2644 | if (tline) { |
| 2645 | if (!tok_type_(tline, TOK_ID)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2646 | nasm_error(ERR_NONFATAL, "`%s' expects a context identifier", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2647 | pp_directives[i]); |
| 2648 | free_tlist(origline); |
| 2649 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2650 | } |
| 2651 | if (tline->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2652 | nasm_error(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2653 | "trailing garbage after `%s' ignored", |
| 2654 | pp_directives[i]); |
| 2655 | p = nasm_strdup(tline->text); |
| 2656 | } else { |
| 2657 | p = NULL; /* Anonymous */ |
| 2658 | } |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2659 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2660 | if (i == PP_PUSH) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2661 | ctx = nasm_malloc(sizeof(Context)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2662 | ctx->next = cstk; |
| 2663 | hash_init(&ctx->localmac, HASH_SMALL); |
| 2664 | ctx->name = p; |
| 2665 | ctx->number = unique++; |
| 2666 | cstk = ctx; |
| 2667 | } else { |
| 2668 | /* %pop or %repl */ |
| 2669 | if (!cstk) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2670 | nasm_error(ERR_NONFATAL, "`%s': context stack is empty", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2671 | pp_directives[i]); |
| 2672 | } else if (i == PP_POP) { |
| 2673 | if (p && (!cstk->name || nasm_stricmp(p, cstk->name))) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2674 | nasm_error(ERR_NONFATAL, "`%%pop' in wrong context: %s, " |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2675 | "expected %s", |
| 2676 | cstk->name ? cstk->name : "anonymous", p); |
| 2677 | else |
| 2678 | ctx_pop(); |
| 2679 | } else { |
| 2680 | /* i == PP_REPL */ |
| 2681 | nasm_free(cstk->name); |
| 2682 | cstk->name = p; |
| 2683 | p = NULL; |
| 2684 | } |
| 2685 | nasm_free(p); |
| 2686 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2687 | free_tlist(origline); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2688 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2689 | case PP_FATAL: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2690 | severity = ERR_FATAL; |
| 2691 | goto issue_error; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2692 | case PP_ERROR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2693 | severity = ERR_NONFATAL; |
| 2694 | goto issue_error; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2695 | case PP_WARNING: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2696 | severity = ERR_WARNING|ERR_WARN_USER; |
| 2697 | goto issue_error; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2698 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2699 | issue_error: |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2700 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2701 | /* Only error out if this is the final pass */ |
| 2702 | if (pass != 2 && i != PP_FATAL) |
| 2703 | return DIRECTIVE_FOUND; |
| 2704 | |
| 2705 | tline->next = expand_smacro(tline->next); |
| 2706 | tline = tline->next; |
| 2707 | skip_white_(tline); |
| 2708 | t = tline ? tline->next : NULL; |
| 2709 | skip_white_(t); |
| 2710 | if (tok_type_(tline, TOK_STRING) && !t) { |
| 2711 | /* The line contains only a quoted string */ |
| 2712 | p = tline->text; |
| 2713 | nasm_unquote(p, NULL); /* Ignore NUL character truncation */ |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2714 | nasm_error(severity, "%s", p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2715 | } else { |
| 2716 | /* Not a quoted string, or more than a quoted string */ |
| 2717 | p = detoken(tline, false); |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2718 | nasm_error(severity, "%s", p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2719 | nasm_free(p); |
| 2720 | } |
| 2721 | free_tlist(origline); |
| 2722 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2723 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2724 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2725 | CASE_PP_IF: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2726 | if (istk->conds && !emitting(istk->conds->state)) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2727 | j = COND_NEVER; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2728 | else { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2729 | j = if_condition(tline->next, i); |
| 2730 | tline->next = NULL; /* it got freed */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2731 | j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2732 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2733 | cond = nasm_malloc(sizeof(Cond)); |
| 2734 | cond->next = istk->conds; |
| 2735 | cond->state = j; |
| 2736 | istk->conds = cond; |
| 2737 | if(istk->mstk) |
| 2738 | istk->mstk->condcnt ++; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2739 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2740 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2741 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2742 | CASE_PP_ELIF: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2743 | if (!istk->conds) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2744 | nasm_error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2745 | switch(istk->conds->state) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2746 | case COND_IF_TRUE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2747 | istk->conds->state = COND_DONE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2748 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2749 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2750 | case COND_DONE: |
| 2751 | case COND_NEVER: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2752 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2753 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2754 | case COND_ELSE_TRUE: |
| 2755 | case COND_ELSE_FALSE: |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2756 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND, |
| 2757 | "`%%elif' after `%%else' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2758 | istk->conds->state = COND_NEVER; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +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_IF_FALSE: |
| 2762 | /* |
| 2763 | * IMPORTANT: In the case of %if, we will already have |
| 2764 | * called expand_mmac_params(); however, if we're |
| 2765 | * processing an %elif we must have been in a |
| 2766 | * non-emitting mode, which would have inhibited |
| 2767 | * the normal invocation of expand_mmac_params(). |
| 2768 | * Therefore, we have to do it explicitly here. |
| 2769 | */ |
| 2770 | j = if_condition(expand_mmac_params(tline->next), i); |
| 2771 | tline->next = NULL; /* it got freed */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2772 | istk->conds->state = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2773 | j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; |
| 2774 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2775 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2776 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2777 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2778 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2779 | case PP_ELSE: |
| 2780 | if (tline->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2781 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND, |
| 2782 | "trailing garbage after `%%else' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2783 | if (!istk->conds) |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 2784 | nasm_fatal("`%%else: no matching `%%if'"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2785 | switch(istk->conds->state) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2786 | case COND_IF_TRUE: |
| 2787 | case COND_DONE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2788 | istk->conds->state = COND_ELSE_FALSE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2789 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2790 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2791 | case COND_NEVER: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2792 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2793 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2794 | case COND_IF_FALSE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2795 | istk->conds->state = COND_ELSE_TRUE; |
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 | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2798 | case COND_ELSE_TRUE: |
| 2799 | case COND_ELSE_FALSE: |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2800 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2801 | "`%%else' after `%%else' ignored."); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2802 | istk->conds->state = COND_NEVER; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2803 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2804 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2805 | free_tlist(origline); |
| 2806 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2807 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2808 | case PP_ENDIF: |
| 2809 | if (tline->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2810 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND, |
| 2811 | "trailing garbage after `%%endif' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2812 | if (!istk->conds) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2813 | nasm_error(ERR_FATAL, "`%%endif': no matching `%%if'"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2814 | cond = istk->conds; |
| 2815 | istk->conds = cond->next; |
| 2816 | nasm_free(cond); |
| 2817 | if(istk->mstk) |
| 2818 | istk->mstk->condcnt --; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2819 | free_tlist(origline); |
| 2820 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2821 | |
H. Peter Anvin | db8f96e | 2009-07-15 09:07:29 -0400 | [diff] [blame] | 2822 | case PP_RMACRO: |
| 2823 | case PP_IRMACRO: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2824 | case PP_MACRO: |
| 2825 | case PP_IMACRO: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2826 | if (defining) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2827 | nasm_error(ERR_FATAL, "`%s': already defining a macro", |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2828 | pp_directives[i]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2829 | return DIRECTIVE_FOUND; |
| 2830 | } |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 2831 | defining = nasm_zalloc(sizeof(MMacro)); |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 2832 | defining->max_depth = ((i == PP_RMACRO) || (i == PP_IRMACRO)) |
| 2833 | ? nasm_limit[LIMIT_MACROS] : 0; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2834 | defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO); |
| 2835 | if (!parse_mmacro_spec(tline, defining, pp_directives[i])) { |
| 2836 | nasm_free(defining); |
| 2837 | defining = NULL; |
| 2838 | return DIRECTIVE_FOUND; |
| 2839 | } |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2840 | |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 2841 | src_get(&defining->xline, &defining->fname); |
| 2842 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2843 | mmac = (MMacro *) hash_findix(&mmacros, defining->name); |
| 2844 | while (mmac) { |
| 2845 | if (!strcmp(mmac->name, defining->name) && |
| 2846 | (mmac->nparam_min <= defining->nparam_max |
| 2847 | || defining->plus) |
| 2848 | && (defining->nparam_min <= mmac->nparam_max |
| 2849 | || mmac->plus)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2850 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2851 | "redefining multi-line macro `%s'", defining->name); |
| 2852 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2853 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2854 | mmac = mmac->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2855 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2856 | free_tlist(origline); |
| 2857 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2858 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2859 | case PP_ENDM: |
| 2860 | case PP_ENDMACRO: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2861 | if (! (defining && defining->name)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2862 | nasm_error(ERR_NONFATAL, "`%s': not defining a macro", tline->text); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2863 | return DIRECTIVE_FOUND; |
| 2864 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2865 | mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name); |
| 2866 | defining->next = *mmhead; |
| 2867 | *mmhead = defining; |
| 2868 | defining = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2869 | free_tlist(origline); |
| 2870 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2871 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2872 | case PP_EXITMACRO: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2873 | /* |
| 2874 | * We must search along istk->expansion until we hit a |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2875 | * macro-end marker for a macro with a name. Then we |
| 2876 | * bypass all lines between exitmacro and endmacro. |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2877 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2878 | list_for_each(l, istk->expansion) |
| 2879 | if (l->finishes && l->finishes->name) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2880 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2881 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2882 | if (l) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2883 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2884 | * Remove all conditional entries relative to this |
| 2885 | * macro invocation. (safe to do in this context) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2886 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2887 | for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) { |
| 2888 | cond = istk->conds; |
| 2889 | istk->conds = cond->next; |
| 2890 | nasm_free(cond); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2891 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2892 | istk->expansion = l; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2893 | } else { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2894 | nasm_error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2895 | } |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 2896 | free_tlist(origline); |
| 2897 | return DIRECTIVE_FOUND; |
| 2898 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2899 | case PP_UNMACRO: |
| 2900 | case PP_UNIMACRO: |
| 2901 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2902 | MMacro **mmac_p; |
| 2903 | MMacro spec; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2904 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2905 | spec.casesense = (i == PP_UNMACRO); |
| 2906 | if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) { |
| 2907 | return DIRECTIVE_FOUND; |
| 2908 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2909 | mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL); |
| 2910 | while (mmac_p && *mmac_p) { |
| 2911 | mmac = *mmac_p; |
| 2912 | if (mmac->casesense == spec.casesense && |
| 2913 | !mstrcmp(mmac->name, spec.name, spec.casesense) && |
| 2914 | mmac->nparam_min == spec.nparam_min && |
| 2915 | mmac->nparam_max == spec.nparam_max && |
| 2916 | mmac->plus == spec.plus) { |
| 2917 | *mmac_p = mmac->next; |
| 2918 | free_mmacro(mmac); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2919 | } else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2920 | mmac_p = &mmac->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2921 | } |
| 2922 | } |
| 2923 | free_tlist(origline); |
| 2924 | free_tlist(spec.dlist); |
| 2925 | return DIRECTIVE_FOUND; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2926 | } |
| 2927 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2928 | case PP_ROTATE: |
| 2929 | if (tline->next && tline->next->type == TOK_WHITESPACE) |
| 2930 | tline = tline->next; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2931 | if (!tline->next) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2932 | free_tlist(origline); |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2933 | nasm_error(ERR_NONFATAL, "`%%rotate' missing rotate count"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2934 | return DIRECTIVE_FOUND; |
| 2935 | } |
| 2936 | t = expand_smacro(tline->next); |
| 2937 | tline->next = NULL; |
| 2938 | free_tlist(origline); |
| 2939 | tline = t; |
| 2940 | tptr = &t; |
| 2941 | tokval.t_type = TOKEN_INVALID; |
| 2942 | evalresult = |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2943 | evaluate(ppscan, tptr, &tokval, NULL, pass, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2944 | free_tlist(tline); |
| 2945 | if (!evalresult) |
| 2946 | return DIRECTIVE_FOUND; |
| 2947 | if (tokval.t_type) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2948 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2949 | "trailing garbage after expression ignored"); |
| 2950 | if (!is_simple(evalresult)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2951 | nasm_error(ERR_NONFATAL, "non-constant value given to `%%rotate'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2952 | return DIRECTIVE_FOUND; |
| 2953 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2954 | mmac = istk->mstk; |
| 2955 | while (mmac && !mmac->name) /* avoid mistaking %reps for macros */ |
| 2956 | mmac = mmac->next_active; |
| 2957 | if (!mmac) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2958 | nasm_error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2959 | } else if (mmac->nparam == 0) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2960 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2961 | "`%%rotate' invoked within macro without parameters"); |
| 2962 | } else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2963 | int rotate = mmac->rotate + reloc_value(evalresult); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2964 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2965 | rotate %= (int)mmac->nparam; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2966 | if (rotate < 0) |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2967 | rotate += mmac->nparam; |
| 2968 | |
| 2969 | mmac->rotate = rotate; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2970 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2971 | return DIRECTIVE_FOUND; |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 2972 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2973 | case PP_REP: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 2974 | nolist = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2975 | do { |
| 2976 | tline = tline->next; |
| 2977 | } while (tok_type_(tline, TOK_WHITESPACE)); |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 2978 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2979 | if (tok_type_(tline, TOK_ID) && |
| 2980 | nasm_stricmp(tline->text, ".nolist") == 0) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 2981 | nolist = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2982 | do { |
| 2983 | tline = tline->next; |
| 2984 | } while (tok_type_(tline, TOK_WHITESPACE)); |
| 2985 | } |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 2986 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2987 | if (tline) { |
| 2988 | t = expand_smacro(tline); |
| 2989 | tptr = &t; |
| 2990 | tokval.t_type = TOKEN_INVALID; |
| 2991 | evalresult = |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2992 | evaluate(ppscan, tptr, &tokval, NULL, pass, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2993 | if (!evalresult) { |
| 2994 | free_tlist(origline); |
| 2995 | return DIRECTIVE_FOUND; |
| 2996 | } |
| 2997 | if (tokval.t_type) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2998 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2999 | "trailing garbage after expression ignored"); |
| 3000 | if (!is_simple(evalresult)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3001 | nasm_error(ERR_NONFATAL, "non-constant value given to `%%rep'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3002 | return DIRECTIVE_FOUND; |
| 3003 | } |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 3004 | count = reloc_value(evalresult); |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 3005 | if (count > nasm_limit[LIMIT_REP]) { |
| 3006 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | a3d96d0 | 2018-06-15 17:51:39 -0700 | [diff] [blame] | 3007 | "`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")", |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 3008 | count, nasm_limit[LIMIT_REP]); |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 3009 | count = 0; |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 3010 | } else if (count < 0) { |
| 3011 | nasm_error(ERR_WARNING|ERR_PASS2|ERR_WARN_NEG_REP, |
| 3012 | "negative `%%rep' count: %"PRId64, count); |
| 3013 | count = 0; |
| 3014 | } else { |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 3015 | count++; |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 3016 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3017 | } else { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3018 | nasm_error(ERR_NONFATAL, "`%%rep' expects a repeat count"); |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 3019 | count = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3020 | } |
| 3021 | free_tlist(origline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3022 | |
| 3023 | tmp_defining = defining; |
| 3024 | defining = nasm_malloc(sizeof(MMacro)); |
| 3025 | defining->prev = NULL; |
| 3026 | defining->name = NULL; /* flags this macro as a %rep block */ |
| 3027 | defining->casesense = false; |
| 3028 | defining->plus = false; |
| 3029 | defining->nolist = nolist; |
| 3030 | defining->in_progress = count; |
| 3031 | defining->max_depth = 0; |
| 3032 | defining->nparam_min = defining->nparam_max = 0; |
| 3033 | defining->defaults = NULL; |
| 3034 | defining->dlist = NULL; |
| 3035 | defining->expansion = NULL; |
| 3036 | defining->next_active = istk->mstk; |
| 3037 | defining->rep_nest = tmp_defining; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3038 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3039 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3040 | case PP_ENDREP: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3041 | if (!defining || defining->name) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3042 | nasm_error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3043 | return DIRECTIVE_FOUND; |
| 3044 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3045 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3046 | /* |
| 3047 | * Now we have a "macro" defined - although it has no name |
| 3048 | * and we won't be entering it in the hash tables - we must |
| 3049 | * push a macro-end marker for it on to istk->expansion. |
| 3050 | * After that, it will take care of propagating itself (a |
| 3051 | * macro-end marker line for a macro which is really a %rep |
| 3052 | * block will cause the macro to be re-expanded, complete |
| 3053 | * with another macro-end marker to ensure the process |
| 3054 | * continues) until the whole expansion is forcibly removed |
| 3055 | * from istk->expansion by a %exitrep. |
| 3056 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3057 | l = nasm_malloc(sizeof(Line)); |
| 3058 | l->next = istk->expansion; |
| 3059 | l->finishes = defining; |
| 3060 | l->first = NULL; |
| 3061 | istk->expansion = l; |
| 3062 | |
| 3063 | istk->mstk = defining; |
| 3064 | |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 3065 | lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3066 | tmp_defining = defining; |
| 3067 | defining = defining->rep_nest; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3068 | free_tlist(origline); |
| 3069 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3070 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3071 | case PP_EXITREP: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3072 | /* |
| 3073 | * We must search along istk->expansion until we hit a |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3074 | * macro-end marker for a macro with no name. Then we set |
| 3075 | * its `in_progress' flag to 0. |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3076 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3077 | list_for_each(l, istk->expansion) |
| 3078 | if (l->finishes && !l->finishes->name) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3079 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3080 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3081 | if (l) |
| 3082 | l->finishes->in_progress = 1; |
| 3083 | else |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3084 | nasm_error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3085 | free_tlist(origline); |
| 3086 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3087 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3088 | case PP_XDEFINE: |
| 3089 | case PP_IXDEFINE: |
| 3090 | case PP_DEFINE: |
| 3091 | case PP_IDEFINE: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3092 | casesense = (i == PP_DEFINE || i == PP_XDEFINE); |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3093 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3094 | tline = tline->next; |
| 3095 | skip_white_(tline); |
| 3096 | tline = expand_id(tline); |
| 3097 | if (!tline || (tline->type != TOK_ID && |
| 3098 | (tline->type != TOK_PREPROC_ID || |
| 3099 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3100 | nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3101 | pp_directives[i]); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3102 | free_tlist(origline); |
| 3103 | return DIRECTIVE_FOUND; |
| 3104 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3105 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3106 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3107 | last = tline; |
| 3108 | param_start = tline = tline->next; |
| 3109 | nparam = 0; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3110 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3111 | /* Expand the macro definition now for %xdefine and %ixdefine */ |
| 3112 | if ((i == PP_XDEFINE) || (i == PP_IXDEFINE)) |
| 3113 | tline = expand_smacro(tline); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3114 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3115 | if (tok_is_(tline, "(")) { |
| 3116 | /* |
| 3117 | * This macro has parameters. |
| 3118 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3119 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3120 | tline = tline->next; |
| 3121 | while (1) { |
| 3122 | skip_white_(tline); |
| 3123 | if (!tline) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3124 | nasm_error(ERR_NONFATAL, "parameter identifier expected"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3125 | free_tlist(origline); |
| 3126 | return DIRECTIVE_FOUND; |
| 3127 | } |
| 3128 | if (tline->type != TOK_ID) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3129 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3130 | "`%s': parameter identifier expected", |
| 3131 | tline->text); |
| 3132 | free_tlist(origline); |
| 3133 | return DIRECTIVE_FOUND; |
| 3134 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3135 | tline->type = TOK_SMAC_PARAM + nparam++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3136 | tline = tline->next; |
| 3137 | skip_white_(tline); |
| 3138 | if (tok_is_(tline, ",")) { |
| 3139 | tline = tline->next; |
H. Peter Anvin | ca348b6 | 2008-07-23 10:49:26 -0400 | [diff] [blame] | 3140 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3141 | if (!tok_is_(tline, ")")) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3142 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3143 | "`)' expected to terminate macro template"); |
| 3144 | free_tlist(origline); |
| 3145 | return DIRECTIVE_FOUND; |
| 3146 | } |
| 3147 | break; |
| 3148 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3149 | } |
| 3150 | last = tline; |
| 3151 | tline = tline->next; |
| 3152 | } |
| 3153 | if (tok_type_(tline, TOK_WHITESPACE)) |
| 3154 | last = tline, tline = tline->next; |
| 3155 | macro_start = NULL; |
| 3156 | last->next = NULL; |
| 3157 | t = tline; |
| 3158 | while (t) { |
| 3159 | if (t->type == TOK_ID) { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3160 | list_for_each(tt, param_start) |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3161 | if (tt->type >= TOK_SMAC_PARAM && |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3162 | !strcmp(tt->text, t->text)) |
| 3163 | t->type = tt->type; |
| 3164 | } |
| 3165 | tt = t->next; |
| 3166 | t->next = macro_start; |
| 3167 | macro_start = t; |
| 3168 | t = tt; |
| 3169 | } |
| 3170 | /* |
| 3171 | * Good. We now have a macro name, a parameter count, and a |
| 3172 | * token list (in reverse order) for an expansion. We ought |
| 3173 | * to be OK just to create an SMacro, store it, and let |
| 3174 | * free_tlist have the rest of the line (which we have |
| 3175 | * carefully re-terminated after chopping off the expansion |
| 3176 | * from the end). |
| 3177 | */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 3178 | define_smacro(ctx, mname, casesense, nparam, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3179 | free_tlist(origline); |
| 3180 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3181 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3182 | case PP_UNDEF: |
| 3183 | tline = tline->next; |
| 3184 | skip_white_(tline); |
| 3185 | tline = expand_id(tline); |
| 3186 | if (!tline || (tline->type != TOK_ID && |
| 3187 | (tline->type != TOK_PREPROC_ID || |
| 3188 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3189 | nasm_error(ERR_NONFATAL, "`%%undef' expects a macro identifier"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3190 | free_tlist(origline); |
| 3191 | return DIRECTIVE_FOUND; |
| 3192 | } |
| 3193 | if (tline->next) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3194 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3195 | "trailing garbage after macro name ignored"); |
| 3196 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3197 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3198 | /* Find the context that symbol belongs to */ |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3199 | ctx = get_ctx(tline->text, &mname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3200 | undef_smacro(ctx, mname); |
| 3201 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3202 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3203 | |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3204 | case PP_DEFSTR: |
| 3205 | case PP_IDEFSTR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3206 | casesense = (i == PP_DEFSTR); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3207 | |
| 3208 | tline = tline->next; |
| 3209 | skip_white_(tline); |
| 3210 | tline = expand_id(tline); |
| 3211 | if (!tline || (tline->type != TOK_ID && |
| 3212 | (tline->type != TOK_PREPROC_ID || |
| 3213 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3214 | nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3215 | pp_directives[i]); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3216 | free_tlist(origline); |
| 3217 | return DIRECTIVE_FOUND; |
| 3218 | } |
| 3219 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3220 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3221 | last = tline; |
| 3222 | tline = expand_smacro(tline->next); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3223 | last->next = NULL; |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3224 | |
| 3225 | while (tok_type_(tline, TOK_WHITESPACE)) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3226 | tline = delete_Token(tline); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3227 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3228 | p = detoken(tline, false); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3229 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3230 | macro_start->next = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3231 | macro_start->text = nasm_quote(p, strlen(p)); |
| 3232 | macro_start->type = TOK_STRING; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3233 | macro_start->a.mac = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3234 | nasm_free(p); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3235 | |
| 3236 | /* |
| 3237 | * We now have a macro name, an implicit parameter count of |
| 3238 | * zero, and a string token to use as an expansion. Create |
| 3239 | * and store an SMacro. |
| 3240 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3241 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3242 | free_tlist(origline); |
| 3243 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3244 | |
H. Peter Anvin | 2f55bda | 2009-07-14 15:04:04 -0400 | [diff] [blame] | 3245 | case PP_DEFTOK: |
| 3246 | case PP_IDEFTOK: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3247 | casesense = (i == PP_DEFTOK); |
| 3248 | |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3249 | tline = tline->next; |
| 3250 | skip_white_(tline); |
| 3251 | tline = expand_id(tline); |
| 3252 | if (!tline || (tline->type != TOK_ID && |
| 3253 | (tline->type != TOK_PREPROC_ID || |
| 3254 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3255 | nasm_error(ERR_NONFATAL, |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3256 | "`%s' expects a macro identifier as first parameter", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3257 | pp_directives[i]); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3258 | free_tlist(origline); |
| 3259 | return DIRECTIVE_FOUND; |
| 3260 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3261 | ctx = get_ctx(tline->text, &mname); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3262 | last = tline; |
| 3263 | tline = expand_smacro(tline->next); |
| 3264 | last->next = NULL; |
| 3265 | |
| 3266 | t = tline; |
| 3267 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3268 | t = t->next; |
| 3269 | /* t should now point to the string */ |
Cyrill Gorcunov | 6908e58 | 2010-09-06 19:36:15 +0400 | [diff] [blame] | 3270 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3271 | nasm_error(ERR_NONFATAL, |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3272 | "`%s` requires string as second parameter", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3273 | pp_directives[i]); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3274 | free_tlist(tline); |
| 3275 | free_tlist(origline); |
| 3276 | return DIRECTIVE_FOUND; |
| 3277 | } |
| 3278 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 3279 | /* |
| 3280 | * Convert the string to a token stream. Note that smacros |
| 3281 | * are stored with the token stream reversed, so we have to |
| 3282 | * reverse the output of tokenize(). |
| 3283 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3284 | nasm_unquote_cstr(t->text, i); |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 3285 | macro_start = reverse_tokens(tokenize(t->text)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3286 | |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3287 | /* |
| 3288 | * We now have a macro name, an implicit parameter count of |
| 3289 | * zero, and a numeric token to use as an expansion. Create |
| 3290 | * and store an SMacro. |
| 3291 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3292 | define_smacro(ctx, mname, casesense, 0, macro_start); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3293 | free_tlist(tline); |
| 3294 | free_tlist(origline); |
| 3295 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3296 | |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3297 | case PP_PATHSEARCH: |
| 3298 | { |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 3299 | const char *found_path; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3300 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3301 | casesense = true; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3302 | |
| 3303 | tline = tline->next; |
| 3304 | skip_white_(tline); |
| 3305 | tline = expand_id(tline); |
| 3306 | if (!tline || (tline->type != TOK_ID && |
| 3307 | (tline->type != TOK_PREPROC_ID || |
| 3308 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3309 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3310 | "`%%pathsearch' expects a macro identifier as first parameter"); |
| 3311 | free_tlist(origline); |
| 3312 | return DIRECTIVE_FOUND; |
| 3313 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3314 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3315 | last = tline; |
| 3316 | tline = expand_smacro(tline->next); |
| 3317 | last->next = NULL; |
| 3318 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3319 | t = tline; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3320 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3321 | t = t->next; |
| 3322 | |
| 3323 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3324 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3325 | nasm_error(ERR_NONFATAL, "`%%pathsearch' expects a file name"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3326 | free_tlist(tline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3327 | free_tlist(origline); |
| 3328 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 3329 | } |
| 3330 | if (t->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3331 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3332 | "trailing garbage after `%%pathsearch' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3333 | p = t->text; |
H. Peter Anvin | 427cc91 | 2008-06-01 21:43:03 -0700 | [diff] [blame] | 3334 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3335 | nasm_unquote(p, NULL); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3336 | |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 3337 | inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY); |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 3338 | if (!found_path) |
| 3339 | found_path = p; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3340 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3341 | macro_start->next = NULL; |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 3342 | macro_start->text = nasm_quote(found_path, strlen(found_path)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3343 | macro_start->type = TOK_STRING; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3344 | macro_start->a.mac = NULL; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3345 | |
| 3346 | /* |
| 3347 | * We now have a macro name, an implicit parameter count of |
| 3348 | * zero, and a string token to use as an expansion. Create |
| 3349 | * and store an SMacro. |
| 3350 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3351 | define_smacro(ctx, mname, casesense, 0, macro_start); |
| 3352 | free_tlist(tline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3353 | free_tlist(origline); |
| 3354 | return DIRECTIVE_FOUND; |
| 3355 | } |
| 3356 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3357 | case PP_STRLEN: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3358 | casesense = true; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 3359 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3360 | tline = tline->next; |
| 3361 | skip_white_(tline); |
| 3362 | tline = expand_id(tline); |
| 3363 | if (!tline || (tline->type != TOK_ID && |
| 3364 | (tline->type != TOK_PREPROC_ID || |
| 3365 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3366 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3367 | "`%%strlen' expects a macro identifier as first parameter"); |
| 3368 | free_tlist(origline); |
| 3369 | return DIRECTIVE_FOUND; |
| 3370 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3371 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3372 | last = tline; |
| 3373 | tline = expand_smacro(tline->next); |
| 3374 | last->next = NULL; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3375 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3376 | t = tline; |
| 3377 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3378 | t = t->next; |
| 3379 | /* t should now point to the string */ |
Cyrill Gorcunov | 4e1d5ab | 2010-07-23 18:51:51 +0400 | [diff] [blame] | 3380 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3381 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3382 | "`%%strlen` requires string as second parameter"); |
| 3383 | free_tlist(tline); |
| 3384 | free_tlist(origline); |
| 3385 | return DIRECTIVE_FOUND; |
| 3386 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3387 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3388 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3389 | macro_start->next = NULL; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 3390 | make_tok_num(macro_start, nasm_unquote(t->text, NULL)); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3391 | macro_start->a.mac = NULL; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3392 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3393 | /* |
| 3394 | * We now have a macro name, an implicit parameter count of |
| 3395 | * zero, and a numeric token to use as an expansion. Create |
| 3396 | * and store an SMacro. |
| 3397 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3398 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3399 | free_tlist(tline); |
| 3400 | free_tlist(origline); |
| 3401 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3402 | |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3403 | case PP_STRCAT: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3404 | casesense = true; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3405 | |
| 3406 | tline = tline->next; |
| 3407 | skip_white_(tline); |
| 3408 | tline = expand_id(tline); |
| 3409 | if (!tline || (tline->type != TOK_ID && |
| 3410 | (tline->type != TOK_PREPROC_ID || |
| 3411 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3412 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3413 | "`%%strcat' expects a macro identifier as first parameter"); |
| 3414 | free_tlist(origline); |
| 3415 | return DIRECTIVE_FOUND; |
| 3416 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3417 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3418 | last = tline; |
| 3419 | tline = expand_smacro(tline->next); |
| 3420 | last->next = NULL; |
| 3421 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3422 | len = 0; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3423 | list_for_each(t, tline) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3424 | switch (t->type) { |
| 3425 | case TOK_WHITESPACE: |
| 3426 | break; |
| 3427 | case TOK_STRING: |
| 3428 | len += t->a.len = nasm_unquote(t->text, NULL); |
| 3429 | break; |
| 3430 | case TOK_OTHER: |
| 3431 | if (!strcmp(t->text, ",")) /* permit comma separators */ |
| 3432 | break; |
| 3433 | /* else fall through */ |
| 3434 | default: |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3435 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3436 | "non-string passed to `%%strcat' (%d)", t->type); |
| 3437 | free_tlist(tline); |
| 3438 | free_tlist(origline); |
| 3439 | return DIRECTIVE_FOUND; |
| 3440 | } |
| 3441 | } |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3442 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3443 | p = pp = nasm_malloc(len); |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3444 | list_for_each(t, tline) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3445 | if (t->type == TOK_STRING) { |
| 3446 | memcpy(p, t->text, t->a.len); |
| 3447 | p += t->a.len; |
| 3448 | } |
| 3449 | } |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3450 | |
| 3451 | /* |
| 3452 | * We now have a macro name, an implicit parameter count of |
| 3453 | * zero, and a numeric token to use as an expansion. Create |
| 3454 | * and store an SMacro. |
| 3455 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3456 | macro_start = new_Token(NULL, TOK_STRING, NULL, 0); |
| 3457 | macro_start->text = nasm_quote(pp, len); |
| 3458 | nasm_free(pp); |
| 3459 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3460 | free_tlist(tline); |
| 3461 | free_tlist(origline); |
| 3462 | return DIRECTIVE_FOUND; |
| 3463 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3464 | case PP_SUBSTR: |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3465 | { |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3466 | int64_t start, count; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3467 | size_t len; |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 3468 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3469 | casesense = true; |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3470 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3471 | tline = tline->next; |
| 3472 | skip_white_(tline); |
| 3473 | tline = expand_id(tline); |
| 3474 | if (!tline || (tline->type != TOK_ID && |
| 3475 | (tline->type != TOK_PREPROC_ID || |
| 3476 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3477 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3478 | "`%%substr' expects a macro identifier as first parameter"); |
| 3479 | free_tlist(origline); |
| 3480 | return DIRECTIVE_FOUND; |
| 3481 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3482 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3483 | last = tline; |
| 3484 | tline = expand_smacro(tline->next); |
| 3485 | last->next = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3486 | |
Cyrill Gorcunov | 35519d6 | 2010-09-06 23:49:52 +0400 | [diff] [blame] | 3487 | if (tline) /* skip expanded id */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3488 | t = tline->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3489 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3490 | t = t->next; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3491 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3492 | /* t should now point to the string */ |
Cyrill Gorcunov | 35519d6 | 2010-09-06 23:49:52 +0400 | [diff] [blame] | 3493 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3494 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3495 | "`%%substr` requires string as second parameter"); |
| 3496 | free_tlist(tline); |
| 3497 | free_tlist(origline); |
| 3498 | return DIRECTIVE_FOUND; |
| 3499 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3500 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3501 | tt = t->next; |
| 3502 | tptr = &tt; |
| 3503 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3504 | evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3505 | if (!evalresult) { |
| 3506 | free_tlist(tline); |
| 3507 | free_tlist(origline); |
| 3508 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3509 | } else if (!is_simple(evalresult)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3510 | nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3511 | free_tlist(tline); |
| 3512 | free_tlist(origline); |
| 3513 | return DIRECTIVE_FOUND; |
| 3514 | } |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3515 | start = evalresult->value - 1; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3516 | |
| 3517 | while (tok_type_(tt, TOK_WHITESPACE)) |
| 3518 | tt = tt->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3519 | if (!tt) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3520 | count = 1; /* Backwards compatibility: one character */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3521 | } else { |
| 3522 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3523 | evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3524 | if (!evalresult) { |
| 3525 | free_tlist(tline); |
| 3526 | free_tlist(origline); |
| 3527 | return DIRECTIVE_FOUND; |
| 3528 | } else if (!is_simple(evalresult)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3529 | nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3530 | free_tlist(tline); |
| 3531 | free_tlist(origline); |
| 3532 | return DIRECTIVE_FOUND; |
| 3533 | } |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3534 | count = evalresult->value; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3535 | } |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3536 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3537 | len = nasm_unquote(t->text, NULL); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3538 | |
Cyrill Gorcunov | cff031e | 2010-09-07 20:31:11 +0400 | [diff] [blame] | 3539 | /* make start and count being in range */ |
| 3540 | if (start < 0) |
| 3541 | start = 0; |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3542 | if (count < 0) |
| 3543 | count = len + count + 1 - start; |
| 3544 | if (start + count > (int64_t)len) |
Cyrill Gorcunov | cff031e | 2010-09-07 20:31:11 +0400 | [diff] [blame] | 3545 | count = len - start; |
| 3546 | if (!len || count < 0 || start >=(int64_t)len) |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3547 | start = -1, count = 0; /* empty string */ |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3548 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3549 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3550 | macro_start->next = NULL; |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3551 | macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3552 | macro_start->type = TOK_STRING; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3553 | macro_start->a.mac = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3554 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3555 | /* |
| 3556 | * We now have a macro name, an implicit parameter count of |
| 3557 | * zero, and a numeric token to use as an expansion. Create |
| 3558 | * and store an SMacro. |
| 3559 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3560 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3561 | free_tlist(tline); |
| 3562 | free_tlist(origline); |
| 3563 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3564 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3565 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3566 | case PP_ASSIGN: |
| 3567 | case PP_IASSIGN: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3568 | casesense = (i == PP_ASSIGN); |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3569 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3570 | tline = tline->next; |
| 3571 | skip_white_(tline); |
| 3572 | tline = expand_id(tline); |
| 3573 | if (!tline || (tline->type != TOK_ID && |
| 3574 | (tline->type != TOK_PREPROC_ID || |
| 3575 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3576 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3577 | "`%%%sassign' expects a macro identifier", |
| 3578 | (i == PP_IASSIGN ? "i" : "")); |
| 3579 | free_tlist(origline); |
| 3580 | return DIRECTIVE_FOUND; |
| 3581 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3582 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3583 | last = tline; |
| 3584 | tline = expand_smacro(tline->next); |
| 3585 | last->next = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3586 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3587 | t = tline; |
| 3588 | tptr = &t; |
| 3589 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3590 | evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3591 | free_tlist(tline); |
| 3592 | if (!evalresult) { |
| 3593 | free_tlist(origline); |
| 3594 | return DIRECTIVE_FOUND; |
| 3595 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3596 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3597 | if (tokval.t_type) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3598 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3599 | "trailing garbage after expression ignored"); |
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 (!is_simple(evalresult)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3602 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3603 | "non-constant value given to `%%%sassign'", |
| 3604 | (i == PP_IASSIGN ? "i" : "")); |
| 3605 | free_tlist(origline); |
| 3606 | return DIRECTIVE_FOUND; |
| 3607 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3608 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3609 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3610 | macro_start->next = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3611 | make_tok_num(macro_start, reloc_value(evalresult)); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3612 | macro_start->a.mac = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3613 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3614 | /* |
| 3615 | * We now have a macro name, an implicit parameter count of |
| 3616 | * zero, and a numeric token to use as an expansion. Create |
| 3617 | * and store an SMacro. |
| 3618 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3619 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3620 | free_tlist(origline); |
| 3621 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3622 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3623 | case PP_LINE: |
| 3624 | /* |
| 3625 | * Syntax is `%line nnn[+mmm] [filename]' |
| 3626 | */ |
| 3627 | tline = tline->next; |
| 3628 | skip_white_(tline); |
| 3629 | if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3630 | nasm_error(ERR_NONFATAL, "`%%line' expects line number"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3631 | free_tlist(origline); |
| 3632 | return DIRECTIVE_FOUND; |
| 3633 | } |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 3634 | k = readnum(tline->text, &err); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3635 | m = 1; |
| 3636 | tline = tline->next; |
| 3637 | if (tok_is_(tline, "+")) { |
| 3638 | tline = tline->next; |
| 3639 | if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3640 | nasm_error(ERR_NONFATAL, "`%%line' expects line increment"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3641 | free_tlist(origline); |
| 3642 | return DIRECTIVE_FOUND; |
| 3643 | } |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 3644 | m = readnum(tline->text, &err); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3645 | tline = tline->next; |
| 3646 | } |
| 3647 | skip_white_(tline); |
| 3648 | src_set_linnum(k); |
| 3649 | istk->lineinc = m; |
| 3650 | if (tline) { |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 3651 | char *fname = detoken(tline, false); |
| 3652 | src_set_fname(fname); |
| 3653 | nasm_free(fname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3654 | } |
| 3655 | free_tlist(origline); |
| 3656 | return DIRECTIVE_FOUND; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3657 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3658 | default: |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3659 | nasm_error(ERR_FATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3660 | "preprocessor directive `%s' not yet implemented", |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 3661 | pp_directives[i]); |
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) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3748 | nasm_error(ERR_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: |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3948 | nasm_error(ERR_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) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3986 | nasm_error(ERR_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) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4017 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4018 | "macro parameter %d is not a condition code", |
| 4019 | n + 1); |
| 4020 | text = NULL; |
| 4021 | } else { |
| 4022 | type = TOK_ID; |
| 4023 | if (inverse_ccs[cc] == -1) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4024 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4025 | "condition code `%s' is not invertible", |
| 4026 | conditions[cc]); |
| 4027 | text = NULL; |
| 4028 | } else |
| 4029 | text = nasm_strdup(conditions[inverse_ccs[cc]]); |
| 4030 | } |
| 4031 | break; |
| 4032 | case '+': |
| 4033 | n = atoi(t->text + 2) - 1; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4034 | if (n >= mac->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4035 | tt = NULL; |
| 4036 | else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4037 | if (mac->nparam > 1) |
| 4038 | n = (n + mac->rotate) % mac->nparam; |
| 4039 | tt = mac->params[n]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4040 | } |
| 4041 | cc = find_cc(tt); |
| 4042 | if (cc == -1) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4043 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4044 | "macro parameter %d is not a condition code", |
| 4045 | n + 1); |
| 4046 | text = NULL; |
| 4047 | } else { |
| 4048 | type = TOK_ID; |
| 4049 | text = nasm_strdup(conditions[cc]); |
| 4050 | } |
| 4051 | break; |
| 4052 | default: |
| 4053 | n = atoi(t->text + 1) - 1; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4054 | if (n >= mac->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4055 | tt = NULL; |
| 4056 | else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4057 | if (mac->nparam > 1) |
| 4058 | n = (n + mac->rotate) % mac->nparam; |
| 4059 | tt = mac->params[n]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4060 | } |
| 4061 | if (tt) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4062 | for (i = 0; i < mac->paramlen[n]; i++) { |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4063 | *tail = new_Token(NULL, tt->type, tt->text, 0); |
| 4064 | tail = &(*tail)->next; |
| 4065 | tt = tt->next; |
| 4066 | } |
| 4067 | } |
| 4068 | text = NULL; /* we've done it here */ |
| 4069 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4070 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4071 | } else { |
| 4072 | /* |
| 4073 | * seems we have a parameters range here |
| 4074 | */ |
| 4075 | Token *head, **last; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4076 | head = expand_mmac_params_range(mac, t, &last); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4077 | if (head != t) { |
| 4078 | *tail = head; |
| 4079 | *last = tline; |
| 4080 | tline = head; |
| 4081 | text = NULL; |
| 4082 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4083 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4084 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4085 | if (!text) { |
| 4086 | delete_Token(t); |
| 4087 | } else { |
| 4088 | *tail = t; |
| 4089 | tail = &t->next; |
| 4090 | t->type = type; |
| 4091 | nasm_free(t->text); |
| 4092 | t->text = text; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4093 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4094 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4095 | changed = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4096 | continue; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4097 | } else if (tline->type == TOK_INDIRECT) { |
| 4098 | t = tline; |
| 4099 | tline = tline->next; |
| 4100 | tt = tokenize(t->text); |
| 4101 | tt = expand_mmac_params(tt); |
| 4102 | tt = expand_smacro(tt); |
| 4103 | *tail = tt; |
| 4104 | while (tt) { |
| 4105 | tt->a.mac = NULL; /* Necessary? */ |
| 4106 | tail = &tt->next; |
| 4107 | tt = tt->next; |
| 4108 | } |
| 4109 | delete_Token(t); |
| 4110 | changed = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4111 | } else { |
| 4112 | t = *tail = tline; |
| 4113 | tline = tline->next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4114 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4115 | tail = &t->next; |
| 4116 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4117 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4118 | *tail = NULL; |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 4119 | |
Cyrill Gorcunov | c6a742c | 2011-06-27 01:23:09 +0400 | [diff] [blame] | 4120 | if (changed) { |
| 4121 | const struct tokseq_match t[] = { |
| 4122 | { |
| 4123 | PP_CONCAT_MASK(TOK_ID) | |
| 4124 | PP_CONCAT_MASK(TOK_FLOAT), /* head */ |
| 4125 | PP_CONCAT_MASK(TOK_ID) | |
| 4126 | PP_CONCAT_MASK(TOK_NUMBER) | |
| 4127 | PP_CONCAT_MASK(TOK_FLOAT) | |
| 4128 | PP_CONCAT_MASK(TOK_OTHER) /* tail */ |
| 4129 | }, |
| 4130 | { |
| 4131 | PP_CONCAT_MASK(TOK_NUMBER), /* head */ |
| 4132 | PP_CONCAT_MASK(TOK_NUMBER) /* tail */ |
| 4133 | } |
| 4134 | }; |
| 4135 | paste_tokens(&thead, t, ARRAY_SIZE(t), false); |
| 4136 | } |
H. Peter Anvin | 6125b62 | 2009-04-08 14:02:25 -0700 | [diff] [blame] | 4137 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4138 | return thead; |
| 4139 | } |
| 4140 | |
| 4141 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4142 | * Expand all single-line macro calls made in the given line. |
| 4143 | * Return the expanded version of the line. The original is deemed |
| 4144 | * to be destroyed in the process. (In reality we'll just move |
| 4145 | * Tokens from input to output a lot of the time, rather than |
| 4146 | * actually bothering to destroy and replicate.) |
| 4147 | */ |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4148 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4149 | static Token *expand_smacro(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4150 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4151 | Token *t, *tt, *mstart, **tail, *thead; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4152 | SMacro *head = NULL, *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4153 | Token **params; |
| 4154 | int *paramsize; |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 4155 | unsigned int nparam, sparam; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 4156 | int brackets; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4157 | Token *org_tline = tline; |
| 4158 | Context *ctx; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 4159 | const char *mname; |
H. Peter Anvin | a3d96d0 | 2018-06-15 17:51:39 -0700 | [diff] [blame] | 4160 | int64_t deadman = nasm_limit[LIMIT_MACROS]; |
H. Peter Anvin | 8287daf | 2009-07-07 16:00:58 -0700 | [diff] [blame] | 4161 | bool expanded; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4162 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4163 | /* |
| 4164 | * Trick: we should avoid changing the start token pointer since it can |
| 4165 | * be contained in "next" field of other token. Because of this |
| 4166 | * we allocate a copy of first token and work with it; at the end of |
| 4167 | * routine we copy it back |
| 4168 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4169 | if (org_tline) { |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4170 | tline = new_Token(org_tline->next, org_tline->type, |
| 4171 | org_tline->text, 0); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4172 | tline->a.mac = org_tline->a.mac; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4173 | nasm_free(org_tline->text); |
| 4174 | org_tline->text = NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4175 | } |
| 4176 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4177 | expanded = true; /* Always expand %+ at least once */ |
H. Peter Anvin | 8287daf | 2009-07-07 16:00:58 -0700 | [diff] [blame] | 4178 | |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4179 | again: |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4180 | thead = NULL; |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4181 | tail = &thead; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4182 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4183 | while (tline) { /* main token loop */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4184 | if (!--deadman) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4185 | nasm_error(ERR_NONFATAL, "interminable macro recursion"); |
Cyrill Gorcunov | bd38c8f | 2009-11-21 11:11:23 +0300 | [diff] [blame] | 4186 | goto err; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4187 | } |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4188 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4189 | if ((mname = tline->text)) { |
| 4190 | /* if this token is a local macro, look in local context */ |
Cyrill Gorcunov | c56d9ad | 2010-02-11 15:12:19 +0300 | [diff] [blame] | 4191 | if (tline->type == TOK_ID) { |
| 4192 | head = (SMacro *)hash_findix(&smacros, mname); |
| 4193 | } else if (tline->type == TOK_PREPROC_ID) { |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 4194 | ctx = get_ctx(mname, &mname); |
Cyrill Gorcunov | c56d9ad | 2010-02-11 15:12:19 +0300 | [diff] [blame] | 4195 | head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL; |
| 4196 | } else |
| 4197 | head = NULL; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 4198 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4199 | /* |
| 4200 | * We've hit an identifier. As in is_mmacro below, we first |
| 4201 | * check whether the identifier is a single-line macro at |
| 4202 | * all, then think about checking for parameters if |
| 4203 | * necessary. |
| 4204 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4205 | list_for_each(m, head) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4206 | if (!mstrcmp(m->name, mname, m->casesense)) |
| 4207 | break; |
| 4208 | if (m) { |
| 4209 | mstart = tline; |
| 4210 | params = NULL; |
| 4211 | paramsize = NULL; |
| 4212 | if (m->nparam == 0) { |
| 4213 | /* |
| 4214 | * Simple case: the macro is parameterless. Discard the |
| 4215 | * one token that the macro call took, and push the |
| 4216 | * expansion back on the to-do stack. |
| 4217 | */ |
| 4218 | if (!m->expansion) { |
| 4219 | if (!strcmp("__FILE__", m->name)) { |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 4220 | const char *file = src_get_fname(); |
| 4221 | /* nasm_free(tline->text); here? */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4222 | tline->text = nasm_quote(file, strlen(file)); |
| 4223 | tline->type = TOK_STRING; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4224 | continue; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4225 | } |
| 4226 | if (!strcmp("__LINE__", m->name)) { |
| 4227 | nasm_free(tline->text); |
| 4228 | make_tok_num(tline, src_get_linnum()); |
| 4229 | continue; |
| 4230 | } |
| 4231 | if (!strcmp("__BITS__", m->name)) { |
| 4232 | nasm_free(tline->text); |
| 4233 | make_tok_num(tline, globalbits); |
| 4234 | continue; |
| 4235 | } |
| 4236 | tline = delete_Token(tline); |
| 4237 | continue; |
| 4238 | } |
| 4239 | } else { |
| 4240 | /* |
| 4241 | * Complicated case: at least one macro with this name |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4242 | * exists and takes parameters. We must find the |
| 4243 | * parameters in the call, count them, find the SMacro |
| 4244 | * that corresponds to that form of the macro call, and |
| 4245 | * substitute for the parameters when we expand. What a |
| 4246 | * pain. |
| 4247 | */ |
| 4248 | /*tline = tline->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4249 | skip_white_(tline); */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4250 | do { |
| 4251 | t = tline->next; |
| 4252 | while (tok_type_(t, TOK_SMAC_END)) { |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4253 | t->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4254 | t->text = NULL; |
| 4255 | t = tline->next = delete_Token(t); |
| 4256 | } |
| 4257 | tline = t; |
| 4258 | } while (tok_type_(tline, TOK_WHITESPACE)); |
| 4259 | if (!tok_is_(tline, "(")) { |
| 4260 | /* |
| 4261 | * This macro wasn't called with parameters: ignore |
| 4262 | * the call. (Behaviour borrowed from gnu cpp.) |
| 4263 | */ |
| 4264 | tline = mstart; |
| 4265 | m = NULL; |
| 4266 | } else { |
| 4267 | int paren = 0; |
| 4268 | int white = 0; |
| 4269 | brackets = 0; |
| 4270 | nparam = 0; |
| 4271 | sparam = PARAM_DELTA; |
| 4272 | params = nasm_malloc(sparam * sizeof(Token *)); |
| 4273 | params[0] = tline->next; |
| 4274 | paramsize = nasm_malloc(sparam * sizeof(int)); |
| 4275 | paramsize[0] = 0; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4276 | while (true) { /* parameter loop */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4277 | /* |
| 4278 | * For some unusual expansions |
| 4279 | * which concatenates function call |
| 4280 | */ |
| 4281 | t = tline->next; |
| 4282 | while (tok_type_(t, TOK_SMAC_END)) { |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4283 | t->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4284 | t->text = NULL; |
| 4285 | t = tline->next = delete_Token(t); |
| 4286 | } |
| 4287 | tline = t; |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 4288 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4289 | if (!tline) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4290 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4291 | "macro call expects terminating `)'"); |
| 4292 | break; |
| 4293 | } |
| 4294 | if (tline->type == TOK_WHITESPACE |
| 4295 | && brackets <= 0) { |
| 4296 | if (paramsize[nparam]) |
| 4297 | white++; |
| 4298 | else |
| 4299 | params[nparam] = tline->next; |
| 4300 | continue; /* parameter loop */ |
| 4301 | } |
| 4302 | if (tline->type == TOK_OTHER |
| 4303 | && tline->text[1] == 0) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4304 | char ch = tline->text[0]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4305 | if (ch == ',' && !paren && brackets <= 0) { |
| 4306 | if (++nparam >= sparam) { |
| 4307 | sparam += PARAM_DELTA; |
| 4308 | params = nasm_realloc(params, |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4309 | sparam * sizeof(Token *)); |
| 4310 | paramsize = nasm_realloc(paramsize, |
| 4311 | sparam * sizeof(int)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4312 | } |
| 4313 | params[nparam] = tline->next; |
| 4314 | paramsize[nparam] = 0; |
| 4315 | white = 0; |
| 4316 | continue; /* parameter loop */ |
| 4317 | } |
| 4318 | if (ch == '{' && |
| 4319 | (brackets > 0 || (brackets == 0 && |
| 4320 | !paramsize[nparam]))) |
| 4321 | { |
| 4322 | if (!(brackets++)) { |
| 4323 | params[nparam] = tline->next; |
| 4324 | continue; /* parameter loop */ |
| 4325 | } |
| 4326 | } |
| 4327 | if (ch == '}' && brackets > 0) |
| 4328 | if (--brackets == 0) { |
| 4329 | brackets = -1; |
| 4330 | continue; /* parameter loop */ |
| 4331 | } |
| 4332 | if (ch == '(' && !brackets) |
| 4333 | paren++; |
| 4334 | if (ch == ')' && brackets <= 0) |
| 4335 | if (--paren < 0) |
| 4336 | break; |
| 4337 | } |
| 4338 | if (brackets < 0) { |
| 4339 | brackets = 0; |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4340 | nasm_error(ERR_NONFATAL, "braces do not " |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4341 | "enclose all of macro parameter"); |
| 4342 | } |
| 4343 | paramsize[nparam] += white + 1; |
| 4344 | white = 0; |
| 4345 | } /* parameter loop */ |
| 4346 | nparam++; |
| 4347 | while (m && (m->nparam != nparam || |
| 4348 | mstrcmp(m->name, mname, |
| 4349 | m->casesense))) |
| 4350 | m = m->next; |
| 4351 | if (!m) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4352 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4353 | "macro `%s' exists, " |
| 4354 | "but not taking %d parameters", |
| 4355 | mstart->text, nparam); |
| 4356 | } |
| 4357 | } |
| 4358 | if (m && m->in_progress) |
| 4359 | m = NULL; |
| 4360 | if (!m) { /* in progess or didn't find '(' or wrong nparam */ |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 4361 | /* |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4362 | * Design question: should we handle !tline, which |
| 4363 | * indicates missing ')' here, or expand those |
| 4364 | * macros anyway, which requires the (t) test a few |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 4365 | * lines down? |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4366 | */ |
| 4367 | nasm_free(params); |
| 4368 | nasm_free(paramsize); |
| 4369 | tline = mstart; |
| 4370 | } else { |
| 4371 | /* |
| 4372 | * Expand the macro: we are placed on the last token of the |
| 4373 | * call, so that we can easily split the call from the |
| 4374 | * following tokens. We also start by pushing an SMAC_END |
| 4375 | * token for the cycle removal. |
| 4376 | */ |
| 4377 | t = tline; |
| 4378 | if (t) { |
| 4379 | tline = t->next; |
| 4380 | t->next = NULL; |
| 4381 | } |
| 4382 | tt = new_Token(tline, TOK_SMAC_END, NULL, 0); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4383 | tt->a.mac = m; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4384 | m->in_progress = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4385 | tline = tt; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 4386 | list_for_each(t, m->expansion) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4387 | if (t->type >= TOK_SMAC_PARAM) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4388 | Token *pcopy = tline, **ptail = &pcopy; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4389 | Token *ttt, *pt; |
| 4390 | int i; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4391 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4392 | ttt = params[t->type - TOK_SMAC_PARAM]; |
| 4393 | i = paramsize[t->type - TOK_SMAC_PARAM]; |
| 4394 | while (--i >= 0) { |
| 4395 | pt = *ptail = new_Token(tline, ttt->type, |
| 4396 | ttt->text, 0); |
| 4397 | ptail = &pt->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4398 | ttt = ttt->next; |
Cyrill Gorcunov | 59ce1c6 | 2017-10-22 18:42:07 +0300 | [diff] [blame] | 4399 | if (!ttt && i > 0) { |
| 4400 | /* |
| 4401 | * FIXME: Need to handle more gracefully, |
| 4402 | * exiting early on agruments analysis. |
| 4403 | */ |
| 4404 | nasm_error(ERR_FATAL, |
| 4405 | "macro `%s' expects %d args", |
| 4406 | mstart->text, |
| 4407 | (int)paramsize[t->type - TOK_SMAC_PARAM]); |
| 4408 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4409 | } |
| 4410 | tline = pcopy; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4411 | } else if (t->type == TOK_PREPROC_Q) { |
| 4412 | tt = new_Token(tline, TOK_ID, mname, 0); |
| 4413 | tline = tt; |
| 4414 | } else if (t->type == TOK_PREPROC_QQ) { |
| 4415 | tt = new_Token(tline, TOK_ID, m->name, 0); |
| 4416 | tline = tt; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4417 | } else { |
| 4418 | tt = new_Token(tline, t->type, t->text, 0); |
| 4419 | tline = tt; |
| 4420 | } |
| 4421 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4422 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4423 | /* |
| 4424 | * Having done that, get rid of the macro call, and clean |
| 4425 | * up the parameters. |
| 4426 | */ |
| 4427 | nasm_free(params); |
| 4428 | nasm_free(paramsize); |
| 4429 | free_tlist(mstart); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4430 | expanded = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4431 | continue; /* main token loop */ |
| 4432 | } |
| 4433 | } |
| 4434 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4435 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4436 | if (tline->type == TOK_SMAC_END) { |
Cyrill Gorcunov | 980dd65 | 2018-10-14 19:25:32 +0300 | [diff] [blame] | 4437 | /* On error path it might already be dropped */ |
| 4438 | if (tline->a.mac) |
| 4439 | tline->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4440 | tline = delete_Token(tline); |
| 4441 | } else { |
| 4442 | t = *tail = tline; |
| 4443 | tline = tline->next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4444 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4445 | t->next = NULL; |
| 4446 | tail = &t->next; |
| 4447 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4448 | } |
| 4449 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4450 | /* |
| 4451 | * 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] | 4452 | * after expansion (they can't be produced by tokenize()). The successive |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4453 | * TOK_IDs should be concatenated. |
| 4454 | * Also we look for %+ tokens and concatenate the tokens before and after |
| 4455 | * them (without white spaces in between). |
| 4456 | */ |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4457 | if (expanded) { |
Cyrill Gorcunov | c6a742c | 2011-06-27 01:23:09 +0400 | [diff] [blame] | 4458 | const struct tokseq_match t[] = { |
| 4459 | { |
| 4460 | PP_CONCAT_MASK(TOK_ID) | |
| 4461 | PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */ |
| 4462 | PP_CONCAT_MASK(TOK_ID) | |
| 4463 | PP_CONCAT_MASK(TOK_PREPROC_ID) | |
| 4464 | PP_CONCAT_MASK(TOK_NUMBER) /* tail */ |
| 4465 | } |
| 4466 | }; |
| 4467 | if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) { |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4468 | /* |
| 4469 | * If we concatenated something, *and* we had previously expanded |
| 4470 | * an actual macro, scan the lines again for macros... |
| 4471 | */ |
| 4472 | tline = thead; |
| 4473 | expanded = false; |
| 4474 | goto again; |
| 4475 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4476 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4477 | |
Cyrill Gorcunov | bd38c8f | 2009-11-21 11:11:23 +0300 | [diff] [blame] | 4478 | err: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4479 | if (org_tline) { |
| 4480 | if (thead) { |
| 4481 | *org_tline = *thead; |
| 4482 | /* since we just gave text to org_line, don't free it */ |
| 4483 | thead->text = NULL; |
| 4484 | delete_Token(thead); |
| 4485 | } else { |
| 4486 | /* the expression expanded to empty line; |
| 4487 | we can't return NULL for some reasons |
| 4488 | we just set the line to a single WHITESPACE token. */ |
| 4489 | memset(org_tline, 0, sizeof(*org_tline)); |
| 4490 | org_tline->text = NULL; |
| 4491 | org_tline->type = TOK_WHITESPACE; |
| 4492 | } |
| 4493 | thead = org_tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4494 | } |
| 4495 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4496 | return thead; |
| 4497 | } |
| 4498 | |
| 4499 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4500 | * Similar to expand_smacro but used exclusively with macro identifiers |
| 4501 | * right before they are fetched in. The reason is that there can be |
| 4502 | * identifiers consisting of several subparts. We consider that if there |
| 4503 | * are more than one element forming the name, user wants a expansion, |
| 4504 | * otherwise it will be left as-is. Example: |
| 4505 | * |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4506 | * %define %$abc cde |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4507 | * |
| 4508 | * the identifier %$abc will be left as-is so that the handler for %define |
| 4509 | * will suck it and define the corresponding value. Other case: |
| 4510 | * |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4511 | * %define _%$abc cde |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4512 | * |
| 4513 | * In this case user wants name to be expanded *before* %define starts |
| 4514 | * working, so we'll expand %$abc into something (if it has a value; |
| 4515 | * otherwise it will be left as-is) then concatenate all successive |
| 4516 | * PP_IDs into one. |
| 4517 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4518 | static Token *expand_id(Token * tline) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4519 | { |
| 4520 | Token *cur, *oldnext = NULL; |
| 4521 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4522 | if (!tline || !tline->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4523 | return tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4524 | |
| 4525 | cur = tline; |
| 4526 | while (cur->next && |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4527 | (cur->next->type == TOK_ID || |
| 4528 | cur->next->type == TOK_PREPROC_ID |
| 4529 | || cur->next->type == TOK_NUMBER)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4530 | cur = cur->next; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4531 | |
| 4532 | /* If identifier consists of just one token, don't expand */ |
| 4533 | if (cur == tline) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4534 | return tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4535 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4536 | if (cur) { |
| 4537 | oldnext = cur->next; /* Detach the tail past identifier */ |
| 4538 | cur->next = NULL; /* so that expand_smacro stops here */ |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4539 | } |
| 4540 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4541 | tline = expand_smacro(tline); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4542 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4543 | if (cur) { |
| 4544 | /* expand_smacro possibly changhed tline; re-scan for EOL */ |
| 4545 | cur = tline; |
| 4546 | while (cur && cur->next) |
| 4547 | cur = cur->next; |
| 4548 | if (cur) |
| 4549 | cur->next = oldnext; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4550 | } |
| 4551 | |
| 4552 | return tline; |
| 4553 | } |
| 4554 | |
| 4555 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4556 | * Determine whether the given line constitutes a multi-line macro |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4557 | * 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] | 4558 | * to check for an initial label - that's taken care of in |
| 4559 | * expand_mmacro - but must check numbers of parameters. Guaranteed |
| 4560 | * to be called with tline->type == TOK_ID, so the putative macro |
| 4561 | * name is easy to find. |
| 4562 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4563 | static MMacro *is_mmacro(Token * tline, Token *** params_array) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4564 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4565 | MMacro *head, *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4566 | Token **params; |
| 4567 | int nparam; |
| 4568 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4569 | head = (MMacro *) hash_findix(&mmacros, tline->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4570 | |
| 4571 | /* |
| 4572 | * Efficiency: first we see if any macro exists with the given |
| 4573 | * name. If not, we can return NULL immediately. _Then_ we |
| 4574 | * count the parameters, and then we look further along the |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4575 | * list if necessary to find the proper MMacro. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4576 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4577 | list_for_each(m, head) |
| 4578 | if (!mstrcmp(m->name, tline->text, m->casesense)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4579 | break; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4580 | if (!m) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4581 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4582 | |
| 4583 | /* |
| 4584 | * OK, we have a potential macro. Count and demarcate the |
| 4585 | * parameters. |
| 4586 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4587 | count_mmac_params(tline->next, &nparam, ¶ms); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4588 | |
| 4589 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4590 | * 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] | 4591 | * structure that handles this number. |
| 4592 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4593 | while (m) { |
| 4594 | if (m->nparam_min <= nparam |
| 4595 | && (m->plus || nparam <= m->nparam_max)) { |
| 4596 | /* |
| 4597 | * This one is right. Just check if cycle removal |
| 4598 | * prohibits us using it before we actually celebrate... |
| 4599 | */ |
| 4600 | if (m->in_progress > m->max_depth) { |
| 4601 | if (m->max_depth > 0) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4602 | nasm_error(ERR_WARNING, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4603 | "reached maximum recursion depth of %i", |
| 4604 | m->max_depth); |
| 4605 | } |
| 4606 | nasm_free(params); |
| 4607 | return NULL; |
| 4608 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4609 | /* |
| 4610 | * It's right, and we can use it. Add its default |
| 4611 | * parameters to the end of our list if necessary. |
| 4612 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4613 | if (m->defaults && nparam < m->nparam_min + m->ndefs) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4614 | params = |
| 4615 | nasm_realloc(params, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4616 | ((m->nparam_min + m->ndefs + |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4617 | 1) * sizeof(*params))); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4618 | while (nparam < m->nparam_min + m->ndefs) { |
| 4619 | params[nparam] = m->defaults[nparam - m->nparam_min]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4620 | nparam++; |
| 4621 | } |
| 4622 | } |
| 4623 | /* |
| 4624 | * If we've gone over the maximum parameter count (and |
| 4625 | * we're in Plus mode), ignore parameters beyond |
| 4626 | * nparam_max. |
| 4627 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4628 | if (m->plus && nparam > m->nparam_max) |
| 4629 | nparam = m->nparam_max; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4630 | /* |
| 4631 | * Then terminate the parameter list, and leave. |
| 4632 | */ |
| 4633 | if (!params) { /* need this special case */ |
| 4634 | params = nasm_malloc(sizeof(*params)); |
| 4635 | nparam = 0; |
| 4636 | } |
| 4637 | params[nparam] = NULL; |
| 4638 | *params_array = params; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4639 | return m; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4640 | } |
| 4641 | /* |
| 4642 | * This one wasn't right: look for the next one with the |
| 4643 | * same name. |
| 4644 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4645 | list_for_each(m, m->next) |
| 4646 | if (!mstrcmp(m->name, tline->text, m->casesense)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4647 | break; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4648 | } |
| 4649 | |
| 4650 | /* |
| 4651 | * After all that, we didn't find one with the right number of |
| 4652 | * parameters. Issue a warning, and fail to expand the macro. |
| 4653 | */ |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4654 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4655 | "macro `%s' exists, but not taking %d parameters", |
| 4656 | tline->text, nparam); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4657 | nasm_free(params); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4658 | return NULL; |
| 4659 | } |
| 4660 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4661 | |
| 4662 | /* |
| 4663 | * Save MMacro invocation specific fields in |
| 4664 | * preparation for a recursive macro expansion |
| 4665 | */ |
| 4666 | static void push_mmacro(MMacro *m) |
| 4667 | { |
| 4668 | MMacroInvocation *i; |
| 4669 | |
| 4670 | i = nasm_malloc(sizeof(MMacroInvocation)); |
| 4671 | i->prev = m->prev; |
| 4672 | i->params = m->params; |
| 4673 | i->iline = m->iline; |
| 4674 | i->nparam = m->nparam; |
| 4675 | i->rotate = m->rotate; |
| 4676 | i->paramlen = m->paramlen; |
| 4677 | i->unique = m->unique; |
| 4678 | i->condcnt = m->condcnt; |
| 4679 | m->prev = i; |
| 4680 | } |
| 4681 | |
| 4682 | |
| 4683 | /* |
| 4684 | * Restore MMacro invocation specific fields that were |
| 4685 | * saved during a previous recursive macro expansion |
| 4686 | */ |
| 4687 | static void pop_mmacro(MMacro *m) |
| 4688 | { |
| 4689 | MMacroInvocation *i; |
| 4690 | |
| 4691 | if (m->prev) { |
| 4692 | i = m->prev; |
| 4693 | m->prev = i->prev; |
| 4694 | m->params = i->params; |
| 4695 | m->iline = i->iline; |
| 4696 | m->nparam = i->nparam; |
| 4697 | m->rotate = i->rotate; |
| 4698 | m->paramlen = i->paramlen; |
| 4699 | m->unique = i->unique; |
| 4700 | m->condcnt = i->condcnt; |
| 4701 | nasm_free(i); |
| 4702 | } |
| 4703 | } |
| 4704 | |
| 4705 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4706 | /* |
| 4707 | * Expand the multi-line macro call made by the given line, if |
| 4708 | * 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] | 4709 | * istk->expansion and return 1. Otherwise return 0. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4710 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4711 | static int expand_mmacro(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4712 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4713 | Token *startline = tline; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4714 | Token *label = NULL; |
| 4715 | int dont_prepend = 0; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4716 | Token **params, *t, *tt; |
| 4717 | MMacro *m; |
| 4718 | Line *l, *ll; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4719 | int i, nparam, *paramlen; |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 4720 | const char *mname; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4721 | |
| 4722 | t = tline; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4723 | skip_white_(t); |
H. Peter Anvin | ce2233b | 2008-05-25 21:57:00 -0700 | [diff] [blame] | 4724 | /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */ |
H. Peter Anvin | dce1e2f | 2002-04-30 21:06:37 +0000 | [diff] [blame] | 4725 | 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] | 4726 | return 0; |
| 4727 | m = is_mmacro(t, ¶ms); |
| 4728 | if (m) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4729 | mname = t->text; |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 4730 | } else { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4731 | Token *last; |
| 4732 | /* |
| 4733 | * We have an id which isn't a macro call. We'll assume |
| 4734 | * it might be a label; we'll also check to see if a |
| 4735 | * colon follows it. Then, if there's another id after |
| 4736 | * that lot, we'll check it again for macro-hood. |
| 4737 | */ |
| 4738 | label = last = t; |
| 4739 | t = t->next; |
| 4740 | if (tok_type_(t, TOK_WHITESPACE)) |
| 4741 | last = t, t = t->next; |
| 4742 | if (tok_is_(t, ":")) { |
| 4743 | dont_prepend = 1; |
| 4744 | last = t, t = t->next; |
| 4745 | if (tok_type_(t, TOK_WHITESPACE)) |
| 4746 | last = t, t = t->next; |
| 4747 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4748 | if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, ¶ms))) |
| 4749 | return 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4750 | last->next = NULL; |
Keith Kanios | 891775e | 2009-07-11 06:08:54 -0500 | [diff] [blame] | 4751 | mname = t->text; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4752 | tline = t; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4753 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4754 | |
| 4755 | /* |
| 4756 | * Fix up the parameters: this involves stripping leading and |
| 4757 | * trailing whitespace, then stripping braces if they are |
| 4758 | * present. |
| 4759 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4760 | for (nparam = 0; params[nparam]; nparam++) ; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4761 | paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4762 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4763 | for (i = 0; params[i]; i++) { |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4764 | int brace = 0; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4765 | int comma = (!m->plus || i < nparam - 1); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4766 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4767 | t = params[i]; |
| 4768 | skip_white_(t); |
| 4769 | if (tok_is_(t, "{")) |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4770 | t = t->next, brace++, comma = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4771 | params[i] = t; |
| 4772 | paramlen[i] = 0; |
| 4773 | while (t) { |
| 4774 | if (comma && t->type == TOK_OTHER && !strcmp(t->text, ",")) |
| 4775 | break; /* ... because we have hit a comma */ |
| 4776 | if (comma && t->type == TOK_WHITESPACE |
| 4777 | && tok_is_(t->next, ",")) |
| 4778 | break; /* ... or a space then a comma */ |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4779 | if (brace && t->type == TOK_OTHER) { |
| 4780 | if (t->text[0] == '{') |
| 4781 | brace++; /* ... or a nested opening brace */ |
| 4782 | else if (t->text[0] == '}') |
| 4783 | if (!--brace) |
| 4784 | break; /* ... or a brace */ |
| 4785 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4786 | t = t->next; |
| 4787 | paramlen[i]++; |
| 4788 | } |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4789 | if (brace) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4790 | nasm_error(ERR_NONFATAL, "macro params should be enclosed in braces"); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4791 | } |
| 4792 | |
| 4793 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4794 | * OK, we have a MMacro structure together with a set of |
| 4795 | * parameters. We must now go through the expansion and push |
| 4796 | * copies of each Line on to istk->expansion. Substitution of |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4797 | * parameter tokens and macro-local tokens doesn't get done |
| 4798 | * until the single-line macro substitution process; this is |
| 4799 | * because delaying them allows us to change the semantics |
| 4800 | * later through %rotate. |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4801 | * |
| 4802 | * First, push an end marker on to istk->expansion, mark this |
| 4803 | * macro as in progress, and set up its invocation-specific |
| 4804 | * variables. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4805 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4806 | ll = nasm_malloc(sizeof(Line)); |
| 4807 | ll->next = istk->expansion; |
| 4808 | ll->finishes = m; |
| 4809 | ll->first = NULL; |
| 4810 | istk->expansion = ll; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4811 | |
| 4812 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4813 | * Save the previous MMacro expansion in the case of |
| 4814 | * macro recursion |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4815 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4816 | if (m->max_depth && m->in_progress) |
| 4817 | push_mmacro(m); |
| 4818 | |
| 4819 | m->in_progress ++; |
| 4820 | m->params = params; |
| 4821 | m->iline = tline; |
| 4822 | m->nparam = nparam; |
| 4823 | m->rotate = 0; |
| 4824 | m->paramlen = paramlen; |
| 4825 | m->unique = unique++; |
| 4826 | m->lineno = 0; |
| 4827 | m->condcnt = 0; |
| 4828 | |
| 4829 | m->next_active = istk->mstk; |
| 4830 | istk->mstk = m; |
| 4831 | |
| 4832 | list_for_each(l, m->expansion) { |
| 4833 | Token **tail; |
| 4834 | |
| 4835 | ll = nasm_malloc(sizeof(Line)); |
| 4836 | ll->finishes = NULL; |
| 4837 | ll->next = istk->expansion; |
| 4838 | istk->expansion = ll; |
| 4839 | tail = &ll->first; |
| 4840 | |
| 4841 | list_for_each(t, l->first) { |
| 4842 | Token *x = t; |
| 4843 | switch (t->type) { |
| 4844 | case TOK_PREPROC_Q: |
| 4845 | tt = *tail = new_Token(NULL, TOK_ID, mname, 0); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4846 | break; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4847 | case TOK_PREPROC_QQ: |
| 4848 | tt = *tail = new_Token(NULL, TOK_ID, m->name, 0); |
| 4849 | break; |
| 4850 | case TOK_PREPROC_ID: |
| 4851 | if (t->text[1] == '0' && t->text[2] == '0') { |
| 4852 | dont_prepend = -1; |
| 4853 | x = label; |
| 4854 | if (!x) |
| 4855 | continue; |
| 4856 | } |
| 4857 | /* fall through */ |
| 4858 | default: |
| 4859 | tt = *tail = new_Token(NULL, x->type, x->text, 0); |
| 4860 | break; |
| 4861 | } |
| 4862 | tail = &tt->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4863 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4864 | *tail = NULL; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4865 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4866 | |
| 4867 | /* |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4868 | * If we had a label, push it on as the first line of |
| 4869 | * the macro expansion. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4870 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4871 | if (label) { |
| 4872 | if (dont_prepend < 0) |
| 4873 | free_tlist(startline); |
| 4874 | else { |
| 4875 | ll = nasm_malloc(sizeof(Line)); |
| 4876 | ll->finishes = NULL; |
| 4877 | ll->next = istk->expansion; |
| 4878 | istk->expansion = ll; |
| 4879 | ll->first = startline; |
| 4880 | if (!dont_prepend) { |
| 4881 | while (label->next) |
| 4882 | label = label->next; |
| 4883 | label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4884 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4885 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4886 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4887 | |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 4888 | lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4889 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4890 | return 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4891 | } |
| 4892 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4893 | /* |
| 4894 | * This function adds macro names to error messages, and suppresses |
| 4895 | * them if necessary. |
| 4896 | */ |
| 4897 | static void pp_verror(int severity, const char *fmt, va_list arg) |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4898 | { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4899 | char buff[BUFSIZ]; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4900 | MMacro *mmac = NULL; |
| 4901 | int delta = 0; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4902 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4903 | /* |
| 4904 | * If we're in a dead branch of IF or something like it, ignore the error. |
| 4905 | * However, because %else etc are evaluated in the state context |
| 4906 | * of the previous branch, errors might get lost: |
| 4907 | * %if 0 ... %else trailing garbage ... %endif |
| 4908 | * So %else etc should set the ERR_PP_PRECOND flag. |
| 4909 | */ |
| 4910 | if ((severity & ERR_MASK) < ERR_FATAL && |
| 4911 | istk && istk->conds && |
| 4912 | ((severity & ERR_PP_PRECOND) ? |
| 4913 | istk->conds->state == COND_NEVER : |
H. Peter Anvin | eb6653f | 2016-04-05 13:03:10 -0700 | [diff] [blame] | 4914 | !emitting(istk->conds->state))) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4915 | return; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4916 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4917 | /* get %macro name */ |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4918 | if (!(severity & ERR_NOFILE) && istk && istk->mstk) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4919 | mmac = istk->mstk; |
| 4920 | /* but %rep blocks should be skipped */ |
| 4921 | while (mmac && !mmac->name) |
| 4922 | mmac = mmac->next_active, delta++; |
Cyrill Gorcunov | 9900c6b | 2011-10-09 18:58:46 +0400 | [diff] [blame] | 4923 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4924 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4925 | if (mmac) { |
| 4926 | vsnprintf(buff, sizeof(buff), fmt, arg); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4927 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4928 | nasm_set_verror(real_verror); |
| 4929 | nasm_error(severity, "(%s:%d) %s", |
| 4930 | mmac->name, mmac->lineno - delta, buff); |
| 4931 | nasm_set_verror(pp_verror); |
| 4932 | } else { |
| 4933 | real_verror(severity, fmt, arg); |
| 4934 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4935 | } |
| 4936 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4937 | static void |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 4938 | pp_reset(const char *file, int apass, struct strlist *dep_list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4939 | { |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 4940 | Token *t; |
| 4941 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4942 | cstk = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4943 | istk = nasm_malloc(sizeof(Include)); |
| 4944 | istk->next = NULL; |
| 4945 | istk->conds = NULL; |
| 4946 | istk->expansion = NULL; |
| 4947 | istk->mstk = NULL; |
H. Peter Anvin | 3e83cec | 2016-05-25 04:28:46 -0700 | [diff] [blame] | 4948 | istk->fp = nasm_open_read(file, NF_TEXT); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4949 | istk->fname = NULL; |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 4950 | src_set(0, file); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4951 | istk->lineinc = 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4952 | if (!istk->fp) |
Cyrill Gorcunov | c3527dd | 2018-11-24 22:17:47 +0300 | [diff] [blame^] | 4953 | nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4954 | defining = NULL; |
Charles Crayne | d4200be | 2008-07-12 16:42:33 -0700 | [diff] [blame] | 4955 | nested_mac_count = 0; |
| 4956 | nested_rep_count = 0; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 4957 | init_macros(); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4958 | unique = 0; |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 4959 | deplist = dep_list; |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 4960 | |
| 4961 | if (tasm_compatible_mode) |
| 4962 | pp_add_stdmac(nasm_stdmac_tasm); |
| 4963 | |
| 4964 | pp_add_stdmac(nasm_stdmac_nasm); |
| 4965 | pp_add_stdmac(nasm_stdmac_version); |
| 4966 | |
Cyrill Gorcunov | 15ce78f | 2017-01-06 20:21:28 +0300 | [diff] [blame] | 4967 | if (extrastdmac) |
| 4968 | pp_add_stdmac(extrastdmac); |
| 4969 | |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 4970 | stdmacpos = stdmacros[0]; |
| 4971 | stdmacnext = &stdmacros[1]; |
| 4972 | |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 4973 | do_predef = true; |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 4974 | |
| 4975 | /* |
| 4976 | * 0 for dependencies, 1 for preparatory passes, 2 for final pass. |
| 4977 | * The caller, however, will also pass in 3 for preprocess-only so |
| 4978 | * we can set __PASS__ accordingly. |
| 4979 | */ |
| 4980 | pass = apass > 2 ? 2 : apass; |
| 4981 | |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 4982 | strlist_add(deplist, file); |
H. Peter Anvin (Intel) | f7106d0 | 2018-10-25 12:33:58 -0700 | [diff] [blame] | 4983 | |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 4984 | /* |
| 4985 | * Define the __PASS__ macro. This is defined here unlike |
| 4986 | * all the other builtins, because it is special -- it varies between |
| 4987 | * passes. |
| 4988 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4989 | t = nasm_malloc(sizeof(*t)); |
| 4990 | t->next = NULL; |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 4991 | make_tok_num(t, apass); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4992 | t->a.mac = NULL; |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 4993 | define_smacro(NULL, "__PASS__", true, 0, t); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4994 | } |
| 4995 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 4996 | static void pp_init(void) |
| 4997 | { |
| 4998 | hash_init(&FileHash, HASH_MEDIUM); |
| 4999 | } |
| 5000 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5001 | static char *pp_getline(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5002 | { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5003 | char *line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5004 | Token *tline; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5005 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5006 | real_verror = nasm_set_verror(pp_verror); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5007 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5008 | while (1) { |
| 5009 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5010 | * Fetch a tokenized line, either from the macro-expansion |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5011 | * buffer or from the input file. |
| 5012 | */ |
| 5013 | tline = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5014 | while (istk->expansion && istk->expansion->finishes) { |
| 5015 | Line *l = istk->expansion; |
| 5016 | if (!l->finishes->name && l->finishes->in_progress > 1) { |
| 5017 | Line *ll; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5018 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5019 | /* |
| 5020 | * This is a macro-end marker for a macro with no |
| 5021 | * name, which means it's not really a macro at all |
| 5022 | * but a %rep block, and the `in_progress' field is |
| 5023 | * more than 1, meaning that we still need to |
| 5024 | * repeat. (1 means the natural last repetition; 0 |
| 5025 | * means termination by %exitrep.) We have |
| 5026 | * therefore expanded up to the %endrep, and must |
| 5027 | * push the whole block on to the expansion buffer |
| 5028 | * again. We don't bother to remove the macro-end |
| 5029 | * marker: we'd only have to generate another one |
| 5030 | * if we did. |
| 5031 | */ |
| 5032 | l->finishes->in_progress--; |
| 5033 | list_for_each(l, l->finishes->expansion) { |
| 5034 | Token *t, *tt, **tail; |
| 5035 | |
| 5036 | ll = nasm_malloc(sizeof(Line)); |
| 5037 | ll->next = istk->expansion; |
| 5038 | ll->finishes = NULL; |
| 5039 | ll->first = NULL; |
| 5040 | tail = &ll->first; |
| 5041 | |
| 5042 | list_for_each(t, l->first) { |
| 5043 | if (t->text || t->type == TOK_WHITESPACE) { |
| 5044 | tt = *tail = new_Token(NULL, t->type, t->text, 0); |
| 5045 | tail = &tt->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5046 | } |
| 5047 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5048 | |
| 5049 | istk->expansion = ll; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5050 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5051 | } else { |
| 5052 | /* |
| 5053 | * Check whether a `%rep' was started and not ended |
| 5054 | * within this macro expansion. This can happen and |
| 5055 | * should be detected. It's a fatal error because |
| 5056 | * I'm too confused to work out how to recover |
| 5057 | * sensibly from it. |
| 5058 | */ |
| 5059 | if (defining) { |
| 5060 | if (defining->name) |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 5061 | nasm_panic("defining with name in expansion"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5062 | else if (istk->mstk->name) |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 5063 | nasm_fatal("`%%rep' without `%%endrep' within" |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5064 | " expansion of macro `%s'", |
| 5065 | istk->mstk->name); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5066 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5067 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5068 | /* |
| 5069 | * FIXME: investigate the relationship at this point between |
| 5070 | * istk->mstk and l->finishes |
| 5071 | */ |
| 5072 | { |
| 5073 | MMacro *m = istk->mstk; |
| 5074 | istk->mstk = m->next_active; |
| 5075 | if (m->name) { |
| 5076 | /* |
| 5077 | * This was a real macro call, not a %rep, and |
| 5078 | * therefore the parameter information needs to |
| 5079 | * be freed. |
| 5080 | */ |
| 5081 | if (m->prev) { |
| 5082 | pop_mmacro(m); |
| 5083 | l->finishes->in_progress --; |
| 5084 | } else { |
| 5085 | nasm_free(m->params); |
| 5086 | free_tlist(m->iline); |
| 5087 | nasm_free(m->paramlen); |
| 5088 | l->finishes->in_progress = 0; |
| 5089 | } |
Adam Majer | 91e7240 | 2017-07-25 10:42:01 +0200 | [diff] [blame] | 5090 | } |
| 5091 | |
| 5092 | /* |
| 5093 | * FIXME It is incorrect to always free_mmacro here. |
| 5094 | * It leads to usage-after-free. |
| 5095 | * |
| 5096 | * https://bugzilla.nasm.us/show_bug.cgi?id=3392414 |
| 5097 | */ |
| 5098 | #if 0 |
| 5099 | else |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5100 | free_mmacro(m); |
Adam Majer | 91e7240 | 2017-07-25 10:42:01 +0200 | [diff] [blame] | 5101 | #endif |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5102 | } |
| 5103 | istk->expansion = l->next; |
| 5104 | nasm_free(l); |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 5105 | lfmt->downlevel(LIST_MACRO); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5106 | } |
| 5107 | } |
| 5108 | while (1) { /* until we get a line we can use */ |
| 5109 | |
| 5110 | if (istk->expansion) { /* from a macro expansion */ |
| 5111 | char *p; |
| 5112 | Line *l = istk->expansion; |
| 5113 | if (istk->mstk) |
| 5114 | istk->mstk->lineno++; |
| 5115 | tline = l->first; |
| 5116 | istk->expansion = l->next; |
| 5117 | nasm_free(l); |
| 5118 | p = detoken(tline, false); |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 5119 | lfmt->line(LIST_MACRO, p); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5120 | nasm_free(p); |
| 5121 | break; |
| 5122 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5123 | line = read_line(); |
| 5124 | if (line) { /* from the current input file */ |
| 5125 | line = prepreproc(line); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5126 | tline = tokenize(line); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5127 | nasm_free(line); |
| 5128 | break; |
| 5129 | } |
| 5130 | /* |
| 5131 | * The current file has ended; work down the istk |
| 5132 | */ |
| 5133 | { |
| 5134 | Include *i = istk; |
| 5135 | fclose(i->fp); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5136 | if (i->conds) { |
| 5137 | /* nasm_error can't be conditionally suppressed */ |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 5138 | nasm_fatal("expected `%%endif' before end of file"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5139 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5140 | /* 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] | 5141 | if (i->next) |
| 5142 | src_set(i->lineno, i->fname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5143 | istk = i->next; |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 5144 | lfmt->downlevel(LIST_INCLUDE); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5145 | nasm_free(i); |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5146 | if (!istk) { |
| 5147 | line = NULL; |
| 5148 | goto done; |
| 5149 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5150 | if (istk->expansion && istk->expansion->finishes) |
| 5151 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5152 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5153 | } |
| 5154 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5155 | /* |
| 5156 | * We must expand MMacro parameters and MMacro-local labels |
| 5157 | * _before_ we plunge into directive processing, to cope |
| 5158 | * with things like `%define something %1' such as STRUC |
| 5159 | * uses. Unless we're _defining_ a MMacro, in which case |
| 5160 | * those tokens should be left alone to go into the |
| 5161 | * definition; and unless we're in a non-emitting |
| 5162 | * condition, in which case we don't want to meddle with |
| 5163 | * anything. |
| 5164 | */ |
| 5165 | if (!defining && !(istk->conds && !emitting(istk->conds->state)) |
| 5166 | && !(istk->mstk && !istk->mstk->in_progress)) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5167 | tline = expand_mmac_params(tline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5168 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5169 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5170 | /* |
| 5171 | * Check the line to see if it's a preprocessor directive. |
| 5172 | */ |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 5173 | if (do_directive(tline, &line) == DIRECTIVE_FOUND) { |
| 5174 | if (line) |
| 5175 | break; /* Directive generated output */ |
| 5176 | else |
| 5177 | continue; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5178 | } else if (defining) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5179 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5180 | * We're defining a multi-line macro. We emit nothing |
| 5181 | * at all, and just |
| 5182 | * shove the tokenized line on to the macro definition. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5183 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5184 | Line *l = nasm_malloc(sizeof(Line)); |
| 5185 | l->next = defining->expansion; |
| 5186 | l->first = tline; |
| 5187 | l->finishes = NULL; |
| 5188 | defining->expansion = l; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5189 | continue; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5190 | } else if (istk->conds && !emitting(istk->conds->state)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5191 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5192 | * We're in a non-emitting branch of a condition block. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5193 | * Emit nothing at all, not even a blank line: when we |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5194 | * emerge from the condition we'll give a line-number |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5195 | * directive so we keep our place correctly. |
| 5196 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5197 | free_tlist(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5198 | continue; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5199 | } else if (istk->mstk && !istk->mstk->in_progress) { |
| 5200 | /* |
| 5201 | * We're in a %rep block which has been terminated, so |
| 5202 | * we're walking through to the %endrep without |
| 5203 | * emitting anything. Emit nothing at all, not even a |
| 5204 | * blank line: when we emerge from the %rep block we'll |
| 5205 | * give a line-number directive so we keep our place |
| 5206 | * correctly. |
| 5207 | */ |
| 5208 | free_tlist(tline); |
| 5209 | continue; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5210 | } else { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5211 | tline = expand_smacro(tline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5212 | if (!expand_mmacro(tline)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5213 | /* |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5214 | * De-tokenize the line again, and emit it. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5215 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5216 | line = detoken(tline, true); |
| 5217 | free_tlist(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5218 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5219 | } else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5220 | continue; /* expand_mmacro calls free_tlist */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5221 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5222 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5223 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5224 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5225 | done: |
| 5226 | nasm_set_verror(real_verror); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5227 | return line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5228 | } |
| 5229 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5230 | static void pp_cleanup(int pass) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5231 | { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5232 | real_verror = nasm_set_verror(pp_verror); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5233 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5234 | if (defining) { |
| 5235 | if (defining->name) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5236 | nasm_error(ERR_NONFATAL, |
| 5237 | "end of file while still defining macro `%s'", |
| 5238 | defining->name); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5239 | } else { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5240 | nasm_error(ERR_NONFATAL, "end of file while still in %%rep"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5241 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5242 | |
| 5243 | free_mmacro(defining); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5244 | defining = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5245 | } |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5246 | |
| 5247 | nasm_set_verror(real_verror); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5248 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5249 | while (cstk) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5250 | ctx_pop(); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 5251 | free_macros(); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5252 | while (istk) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5253 | Include *i = istk; |
| 5254 | istk = istk->next; |
| 5255 | fclose(i->fp); |
Cyrill Gorcunov | 8dcfd88 | 2011-03-03 09:18:56 +0300 | [diff] [blame] | 5256 | nasm_free(i); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5257 | } |
| 5258 | while (cstk) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5259 | ctx_pop(); |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 5260 | src_set_fname(NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5261 | if (pass == 0) { |
| 5262 | free_llist(predef); |
Cyrill Gorcunov | dae24d7 | 2014-06-28 10:17:39 +0400 | [diff] [blame] | 5263 | predef = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5264 | delete_Blocks(); |
Cyrill Gorcunov | dae24d7 | 2014-06-28 10:17:39 +0400 | [diff] [blame] | 5265 | freeTokens = NULL; |
Cyrill Gorcunov | 8c0666b | 2018-11-24 14:33:48 +0300 | [diff] [blame] | 5266 | ipath_list = NULL; |
H. Peter Anvin | 11dfa1a | 2008-07-02 18:11:04 -0700 | [diff] [blame] | 5267 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5268 | } |
| 5269 | |
Cyrill Gorcunov | 8c0666b | 2018-11-24 14:33:48 +0300 | [diff] [blame] | 5270 | static void pp_include_path(struct strlist *list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5271 | { |
Cyrill Gorcunov | 8c0666b | 2018-11-24 14:33:48 +0300 | [diff] [blame] | 5272 | ipath_list = list; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5273 | } |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5274 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5275 | static void pp_pre_include(char *fname) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5276 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5277 | Token *inc, *space, *name; |
| 5278 | Line *l; |
| 5279 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5280 | name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0); |
| 5281 | space = new_Token(name, TOK_WHITESPACE, NULL, 0); |
| 5282 | inc = new_Token(space, TOK_PREPROC_ID, "%include", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5283 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5284 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5285 | l->next = predef; |
| 5286 | l->first = inc; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5287 | l->finishes = NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5288 | predef = l; |
| 5289 | } |
| 5290 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5291 | static void pp_pre_define(char *definition) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5292 | { |
| 5293 | Token *def, *space; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5294 | Line *l; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5295 | char *equals; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5296 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5297 | real_verror = nasm_set_verror(pp_verror); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5298 | |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5299 | equals = strchr(definition, '='); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5300 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 5301 | def = new_Token(space, TOK_PREPROC_ID, "%define", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5302 | if (equals) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5303 | *equals = ' '; |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5304 | space->next = tokenize(definition); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5305 | if (equals) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5306 | *equals = '='; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5307 | |
Cyrill Gorcunov | 6d42e9b | 2015-02-08 11:07:17 +0300 | [diff] [blame] | 5308 | if (space->next->type != TOK_PREPROC_ID && |
| 5309 | space->next->type != TOK_ID) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5310 | nasm_error(ERR_WARNING, "pre-defining non ID `%s\'\n", definition); |
Cyrill Gorcunov | 6d42e9b | 2015-02-08 11:07:17 +0300 | [diff] [blame] | 5311 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5312 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5313 | l->next = predef; |
| 5314 | l->first = def; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5315 | l->finishes = NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5316 | predef = l; |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5317 | |
| 5318 | nasm_set_verror(real_verror); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5319 | } |
| 5320 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5321 | static void pp_pre_undefine(char *definition) |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5322 | { |
| 5323 | Token *def, *space; |
| 5324 | Line *l; |
| 5325 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5326 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 5327 | def = new_Token(space, TOK_PREPROC_ID, "%undef", 0); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5328 | space->next = tokenize(definition); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5329 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5330 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5331 | l->next = predef; |
| 5332 | l->first = def; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5333 | l->finishes = NULL; |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5334 | predef = l; |
| 5335 | } |
| 5336 | |
H. Peter Anvin | 0599034 | 2018-06-11 13:32:42 -0700 | [diff] [blame] | 5337 | /* Insert an early preprocessor command that doesn't need special handling */ |
| 5338 | static void pp_pre_command(const char *what, char *string) |
| 5339 | { |
| 5340 | char *cmd; |
| 5341 | Token *def, *space; |
| 5342 | Line *l; |
| 5343 | |
| 5344 | def = tokenize(string); |
| 5345 | if (what) { |
| 5346 | cmd = nasm_strcat(what[0] == '%' ? "" : "%", what); |
| 5347 | space = new_Token(def, TOK_WHITESPACE, NULL, 0); |
| 5348 | def = new_Token(space, TOK_PREPROC_ID, cmd, 0); |
| 5349 | } |
| 5350 | |
| 5351 | l = nasm_malloc(sizeof(Line)); |
| 5352 | l->next = predef; |
| 5353 | l->first = def; |
| 5354 | l->finishes = NULL; |
| 5355 | predef = l; |
| 5356 | } |
| 5357 | |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 5358 | static void pp_add_stdmac(macros_t *macros) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5359 | { |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 5360 | macros_t **mp; |
| 5361 | |
| 5362 | /* Find the end of the list and avoid duplicates */ |
| 5363 | for (mp = stdmacros; *mp; mp++) { |
| 5364 | if (*mp == macros) |
| 5365 | return; /* Nothing to do */ |
| 5366 | } |
| 5367 | |
| 5368 | nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]); |
| 5369 | |
| 5370 | *mp = macros; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 5371 | } |
| 5372 | |
Cyrill Gorcunov | 15ce78f | 2017-01-06 20:21:28 +0300 | [diff] [blame] | 5373 | static void pp_extra_stdmac(macros_t *macros) |
| 5374 | { |
| 5375 | extrastdmac = macros; |
| 5376 | } |
| 5377 | |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 5378 | static void make_tok_num(Token * tok, int64_t val) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5379 | { |
Cyrill Gorcunov | ce65274 | 2013-05-06 23:43:43 +0400 | [diff] [blame] | 5380 | char numbuf[32]; |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 5381 | snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5382 | tok->text = nasm_strdup(numbuf); |
| 5383 | tok->type = TOK_NUMBER; |
| 5384 | } |
| 5385 | |
H. Peter Anvin | 3736895 | 2016-05-09 14:10:32 -0700 | [diff] [blame] | 5386 | static void pp_list_one_macro(MMacro *m, int severity) |
| 5387 | { |
| 5388 | if (!m) |
| 5389 | return; |
| 5390 | |
| 5391 | /* We need to print the next_active list in reverse order */ |
| 5392 | pp_list_one_macro(m->next_active, severity); |
| 5393 | |
| 5394 | if (m->name && !m->nolist) { |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 5395 | src_set(m->xline + m->lineno, m->fname); |
H. Peter Anvin | 3736895 | 2016-05-09 14:10:32 -0700 | [diff] [blame] | 5396 | nasm_error(severity, "... from macro `%s' defined here", m->name); |
| 5397 | } |
| 5398 | } |
| 5399 | |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5400 | static void pp_error_list_macros(int severity) |
| 5401 | { |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5402 | int32_t saved_line; |
| 5403 | const char *saved_fname = NULL; |
| 5404 | |
| 5405 | severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY; |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 5406 | src_get(&saved_line, &saved_fname); |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5407 | |
Cyrill Gorcunov | 771d04e | 2016-05-10 23:27:03 +0300 | [diff] [blame] | 5408 | if (istk) |
| 5409 | pp_list_one_macro(istk->mstk, severity); |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5410 | |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 5411 | src_set(saved_line, saved_fname); |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5412 | } |
| 5413 | |
H. Peter Anvin | e746971 | 2016-02-18 02:20:59 -0800 | [diff] [blame] | 5414 | const struct preproc_ops nasmpp = { |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 5415 | pp_init, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5416 | pp_reset, |
| 5417 | pp_getline, |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5418 | pp_cleanup, |
Cyrill Gorcunov | 15ce78f | 2017-01-06 20:21:28 +0300 | [diff] [blame] | 5419 | pp_extra_stdmac, |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5420 | pp_pre_define, |
| 5421 | pp_pre_undefine, |
| 5422 | pp_pre_include, |
H. Peter Anvin | 0599034 | 2018-06-11 13:32:42 -0700 | [diff] [blame] | 5423 | pp_pre_command, |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5424 | pp_include_path, |
| 5425 | pp_error_list_macros, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5426 | }; |