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 | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 94 | typedef struct IncPath IncPath; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 95 | |
| 96 | /* |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 97 | * Note on the storage of both SMacro and MMacros: the hash table |
| 98 | * indexes them case-insensitively, and we then have to go through a |
| 99 | * linked list of potential case aliases (and, for MMacros, parameter |
| 100 | * ranges); this is to preserve the matching semantics of the earlier |
| 101 | * code. If the number of case aliases for a specific macro is a |
| 102 | * performance issue, you may want to reconsider your coding style. |
| 103 | */ |
| 104 | |
| 105 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 106 | * Store the definition of a single-line macro. |
| 107 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 108 | struct SMacro { |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 109 | SMacro *next; /* MUST BE FIRST - see free_smacro() */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 110 | char *name; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 111 | union { |
| 112 | Token *expansion; |
| 113 | Token *(*magic)(const SMacro *s, Token **params, int *paramsize); |
| 114 | } e; |
| 115 | bool *eval_param; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 116 | unsigned int nparam; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 117 | bool casesense; |
| 118 | bool magic; |
| 119 | bool in_progress; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 123 | * Store the definition of a multi-line macro. This is also used to |
| 124 | * store the interiors of `%rep...%endrep' blocks, which are |
| 125 | * effectively self-re-invoking multi-line macros which simply |
| 126 | * don't have a name or bother to appear in the hash tables. %rep |
| 127 | * blocks are signified by having a NULL `name' field. |
| 128 | * |
| 129 | * In a MMacro describing a `%rep' block, the `in_progress' field |
| 130 | * isn't merely boolean, but gives the number of repeats left to |
| 131 | * run. |
| 132 | * |
| 133 | * The `next' field is used for storing MMacros in hash tables; the |
| 134 | * `next_active' field is for stacking them on istk entries. |
| 135 | * |
| 136 | * When a MMacro is being expanded, `params', `iline', `nparam', |
| 137 | * `paramlen', `rotate' and `unique' are local to the invocation. |
| 138 | */ |
| 139 | struct MMacro { |
| 140 | MMacro *next; |
| 141 | MMacroInvocation *prev; /* previous invocation */ |
| 142 | char *name; |
| 143 | int nparam_min, nparam_max; |
| 144 | bool casesense; |
| 145 | bool plus; /* is the last parameter greedy? */ |
| 146 | bool nolist; /* is this macro listing-inhibited? */ |
| 147 | int64_t in_progress; /* is this macro currently being expanded? */ |
| 148 | int32_t max_depth; /* maximum number of recursive expansions allowed */ |
| 149 | Token *dlist; /* All defaults as one list */ |
| 150 | Token **defaults; /* Parameter default pointers */ |
| 151 | int ndefs; /* number of default parameters */ |
| 152 | Line *expansion; |
| 153 | |
| 154 | MMacro *next_active; |
| 155 | MMacro *rep_nest; /* used for nesting %rep */ |
| 156 | Token **params; /* actual parameters */ |
| 157 | Token *iline; /* invocation line */ |
| 158 | unsigned int nparam, rotate; |
| 159 | int *paramlen; |
| 160 | uint64_t unique; |
| 161 | int lineno; /* Current line number on expansion */ |
| 162 | uint64_t condcnt; /* number of if blocks... */ |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 163 | |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 164 | const char *fname; /* File where defined */ |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 165 | int32_t xline; /* First line in macro */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | |
| 169 | /* Store the definition of a multi-line macro, as defined in a |
| 170 | * previous recursive macro expansion. |
| 171 | */ |
| 172 | struct MMacroInvocation { |
| 173 | MMacroInvocation *prev; /* previous invocation */ |
| 174 | Token **params; /* actual parameters */ |
| 175 | Token *iline; /* invocation line */ |
| 176 | unsigned int nparam, rotate; |
| 177 | int *paramlen; |
| 178 | uint64_t unique; |
| 179 | uint64_t condcnt; |
| 180 | }; |
| 181 | |
| 182 | |
| 183 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 184 | * The context stack is composed of a linked list of these. |
| 185 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 186 | struct Context { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 187 | Context *next; |
| 188 | char *name; |
| 189 | struct hash_table localmac; |
| 190 | uint32_t number; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | /* |
| 194 | * This is the internal form which we break input lines up into. |
| 195 | * Typically stored in linked lists. |
| 196 | * |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 197 | * Note that `type' serves a double meaning: TOK_SMAC_START_PARAMS is |
| 198 | * not necessarily used as-is, but is also used to encode the number |
| 199 | * and expansion type of substituted parameter. So in the definition |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 200 | * |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 201 | * %define a(x,=y) ( (x) & ~(y) ) |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 202 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 203 | * the token representing `x' will have its type changed to |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 204 | * tok_smac_param(0) but the one representing `y' will be |
| 205 | * tok_smac_param(1); see the accessor functions below. |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 206 | * |
| 207 | * TOK_INTERNAL_STRING is a dirty hack: it's a single string token |
| 208 | * which doesn't need quotes around it. Used in the pre-include |
| 209 | * mechanism as an alternative to trying to find a sensible type of |
| 210 | * quote to use on the filename we were passed. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 211 | */ |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 212 | enum pp_token_type { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 213 | TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID, |
| 214 | TOK_PREPROC_ID, TOK_STRING, |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 215 | TOK_NUMBER, TOK_FLOAT, TOK_OTHER, |
H. Peter Anvin | 6c81f0a | 2008-05-25 21:46:17 -0700 | [diff] [blame] | 216 | TOK_INTERNAL_STRING, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 217 | TOK_PREPROC_Q, TOK_PREPROC_QQ, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 218 | TOK_PASTE, /* %+ */ |
| 219 | TOK_INDIRECT, /* %[...] */ |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 220 | TOK_SMAC_END, /* Marker for the end of smacro expansion */ |
| 221 | TOK_SMAC_START_PARAMS, /* MUST BE LAST IN THE LIST!!! */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 222 | TOK_MAX = INT_MAX /* Keep compiler from reducing the range */ |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 223 | }; |
| 224 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 225 | static inline enum pp_token_type tok_smac_param(int param) |
| 226 | { |
| 227 | return TOK_SMAC_START_PARAMS + param; |
| 228 | } |
| 229 | static int smac_nparam(enum pp_token_type toktype) |
| 230 | { |
| 231 | return toktype - TOK_SMAC_START_PARAMS; |
| 232 | } |
| 233 | static bool is_smac_param(enum pp_token_type toktype) |
| 234 | { |
| 235 | return toktype >= TOK_SMAC_START_PARAMS; |
| 236 | } |
| 237 | |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 238 | #define PP_CONCAT_MASK(x) (1 << (x)) |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 239 | #define PP_CONCAT_MATCH(t, mask) (PP_CONCAT_MASK((t)->type) & mask) |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 240 | |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 241 | struct tokseq_match { |
| 242 | int mask_head; |
| 243 | int mask_tail; |
| 244 | }; |
| 245 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 246 | struct Token { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 247 | Token *next; |
| 248 | char *text; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 249 | union { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 250 | SMacro *mac; /* associated macro for TOK_SMAC_END */ |
| 251 | size_t len; /* scratch length field */ |
| 252 | } a; /* Auxiliary data */ |
| 253 | enum pp_token_type type; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 254 | }; |
| 255 | |
| 256 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 257 | * Multi-line macro definitions are stored as a linked list of |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 258 | * these, which is essentially a container to allow several linked |
| 259 | * lists of Tokens. |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 260 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 261 | * Note that in this module, linked lists are treated as stacks |
| 262 | * wherever possible. For this reason, Lines are _pushed_ on to the |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 263 | * `expansion' field in MMacro structures, so that the linked list, |
| 264 | * if walked, would give the macro lines in reverse order; this |
| 265 | * means that we can walk the list when expanding a macro, and thus |
| 266 | * push the lines on to the `expansion' field in _istk_ in reverse |
| 267 | * order (so that when popped back off they are in the right |
| 268 | * order). It may seem cockeyed, and it relies on my design having |
| 269 | * an even number of steps in, but it works... |
| 270 | * |
| 271 | * Some of these structures, rather than being actual lines, are |
| 272 | * markers delimiting the end of the expansion of a given macro. |
| 273 | * This is for use in the cycle-tracking and %rep-handling code. |
| 274 | * Such structures have `finishes' non-NULL, and `first' NULL. All |
| 275 | * others have `finishes' NULL, but `first' may still be NULL if |
| 276 | * the line is blank. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 277 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 278 | struct Line { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 279 | Line *next; |
| 280 | MMacro *finishes; |
| 281 | Token *first; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 282 | }; |
| 283 | |
| 284 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 285 | * To handle an arbitrary level of file inclusion, we maintain a |
| 286 | * stack (ie linked list) of these things. |
| 287 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 288 | struct Include { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 289 | Include *next; |
| 290 | FILE *fp; |
| 291 | Cond *conds; |
| 292 | Line *expansion; |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 293 | const char *fname; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 294 | int lineno, lineinc; |
| 295 | MMacro *mstk; /* stack of active macros/reps */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 296 | }; |
| 297 | |
| 298 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 299 | * Include search path. This is simply a list of strings which get |
| 300 | * prepended, in turn, to the name of an include file, in an |
| 301 | * attempt to find the file if it's not in the current directory. |
| 302 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 303 | struct IncPath { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 304 | IncPath *next; |
| 305 | char *path; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 306 | }; |
| 307 | |
| 308 | /* |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 309 | * File real name hash, so we don't have to re-search the include |
| 310 | * path for every pass (and potentially more than that if a file |
| 311 | * is used more than once.) |
| 312 | */ |
| 313 | struct hash_table FileHash; |
| 314 | |
| 315 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 316 | * Conditional assembly: we maintain a separate stack of these for |
| 317 | * each level of file inclusion. (The only reason we keep the |
| 318 | * stacks separate is to ensure that a stray `%endif' in a file |
| 319 | * included from within the true branch of a `%if' won't terminate |
| 320 | * it and cause confusion: instead, rightly, it'll cause an error.) |
| 321 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 322 | struct Cond { |
| 323 | Cond *next; |
| 324 | int state; |
| 325 | }; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 326 | enum { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 327 | /* |
| 328 | * These states are for use just after %if or %elif: IF_TRUE |
| 329 | * means the condition has evaluated to truth so we are |
| 330 | * currently emitting, whereas IF_FALSE means we are not |
| 331 | * currently emitting but will start doing so if a %else comes |
| 332 | * up. In these states, all directives are admissible: %elif, |
| 333 | * %else and %endif. (And of course %if.) |
| 334 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 335 | COND_IF_TRUE, COND_IF_FALSE, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 336 | /* |
| 337 | * These states come up after a %else: ELSE_TRUE means we're |
| 338 | * emitting, and ELSE_FALSE means we're not. In ELSE_* states, |
| 339 | * any %elif or %else will cause an error. |
| 340 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 341 | COND_ELSE_TRUE, COND_ELSE_FALSE, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 342 | /* |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 343 | * These states mean that we're not emitting now, and also that |
| 344 | * nothing until %endif will be emitted at all. COND_DONE is |
| 345 | * used when we've had our moment of emission |
| 346 | * and have now started seeing %elifs. COND_NEVER is used when |
| 347 | * the condition construct in question is contained within a |
| 348 | * non-emitting branch of a larger condition construct, |
| 349 | * or if there is an error. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 350 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 351 | COND_DONE, COND_NEVER |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 352 | }; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 353 | #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE ) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 354 | |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 355 | /* |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 356 | * These defines are used as the possible return values for do_directive |
| 357 | */ |
| 358 | #define NO_DIRECTIVE_FOUND 0 |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 359 | #define DIRECTIVE_FOUND 1 |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 360 | |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 361 | /* max reps */ |
| 362 | #define REP_LIMIT ((INT64_C(1) << 62)) |
| 363 | |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 364 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 365 | * Condition codes. Note that we use c_ prefix not C_ because C_ is |
| 366 | * used in nasm.h for the "real" condition codes. At _this_ level, |
| 367 | * we treat CXZ and ECXZ as condition codes, albeit non-invertible |
| 368 | * ones, so we need a different enum... |
| 369 | */ |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 370 | static const char * const conditions[] = { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 371 | "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le", |
| 372 | "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no", |
H. Peter Anvin | ce9be34 | 2007-09-12 00:22:29 +0000 | [diff] [blame] | 373 | "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z" |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 374 | }; |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 375 | enum pp_conds { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 376 | c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE, |
| 377 | 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] | 378 | c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z, |
| 379 | c_none = -1 |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 380 | }; |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 381 | static const enum pp_conds inverse_ccs[] = { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 382 | c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE, |
| 383 | 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] | 384 | 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] | 385 | }; |
| 386 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 387 | /* |
| 388 | * Directive names. |
| 389 | */ |
| 390 | /* If this is a an IF, ELIF, ELSE or ENDIF keyword */ |
| 391 | static int is_condition(enum preproc_token arg) |
| 392 | { |
| 393 | return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF); |
| 394 | } |
| 395 | |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 396 | /* For TASM compatibility we need to be able to recognise TASM compatible |
| 397 | * conditional compilation directives. Using the NASM pre-processor does |
| 398 | * not work, so we look for them specifically from the following list and |
| 399 | * then jam in the equivalent NASM directive into the input stream. |
| 400 | */ |
| 401 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 402 | enum { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 403 | TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI, |
| 404 | TM_IFNDEF, TM_INCLUDE, TM_LOCAL |
| 405 | }; |
| 406 | |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 407 | static const char * const tasm_directives[] = { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 408 | "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi", |
| 409 | "ifndef", "include", "local" |
| 410 | }; |
| 411 | |
| 412 | static int StackSize = 4; |
H. Peter Anvin | 6c8b2be | 2016-05-24 23:46:50 -0700 | [diff] [blame] | 413 | static const char *StackPointer = "ebp"; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 414 | static int ArgOffset = 8; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 415 | static int LocalOffset = 0; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 416 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 417 | static Context *cstk; |
| 418 | static Include *istk; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 419 | static IncPath *ipath = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 420 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 421 | static int pass; /* HACK: pass 0 = generate dependencies only */ |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 422 | static StrList **dephead; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 423 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 424 | static uint64_t unique; /* unique identifier numbers */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 425 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 426 | static Line *predef = NULL; |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 427 | static bool do_predef; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 428 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 429 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 430 | * The current set of multi-line macros we have defined. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 431 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 432 | static struct hash_table mmacros; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 433 | |
| 434 | /* |
| 435 | * The current set of single-line macros we have defined. |
| 436 | */ |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 437 | static struct hash_table smacros; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 438 | |
| 439 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 440 | * The multi-line macro we are currently defining, or the %rep |
| 441 | * block we are currently reading, if any. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 442 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 443 | static MMacro *defining; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 444 | |
Charles Crayne | d4200be | 2008-07-12 16:42:33 -0700 | [diff] [blame] | 445 | static uint64_t nested_mac_count; |
| 446 | static uint64_t nested_rep_count; |
| 447 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 448 | /* |
| 449 | * The number of macro parameters to allocate space for at a time. |
| 450 | */ |
| 451 | #define PARAM_DELTA 16 |
| 452 | |
| 453 | /* |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 454 | * The standard macro set: defined in macros.c in a set of arrays. |
| 455 | * This gives our position in any macro set, while we are processing it. |
| 456 | * The stdmacset is an array of such macro sets. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 457 | */ |
H. Peter Anvin | a70547f | 2008-07-19 21:44:26 -0700 | [diff] [blame] | 458 | static macros_t *stdmacpos; |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 459 | static macros_t **stdmacnext; |
| 460 | static macros_t *stdmacros[8]; |
Cyrill Gorcunov | 15ce78f | 2017-01-06 20:21:28 +0300 | [diff] [blame] | 461 | static macros_t *extrastdmac; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 462 | |
| 463 | /* |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 464 | * Tokens are allocated in blocks to improve speed |
| 465 | */ |
| 466 | #define TOKEN_BLOCKSIZE 4096 |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 467 | static Token *freeTokens = NULL; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 468 | struct Blocks { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 469 | Blocks *next; |
| 470 | void *chunk; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 471 | }; |
| 472 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 473 | static Blocks blocks = { NULL, NULL }; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 474 | |
| 475 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 476 | * Forward declarations. |
| 477 | */ |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 478 | static void pp_add_stdmac(macros_t *macros); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 479 | static Token *expand_mmac_params(Token * tline); |
| 480 | static Token *expand_smacro(Token * tline); |
| 481 | static Token *expand_id(Token * tline); |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 482 | static Context *get_ctx(const char *name, const char **namep); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 483 | static Token *make_tok_num(int64_t val); |
| 484 | static Token *make_tok_qstr(const char *str); |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 485 | static void pp_verror(int severity, const char *fmt, va_list ap); |
| 486 | static vefunc real_verror; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 487 | static void *new_Block(size_t size); |
| 488 | static void delete_Blocks(void); |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 489 | static Token *new_Token(Token * next, enum pp_token_type type, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 490 | const char *text, int txtlen); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 491 | static Token *delete_Token(Token * t); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 492 | |
| 493 | /* |
| 494 | * Macros for safe checking of token pointers, avoid *(NULL) |
| 495 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 496 | #define tok_type_(x,t) ((x) && (x)->type == (t)) |
| 497 | #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next |
| 498 | #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v))) |
| 499 | #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] | 500 | |
Cyrill Gorcunov | 194ba89 | 2011-06-30 01:16:35 +0400 | [diff] [blame] | 501 | /* |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 502 | * nasm_unquote with error if the string contains NUL characters. |
| 503 | * If the string contains NUL characters, issue an error and return |
| 504 | * the C len, i.e. truncate at the NUL. |
| 505 | */ |
| 506 | static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive) |
| 507 | { |
| 508 | size_t len = nasm_unquote(qstr, NULL); |
| 509 | size_t clen = strlen(qstr); |
| 510 | |
| 511 | if (len != clen) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 512 | nasm_error(ERR_NONFATAL, "NUL character in `%s' directive", |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 513 | pp_directives[directive]); |
| 514 | |
| 515 | return clen; |
| 516 | } |
| 517 | |
| 518 | /* |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 519 | * In-place reverse a list of tokens. |
| 520 | */ |
| 521 | static Token *reverse_tokens(Token *t) |
| 522 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 523 | Token *prev = NULL; |
| 524 | Token *next; |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 525 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 526 | while (t) { |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 527 | next = t->next; |
| 528 | t->next = prev; |
| 529 | prev = t; |
| 530 | t = next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 531 | } |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 532 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 533 | return prev; |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | /* |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 537 | * Handle TASM specific directives, which do not contain a % in |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 538 | * front of them. We do it here because I could not find any other |
| 539 | * place to do it for the moment, and it is a hack (ideally it would |
| 540 | * be nice to be able to use the NASM pre-processor to do it). |
| 541 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 542 | static char *check_tasm_directive(char *line) |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 543 | { |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 544 | int32_t i, j, k, m, len; |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 545 | char *p, *q, *oldline, oldchar; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 546 | |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 547 | p = nasm_skip_spaces(line); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 548 | |
| 549 | /* Binary search for the directive name */ |
| 550 | i = -1; |
Cyrill Gorcunov | a731924 | 2010-06-03 22:04:36 +0400 | [diff] [blame] | 551 | j = ARRAY_SIZE(tasm_directives); |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 552 | q = nasm_skip_word(p); |
| 553 | len = q - p; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 554 | if (len) { |
| 555 | oldchar = p[len]; |
| 556 | p[len] = 0; |
| 557 | while (j - i > 1) { |
| 558 | k = (j + i) / 2; |
| 559 | m = nasm_stricmp(p, tasm_directives[k]); |
| 560 | if (m == 0) { |
| 561 | /* We have found a directive, so jam a % in front of it |
| 562 | * so that NASM will then recognise it as one if it's own. |
| 563 | */ |
| 564 | p[len] = oldchar; |
| 565 | len = strlen(p); |
| 566 | oldline = line; |
| 567 | line = nasm_malloc(len + 2); |
| 568 | line[0] = '%'; |
| 569 | if (k == TM_IFDIFI) { |
H. Peter Anvin | 18f4879 | 2009-06-27 15:56:27 -0700 | [diff] [blame] | 570 | /* |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 571 | * NASM does not recognise IFDIFI, so we convert |
| 572 | * it to %if 0. This is not used in NASM |
| 573 | * compatible code, but does need to parse for the |
| 574 | * TASM macro package. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 575 | */ |
H. Peter Anvin | 18f4879 | 2009-06-27 15:56:27 -0700 | [diff] [blame] | 576 | strcpy(line + 1, "if 0"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 577 | } else { |
| 578 | memcpy(line + 1, p, len + 1); |
| 579 | } |
| 580 | nasm_free(oldline); |
| 581 | return line; |
| 582 | } else if (m < 0) { |
| 583 | j = k; |
| 584 | } else |
| 585 | i = k; |
| 586 | } |
| 587 | p[len] = oldchar; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 588 | } |
| 589 | return line; |
| 590 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 591 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 592 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 593 | * The pre-preprocessing stage... This function translates line |
| 594 | * number indications as they emerge from GNU cpp (`# lineno "file" |
| 595 | * flags') into NASM preprocessor line number indications (`%line |
| 596 | * lineno file'). |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 597 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 598 | static char *prepreproc(char *line) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 599 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 600 | int lineno, fnlen; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 601 | char *fname, *oldline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 602 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 603 | if (line[0] == '#' && line[1] == ' ') { |
| 604 | oldline = line; |
| 605 | fname = oldline + 2; |
| 606 | lineno = atoi(fname); |
| 607 | fname += strspn(fname, "0123456789 "); |
| 608 | if (*fname == '"') |
| 609 | fname++; |
| 610 | fnlen = strcspn(fname, "\""); |
| 611 | line = nasm_malloc(20 + fnlen); |
| 612 | snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname); |
| 613 | nasm_free(oldline); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 614 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 615 | if (tasm_compatible_mode) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 616 | return check_tasm_directive(line); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 617 | return line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 621 | * Free a linked list of tokens. |
| 622 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 623 | static void free_tlist(Token * list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 624 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 625 | while (list) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 626 | list = delete_Token(list); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | /* |
| 630 | * Free a linked list of lines. |
| 631 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 632 | static void free_llist(Line * list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 633 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 634 | Line *l, *tmp; |
| 635 | list_for_each_safe(l, tmp, list) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 636 | free_tlist(l->first); |
| 637 | nasm_free(l); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 638 | } |
| 639 | } |
| 640 | |
| 641 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 642 | * Free an MMacro |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 643 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 644 | static void free_mmacro(MMacro * m) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 645 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 646 | nasm_free(m->name); |
| 647 | free_tlist(m->dlist); |
| 648 | nasm_free(m->defaults); |
| 649 | free_llist(m->expansion); |
| 650 | nasm_free(m); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | /* |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 654 | * Free an SMacro |
| 655 | */ |
| 656 | static void free_smacro(SMacro *s, bool really) |
| 657 | { |
| 658 | nasm_free(s->name); |
| 659 | if (!s->magic) |
| 660 | free_tlist(s->e.expansion); |
| 661 | nasm_free(s->eval_param); |
| 662 | if (really) { |
| 663 | nasm_free(s); |
| 664 | } else { |
| 665 | /* Wipe everything except the next pointer */ |
| 666 | memset(&s->next + 1, 0, sizeof *s - sizeof s->next); |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | /* |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 671 | * Free all currently defined macros, and free the hash tables |
| 672 | */ |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 673 | static void free_smacro_table(struct hash_table *smt) |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 674 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 675 | SMacro *s, *tmp; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 676 | const char *key; |
| 677 | struct hash_tbl_node *it = NULL; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 678 | |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 679 | while ((s = hash_iterate(smt, &it, &key)) != NULL) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 680 | nasm_free((void *)key); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 681 | list_for_each_safe(s, tmp, s) |
| 682 | free_smacro(s, true); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 683 | } |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 684 | hash_free(smt); |
| 685 | } |
| 686 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 687 | static void free_mmacro_table(struct hash_table *mmt) |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 688 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 689 | MMacro *m, *tmp; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 690 | const char *key; |
| 691 | struct hash_tbl_node *it = NULL; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 692 | |
| 693 | it = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 694 | while ((m = hash_iterate(mmt, &it, &key)) != NULL) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 695 | nasm_free((void *)key); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 696 | list_for_each_safe(m ,tmp, m) |
| 697 | free_mmacro(m); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 698 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 699 | hash_free(mmt); |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | static void free_macros(void) |
| 703 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 704 | free_smacro_table(&smacros); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 705 | free_mmacro_table(&mmacros); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | /* |
| 709 | * Initialize the hash tables |
| 710 | */ |
| 711 | static void init_macros(void) |
| 712 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 713 | hash_init(&smacros, HASH_LARGE); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 714 | hash_init(&mmacros, HASH_LARGE); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 718 | * Pop the context stack. |
| 719 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 720 | static void ctx_pop(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 721 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 722 | Context *c = cstk; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 723 | |
| 724 | cstk = cstk->next; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 725 | free_smacro_table(&c->localmac); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 726 | nasm_free(c->name); |
| 727 | nasm_free(c); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 728 | } |
| 729 | |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 730 | /* |
| 731 | * Search for a key in the hash index; adding it if necessary |
| 732 | * (in which case we initialize the data pointer to NULL.) |
| 733 | */ |
| 734 | static void ** |
| 735 | hash_findi_add(struct hash_table *hash, const char *str) |
| 736 | { |
| 737 | struct hash_insert hi; |
| 738 | void **r; |
| 739 | char *strx; |
| 740 | |
| 741 | r = hash_findi(hash, str, &hi); |
| 742 | if (r) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 743 | return r; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 744 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 745 | strx = nasm_strdup(str); /* Use a more efficient allocator here? */ |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 746 | return hash_add(&hi, strx, NULL); |
| 747 | } |
| 748 | |
| 749 | /* |
| 750 | * Like hash_findi, but returns the data element rather than a pointer |
| 751 | * to it. Used only when not adding a new element, hence no third |
| 752 | * argument. |
| 753 | */ |
| 754 | static void * |
| 755 | hash_findix(struct hash_table *hash, const char *str) |
| 756 | { |
| 757 | void **p; |
| 758 | |
| 759 | p = hash_findi(hash, str, NULL); |
| 760 | return p ? *p : NULL; |
| 761 | } |
| 762 | |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 763 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 764 | * read line from standart macros set, |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 765 | * if there no more left -- return NULL |
| 766 | */ |
| 767 | static char *line_from_stdmac(void) |
| 768 | { |
| 769 | unsigned char c; |
| 770 | const unsigned char *p = stdmacpos; |
| 771 | char *line, *q; |
| 772 | size_t len = 0; |
| 773 | |
| 774 | if (!stdmacpos) |
| 775 | return NULL; |
| 776 | |
| 777 | while ((c = *p++)) { |
| 778 | if (c >= 0x80) |
| 779 | len += pp_directives_len[c - 0x80] + 1; |
| 780 | else |
| 781 | len++; |
| 782 | } |
| 783 | |
| 784 | line = nasm_malloc(len + 1); |
| 785 | q = line; |
| 786 | while ((c = *stdmacpos++)) { |
| 787 | if (c >= 0x80) { |
| 788 | memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]); |
| 789 | q += pp_directives_len[c - 0x80]; |
| 790 | *q++ = ' '; |
| 791 | } else { |
| 792 | *q++ = c; |
| 793 | } |
| 794 | } |
| 795 | stdmacpos = p; |
| 796 | *q = '\0'; |
| 797 | |
| 798 | if (!*stdmacpos) { |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 799 | /* This was the last of this particular macro set */ |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 800 | stdmacpos = NULL; |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 801 | if (*stdmacnext) { |
| 802 | stdmacpos = *stdmacnext++; |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 803 | } else if (do_predef) { |
| 804 | Line *pd, *l; |
| 805 | Token *head, **tail, *t; |
| 806 | |
| 807 | /* |
| 808 | * Nasty hack: here we push the contents of |
| 809 | * `predef' on to the top-level expansion stack, |
| 810 | * since this is the most convenient way to |
| 811 | * implement the pre-include and pre-define |
| 812 | * features. |
| 813 | */ |
| 814 | list_for_each(pd, predef) { |
| 815 | head = NULL; |
| 816 | tail = &head; |
| 817 | list_for_each(t, pd->first) { |
| 818 | *tail = new_Token(NULL, t->type, t->text, 0); |
| 819 | tail = &(*tail)->next; |
| 820 | } |
| 821 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 822 | l = nasm_malloc(sizeof(Line)); |
| 823 | l->next = istk->expansion; |
| 824 | l->first = head; |
| 825 | l->finishes = NULL; |
| 826 | |
| 827 | istk->expansion = l; |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 828 | } |
| 829 | do_predef = false; |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | return line; |
| 834 | } |
| 835 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 836 | static char *read_line(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 837 | { |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 838 | unsigned int size, c, next; |
| 839 | const unsigned int delta = 512; |
| 840 | const unsigned int pad = 8; |
| 841 | unsigned int nr_cont = 0; |
| 842 | bool cont = false; |
| 843 | char *buffer, *p; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 844 | |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 845 | /* Standart macros set (predefined) goes first */ |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 846 | p = line_from_stdmac(); |
| 847 | if (p) |
| 848 | return p; |
H. Peter Anvin | 72edbb8 | 2008-06-19 16:00:04 -0700 | [diff] [blame] | 849 | |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 850 | size = delta; |
| 851 | p = buffer = nasm_malloc(size); |
| 852 | |
| 853 | for (;;) { |
| 854 | c = fgetc(istk->fp); |
| 855 | if ((int)(c) == EOF) { |
| 856 | p[0] = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 857 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 858 | } |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 859 | |
| 860 | switch (c) { |
| 861 | case '\r': |
| 862 | next = fgetc(istk->fp); |
| 863 | if (next != '\n') |
| 864 | ungetc(next, istk->fp); |
| 865 | if (cont) { |
| 866 | cont = false; |
| 867 | continue; |
| 868 | } |
| 869 | break; |
| 870 | |
| 871 | case '\n': |
| 872 | if (cont) { |
| 873 | cont = false; |
| 874 | continue; |
| 875 | } |
| 876 | break; |
| 877 | |
| 878 | case '\\': |
| 879 | next = fgetc(istk->fp); |
| 880 | ungetc(next, istk->fp); |
Cyrill Gorcunov | 490f85e | 2012-12-27 20:02:17 +0400 | [diff] [blame] | 881 | if (next == '\r' || next == '\n') { |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 882 | cont = true; |
| 883 | nr_cont++; |
| 884 | continue; |
| 885 | } |
| 886 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 887 | } |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 888 | |
| 889 | if (c == '\r' || c == '\n') { |
| 890 | *p++ = 0; |
| 891 | break; |
| 892 | } |
| 893 | |
| 894 | if (p >= (buffer + size - pad)) { |
| 895 | buffer = nasm_realloc(buffer, size + delta); |
| 896 | p = buffer + size - pad; |
| 897 | size += delta; |
| 898 | } |
| 899 | |
| 900 | *p++ = (unsigned char)c; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 901 | } |
| 902 | |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 903 | if (p == buffer) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 904 | nasm_free(buffer); |
| 905 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 906 | } |
| 907 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 908 | src_set_linnum(src_get_linnum() + istk->lineinc + |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 909 | (nr_cont * istk->lineinc)); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 910 | |
| 911 | /* |
| 912 | * Handle spurious ^Z, which may be inserted into source files |
| 913 | * by some file transfer utilities. |
| 914 | */ |
| 915 | buffer[strcspn(buffer, "\032")] = '\0'; |
| 916 | |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 917 | lfmt->line(LIST_READ, buffer); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 918 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 919 | return buffer; |
| 920 | } |
| 921 | |
| 922 | /* |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 923 | * 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] | 924 | * don't need to parse the value out of e.g. numeric tokens: we |
| 925 | * simply split one string into many. |
| 926 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 927 | static Token *tokenize(char *line) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 928 | { |
H. Peter Anvin | ca544db | 2008-10-19 19:30:11 -0700 | [diff] [blame] | 929 | char c, *p = line; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 930 | enum pp_token_type type; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 931 | Token *list = NULL; |
| 932 | Token *t, **tail = &list; |
| 933 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 934 | while (*line) { |
| 935 | p = line; |
| 936 | if (*p == '%') { |
| 937 | p++; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 938 | if (*p == '+' && !nasm_isdigit(p[1])) { |
| 939 | p++; |
| 940 | type = TOK_PASTE; |
| 941 | } else if (nasm_isdigit(*p) || |
| 942 | ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 943 | do { |
| 944 | p++; |
| 945 | } |
H. Peter Anvin | bda7a6e | 2008-06-21 10:23:17 -0700 | [diff] [blame] | 946 | while (nasm_isdigit(*p)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 947 | type = TOK_PREPROC_ID; |
| 948 | } else if (*p == '{') { |
| 949 | p++; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 950 | while (*p) { |
| 951 | if (*p == '}') |
| 952 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 953 | p[-1] = *p; |
| 954 | p++; |
| 955 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 956 | if (*p != '}') |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 957 | nasm_error(ERR_WARNING | ERR_PASS1, |
| 958 | "unterminated %%{ construct"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 959 | p[-1] = '\0'; |
| 960 | if (*p) |
| 961 | p++; |
| 962 | type = TOK_PREPROC_ID; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 963 | } else if (*p == '[') { |
| 964 | int lvl = 1; |
| 965 | line += 2; /* Skip the leading %[ */ |
| 966 | p++; |
| 967 | while (lvl && (c = *p++)) { |
| 968 | switch (c) { |
| 969 | case ']': |
| 970 | lvl--; |
| 971 | break; |
| 972 | case '%': |
| 973 | if (*p == '[') |
| 974 | lvl++; |
| 975 | break; |
| 976 | case '\'': |
| 977 | case '\"': |
| 978 | case '`': |
Cyrill Gorcunov | 3144e84 | 2017-10-22 10:50:55 +0300 | [diff] [blame] | 979 | p = nasm_skip_string(p - 1); |
| 980 | if (*p) |
| 981 | p++; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 982 | break; |
| 983 | default: |
| 984 | break; |
| 985 | } |
| 986 | } |
| 987 | p--; |
| 988 | if (*p) |
| 989 | *p++ = '\0'; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 990 | if (lvl) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 991 | nasm_error(ERR_NONFATAL|ERR_PASS1, |
| 992 | "unterminated %%[ construct"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 993 | type = TOK_INDIRECT; |
| 994 | } else if (*p == '?') { |
| 995 | type = TOK_PREPROC_Q; /* %? */ |
| 996 | p++; |
| 997 | if (*p == '?') { |
| 998 | type = TOK_PREPROC_QQ; /* %?? */ |
| 999 | p++; |
| 1000 | } |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1001 | } else if (*p == '!') { |
| 1002 | type = TOK_PREPROC_ID; |
| 1003 | p++; |
| 1004 | if (isidchar(*p)) { |
| 1005 | do { |
| 1006 | p++; |
| 1007 | } |
| 1008 | while (isidchar(*p)); |
| 1009 | } else if (*p == '\'' || *p == '\"' || *p == '`') { |
| 1010 | p = nasm_skip_string(p); |
| 1011 | if (*p) |
| 1012 | p++; |
| 1013 | else |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1014 | nasm_error(ERR_NONFATAL|ERR_PASS1, |
| 1015 | "unterminated %%! string"); |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1016 | } else { |
| 1017 | /* %! without string or identifier */ |
| 1018 | type = TOK_OTHER; /* Legacy behavior... */ |
| 1019 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1020 | } else if (isidchar(*p) || |
| 1021 | ((*p == '!' || *p == '%' || *p == '$') && |
| 1022 | isidchar(p[1]))) { |
| 1023 | do { |
| 1024 | p++; |
| 1025 | } |
| 1026 | while (isidchar(*p)); |
| 1027 | type = TOK_PREPROC_ID; |
| 1028 | } else { |
| 1029 | type = TOK_OTHER; |
| 1030 | if (*p == '%') |
| 1031 | p++; |
| 1032 | } |
| 1033 | } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) { |
| 1034 | type = TOK_ID; |
| 1035 | p++; |
| 1036 | while (*p && isidchar(*p)) |
| 1037 | p++; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 1038 | } else if (*p == '\'' || *p == '"' || *p == '`') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1039 | /* |
| 1040 | * A string token. |
| 1041 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1042 | type = TOK_STRING; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1043 | p = nasm_skip_string(p); |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1044 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1045 | if (*p) { |
| 1046 | p++; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1047 | } else { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1048 | nasm_error(ERR_WARNING|ERR_PASS1, "unterminated string"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1049 | /* Handling unterminated strings by UNV */ |
| 1050 | /* type = -1; */ |
| 1051 | } |
Victor van den Elzen | fb5f251 | 2009-04-17 16:17:59 +0200 | [diff] [blame] | 1052 | } else if (p[0] == '$' && p[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1053 | type = TOK_OTHER; /* TOKEN_BASE */ |
Victor van den Elzen | fb5f251 | 2009-04-17 16:17:59 +0200 | [diff] [blame] | 1054 | p += 2; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1055 | } else if (isnumstart(*p)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1056 | bool is_hex = false; |
| 1057 | bool is_float = false; |
| 1058 | bool has_e = false; |
| 1059 | char c, *r; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1060 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1061 | /* |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1062 | * A numeric token. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1063 | */ |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1064 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1065 | if (*p == '$') { |
| 1066 | p++; |
| 1067 | is_hex = true; |
| 1068 | } |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1069 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1070 | for (;;) { |
| 1071 | c = *p++; |
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 (!is_hex && (c == 'e' || c == 'E')) { |
| 1074 | has_e = true; |
| 1075 | if (*p == '+' || *p == '-') { |
| 1076 | /* |
| 1077 | * e can only be followed by +/- if it is either a |
| 1078 | * prefixed hex number or a floating-point number |
| 1079 | */ |
| 1080 | p++; |
| 1081 | is_float = true; |
| 1082 | } |
| 1083 | } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') { |
| 1084 | is_hex = true; |
| 1085 | } else if (c == 'P' || c == 'p') { |
| 1086 | is_float = true; |
| 1087 | if (*p == '+' || *p == '-') |
| 1088 | p++; |
Martin Lindhe | b150e38 | 2016-11-16 15:36:53 +0100 | [diff] [blame] | 1089 | } else if (isnumchar(c)) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1090 | ; /* just advance */ |
| 1091 | else if (c == '.') { |
| 1092 | /* |
| 1093 | * we need to deal with consequences of the legacy |
| 1094 | * parser, like "1.nolist" being two tokens |
| 1095 | * (TOK_NUMBER, TOK_ID) here; at least give it |
| 1096 | * a shot for now. In the future, we probably need |
| 1097 | * a flex-based scanner with proper pattern matching |
| 1098 | * to do it as well as it can be done. Nothing in |
| 1099 | * the world is going to help the person who wants |
| 1100 | * 0x123.p16 interpreted as two tokens, though. |
| 1101 | */ |
| 1102 | r = p; |
| 1103 | while (*r == '_') |
| 1104 | r++; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1105 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1106 | if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) || |
| 1107 | (!is_hex && (*r == 'e' || *r == 'E')) || |
| 1108 | (*r == 'p' || *r == 'P')) { |
| 1109 | p = r; |
| 1110 | is_float = true; |
| 1111 | } else |
| 1112 | break; /* Terminate the token */ |
| 1113 | } else |
| 1114 | break; |
| 1115 | } |
| 1116 | p--; /* Point to first character beyond number */ |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1117 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1118 | if (p == line+1 && *line == '$') { |
| 1119 | type = TOK_OTHER; /* TOKEN_HERE */ |
| 1120 | } else { |
| 1121 | if (has_e && !is_hex) { |
| 1122 | /* 1e13 is floating-point, but 1e13h is not */ |
| 1123 | is_float = true; |
| 1124 | } |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 1125 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1126 | type = is_float ? TOK_FLOAT : TOK_NUMBER; |
| 1127 | } |
H. Peter Anvin | bda7a6e | 2008-06-21 10:23:17 -0700 | [diff] [blame] | 1128 | } else if (nasm_isspace(*p)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1129 | type = TOK_WHITESPACE; |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 1130 | p = nasm_skip_spaces(p); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1131 | /* |
| 1132 | * Whitespace just before end-of-line is discarded by |
| 1133 | * pretending it's a comment; whitespace just before a |
| 1134 | * comment gets lumped into the comment. |
| 1135 | */ |
| 1136 | if (!*p || *p == ';') { |
| 1137 | type = TOK_COMMENT; |
| 1138 | while (*p) |
| 1139 | p++; |
| 1140 | } |
| 1141 | } else if (*p == ';') { |
| 1142 | type = TOK_COMMENT; |
| 1143 | while (*p) |
| 1144 | p++; |
| 1145 | } else { |
| 1146 | /* |
| 1147 | * Anything else is an operator of some kind. We check |
| 1148 | * for all the double-character operators (>>, <<, //, |
| 1149 | * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1150 | * else is a single-character operator. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1151 | */ |
| 1152 | type = TOK_OTHER; |
| 1153 | if ((p[0] == '>' && p[1] == '>') || |
| 1154 | (p[0] == '<' && p[1] == '<') || |
| 1155 | (p[0] == '/' && p[1] == '/') || |
| 1156 | (p[0] == '<' && p[1] == '=') || |
| 1157 | (p[0] == '>' && p[1] == '=') || |
| 1158 | (p[0] == '=' && p[1] == '=') || |
| 1159 | (p[0] == '!' && p[1] == '=') || |
| 1160 | (p[0] == '<' && p[1] == '>') || |
| 1161 | (p[0] == '&' && p[1] == '&') || |
| 1162 | (p[0] == '|' && p[1] == '|') || |
| 1163 | (p[0] == '^' && p[1] == '^')) { |
| 1164 | p++; |
| 1165 | } |
| 1166 | p++; |
| 1167 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1168 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1169 | /* Handling unterminated string by UNV */ |
| 1170 | /*if (type == -1) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1171 | { |
| 1172 | *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1); |
| 1173 | t->text[p-line] = *line; |
| 1174 | tail = &t->next; |
| 1175 | } |
| 1176 | else */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1177 | if (type != TOK_COMMENT) { |
| 1178 | *tail = t = new_Token(NULL, type, line, p - line); |
| 1179 | tail = &t->next; |
| 1180 | } |
| 1181 | line = p; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1182 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1183 | return list; |
| 1184 | } |
| 1185 | |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1186 | /* |
| 1187 | * this function allocates a new managed block of memory and |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1188 | * returns a pointer to the block. The managed blocks are |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1189 | * deleted only all at once by the delete_Blocks function. |
| 1190 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1191 | static void *new_Block(size_t size) |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1192 | { |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1193 | Blocks *b = &blocks; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1194 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1195 | /* first, get to the end of the linked list */ |
| 1196 | while (b->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1197 | b = b->next; |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1198 | /* now allocate the requested chunk */ |
| 1199 | b->chunk = nasm_malloc(size); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1200 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1201 | /* now allocate a new block for the next request */ |
Cyrill Gorcunov | c31767c | 2014-06-28 02:22:17 +0400 | [diff] [blame] | 1202 | b->next = nasm_zalloc(sizeof(Blocks)); |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1203 | return b->chunk; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
| 1206 | /* |
| 1207 | * this function deletes all managed blocks of memory |
| 1208 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1209 | static void delete_Blocks(void) |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1210 | { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1211 | Blocks *a, *b = &blocks; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1212 | |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1213 | /* |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1214 | * keep in mind that the first block, pointed to by blocks |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1215 | * is a static and not dynamically allocated, so we don't |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1216 | * free it. |
| 1217 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1218 | while (b) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1219 | if (b->chunk) |
| 1220 | nasm_free(b->chunk); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1221 | a = b; |
| 1222 | b = b->next; |
| 1223 | if (a != &blocks) |
| 1224 | nasm_free(a); |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1225 | } |
Cyrill Gorcunov | dae24d7 | 2014-06-28 10:17:39 +0400 | [diff] [blame] | 1226 | memset(&blocks, 0, sizeof(blocks)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1227 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1228 | |
| 1229 | /* |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1230 | * 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] | 1231 | * 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] | 1232 | * also the a.mac and next elements to NULL. |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1233 | */ |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1234 | static Token *new_Token(Token * next, enum pp_token_type type, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1235 | const char *text, int txtlen) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1236 | { |
| 1237 | Token *t; |
| 1238 | int i; |
| 1239 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1240 | if (!freeTokens) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1241 | freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token)); |
| 1242 | for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++) |
| 1243 | freeTokens[i].next = &freeTokens[i + 1]; |
| 1244 | freeTokens[i].next = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1245 | } |
| 1246 | t = freeTokens; |
| 1247 | freeTokens = t->next; |
| 1248 | t->next = next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 1249 | t->a.mac = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1250 | t->type = type; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1251 | if (type == TOK_WHITESPACE || !text) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1252 | t->text = NULL; |
| 1253 | } else { |
| 1254 | if (txtlen == 0) |
| 1255 | txtlen = strlen(text); |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1256 | t->text = nasm_malloc(txtlen+1); |
| 1257 | memcpy(t->text, text, txtlen); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1258 | t->text[txtlen] = '\0'; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1259 | } |
| 1260 | return t; |
| 1261 | } |
| 1262 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1263 | static Token *delete_Token(Token * t) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1264 | { |
| 1265 | Token *next = t->next; |
| 1266 | nasm_free(t->text); |
H. Peter Anvin | 788e6c1 | 2002-04-30 21:02:01 +0000 | [diff] [blame] | 1267 | t->next = freeTokens; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1268 | freeTokens = t; |
| 1269 | return next; |
| 1270 | } |
| 1271 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1272 | /* |
| 1273 | * Convert a line of tokens back into text. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1274 | * If expand_locals is not zero, identifiers of the form "%$*xxx" |
| 1275 | * will be transformed into ..@ctxnum.xxx |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1276 | */ |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 1277 | static char *detoken(Token * tlist, bool expand_locals) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1278 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1279 | Token *t; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1280 | char *line, *p; |
H. Peter Anvin | b4daadc | 2008-01-21 16:31:57 -0800 | [diff] [blame] | 1281 | const char *q; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1282 | int len = 0; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1283 | |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1284 | list_for_each(t, tlist) { |
Cyrill Gorcunov | 9b7ee09 | 2017-10-22 21:42:59 +0300 | [diff] [blame] | 1285 | if (t->type == TOK_PREPROC_ID && t->text && |
| 1286 | t->text[0] && t->text[1] == '!') { |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1287 | char *v; |
| 1288 | char *q = t->text; |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1289 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1290 | v = t->text + 2; |
| 1291 | if (*v == '\'' || *v == '\"' || *v == '`') { |
| 1292 | size_t len = nasm_unquote(v, NULL); |
| 1293 | size_t clen = strlen(v); |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1294 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1295 | if (len != clen) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1296 | nasm_error(ERR_NONFATAL | ERR_PASS1, |
| 1297 | "NUL character in %%! string"); |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1298 | v = NULL; |
| 1299 | } |
| 1300 | } |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1301 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1302 | if (v) { |
| 1303 | char *p = getenv(v); |
| 1304 | if (!p) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1305 | nasm_error(ERR_NONFATAL | ERR_PASS1, |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1306 | "nonexistent environment variable `%s'", v); |
Cyrill Gorcunov | bbb7a1a | 2016-06-19 12:15:24 +0300 | [diff] [blame] | 1307 | /* |
| 1308 | * FIXME We better should investigate if accessing |
| 1309 | * ->text[1] without ->text[0] is safe enough. |
| 1310 | */ |
| 1311 | t->text = nasm_zalloc(2); |
| 1312 | } else |
| 1313 | t->text = nasm_strdup(p); |
Cyrill Gorcunov | 7500487 | 2017-07-26 01:21:16 +0300 | [diff] [blame] | 1314 | nasm_free(q); |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1315 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1316 | } |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1317 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1318 | /* Expand local macros here and not during preprocessing */ |
| 1319 | if (expand_locals && |
| 1320 | t->type == TOK_PREPROC_ID && t->text && |
| 1321 | t->text[0] == '%' && t->text[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1322 | const char *q; |
| 1323 | char *p; |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1324 | Context *ctx = get_ctx(t->text, &q); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1325 | if (ctx) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1326 | char buffer[40]; |
Keith Kanios | 93f2e9a | 2007-04-14 00:10:59 +0000 | [diff] [blame] | 1327 | snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1328 | p = nasm_strcat(buffer, q); |
| 1329 | nasm_free(t->text); |
| 1330 | t->text = p; |
| 1331 | } |
| 1332 | } |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1333 | if (t->type == TOK_WHITESPACE) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1334 | len++; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1335 | else if (t->text) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1336 | len += strlen(t->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1337 | } |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1338 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1339 | p = line = nasm_malloc(len + 1); |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1340 | |
| 1341 | list_for_each(t, tlist) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1342 | if (t->type == TOK_WHITESPACE) { |
H. Peter Anvin | b4daadc | 2008-01-21 16:31:57 -0800 | [diff] [blame] | 1343 | *p++ = ' '; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1344 | } else if (t->text) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1345 | q = t->text; |
| 1346 | while (*q) |
| 1347 | *p++ = *q++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1348 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1349 | } |
| 1350 | *p = '\0'; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1351 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1352 | return line; |
| 1353 | } |
| 1354 | |
| 1355 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1356 | * A scanner, suitable for use by the expression evaluator, which |
| 1357 | * operates on a line of Tokens. Expects a pointer to a pointer to |
| 1358 | * the first token in the line to be passed in as its private_data |
| 1359 | * field. |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1360 | * |
| 1361 | * FIX: This really needs to be unified with stdscan. |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1362 | */ |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 1363 | struct ppscan { |
| 1364 | Token *tptr; |
| 1365 | int ntokens; |
| 1366 | }; |
| 1367 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1368 | static int ppscan(void *private_data, struct tokenval *tokval) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1369 | { |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 1370 | struct ppscan *pps = private_data; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1371 | Token *tline; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1372 | char ourcopy[MAX_KEYWORD+1], *p, *r, *s; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1373 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1374 | do { |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 1375 | if (pps->ntokens && (tline = pps->tptr)) { |
| 1376 | pps->ntokens--; |
| 1377 | pps->tptr = tline->next; |
| 1378 | } else { |
| 1379 | pps->tptr = NULL; |
| 1380 | pps->ntokens = 0; |
| 1381 | return tokval->t_type = TOKEN_EOS; |
| 1382 | } |
| 1383 | } while (tline->type == TOK_WHITESPACE || tline->type == TOK_COMMENT); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1384 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1385 | tokval->t_charptr = tline->text; |
| 1386 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1387 | if (tline->text[0] == '$' && !tline->text[1]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1388 | return tokval->t_type = TOKEN_HERE; |
H. Peter Anvin | 7cf897e | 2002-05-30 21:30:33 +0000 | [diff] [blame] | 1389 | if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1390 | return tokval->t_type = TOKEN_BASE; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1391 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1392 | if (tline->type == TOK_ID) { |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1393 | p = tokval->t_charptr = tline->text; |
| 1394 | if (p[0] == '$') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1395 | tokval->t_charptr++; |
| 1396 | return tokval->t_type = TOKEN_ID; |
| 1397 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1398 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1399 | for (r = p, s = ourcopy; *r; r++) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1400 | if (r >= p+MAX_KEYWORD) |
| 1401 | return tokval->t_type = TOKEN_ID; /* Not a keyword */ |
H. Peter Anvin | ac8f8fc | 2008-06-11 15:49:41 -0700 | [diff] [blame] | 1402 | *s++ = nasm_tolower(*r); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1403 | } |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1404 | *s = '\0'; |
| 1405 | /* right, so we have an identifier sitting in temp storage. now, |
| 1406 | * is it actually a register or instruction name, or what? */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1407 | return nasm_token_hash(ourcopy, tokval); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1408 | } |
| 1409 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1410 | if (tline->type == TOK_NUMBER) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1411 | bool rn_error; |
| 1412 | tokval->t_integer = readnum(tline->text, &rn_error); |
| 1413 | tokval->t_charptr = tline->text; |
| 1414 | if (rn_error) |
| 1415 | return tokval->t_type = TOKEN_ERRNUM; |
| 1416 | else |
| 1417 | return tokval->t_type = TOKEN_NUM; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1418 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1419 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1420 | if (tline->type == TOK_FLOAT) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1421 | return tokval->t_type = TOKEN_FLOAT; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1422 | } |
| 1423 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1424 | if (tline->type == TOK_STRING) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1425 | char bq, *ep; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1426 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1427 | bq = tline->text[0]; |
H. Peter Anvin | 1162704 | 2008-06-09 20:45:19 -0700 | [diff] [blame] | 1428 | tokval->t_charptr = tline->text; |
| 1429 | tokval->t_inttwo = nasm_unquote(tline->text, &ep); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 1430 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1431 | if (ep[0] != bq || ep[1] != '\0') |
| 1432 | return tokval->t_type = TOKEN_ERRSTR; |
| 1433 | else |
| 1434 | return tokval->t_type = TOKEN_STR; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1437 | if (tline->type == TOK_OTHER) { |
| 1438 | if (!strcmp(tline->text, "<<")) |
| 1439 | return tokval->t_type = TOKEN_SHL; |
| 1440 | if (!strcmp(tline->text, ">>")) |
| 1441 | return tokval->t_type = TOKEN_SHR; |
| 1442 | if (!strcmp(tline->text, "//")) |
| 1443 | return tokval->t_type = TOKEN_SDIV; |
| 1444 | if (!strcmp(tline->text, "%%")) |
| 1445 | return tokval->t_type = TOKEN_SMOD; |
| 1446 | if (!strcmp(tline->text, "==")) |
| 1447 | return tokval->t_type = TOKEN_EQ; |
| 1448 | if (!strcmp(tline->text, "<>")) |
| 1449 | return tokval->t_type = TOKEN_NE; |
| 1450 | if (!strcmp(tline->text, "!=")) |
| 1451 | return tokval->t_type = TOKEN_NE; |
| 1452 | if (!strcmp(tline->text, "<=")) |
| 1453 | return tokval->t_type = TOKEN_LE; |
| 1454 | if (!strcmp(tline->text, ">=")) |
| 1455 | return tokval->t_type = TOKEN_GE; |
| 1456 | if (!strcmp(tline->text, "&&")) |
| 1457 | return tokval->t_type = TOKEN_DBL_AND; |
| 1458 | if (!strcmp(tline->text, "^^")) |
| 1459 | return tokval->t_type = TOKEN_DBL_XOR; |
| 1460 | if (!strcmp(tline->text, "||")) |
| 1461 | return tokval->t_type = TOKEN_DBL_OR; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
| 1464 | /* |
| 1465 | * We have no other options: just return the first character of |
| 1466 | * the token text. |
| 1467 | */ |
| 1468 | return tokval->t_type = tline->text[0]; |
| 1469 | } |
| 1470 | |
| 1471 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1472 | * Compare a string to the name of an existing macro; this is a |
| 1473 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 1474 | * depending on the value of the `casesense' parameter. |
| 1475 | */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 1476 | static int mstrcmp(const char *p, const char *q, bool casesense) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1477 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1478 | return casesense ? strcmp(p, q) : nasm_stricmp(p, q); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1479 | } |
| 1480 | |
| 1481 | /* |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1482 | * Compare a string to the name of an existing macro; this is a |
| 1483 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 1484 | * depending on the value of the `casesense' parameter. |
| 1485 | */ |
| 1486 | static int mmemcmp(const char *p, const char *q, size_t l, bool casesense) |
| 1487 | { |
| 1488 | return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l); |
| 1489 | } |
| 1490 | |
| 1491 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1492 | * Return the Context structure associated with a %$ token. Return |
| 1493 | * NULL, having _already_ reported an error condition, if the |
| 1494 | * context stack isn't deep enough for the supplied number of $ |
| 1495 | * signs. |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1496 | * |
| 1497 | * If "namep" is non-NULL, set it to the pointer to the macro name |
| 1498 | * tail, i.e. the part beyond %$... |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1499 | */ |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1500 | static Context *get_ctx(const char *name, const char **namep) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1501 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1502 | Context *ctx; |
| 1503 | int i; |
| 1504 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1505 | if (namep) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1506 | *namep = name; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1507 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1508 | if (!name || name[0] != '%' || name[1] != '$') |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1509 | return NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1510 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1511 | if (!cstk) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1512 | nasm_error(ERR_NONFATAL, "`%s': context stack is empty", name); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1513 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1514 | } |
| 1515 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1516 | name += 2; |
| 1517 | ctx = cstk; |
| 1518 | i = 0; |
| 1519 | while (ctx && *name == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1520 | name++; |
| 1521 | i++; |
| 1522 | ctx = ctx->next; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1523 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1524 | if (!ctx) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1525 | nasm_error(ERR_NONFATAL, "`%s': context stack is only" |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1526 | " %d level%s deep", name, i, (i == 1 ? "" : "s")); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1527 | return NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1528 | } |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1529 | |
| 1530 | if (namep) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1531 | *namep = name; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1532 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1533 | return ctx; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1537 | * Open an include file. This routine must always return a valid |
| 1538 | * file pointer if it returns - it's responsible for throwing an |
| 1539 | * ERR_FATAL and bombing out completely if not. It should also try |
| 1540 | * the include path one by one until it finds the file or reaches |
| 1541 | * the end of the path. |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1542 | * |
| 1543 | * Note: for INC_PROBE the function returns NULL at all times; |
| 1544 | * instead look for the |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1545 | */ |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1546 | enum incopen_mode { |
| 1547 | INC_NEEDED, /* File must exist */ |
| 1548 | INC_OPTIONAL, /* Missing is OK */ |
| 1549 | INC_PROBE /* Only an existence probe */ |
| 1550 | }; |
| 1551 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1552 | /* This is conducts a full pathname search */ |
| 1553 | static FILE *inc_fopen_search(const char *file, StrList **slpath, |
| 1554 | enum incopen_mode omode, enum file_flags fmode) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1555 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1556 | FILE *fp; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1557 | char *prefix = ""; |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1558 | const IncPath *ip = ipath; |
night199uk | fdb1a1b | 2018-10-18 23:19:47 +0200 | [diff] [blame] | 1559 | int len; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1560 | StrList *sl; |
night199uk | fdb1a1b | 2018-10-18 23:19:47 +0200 | [diff] [blame] | 1561 | char *sp; |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1562 | bool found; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1563 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1564 | while (1) { |
night199uk | fdb1a1b | 2018-10-18 23:19:47 +0200 | [diff] [blame] | 1565 | sp = nasm_catfile(prefix, file); |
| 1566 | len = strlen(sp) + 1; |
| 1567 | sl = nasm_malloc(len + sizeof sl->next); |
| 1568 | memcpy(sl->str, sp, len); |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1569 | sl->next = NULL; |
night199uk | fdb1a1b | 2018-10-18 23:19:47 +0200 | [diff] [blame] | 1570 | nasm_free(sp); |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 1571 | |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1572 | if (omode == INC_PROBE) { |
| 1573 | fp = NULL; |
| 1574 | found = nasm_file_exists(sl->str); |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1575 | } else { |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1576 | fp = nasm_open_read(sl->str, fmode); |
| 1577 | found = (fp != NULL); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1578 | } |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1579 | if (found) { |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1580 | *slpath = sl; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1581 | return fp; |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1582 | } |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 1583 | |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1584 | nasm_free(sl); |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 1585 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1586 | if (!ip) |
| 1587 | return NULL; |
| 1588 | |
| 1589 | prefix = ip->path; |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1590 | ip = ip->next; |
| 1591 | } |
| 1592 | } |
| 1593 | |
| 1594 | /* |
| 1595 | * Open a file, or test for the presence of one (depending on omode), |
| 1596 | * considering the include path. |
| 1597 | */ |
| 1598 | static FILE *inc_fopen(const char *file, |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 1599 | StrList **dhead, |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 1600 | const char **found_path, |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1601 | enum incopen_mode omode, |
| 1602 | enum file_flags fmode) |
| 1603 | { |
| 1604 | StrList *sl; |
| 1605 | struct hash_insert hi; |
| 1606 | void **hp; |
| 1607 | char *path; |
| 1608 | FILE *fp = NULL; |
| 1609 | |
| 1610 | hp = hash_find(&FileHash, file, &hi); |
| 1611 | if (hp) { |
| 1612 | path = *hp; |
Martin Storsjö | f283c8f | 2017-08-13 17:28:46 +0300 | [diff] [blame] | 1613 | if (path || omode != INC_NEEDED) { |
H. Peter Anvin | 97fda4c | 2017-08-16 15:52:51 -0700 | [diff] [blame] | 1614 | nasm_add_string_to_strlist(dhead, path ? path : file); |
Martin Storsjö | f283c8f | 2017-08-13 17:28:46 +0300 | [diff] [blame] | 1615 | } |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1616 | } else { |
| 1617 | /* Need to do the actual path search */ |
| 1618 | size_t file_len; |
| 1619 | |
| 1620 | sl = NULL; |
| 1621 | fp = inc_fopen_search(file, &sl, omode, fmode); |
| 1622 | |
| 1623 | file_len = strlen(file); |
| 1624 | |
| 1625 | if (!sl) { |
| 1626 | /* Store negative result for this file */ |
| 1627 | sl = nasm_malloc(file_len + 1 + sizeof sl->next); |
| 1628 | memcpy(sl->str, file, file_len+1); |
| 1629 | sl->next = NULL; |
| 1630 | file = sl->str; |
| 1631 | path = NULL; |
| 1632 | } else { |
| 1633 | path = sl->str; |
| 1634 | file = strchr(path, '\0') - file_len; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1635 | } |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1636 | |
| 1637 | hash_add(&hi, file, path); /* Positive or negative result */ |
| 1638 | |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 1639 | /* |
| 1640 | * Add file to dependency path. The in_list() is needed |
| 1641 | * in case the file was already added with %depend. |
| 1642 | */ |
| 1643 | if (path || omode != INC_NEEDED) |
H. Peter Anvin | 436e367 | 2016-10-04 01:12:28 -0700 | [diff] [blame] | 1644 | nasm_add_to_strlist(dhead, sl); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1645 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1646 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1647 | if (!path) { |
| 1648 | if (omode == INC_NEEDED) |
| 1649 | nasm_fatal(0, "unable to open include file `%s'", file); |
| 1650 | |
| 1651 | if (found_path) |
| 1652 | *found_path = NULL; |
| 1653 | |
| 1654 | return NULL; |
| 1655 | } |
| 1656 | |
| 1657 | if (!fp && omode != INC_PROBE) |
Cyrill Gorcunov | 4ff8c63 | 2017-01-06 00:36:23 +0300 | [diff] [blame] | 1658 | fp = nasm_open_read(path, fmode); |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1659 | |
| 1660 | if (found_path) |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 1661 | *found_path = path; |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1662 | |
| 1663 | return fp; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1664 | } |
| 1665 | |
| 1666 | /* |
Fabian Giesen | 0bbc38d | 2016-04-28 13:48:14 -0700 | [diff] [blame] | 1667 | * Opens an include or input file. Public version, for use by modules |
| 1668 | * that get a file:lineno pair and need to look at the file again |
| 1669 | * (e.g. the CodeView debug backend). Returns NULL on failure. |
| 1670 | */ |
H. Peter Anvin | 3e83cec | 2016-05-25 04:28:46 -0700 | [diff] [blame] | 1671 | FILE *pp_input_fopen(const char *filename, enum file_flags mode) |
Fabian Giesen | 0bbc38d | 2016-04-28 13:48:14 -0700 | [diff] [blame] | 1672 | { |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 1673 | return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode); |
Fabian Giesen | 0bbc38d | 2016-04-28 13:48:14 -0700 | [diff] [blame] | 1674 | } |
| 1675 | |
| 1676 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1677 | * 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] | 1678 | * 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] | 1679 | * return true if _any_ single-line macro of that name is defined. |
| 1680 | * Otherwise, will return true if a single-line macro with either |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1681 | * `nparam' or no parameters is defined. |
| 1682 | * |
| 1683 | * If a macro with precisely the right number of parameters is |
H. Peter Anvin | ef7468f | 2002-04-30 20:57:59 +0000 | [diff] [blame] | 1684 | * defined, or nparam is -1, the address of the definition structure |
| 1685 | * 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] | 1686 | * is NULL, no action will be taken regarding its contents, and no |
| 1687 | * error will occur. |
| 1688 | * |
| 1689 | * Note that this is also called with nparam zero to resolve |
| 1690 | * `ifdef'. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1691 | * |
| 1692 | * If you already know which context macro belongs to, you can pass |
| 1693 | * the context pointer as first parameter; if you won't but name begins |
| 1694 | * with %$ the context will be automatically computed. If all_contexts |
| 1695 | * is true, macro will be searched in outer contexts as well. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1696 | */ |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1697 | static bool |
H. Peter Anvin | b2a5fda | 2008-06-19 21:42:42 -0700 | [diff] [blame] | 1698 | smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn, |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1699 | bool nocase) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1700 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1701 | struct hash_table *smtbl; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1702 | SMacro *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1703 | |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1704 | if (ctx) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1705 | smtbl = &ctx->localmac; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1706 | } else if (name[0] == '%' && name[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1707 | if (cstk) |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1708 | ctx = get_ctx(name, &name); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1709 | if (!ctx) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1710 | return false; /* got to return _something_ */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1711 | smtbl = &ctx->localmac; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1712 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1713 | smtbl = &smacros; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1714 | } |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1715 | m = (SMacro *) hash_findix(smtbl, name); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1716 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1717 | while (m) { |
| 1718 | if (!mstrcmp(m->name, name, m->casesense && nocase) && |
Charles Crayne | 192d5b5 | 2007-10-18 19:02:42 -0700 | [diff] [blame] | 1719 | (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1720 | if (defn) { |
Charles Crayne | 192d5b5 | 2007-10-18 19:02:42 -0700 | [diff] [blame] | 1721 | if (nparam == (int) m->nparam || nparam == -1) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1722 | *defn = m; |
| 1723 | else |
| 1724 | *defn = NULL; |
| 1725 | } |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1726 | return true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1727 | } |
| 1728 | m = m->next; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1729 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1730 | |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1731 | return false; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1732 | } |
| 1733 | |
| 1734 | /* |
| 1735 | * Count and mark off the parameters in a multi-line macro call. |
| 1736 | * This is called both from within the multi-line macro expansion |
| 1737 | * code, and also to mark off the default parameters when provided |
| 1738 | * in a %macro definition line. |
| 1739 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1740 | static void count_mmac_params(Token * t, int *nparam, Token *** params) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1741 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1742 | int paramsize, brace; |
| 1743 | |
| 1744 | *nparam = paramsize = 0; |
| 1745 | *params = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1746 | while (t) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1747 | /* +1: we need space for the final NULL */ |
H. Peter Anvin | 91fb6f1 | 2008-09-01 10:56:33 -0700 | [diff] [blame] | 1748 | if (*nparam+1 >= paramsize) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1749 | paramsize += PARAM_DELTA; |
| 1750 | *params = nasm_realloc(*params, sizeof(**params) * paramsize); |
| 1751 | } |
| 1752 | skip_white_(t); |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1753 | brace = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1754 | if (tok_is_(t, "{")) |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1755 | brace++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1756 | (*params)[(*nparam)++] = t; |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1757 | if (brace) { |
| 1758 | while (brace && (t = t->next) != NULL) { |
| 1759 | if (tok_is_(t, "{")) |
| 1760 | brace++; |
| 1761 | else if (tok_is_(t, "}")) |
| 1762 | brace--; |
| 1763 | } |
| 1764 | |
| 1765 | if (t) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1766 | /* |
| 1767 | * Now we've found the closing brace, look further |
| 1768 | * for the comma. |
| 1769 | */ |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1770 | t = t->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1771 | skip_white_(t); |
| 1772 | if (tok_isnt_(t, ",")) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1773 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1774 | "braces do not enclose all of macro parameter"); |
| 1775 | while (tok_isnt_(t, ",")) |
| 1776 | t = t->next; |
| 1777 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1778 | } |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1779 | } else { |
| 1780 | while (tok_isnt_(t, ",")) |
| 1781 | t = t->next; |
| 1782 | } |
| 1783 | if (t) { /* got a comma/brace */ |
| 1784 | t = t->next; /* eat the comma */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1785 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1786 | } |
| 1787 | } |
| 1788 | |
| 1789 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1790 | * Determine whether one of the various `if' conditions is true or |
| 1791 | * not. |
| 1792 | * |
| 1793 | * We must free the tline we get passed. |
| 1794 | */ |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1795 | static bool if_condition(Token * tline, enum preproc_token ct) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1796 | { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1797 | enum pp_conditional i = PP_COND(ct); |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1798 | bool j; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 1799 | Token *t, *tt, *origline; |
| 1800 | struct ppscan pps; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1801 | struct tokenval tokval; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1802 | expr *evalresult; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1803 | enum pp_token_type needtype; |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1804 | char *p; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1805 | |
| 1806 | origline = tline; |
| 1807 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1808 | switch (i) { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1809 | case PPC_IFCTX: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1810 | j = false; /* have we matched yet? */ |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1811 | while (true) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1812 | skip_white_(tline); |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1813 | if (!tline) |
| 1814 | break; |
| 1815 | if (tline->type != TOK_ID) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1816 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1817 | "`%s' expects context identifiers", pp_directives[ct]); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1818 | free_tlist(origline); |
| 1819 | return -1; |
| 1820 | } |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1821 | if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1822 | j = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1823 | tline = tline->next; |
| 1824 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1825 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1826 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1827 | case PPC_IFDEF: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1828 | j = false; /* have we matched yet? */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1829 | while (tline) { |
| 1830 | skip_white_(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1831 | if (!tline || (tline->type != TOK_ID && |
| 1832 | (tline->type != TOK_PREPROC_ID || |
| 1833 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1834 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1835 | "`%s' expects macro identifiers", pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1836 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1837 | } |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1838 | if (smacro_defined(NULL, tline->text, 0, NULL, true)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1839 | j = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1840 | tline = tline->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1841 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1842 | break; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1843 | |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1844 | case PPC_IFENV: |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1845 | tline = expand_smacro(tline); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1846 | j = false; /* have we matched yet? */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1847 | while (tline) { |
| 1848 | skip_white_(tline); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1849 | if (!tline || (tline->type != TOK_ID && |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1850 | tline->type != TOK_STRING && |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1851 | (tline->type != TOK_PREPROC_ID || |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1852 | tline->text[1] != '!'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1853 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1854 | "`%s' expects environment variable names", |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1855 | pp_directives[ct]); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1856 | goto fail; |
| 1857 | } |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1858 | p = tline->text; |
| 1859 | if (tline->type == TOK_PREPROC_ID) |
| 1860 | p += 2; /* Skip leading %! */ |
| 1861 | if (*p == '\'' || *p == '\"' || *p == '`') |
| 1862 | nasm_unquote_cstr(p, ct); |
| 1863 | if (getenv(p)) |
| 1864 | j = true; |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1865 | tline = tline->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1866 | } |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1867 | break; |
| 1868 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1869 | case PPC_IFIDN: |
| 1870 | case PPC_IFIDNI: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1871 | tline = expand_smacro(tline); |
| 1872 | t = tt = tline; |
| 1873 | while (tok_isnt_(tt, ",")) |
| 1874 | tt = tt->next; |
| 1875 | if (!tt) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1876 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1877 | "`%s' expects two comma-separated arguments", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1878 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1879 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1880 | } |
| 1881 | tt = tt->next; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1882 | j = true; /* assume equality unless proved not */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1883 | while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) { |
| 1884 | if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1885 | nasm_error(ERR_NONFATAL, "`%s': more than one comma on line", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1886 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1887 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1888 | } |
| 1889 | if (t->type == TOK_WHITESPACE) { |
| 1890 | t = t->next; |
| 1891 | continue; |
| 1892 | } |
| 1893 | if (tt->type == TOK_WHITESPACE) { |
| 1894 | tt = tt->next; |
| 1895 | continue; |
| 1896 | } |
| 1897 | if (tt->type != t->type) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1898 | j = false; /* found mismatching tokens */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1899 | break; |
| 1900 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1901 | /* When comparing strings, need to unquote them first */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1902 | if (t->type == TOK_STRING) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1903 | size_t l1 = nasm_unquote(t->text, NULL); |
| 1904 | size_t l2 = nasm_unquote(tt->text, NULL); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 1905 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1906 | if (l1 != l2) { |
| 1907 | j = false; |
| 1908 | break; |
| 1909 | } |
| 1910 | if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) { |
| 1911 | j = false; |
| 1912 | break; |
| 1913 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1914 | } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1915 | j = false; /* found mismatching tokens */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1916 | break; |
| 1917 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1918 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1919 | t = t->next; |
| 1920 | tt = tt->next; |
| 1921 | } |
| 1922 | if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1923 | j = false; /* trailing gunk on one end or other */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1924 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1925 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1926 | case PPC_IFMACRO: |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1927 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1928 | bool found = false; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1929 | MMacro searching, *mmac; |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 1930 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1931 | skip_white_(tline); |
| 1932 | tline = expand_id(tline); |
| 1933 | if (!tok_type_(tline, TOK_ID)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1934 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1935 | "`%s' expects a macro name", pp_directives[ct]); |
| 1936 | goto fail; |
| 1937 | } |
| 1938 | searching.name = nasm_strdup(tline->text); |
| 1939 | searching.casesense = true; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1940 | searching.plus = false; |
| 1941 | searching.nolist = false; |
| 1942 | searching.in_progress = 0; |
| 1943 | searching.max_depth = 0; |
| 1944 | searching.rep_nest = NULL; |
| 1945 | searching.nparam_min = 0; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1946 | searching.nparam_max = INT_MAX; |
| 1947 | tline = expand_smacro(tline->next); |
| 1948 | skip_white_(tline); |
| 1949 | if (!tline) { |
| 1950 | } else if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1951 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1952 | "`%s' expects a parameter count or nothing", |
| 1953 | pp_directives[ct]); |
| 1954 | } else { |
| 1955 | searching.nparam_min = searching.nparam_max = |
| 1956 | readnum(tline->text, &j); |
| 1957 | if (j) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1958 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1959 | "unable to parse parameter count `%s'", |
| 1960 | tline->text); |
| 1961 | } |
| 1962 | if (tline && tok_is_(tline->next, "-")) { |
| 1963 | tline = tline->next->next; |
| 1964 | if (tok_is_(tline, "*")) |
| 1965 | searching.nparam_max = INT_MAX; |
| 1966 | else if (!tok_type_(tline, TOK_NUMBER)) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1967 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1968 | "`%s' expects a parameter count after `-'", |
| 1969 | pp_directives[ct]); |
| 1970 | else { |
| 1971 | searching.nparam_max = readnum(tline->text, &j); |
| 1972 | if (j) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1973 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1974 | "unable to parse parameter count `%s'", |
| 1975 | tline->text); |
Cyrill Gorcunov | c9244ea | 2017-10-22 15:25:48 +0300 | [diff] [blame] | 1976 | if (searching.nparam_min > searching.nparam_max) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1977 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1978 | "minimum parameter count exceeds maximum"); |
Cyrill Gorcunov | c9244ea | 2017-10-22 15:25:48 +0300 | [diff] [blame] | 1979 | searching.nparam_max = searching.nparam_min; |
| 1980 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1981 | } |
| 1982 | } |
| 1983 | if (tline && tok_is_(tline->next, "+")) { |
| 1984 | tline = tline->next; |
| 1985 | searching.plus = true; |
| 1986 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1987 | mmac = (MMacro *) hash_findix(&mmacros, searching.name); |
| 1988 | while (mmac) { |
| 1989 | if (!strcmp(mmac->name, searching.name) && |
| 1990 | (mmac->nparam_min <= searching.nparam_max |
| 1991 | || searching.plus) |
| 1992 | && (searching.nparam_min <= mmac->nparam_max |
| 1993 | || mmac->plus)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1994 | found = true; |
| 1995 | break; |
| 1996 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1997 | mmac = mmac->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1998 | } |
| 1999 | if (tline && tline->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2000 | nasm_error(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2001 | "trailing garbage after %%ifmacro ignored"); |
| 2002 | nasm_free(searching.name); |
| 2003 | j = found; |
| 2004 | break; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2005 | } |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 2006 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2007 | case PPC_IFID: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2008 | needtype = TOK_ID; |
| 2009 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2010 | case PPC_IFNUM: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2011 | needtype = TOK_NUMBER; |
| 2012 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2013 | case PPC_IFSTR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2014 | needtype = TOK_STRING; |
| 2015 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2016 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2017 | iftype: |
| 2018 | t = tline = expand_smacro(tline); |
H. Peter Anvin | d85d250 | 2008-05-04 17:53:31 -0700 | [diff] [blame] | 2019 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2020 | while (tok_type_(t, TOK_WHITESPACE) || |
| 2021 | (needtype == TOK_NUMBER && |
| 2022 | tok_type_(t, TOK_OTHER) && |
| 2023 | (t->text[0] == '-' || t->text[0] == '+') && |
| 2024 | !t->text[1])) |
| 2025 | t = t->next; |
H. Peter Anvin | d85d250 | 2008-05-04 17:53:31 -0700 | [diff] [blame] | 2026 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2027 | j = tok_type_(t, needtype); |
| 2028 | break; |
H. Peter Anvin | cbf768d | 2008-02-16 16:41:25 -0800 | [diff] [blame] | 2029 | |
| 2030 | case PPC_IFTOKEN: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2031 | t = tline = expand_smacro(tline); |
| 2032 | while (tok_type_(t, TOK_WHITESPACE)) |
| 2033 | t = t->next; |
H. Peter Anvin | cbf768d | 2008-02-16 16:41:25 -0800 | [diff] [blame] | 2034 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2035 | j = false; |
| 2036 | if (t) { |
| 2037 | t = t->next; /* Skip the actual token */ |
| 2038 | while (tok_type_(t, TOK_WHITESPACE)) |
| 2039 | t = t->next; |
| 2040 | j = !t; /* Should be nothing left */ |
| 2041 | } |
| 2042 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2043 | |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 2044 | case PPC_IFEMPTY: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2045 | t = tline = expand_smacro(tline); |
| 2046 | while (tok_type_(t, TOK_WHITESPACE)) |
| 2047 | t = t->next; |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 2048 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2049 | j = !t; /* Should be empty */ |
| 2050 | break; |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 2051 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2052 | case PPC_IF: |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 2053 | pps.tptr = tline = expand_smacro(tline); |
| 2054 | pps.ntokens = -1; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2055 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 2056 | evalresult = evaluate(ppscan, &pps, &tokval, |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2057 | NULL, pass | CRITICAL, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2058 | if (!evalresult) |
| 2059 | return -1; |
| 2060 | if (tokval.t_type) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2061 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2062 | "trailing garbage after expression ignored"); |
| 2063 | if (!is_simple(evalresult)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2064 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2065 | "non-constant value given to `%s'", pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2066 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2067 | } |
Chuck Crayne | 60ae75d | 2007-05-02 01:59:16 +0000 | [diff] [blame] | 2068 | j = reloc_value(evalresult) != 0; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2069 | break; |
H. Peter Anvin | 95e2882 | 2007-09-12 04:20:08 +0000 | [diff] [blame] | 2070 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2071 | default: |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2072 | nasm_error(ERR_FATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2073 | "preprocessor directive `%s' not yet implemented", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2074 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2075 | goto fail; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2076 | } |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2077 | |
| 2078 | free_tlist(origline); |
| 2079 | return j ^ PP_NEGATIVE(ct); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2080 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2081 | fail: |
| 2082 | free_tlist(origline); |
| 2083 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2084 | } |
| 2085 | |
| 2086 | /* |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2087 | * Common code for defining an smacro |
| 2088 | */ |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 2089 | static SMacro *define_smacro(Context *ctx, const char *mname, |
| 2090 | bool casesense, int nparam, Token *expansion) |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2091 | { |
| 2092 | SMacro *smac, **smhead; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2093 | struct hash_table *smtbl; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2094 | |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2095 | if (smacro_defined(ctx, mname, nparam, &smac, casesense)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2096 | if (!smac) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2097 | nasm_error(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2098 | "single-line macro `%s' defined both with and" |
| 2099 | " without parameters", mname); |
| 2100 | /* |
| 2101 | * Some instances of the old code considered this a failure, |
| 2102 | * some others didn't. What is the right thing to do here? |
| 2103 | */ |
| 2104 | free_tlist(expansion); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 2105 | return NULL; /* Failure */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2106 | } else { |
| 2107 | /* |
| 2108 | * We're redefining, so we have to take over an |
| 2109 | * existing SMacro structure. This means freeing |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 2110 | * what was already in it, but not the structure itself. |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2111 | */ |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 2112 | free_smacro(smac, false); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2113 | } |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2114 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2115 | smtbl = ctx ? &ctx->localmac : &smacros; |
| 2116 | smhead = (SMacro **) hash_findi_add(smtbl, mname); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 2117 | nasm_new(smac); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2118 | smac->next = *smhead; |
| 2119 | *smhead = smac; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2120 | } |
| 2121 | smac->name = nasm_strdup(mname); |
| 2122 | smac->casesense = casesense; |
| 2123 | smac->nparam = nparam; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 2124 | smac->e.expansion = expansion; |
| 2125 | return smac; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2126 | } |
| 2127 | |
| 2128 | /* |
| 2129 | * Undefine an smacro |
| 2130 | */ |
| 2131 | static void undef_smacro(Context *ctx, const char *mname) |
| 2132 | { |
| 2133 | SMacro **smhead, *s, **sp; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2134 | struct hash_table *smtbl; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2135 | |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2136 | smtbl = ctx ? &ctx->localmac : &smacros; |
| 2137 | smhead = (SMacro **)hash_findi(smtbl, mname, NULL); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2138 | |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2139 | if (smhead) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2140 | /* |
| 2141 | * We now have a macro name... go hunt for it. |
| 2142 | */ |
| 2143 | sp = smhead; |
| 2144 | while ((s = *sp) != NULL) { |
| 2145 | if (!mstrcmp(s->name, mname, s->casesense)) { |
| 2146 | *sp = s->next; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 2147 | free_smacro(s, true); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2148 | } else { |
| 2149 | sp = &s->next; |
| 2150 | } |
| 2151 | } |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2152 | } |
| 2153 | } |
| 2154 | |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2155 | /* |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2156 | * Parse a mmacro specification. |
| 2157 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2158 | 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] | 2159 | { |
| 2160 | bool err; |
| 2161 | |
| 2162 | tline = tline->next; |
| 2163 | skip_white_(tline); |
| 2164 | tline = expand_id(tline); |
| 2165 | if (!tok_type_(tline, TOK_ID)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2166 | nasm_error(ERR_NONFATAL, "`%s' expects a macro name", directive); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2167 | return false; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2168 | } |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2169 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2170 | def->prev = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2171 | def->name = nasm_strdup(tline->text); |
| 2172 | def->plus = false; |
| 2173 | def->nolist = false; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2174 | def->in_progress = 0; |
| 2175 | def->rep_nest = NULL; |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2176 | def->nparam_min = 0; |
| 2177 | def->nparam_max = 0; |
| 2178 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2179 | tline = expand_smacro(tline->next); |
| 2180 | skip_white_(tline); |
| 2181 | if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2182 | nasm_error(ERR_NONFATAL, "`%s' expects a parameter count", directive); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2183 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2184 | def->nparam_min = def->nparam_max = |
| 2185 | readnum(tline->text, &err); |
| 2186 | if (err) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2187 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2188 | "unable to parse parameter count `%s'", tline->text); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2189 | } |
| 2190 | if (tline && tok_is_(tline->next, "-")) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2191 | tline = tline->next->next; |
| 2192 | if (tok_is_(tline, "*")) { |
| 2193 | def->nparam_max = INT_MAX; |
| 2194 | } else if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2195 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2196 | "`%s' expects a parameter count after `-'", directive); |
| 2197 | } else { |
| 2198 | def->nparam_max = readnum(tline->text, &err); |
| 2199 | if (err) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2200 | nasm_error(ERR_NONFATAL, "unable to parse parameter count `%s'", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2201 | tline->text); |
| 2202 | } |
| 2203 | if (def->nparam_min > def->nparam_max) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2204 | nasm_error(ERR_NONFATAL, "minimum parameter count exceeds maximum"); |
Cyrill Gorcunov | c9244ea | 2017-10-22 15:25:48 +0300 | [diff] [blame] | 2205 | def->nparam_max = def->nparam_min; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2206 | } |
| 2207 | } |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2208 | } |
| 2209 | if (tline && tok_is_(tline->next, "+")) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2210 | tline = tline->next; |
| 2211 | def->plus = true; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2212 | } |
| 2213 | if (tline && tok_type_(tline->next, TOK_ID) && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2214 | !nasm_stricmp(tline->next->text, ".nolist")) { |
| 2215 | tline = tline->next; |
| 2216 | def->nolist = true; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2217 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2218 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2219 | /* |
| 2220 | * Handle default parameters. |
| 2221 | */ |
| 2222 | if (tline && tline->next) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2223 | def->dlist = tline->next; |
| 2224 | tline->next = NULL; |
| 2225 | count_mmac_params(def->dlist, &def->ndefs, &def->defaults); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2226 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2227 | def->dlist = NULL; |
| 2228 | def->defaults = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2229 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2230 | def->expansion = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2231 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2232 | if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2233 | !def->plus) |
H. Peter Anvin (Intel) | 77f53ba | 2018-12-12 14:38:50 -0800 | [diff] [blame] | 2234 | nasm_error(ERR_WARNING|ERR_PASS1|WARN_MDP, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2235 | "too many default macro parameters"); |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2236 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2237 | return true; |
| 2238 | } |
| 2239 | |
| 2240 | |
| 2241 | /* |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2242 | * Decode a size directive |
| 2243 | */ |
| 2244 | static int parse_size(const char *str) { |
| 2245 | static const char *size_names[] = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2246 | { "byte", "dword", "oword", "qword", "tword", "word", "yword" }; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2247 | static const int sizes[] = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2248 | { 0, 1, 4, 16, 8, 10, 2, 32 }; |
Cyrill Gorcunov | c713b5f | 2018-09-29 14:30:14 +0300 | [diff] [blame] | 2249 | 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] | 2250 | } |
| 2251 | |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2252 | /* |
| 2253 | * Process a preprocessor %pragma directive. Currently there are none. |
| 2254 | * Gets passed the token list starting with the "preproc" token from |
| 2255 | * "%pragma preproc". |
| 2256 | */ |
| 2257 | static void do_pragma_preproc(Token *tline) |
| 2258 | { |
| 2259 | /* Skip to the real stuff */ |
| 2260 | tline = tline->next; |
| 2261 | skip_white_(tline); |
| 2262 | if (!tline) |
| 2263 | return; |
| 2264 | |
| 2265 | (void)tline; /* Nothing else to do at present */ |
| 2266 | } |
| 2267 | |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2268 | /** |
| 2269 | * find and process preprocessor directive in passed line |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2270 | * Find out if a line contains a preprocessor directive, and deal |
| 2271 | * with it if so. |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2272 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2273 | * If a directive _is_ found, it is the responsibility of this routine |
| 2274 | * (and not the caller) to free_tlist() the line. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2275 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2276 | * @param tline a pointer to the current tokeninzed line linked list |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2277 | * @param output if this directive generated output |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2278 | * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2279 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2280 | */ |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2281 | static int do_directive(Token *tline, char **output) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2282 | { |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2283 | enum preproc_token i; |
| 2284 | int j; |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 2285 | bool err; |
| 2286 | int nparam; |
| 2287 | bool nolist; |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 2288 | bool casesense; |
H. Peter Anvin | 8cfdb9d | 2007-09-14 18:36:01 -0700 | [diff] [blame] | 2289 | int k, m; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2290 | int offset; |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 2291 | char *p, *pp; |
| 2292 | const char *found_path; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 2293 | const char *mname; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 2294 | struct ppscan pps; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2295 | Include *inc; |
| 2296 | Context *ctx; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2297 | Cond *cond; |
| 2298 | MMacro *mmac, **mmhead; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 2299 | Token *t = NULL, *tt, *param_start, *macro_start, *last, *origline; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2300 | Line *l; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2301 | struct tokenval tokval; |
| 2302 | expr *evalresult; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2303 | MMacro *tmp_defining; /* Used when manipulating rep_nest */ |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 2304 | int64_t count; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 2305 | size_t len; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2306 | int severity; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 2307 | const char *dname; /* Name of directive, for messages */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2308 | |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2309 | *output = NULL; /* No output generated */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2310 | origline = tline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2311 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2312 | skip_white_(tline); |
H. Peter Anvin | f2936d7 | 2008-06-04 15:11:23 -0700 | [diff] [blame] | 2313 | if (!tline || !tok_type_(tline, TOK_PREPROC_ID) || |
Cyrill Gorcunov | 4b5b737 | 2018-10-29 22:54:08 +0300 | [diff] [blame] | 2314 | (tline->text[0] && (tline->text[1] == '%' || |
| 2315 | tline->text[1] == '$' || |
| 2316 | tline->text[1] == '!'))) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2317 | return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2318 | |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2319 | i = pp_token_hash(tline->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2320 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2321 | /* |
| 2322 | * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO |
| 2323 | * since they are known to be buggy at moment, we need to fix them |
| 2324 | * in future release (2.09-2.10) |
| 2325 | */ |
Philipp Kloke | b432f57 | 2013-03-31 12:01:23 +0200 | [diff] [blame] | 2326 | if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2327 | nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'", |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2328 | tline->text); |
| 2329 | return NO_DIRECTIVE_FOUND; |
| 2330 | } |
| 2331 | |
| 2332 | /* |
| 2333 | * If we're in a non-emitting branch of a condition construct, |
| 2334 | * or walking to the end of an already terminated %rep block, |
| 2335 | * we should ignore all directives except for condition |
| 2336 | * directives. |
| 2337 | */ |
| 2338 | if (((istk->conds && !emitting(istk->conds->state)) || |
| 2339 | (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) { |
| 2340 | return NO_DIRECTIVE_FOUND; |
| 2341 | } |
| 2342 | |
| 2343 | /* |
| 2344 | * If we're defining a macro or reading a %rep block, we should |
| 2345 | * ignore all directives except for %macro/%imacro (which nest), |
| 2346 | * %endm/%endmacro, and (only if we're in a %rep block) %endrep. |
| 2347 | * If we're in a %rep block, another %rep nests, so should be let through. |
| 2348 | */ |
| 2349 | if (defining && i != PP_MACRO && i != PP_IMACRO && |
| 2350 | i != PP_RMACRO && i != PP_IRMACRO && |
| 2351 | i != PP_ENDMACRO && i != PP_ENDM && |
| 2352 | (defining->name || (i != PP_ENDREP && i != PP_REP))) { |
| 2353 | return NO_DIRECTIVE_FOUND; |
| 2354 | } |
| 2355 | |
| 2356 | if (defining) { |
| 2357 | if (i == PP_MACRO || i == PP_IMACRO || |
| 2358 | i == PP_RMACRO || i == PP_IRMACRO) { |
| 2359 | nested_mac_count++; |
| 2360 | return NO_DIRECTIVE_FOUND; |
| 2361 | } else if (nested_mac_count > 0) { |
| 2362 | if (i == PP_ENDMACRO) { |
| 2363 | nested_mac_count--; |
| 2364 | return NO_DIRECTIVE_FOUND; |
| 2365 | } |
| 2366 | } |
| 2367 | if (!defining->name) { |
| 2368 | if (i == PP_REP) { |
| 2369 | nested_rep_count++; |
| 2370 | return NO_DIRECTIVE_FOUND; |
| 2371 | } else if (nested_rep_count > 0) { |
| 2372 | if (i == PP_ENDREP) { |
| 2373 | nested_rep_count--; |
| 2374 | return NO_DIRECTIVE_FOUND; |
| 2375 | } |
| 2376 | } |
| 2377 | } |
| 2378 | } |
| 2379 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 2380 | dname = pp_directives[i]; /* Directive name, for error messages */ |
| 2381 | casesense = true; /* Default to case sensitive */ |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2382 | switch (i) { |
| 2383 | case PP_INVALID: |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2384 | nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'", |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2385 | tline->text); |
| 2386 | return NO_DIRECTIVE_FOUND; /* didn't get it */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2387 | |
H. Peter Anvin | 3f87a2a | 2016-10-04 14:07:19 -0700 | [diff] [blame] | 2388 | case PP_PRAGMA: |
| 2389 | /* |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2390 | * %pragma namespace options... |
| 2391 | * |
| 2392 | * The namespace "preproc" is reserved for the preprocessor; |
| 2393 | * all other namespaces generate a [pragma] assembly directive. |
| 2394 | * |
| 2395 | * Invalid %pragmas are ignored and may have different |
| 2396 | * meaning in future versions of NASM. |
H. Peter Anvin | 3f87a2a | 2016-10-04 14:07:19 -0700 | [diff] [blame] | 2397 | */ |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2398 | tline = tline->next; |
| 2399 | skip_white_(tline); |
| 2400 | tline = expand_smacro(tline); |
| 2401 | if (tok_type_(tline, TOK_ID)) { |
| 2402 | if (!nasm_stricmp(tline->text, "preproc")) { |
| 2403 | /* Preprocessor pragma */ |
| 2404 | do_pragma_preproc(tline); |
| 2405 | } else { |
| 2406 | /* Build the assembler directive */ |
| 2407 | t = new_Token(NULL, TOK_OTHER, "[", 1); |
| 2408 | t->next = new_Token(NULL, TOK_ID, "pragma", 6); |
| 2409 | t->next->next = new_Token(tline, TOK_WHITESPACE, NULL, 0); |
| 2410 | tline = t; |
| 2411 | for (t = tline; t->next; t = t->next) |
| 2412 | ; |
| 2413 | t->next = new_Token(NULL, TOK_OTHER, "]", 1); |
| 2414 | /* true here can be revisited in the future */ |
| 2415 | *output = detoken(tline, true); |
| 2416 | } |
| 2417 | } |
H. Peter Anvin | 3f87a2a | 2016-10-04 14:07:19 -0700 | [diff] [blame] | 2418 | free_tlist(origline); |
| 2419 | return DIRECTIVE_FOUND; |
| 2420 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2421 | case PP_STACKSIZE: |
| 2422 | /* Directive to tell NASM what the default stack size is. The |
| 2423 | * default is for a 16-bit stack, and this can be overriden with |
| 2424 | * %stacksize large. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2425 | */ |
| 2426 | tline = tline->next; |
| 2427 | if (tline && tline->type == TOK_WHITESPACE) |
| 2428 | tline = tline->next; |
| 2429 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 2430 | nasm_error(ERR_NONFATAL, "`%s' missing size parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2431 | free_tlist(origline); |
| 2432 | return DIRECTIVE_FOUND; |
| 2433 | } |
| 2434 | if (nasm_stricmp(tline->text, "flat") == 0) { |
| 2435 | /* All subsequent ARG directives are for a 32-bit stack */ |
| 2436 | StackSize = 4; |
| 2437 | StackPointer = "ebp"; |
| 2438 | ArgOffset = 8; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2439 | LocalOffset = 0; |
Charles Crayne | 7eaf919 | 2007-11-08 22:11:14 -0800 | [diff] [blame] | 2440 | } else if (nasm_stricmp(tline->text, "flat64") == 0) { |
| 2441 | /* All subsequent ARG directives are for a 64-bit stack */ |
| 2442 | StackSize = 8; |
| 2443 | StackPointer = "rbp"; |
Per Jessen | 53252e0 | 2010-02-11 00:16:59 +0300 | [diff] [blame] | 2444 | ArgOffset = 16; |
Charles Crayne | 7eaf919 | 2007-11-08 22:11:14 -0800 | [diff] [blame] | 2445 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2446 | } else if (nasm_stricmp(tline->text, "large") == 0) { |
| 2447 | /* All subsequent ARG directives are for a 16-bit stack, |
| 2448 | * far function call. |
| 2449 | */ |
| 2450 | StackSize = 2; |
| 2451 | StackPointer = "bp"; |
| 2452 | ArgOffset = 4; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2453 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2454 | } else if (nasm_stricmp(tline->text, "small") == 0) { |
| 2455 | /* All subsequent ARG directives are for a 16-bit stack, |
| 2456 | * far function call. We don't support near functions. |
| 2457 | */ |
| 2458 | StackSize = 2; |
| 2459 | StackPointer = "bp"; |
| 2460 | ArgOffset = 6; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2461 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2462 | } else { |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 2463 | nasm_error(ERR_NONFATAL, "`%s' invalid size type", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2464 | free_tlist(origline); |
| 2465 | return DIRECTIVE_FOUND; |
| 2466 | } |
| 2467 | free_tlist(origline); |
| 2468 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2469 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2470 | case PP_ARG: |
| 2471 | /* TASM like ARG directive to define arguments to functions, in |
| 2472 | * the following form: |
| 2473 | * |
| 2474 | * ARG arg1:WORD, arg2:DWORD, arg4:QWORD |
| 2475 | */ |
| 2476 | offset = ArgOffset; |
| 2477 | do { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 2478 | char *arg, directive[256]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2479 | int size = StackSize; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2480 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2481 | /* Find the argument name */ |
| 2482 | tline = tline->next; |
| 2483 | if (tline && tline->type == TOK_WHITESPACE) |
| 2484 | tline = tline->next; |
| 2485 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 2486 | nasm_error(ERR_NONFATAL, "`%s' missing argument parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2487 | free_tlist(origline); |
| 2488 | return DIRECTIVE_FOUND; |
| 2489 | } |
| 2490 | arg = tline->text; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2491 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2492 | /* Find the argument size type */ |
| 2493 | tline = tline->next; |
| 2494 | if (!tline || tline->type != TOK_OTHER |
| 2495 | || tline->text[0] != ':') { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2496 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 2497 | "syntax error processing `%s' directive", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2498 | free_tlist(origline); |
| 2499 | return DIRECTIVE_FOUND; |
| 2500 | } |
| 2501 | tline = tline->next; |
| 2502 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 2503 | nasm_error(ERR_NONFATAL, "`%s' missing size type parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2504 | free_tlist(origline); |
| 2505 | return DIRECTIVE_FOUND; |
| 2506 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2507 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2508 | /* Allow macro expansion of type parameter */ |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2509 | tt = tokenize(tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2510 | tt = expand_smacro(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2511 | size = parse_size(tt->text); |
| 2512 | if (!size) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2513 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 2514 | "invalid size type for `%s' missing directive", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2515 | free_tlist(tt); |
| 2516 | free_tlist(origline); |
| 2517 | return DIRECTIVE_FOUND; |
| 2518 | } |
| 2519 | free_tlist(tt); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2520 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2521 | /* Round up to even stack slots */ |
| 2522 | size = ALIGN(size, StackSize); |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2523 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2524 | /* Now define the macro for the argument */ |
| 2525 | snprintf(directive, sizeof(directive), "%%define %s (%s+%d)", |
| 2526 | arg, StackPointer, offset); |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2527 | do_directive(tokenize(directive), output); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2528 | offset += size; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2529 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2530 | /* Move to the next argument in the list */ |
| 2531 | tline = tline->next; |
| 2532 | if (tline && tline->type == TOK_WHITESPACE) |
| 2533 | tline = tline->next; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2534 | } while (tline && tline->type == TOK_OTHER && tline->text[0] == ','); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2535 | ArgOffset = offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2536 | free_tlist(origline); |
| 2537 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2538 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2539 | case PP_LOCAL: |
| 2540 | /* TASM like LOCAL directive to define local variables for a |
| 2541 | * function, in the following form: |
| 2542 | * |
| 2543 | * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize |
| 2544 | * |
| 2545 | * The '= LocalSize' at the end is ignored by NASM, but is |
| 2546 | * required by TASM to define the local parameter size (and used |
| 2547 | * by the TASM macro package). |
| 2548 | */ |
| 2549 | offset = LocalOffset; |
| 2550 | do { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 2551 | char *local, directive[256]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2552 | int size = StackSize; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2553 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2554 | /* Find the argument name */ |
| 2555 | tline = tline->next; |
| 2556 | if (tline && tline->type == TOK_WHITESPACE) |
| 2557 | tline = tline->next; |
| 2558 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2559 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 2560 | "`%s' missing argument parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2561 | free_tlist(origline); |
| 2562 | return DIRECTIVE_FOUND; |
| 2563 | } |
| 2564 | local = tline->text; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2565 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2566 | /* Find the argument size type */ |
| 2567 | tline = tline->next; |
| 2568 | if (!tline || tline->type != TOK_OTHER |
| 2569 | || tline->text[0] != ':') { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2570 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 2571 | "syntax error processing `%s' directive", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2572 | free_tlist(origline); |
| 2573 | return DIRECTIVE_FOUND; |
| 2574 | } |
| 2575 | tline = tline->next; |
| 2576 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2577 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 2578 | "`%s' missing size type parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2579 | free_tlist(origline); |
| 2580 | return DIRECTIVE_FOUND; |
| 2581 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2582 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2583 | /* Allow macro expansion of type parameter */ |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2584 | tt = tokenize(tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2585 | tt = expand_smacro(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2586 | size = parse_size(tt->text); |
| 2587 | if (!size) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2588 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 2589 | "invalid size type for `%s' missing directive", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2590 | free_tlist(tt); |
| 2591 | free_tlist(origline); |
| 2592 | return DIRECTIVE_FOUND; |
| 2593 | } |
| 2594 | free_tlist(tt); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2595 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2596 | /* Round up to even stack slots */ |
| 2597 | size = ALIGN(size, StackSize); |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2598 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2599 | offset += size; /* Negative offset, increment before */ |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2600 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2601 | /* Now define the macro for the argument */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2602 | snprintf(directive, sizeof(directive), "%%define %s (%s-%d)", |
| 2603 | local, StackPointer, offset); |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2604 | do_directive(tokenize(directive), output); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2605 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2606 | /* Now define the assign to setup the enter_c macro correctly */ |
| 2607 | snprintf(directive, sizeof(directive), |
| 2608 | "%%assign %%$localsize %%$localsize+%d", size); |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2609 | do_directive(tokenize(directive), output); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2610 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2611 | /* Move to the next argument in the list */ |
| 2612 | tline = tline->next; |
| 2613 | if (tline && tline->type == TOK_WHITESPACE) |
| 2614 | tline = tline->next; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2615 | } while (tline && tline->type == TOK_OTHER && tline->text[0] == ','); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2616 | LocalOffset = offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2617 | free_tlist(origline); |
| 2618 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2619 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2620 | case PP_CLEAR: |
| 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 | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 2623 | "trailing garbage after `%s' ignored", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2624 | free_macros(); |
| 2625 | init_macros(); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2626 | free_tlist(origline); |
| 2627 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2628 | |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2629 | case PP_DEPEND: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2630 | t = tline->next = expand_smacro(tline->next); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2631 | skip_white_(t); |
| 2632 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2633 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 2634 | nasm_error(ERR_NONFATAL, "`%s' expects a file name", dname); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2635 | free_tlist(origline); |
| 2636 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2637 | } |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2638 | if (t->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2639 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 2640 | "trailing garbage after `%s' ignored", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2641 | p = t->text; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2642 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2643 | nasm_unquote_cstr(p, i); |
H. Peter Anvin | 436e367 | 2016-10-04 01:12:28 -0700 | [diff] [blame] | 2644 | nasm_add_string_to_strlist(dephead, p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2645 | free_tlist(origline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2646 | return DIRECTIVE_FOUND; |
| 2647 | |
| 2648 | case PP_INCLUDE: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2649 | t = tline->next = expand_smacro(tline->next); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2650 | skip_white_(t); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2651 | |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2652 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2653 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 2654 | nasm_error(ERR_NONFATAL, "`%s' expects a file name", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2655 | free_tlist(origline); |
| 2656 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2657 | } |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2658 | if (t->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2659 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 2660 | "trailing garbage after `%s' ignored", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2661 | p = t->text; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2662 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2663 | nasm_unquote_cstr(p, i); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2664 | inc = nasm_malloc(sizeof(Include)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2665 | inc->next = istk; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2666 | inc->conds = NULL; |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 2667 | found_path = NULL; |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 2668 | inc->fp = inc_fopen(p, dephead, &found_path, |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 2669 | pass == 0 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2670 | if (!inc->fp) { |
| 2671 | /* -MG given but file not found */ |
| 2672 | nasm_free(inc); |
| 2673 | } else { |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 2674 | inc->fname = src_set_fname(found_path ? found_path : p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2675 | inc->lineno = src_set_linnum(0); |
| 2676 | inc->lineinc = 1; |
| 2677 | inc->expansion = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2678 | inc->mstk = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2679 | istk = inc; |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 2680 | lfmt->uplevel(LIST_INCLUDE); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2681 | } |
| 2682 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2683 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2684 | |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2685 | case PP_USE: |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2686 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2687 | static macros_t *use_pkg; |
| 2688 | const char *pkg_macro = NULL; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2689 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2690 | tline = tline->next; |
| 2691 | skip_white_(tline); |
| 2692 | tline = expand_id(tline); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2693 | |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2694 | if (!tline || (tline->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2695 | tline->type != TOK_INTERNAL_STRING && |
| 2696 | tline->type != TOK_ID)) { |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 2697 | nasm_error(ERR_NONFATAL, "`%s' expects a package name", dname); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2698 | free_tlist(origline); |
| 2699 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2700 | } |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2701 | if (tline->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2702 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 2703 | "trailing garbage after `%s' ignored", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2704 | if (tline->type == TOK_STRING) |
| 2705 | nasm_unquote_cstr(tline->text, i); |
| 2706 | use_pkg = nasm_stdmac_find_package(tline->text); |
| 2707 | if (!use_pkg) |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 2708 | nasm_error(ERR_NONFATAL, "unknown `%s' package: %s", dname, tline->text); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2709 | else |
| 2710 | 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] | 2711 | if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2712 | /* Not already included, go ahead and include it */ |
| 2713 | stdmacpos = use_pkg; |
| 2714 | } |
| 2715 | free_tlist(origline); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2716 | return DIRECTIVE_FOUND; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2717 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2718 | case PP_PUSH: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2719 | case PP_REPL: |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2720 | case PP_POP: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2721 | tline = tline->next; |
| 2722 | skip_white_(tline); |
| 2723 | tline = expand_id(tline); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2724 | if (tline) { |
| 2725 | if (!tok_type_(tline, TOK_ID)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2726 | nasm_error(ERR_NONFATAL, "`%s' expects a context identifier", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2727 | pp_directives[i]); |
| 2728 | free_tlist(origline); |
| 2729 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2730 | } |
| 2731 | if (tline->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2732 | nasm_error(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2733 | "trailing garbage after `%s' ignored", |
| 2734 | pp_directives[i]); |
| 2735 | p = nasm_strdup(tline->text); |
| 2736 | } else { |
| 2737 | p = NULL; /* Anonymous */ |
| 2738 | } |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2739 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2740 | if (i == PP_PUSH) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2741 | ctx = nasm_malloc(sizeof(Context)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2742 | ctx->next = cstk; |
| 2743 | hash_init(&ctx->localmac, HASH_SMALL); |
| 2744 | ctx->name = p; |
| 2745 | ctx->number = unique++; |
| 2746 | cstk = ctx; |
| 2747 | } else { |
| 2748 | /* %pop or %repl */ |
| 2749 | if (!cstk) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2750 | nasm_error(ERR_NONFATAL, "`%s': context stack is empty", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2751 | pp_directives[i]); |
| 2752 | } else if (i == PP_POP) { |
| 2753 | if (p && (!cstk->name || nasm_stricmp(p, cstk->name))) |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 2754 | nasm_error(ERR_NONFATAL, "`%s' in wrong context: %s, " |
| 2755 | "expected %s", |
| 2756 | dname, cstk->name ? cstk->name : "anonymous", p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2757 | else |
| 2758 | ctx_pop(); |
| 2759 | } else { |
| 2760 | /* i == PP_REPL */ |
| 2761 | nasm_free(cstk->name); |
| 2762 | cstk->name = p; |
| 2763 | p = NULL; |
| 2764 | } |
| 2765 | nasm_free(p); |
| 2766 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2767 | free_tlist(origline); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2768 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2769 | case PP_FATAL: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2770 | severity = ERR_FATAL; |
| 2771 | goto issue_error; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2772 | case PP_ERROR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2773 | severity = ERR_NONFATAL; |
| 2774 | goto issue_error; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2775 | case PP_WARNING: |
H. Peter Anvin (Intel) | 77f53ba | 2018-12-12 14:38:50 -0800 | [diff] [blame] | 2776 | severity = ERR_WARNING|WARN_USER; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2777 | goto issue_error; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2778 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2779 | issue_error: |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2780 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2781 | /* Only error out if this is the final pass */ |
| 2782 | if (pass != 2 && i != PP_FATAL) |
| 2783 | return DIRECTIVE_FOUND; |
| 2784 | |
| 2785 | tline->next = expand_smacro(tline->next); |
| 2786 | tline = tline->next; |
| 2787 | skip_white_(tline); |
| 2788 | t = tline ? tline->next : NULL; |
| 2789 | skip_white_(t); |
| 2790 | if (tok_type_(tline, TOK_STRING) && !t) { |
| 2791 | /* The line contains only a quoted string */ |
| 2792 | p = tline->text; |
| 2793 | nasm_unquote(p, NULL); /* Ignore NUL character truncation */ |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2794 | nasm_error(severity, "%s", p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2795 | } else { |
| 2796 | /* Not a quoted string, or more than a quoted string */ |
| 2797 | p = detoken(tline, false); |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2798 | nasm_error(severity, "%s", p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2799 | nasm_free(p); |
| 2800 | } |
| 2801 | free_tlist(origline); |
| 2802 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2803 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2804 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2805 | CASE_PP_IF: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2806 | if (istk->conds && !emitting(istk->conds->state)) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2807 | j = COND_NEVER; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2808 | else { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2809 | j = if_condition(tline->next, i); |
| 2810 | tline->next = NULL; /* it got freed */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2811 | j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2812 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2813 | cond = nasm_malloc(sizeof(Cond)); |
| 2814 | cond->next = istk->conds; |
| 2815 | cond->state = j; |
| 2816 | istk->conds = cond; |
| 2817 | if(istk->mstk) |
| 2818 | istk->mstk->condcnt ++; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2819 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2820 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2821 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2822 | CASE_PP_ELIF: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2823 | if (!istk->conds) |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 2824 | nasm_error(ERR_FATAL, "`%s': no matching `%%if'", dname); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2825 | switch(istk->conds->state) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2826 | case COND_IF_TRUE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2827 | istk->conds->state = COND_DONE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2828 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2829 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2830 | case COND_DONE: |
| 2831 | case COND_NEVER: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2832 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2833 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2834 | case COND_ELSE_TRUE: |
| 2835 | case COND_ELSE_FALSE: |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2836 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND, |
| 2837 | "`%%elif' after `%%else' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2838 | istk->conds->state = COND_NEVER; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2839 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2840 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2841 | case COND_IF_FALSE: |
| 2842 | /* |
| 2843 | * IMPORTANT: In the case of %if, we will already have |
| 2844 | * called expand_mmac_params(); however, if we're |
| 2845 | * processing an %elif we must have been in a |
| 2846 | * non-emitting mode, which would have inhibited |
| 2847 | * the normal invocation of expand_mmac_params(). |
| 2848 | * Therefore, we have to do it explicitly here. |
| 2849 | */ |
| 2850 | j = if_condition(expand_mmac_params(tline->next), i); |
| 2851 | tline->next = NULL; /* it got freed */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2852 | istk->conds->state = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2853 | j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; |
| 2854 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2855 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2856 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 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_ELSE: |
| 2860 | if (tline->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2861 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND, |
| 2862 | "trailing garbage after `%%else' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2863 | if (!istk->conds) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2864 | nasm_fatal(0, "`%%else: no matching `%%if'"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2865 | switch(istk->conds->state) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2866 | case COND_IF_TRUE: |
| 2867 | case COND_DONE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2868 | istk->conds->state = COND_ELSE_FALSE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2869 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2870 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2871 | case COND_NEVER: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2872 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2873 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2874 | case COND_IF_FALSE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2875 | istk->conds->state = COND_ELSE_TRUE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2876 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2877 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2878 | case COND_ELSE_TRUE: |
| 2879 | case COND_ELSE_FALSE: |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2880 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2881 | "`%%else' after `%%else' ignored."); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2882 | istk->conds->state = COND_NEVER; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2883 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2884 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2885 | free_tlist(origline); |
| 2886 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2887 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2888 | case PP_ENDIF: |
| 2889 | if (tline->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2890 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND, |
| 2891 | "trailing garbage after `%%endif' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2892 | if (!istk->conds) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2893 | nasm_error(ERR_FATAL, "`%%endif': no matching `%%if'"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2894 | cond = istk->conds; |
| 2895 | istk->conds = cond->next; |
| 2896 | nasm_free(cond); |
| 2897 | if(istk->mstk) |
| 2898 | istk->mstk->condcnt --; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2899 | free_tlist(origline); |
| 2900 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2901 | |
H. Peter Anvin | db8f96e | 2009-07-15 09:07:29 -0400 | [diff] [blame] | 2902 | case PP_IRMACRO: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2903 | case PP_IMACRO: |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 2904 | casesense = false; |
| 2905 | /* fall through */ |
| 2906 | case PP_RMACRO: |
| 2907 | case PP_MACRO: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2908 | if (defining) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2909 | nasm_error(ERR_FATAL, "`%s': already defining a macro", |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 2910 | dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2911 | return DIRECTIVE_FOUND; |
| 2912 | } |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 2913 | defining = nasm_zalloc(sizeof(MMacro)); |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 2914 | defining->max_depth = ((i == PP_RMACRO) || (i == PP_IRMACRO)) |
| 2915 | ? nasm_limit[LIMIT_MACROS] : 0; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 2916 | defining->casesense = casesense; |
| 2917 | if (!parse_mmacro_spec(tline, defining, dname)) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2918 | nasm_free(defining); |
| 2919 | defining = NULL; |
| 2920 | return DIRECTIVE_FOUND; |
| 2921 | } |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2922 | |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 2923 | src_get(&defining->xline, &defining->fname); |
| 2924 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2925 | mmac = (MMacro *) hash_findix(&mmacros, defining->name); |
| 2926 | while (mmac) { |
| 2927 | if (!strcmp(mmac->name, defining->name) && |
| 2928 | (mmac->nparam_min <= defining->nparam_max |
| 2929 | || defining->plus) |
| 2930 | && (defining->nparam_min <= mmac->nparam_max |
| 2931 | || mmac->plus)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2932 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2933 | "redefining multi-line macro `%s'", defining->name); |
| 2934 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2935 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2936 | mmac = mmac->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2937 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2938 | free_tlist(origline); |
| 2939 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2940 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2941 | case PP_ENDM: |
| 2942 | case PP_ENDMACRO: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2943 | if (! (defining && defining->name)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2944 | nasm_error(ERR_NONFATAL, "`%s': not defining a macro", tline->text); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2945 | return DIRECTIVE_FOUND; |
| 2946 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2947 | mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name); |
| 2948 | defining->next = *mmhead; |
| 2949 | *mmhead = defining; |
| 2950 | defining = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2951 | free_tlist(origline); |
| 2952 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2953 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2954 | case PP_EXITMACRO: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2955 | /* |
| 2956 | * We must search along istk->expansion until we hit a |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2957 | * macro-end marker for a macro with a name. Then we |
| 2958 | * bypass all lines between exitmacro and endmacro. |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2959 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2960 | list_for_each(l, istk->expansion) |
| 2961 | if (l->finishes && l->finishes->name) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2962 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2963 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2964 | if (l) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2965 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2966 | * Remove all conditional entries relative to this |
| 2967 | * macro invocation. (safe to do in this context) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2968 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2969 | for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) { |
| 2970 | cond = istk->conds; |
| 2971 | istk->conds = cond->next; |
| 2972 | nasm_free(cond); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2973 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2974 | istk->expansion = l; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2975 | } else { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2976 | nasm_error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2977 | } |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 2978 | free_tlist(origline); |
| 2979 | return DIRECTIVE_FOUND; |
| 2980 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2981 | case PP_UNIMACRO: |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 2982 | casesense = false; |
| 2983 | /* fall through */ |
| 2984 | case PP_UNMACRO: |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2985 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2986 | MMacro **mmac_p; |
| 2987 | MMacro spec; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2988 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 2989 | spec.casesense = casesense; |
| 2990 | if (!parse_mmacro_spec(tline, &spec, dname)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2991 | return DIRECTIVE_FOUND; |
| 2992 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2993 | mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL); |
| 2994 | while (mmac_p && *mmac_p) { |
| 2995 | mmac = *mmac_p; |
| 2996 | if (mmac->casesense == spec.casesense && |
| 2997 | !mstrcmp(mmac->name, spec.name, spec.casesense) && |
| 2998 | mmac->nparam_min == spec.nparam_min && |
| 2999 | mmac->nparam_max == spec.nparam_max && |
| 3000 | mmac->plus == spec.plus) { |
| 3001 | *mmac_p = mmac->next; |
| 3002 | free_mmacro(mmac); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3003 | } else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3004 | mmac_p = &mmac->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3005 | } |
| 3006 | } |
| 3007 | free_tlist(origline); |
| 3008 | free_tlist(spec.dlist); |
| 3009 | return DIRECTIVE_FOUND; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 3010 | } |
| 3011 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3012 | case PP_ROTATE: |
| 3013 | if (tline->next && tline->next->type == TOK_WHITESPACE) |
| 3014 | tline = tline->next; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 3015 | if (!tline->next) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3016 | free_tlist(origline); |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3017 | nasm_error(ERR_NONFATAL, "`%%rotate' missing rotate count"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3018 | return DIRECTIVE_FOUND; |
| 3019 | } |
| 3020 | t = expand_smacro(tline->next); |
| 3021 | tline->next = NULL; |
| 3022 | free_tlist(origline); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3023 | pps.tptr = tline = t; |
| 3024 | pps.ntokens = -1; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3025 | tokval.t_type = TOKEN_INVALID; |
| 3026 | evalresult = |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3027 | evaluate(ppscan, &pps, &tokval, NULL, pass, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3028 | free_tlist(tline); |
| 3029 | if (!evalresult) |
| 3030 | return DIRECTIVE_FOUND; |
| 3031 | if (tokval.t_type) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3032 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3033 | "trailing garbage after expression ignored"); |
| 3034 | if (!is_simple(evalresult)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3035 | nasm_error(ERR_NONFATAL, "non-constant value given to `%%rotate'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3036 | return DIRECTIVE_FOUND; |
| 3037 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3038 | mmac = istk->mstk; |
| 3039 | while (mmac && !mmac->name) /* avoid mistaking %reps for macros */ |
| 3040 | mmac = mmac->next_active; |
| 3041 | if (!mmac) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3042 | nasm_error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3043 | } else if (mmac->nparam == 0) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3044 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3045 | "`%%rotate' invoked within macro without parameters"); |
| 3046 | } else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3047 | int rotate = mmac->rotate + reloc_value(evalresult); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3048 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3049 | rotate %= (int)mmac->nparam; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3050 | if (rotate < 0) |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3051 | rotate += mmac->nparam; |
| 3052 | |
| 3053 | mmac->rotate = rotate; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3054 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3055 | return DIRECTIVE_FOUND; |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 3056 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3057 | case PP_REP: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 3058 | nolist = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3059 | do { |
| 3060 | tline = tline->next; |
| 3061 | } while (tok_type_(tline, TOK_WHITESPACE)); |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 3062 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3063 | if (tok_type_(tline, TOK_ID) && |
| 3064 | nasm_stricmp(tline->text, ".nolist") == 0) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 3065 | nolist = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3066 | do { |
| 3067 | tline = tline->next; |
| 3068 | } while (tok_type_(tline, TOK_WHITESPACE)); |
| 3069 | } |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 3070 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3071 | if (tline) { |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3072 | pps.tptr = expand_smacro(tline); |
| 3073 | pps.ntokens = -1; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3074 | tokval.t_type = TOKEN_INVALID; |
| 3075 | evalresult = |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3076 | evaluate(ppscan, &pps, &tokval, NULL, pass, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3077 | if (!evalresult) { |
| 3078 | free_tlist(origline); |
| 3079 | return DIRECTIVE_FOUND; |
| 3080 | } |
| 3081 | if (tokval.t_type) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3082 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3083 | "trailing garbage after expression ignored"); |
| 3084 | if (!is_simple(evalresult)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3085 | nasm_error(ERR_NONFATAL, "non-constant value given to `%%rep'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3086 | return DIRECTIVE_FOUND; |
| 3087 | } |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 3088 | count = reloc_value(evalresult); |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 3089 | if (count > nasm_limit[LIMIT_REP]) { |
| 3090 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | 7956102 | 2018-06-15 17:51:39 -0700 | [diff] [blame] | 3091 | "`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")", |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 3092 | count, nasm_limit[LIMIT_REP]); |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 3093 | count = 0; |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 3094 | } else if (count < 0) { |
H. Peter Anvin (Intel) | 77f53ba | 2018-12-12 14:38:50 -0800 | [diff] [blame] | 3095 | nasm_error(ERR_WARNING|ERR_PASS2|WARN_NEG_REP, |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 3096 | "negative `%%rep' count: %"PRId64, count); |
| 3097 | count = 0; |
| 3098 | } else { |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 3099 | count++; |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 3100 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3101 | } else { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3102 | nasm_error(ERR_NONFATAL, "`%%rep' expects a repeat count"); |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 3103 | count = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3104 | } |
| 3105 | free_tlist(origline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3106 | |
| 3107 | tmp_defining = defining; |
| 3108 | defining = nasm_malloc(sizeof(MMacro)); |
| 3109 | defining->prev = NULL; |
| 3110 | defining->name = NULL; /* flags this macro as a %rep block */ |
| 3111 | defining->casesense = false; |
| 3112 | defining->plus = false; |
| 3113 | defining->nolist = nolist; |
| 3114 | defining->in_progress = count; |
| 3115 | defining->max_depth = 0; |
| 3116 | defining->nparam_min = defining->nparam_max = 0; |
| 3117 | defining->defaults = NULL; |
| 3118 | defining->dlist = NULL; |
| 3119 | defining->expansion = NULL; |
| 3120 | defining->next_active = istk->mstk; |
| 3121 | defining->rep_nest = tmp_defining; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3122 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3123 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3124 | case PP_ENDREP: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3125 | if (!defining || defining->name) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3126 | nasm_error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3127 | return DIRECTIVE_FOUND; |
| 3128 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3129 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3130 | /* |
| 3131 | * Now we have a "macro" defined - although it has no name |
| 3132 | * and we won't be entering it in the hash tables - we must |
| 3133 | * push a macro-end marker for it on to istk->expansion. |
| 3134 | * After that, it will take care of propagating itself (a |
| 3135 | * macro-end marker line for a macro which is really a %rep |
| 3136 | * block will cause the macro to be re-expanded, complete |
| 3137 | * with another macro-end marker to ensure the process |
| 3138 | * continues) until the whole expansion is forcibly removed |
| 3139 | * from istk->expansion by a %exitrep. |
| 3140 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3141 | l = nasm_malloc(sizeof(Line)); |
| 3142 | l->next = istk->expansion; |
| 3143 | l->finishes = defining; |
| 3144 | l->first = NULL; |
| 3145 | istk->expansion = l; |
| 3146 | |
| 3147 | istk->mstk = defining; |
| 3148 | |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 3149 | lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3150 | tmp_defining = defining; |
| 3151 | defining = defining->rep_nest; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3152 | free_tlist(origline); |
| 3153 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3154 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3155 | case PP_EXITREP: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3156 | /* |
| 3157 | * We must search along istk->expansion until we hit a |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3158 | * macro-end marker for a macro with no name. Then we set |
| 3159 | * its `in_progress' flag to 0. |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3160 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3161 | list_for_each(l, istk->expansion) |
| 3162 | if (l->finishes && !l->finishes->name) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3163 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3164 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3165 | if (l) |
| 3166 | l->finishes->in_progress = 1; |
| 3167 | else |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3168 | nasm_error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3169 | free_tlist(origline); |
| 3170 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3171 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3172 | case PP_IDEFINE: |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3173 | case PP_IXDEFINE: |
| 3174 | casesense = false; |
| 3175 | /* fall through */ |
| 3176 | case PP_DEFINE: |
| 3177 | case PP_XDEFINE: |
| 3178 | { |
| 3179 | SMacro *s; |
| 3180 | bool have_eval_params = false; |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3181 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3182 | tline = tline->next; |
| 3183 | skip_white_(tline); |
| 3184 | tline = expand_id(tline); |
| 3185 | if (!tline || (tline->type != TOK_ID && |
| 3186 | (tline->type != TOK_PREPROC_ID || |
| 3187 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3188 | nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier", |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3189 | dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3190 | free_tlist(origline); |
| 3191 | return DIRECTIVE_FOUND; |
| 3192 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3193 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3194 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3195 | last = tline; |
| 3196 | param_start = tline = tline->next; |
| 3197 | nparam = 0; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3198 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3199 | if (tok_is_(tline, "(")) { |
| 3200 | /* |
| 3201 | * This macro has parameters. |
| 3202 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3203 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3204 | tline = tline->next; |
| 3205 | while (1) { |
| 3206 | skip_white_(tline); |
| 3207 | if (!tline) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3208 | nasm_error(ERR_NONFATAL, "parameter identifier expected"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3209 | free_tlist(origline); |
| 3210 | return DIRECTIVE_FOUND; |
| 3211 | } |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3212 | if (tok_is_(tline, "=")) { |
| 3213 | have_eval_params = true; |
| 3214 | tline = tline->next; |
| 3215 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3216 | if (tline->type != TOK_ID) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3217 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3218 | "`%s': parameter identifier expected", |
| 3219 | tline->text); |
| 3220 | free_tlist(origline); |
| 3221 | return DIRECTIVE_FOUND; |
| 3222 | } |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3223 | tline->type = tok_smac_param(nparam++); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3224 | tline = tline->next; |
| 3225 | skip_white_(tline); |
| 3226 | if (tok_is_(tline, ",")) { |
| 3227 | tline = tline->next; |
H. Peter Anvin | ca348b6 | 2008-07-23 10:49:26 -0400 | [diff] [blame] | 3228 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3229 | if (!tok_is_(tline, ")")) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3230 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3231 | "`)' expected to terminate macro template"); |
| 3232 | free_tlist(origline); |
| 3233 | return DIRECTIVE_FOUND; |
| 3234 | } |
| 3235 | break; |
| 3236 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3237 | } |
| 3238 | last = tline; |
| 3239 | tline = tline->next; |
| 3240 | } |
| 3241 | if (tok_type_(tline, TOK_WHITESPACE)) |
| 3242 | last = tline, tline = tline->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3243 | last->next = NULL; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3244 | |
| 3245 | /* Expand the macro definition now for %xdefine and %ixdefine */ |
| 3246 | if ((i == PP_XDEFINE) || (i == PP_IXDEFINE)) |
| 3247 | tline = expand_smacro(tline); |
| 3248 | |
| 3249 | macro_start = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3250 | t = tline; |
| 3251 | while (t) { |
| 3252 | if (t->type == TOK_ID) { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3253 | list_for_each(tt, param_start) |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3254 | if (is_smac_param(tt->type) && !strcmp(tt->text, t->text)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3255 | t->type = tt->type; |
| 3256 | } |
| 3257 | tt = t->next; |
| 3258 | t->next = macro_start; |
| 3259 | macro_start = t; |
| 3260 | t = tt; |
| 3261 | } |
| 3262 | /* |
| 3263 | * Good. We now have a macro name, a parameter count, and a |
| 3264 | * token list (in reverse order) for an expansion. We ought |
| 3265 | * to be OK just to create an SMacro, store it, and let |
| 3266 | * free_tlist have the rest of the line (which we have |
| 3267 | * carefully re-terminated after chopping off the expansion |
| 3268 | * from the end). |
| 3269 | */ |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3270 | s = define_smacro(ctx, mname, casesense, nparam, macro_start); |
| 3271 | |
| 3272 | if (have_eval_params) { |
| 3273 | /* Create evaluated parameters table */ |
| 3274 | bool is_eval = false; |
| 3275 | |
| 3276 | nasm_newn(s->eval_param, nparam); |
| 3277 | list_for_each(tt, param_start) { |
| 3278 | if (is_smac_param(tt->type)) |
| 3279 | s->eval_param[smac_nparam(tt->type)] = is_eval; |
| 3280 | is_eval = tok_is_(tt, "="); |
| 3281 | } |
| 3282 | } |
| 3283 | |
| 3284 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3285 | free_tlist(origline); |
| 3286 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3287 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3288 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3289 | case PP_UNDEF: |
| 3290 | tline = tline->next; |
| 3291 | skip_white_(tline); |
| 3292 | tline = expand_id(tline); |
| 3293 | if (!tline || (tline->type != TOK_ID && |
| 3294 | (tline->type != TOK_PREPROC_ID || |
| 3295 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3296 | nasm_error(ERR_NONFATAL, "`%%undef' expects a macro identifier"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3297 | free_tlist(origline); |
| 3298 | return DIRECTIVE_FOUND; |
| 3299 | } |
| 3300 | if (tline->next) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3301 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3302 | "trailing garbage after macro name ignored"); |
| 3303 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3304 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3305 | /* Find the context that symbol belongs to */ |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3306 | ctx = get_ctx(tline->text, &mname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3307 | undef_smacro(ctx, mname); |
| 3308 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3309 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3310 | |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3311 | case PP_IDEFSTR: |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3312 | casesense = false; |
| 3313 | /* fall through */ |
| 3314 | case PP_DEFSTR: |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3315 | tline = tline->next; |
| 3316 | skip_white_(tline); |
| 3317 | tline = expand_id(tline); |
| 3318 | if (!tline || (tline->type != TOK_ID && |
| 3319 | (tline->type != TOK_PREPROC_ID || |
| 3320 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3321 | nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier", |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3322 | dname); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3323 | free_tlist(origline); |
| 3324 | return DIRECTIVE_FOUND; |
| 3325 | } |
| 3326 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3327 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3328 | last = tline; |
| 3329 | tline = expand_smacro(tline->next); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3330 | last->next = NULL; |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3331 | |
| 3332 | while (tok_type_(tline, TOK_WHITESPACE)) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3333 | tline = delete_Token(tline); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3334 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3335 | p = detoken(tline, false); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3336 | macro_start = make_tok_qstr(p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3337 | nasm_free(p); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3338 | |
| 3339 | /* |
| 3340 | * We now have a macro name, an implicit parameter count of |
| 3341 | * zero, and a string token to use as an expansion. Create |
| 3342 | * and store an SMacro. |
| 3343 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3344 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3345 | free_tlist(origline); |
| 3346 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3347 | |
H. Peter Anvin | 2f55bda | 2009-07-14 15:04:04 -0400 | [diff] [blame] | 3348 | case PP_IDEFTOK: |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3349 | casesense = false; |
| 3350 | /* fall through */ |
| 3351 | case PP_DEFTOK: |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3352 | tline = tline->next; |
| 3353 | skip_white_(tline); |
| 3354 | tline = expand_id(tline); |
| 3355 | if (!tline || (tline->type != TOK_ID && |
| 3356 | (tline->type != TOK_PREPROC_ID || |
| 3357 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3358 | nasm_error(ERR_NONFATAL, |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3359 | "`%s' expects a macro identifier as first parameter", |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3360 | dname); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3361 | free_tlist(origline); |
| 3362 | return DIRECTIVE_FOUND; |
| 3363 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3364 | ctx = get_ctx(tline->text, &mname); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3365 | last = tline; |
| 3366 | tline = expand_smacro(tline->next); |
| 3367 | last->next = NULL; |
| 3368 | |
| 3369 | t = tline; |
| 3370 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3371 | t = t->next; |
| 3372 | /* t should now point to the string */ |
Cyrill Gorcunov | 6908e58 | 2010-09-06 19:36:15 +0400 | [diff] [blame] | 3373 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3374 | nasm_error(ERR_NONFATAL, |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3375 | "`%s` requires string as second parameter", |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3376 | dname); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3377 | free_tlist(tline); |
| 3378 | free_tlist(origline); |
| 3379 | return DIRECTIVE_FOUND; |
| 3380 | } |
| 3381 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 3382 | /* |
| 3383 | * Convert the string to a token stream. Note that smacros |
| 3384 | * are stored with the token stream reversed, so we have to |
| 3385 | * reverse the output of tokenize(). |
| 3386 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3387 | nasm_unquote_cstr(t->text, i); |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 3388 | macro_start = reverse_tokens(tokenize(t->text)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3389 | |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3390 | /* |
| 3391 | * We now have a macro name, an implicit parameter count of |
| 3392 | * zero, and a numeric token to use as an expansion. Create |
| 3393 | * and store an SMacro. |
| 3394 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3395 | define_smacro(ctx, mname, casesense, 0, macro_start); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3396 | free_tlist(tline); |
| 3397 | free_tlist(origline); |
| 3398 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3399 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3400 | case PP_IPATHSEARCH: |
| 3401 | casesense = false; |
| 3402 | /* fall through */ |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3403 | case PP_PATHSEARCH: |
| 3404 | { |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 3405 | const char *found_path; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3406 | |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3407 | tline = tline->next; |
| 3408 | skip_white_(tline); |
| 3409 | tline = expand_id(tline); |
| 3410 | if (!tline || (tline->type != TOK_ID && |
| 3411 | (tline->type != TOK_PREPROC_ID || |
| 3412 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3413 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3414 | "`%s' expects a macro identifier as first parameter", dname); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3415 | free_tlist(origline); |
| 3416 | return DIRECTIVE_FOUND; |
| 3417 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3418 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3419 | last = tline; |
| 3420 | tline = expand_smacro(tline->next); |
| 3421 | last->next = NULL; |
| 3422 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3423 | t = tline; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3424 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3425 | t = t->next; |
| 3426 | |
| 3427 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3428 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3429 | nasm_error(ERR_NONFATAL, "`%s' expects a file name", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3430 | free_tlist(tline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3431 | free_tlist(origline); |
| 3432 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 3433 | } |
| 3434 | if (t->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3435 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3436 | "trailing garbage after `%s' ignored", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3437 | p = t->text; |
H. Peter Anvin | 427cc91 | 2008-06-01 21:43:03 -0700 | [diff] [blame] | 3438 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3439 | nasm_unquote(p, NULL); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3440 | |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 3441 | inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY); |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 3442 | if (!found_path) |
| 3443 | found_path = p; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3444 | macro_start = make_tok_qstr(found_path); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3445 | |
| 3446 | /* |
| 3447 | * We now have a macro name, an implicit parameter count of |
| 3448 | * zero, and a string token to use as an expansion. Create |
| 3449 | * and store an SMacro. |
| 3450 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3451 | define_smacro(ctx, mname, casesense, 0, macro_start); |
| 3452 | free_tlist(tline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3453 | free_tlist(origline); |
| 3454 | return DIRECTIVE_FOUND; |
| 3455 | } |
| 3456 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3457 | case PP_ISTRLEN: |
| 3458 | casesense = false; |
| 3459 | /* fall through */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3460 | case PP_STRLEN: |
| 3461 | tline = tline->next; |
| 3462 | skip_white_(tline); |
| 3463 | tline = expand_id(tline); |
| 3464 | if (!tline || (tline->type != TOK_ID && |
| 3465 | (tline->type != TOK_PREPROC_ID || |
| 3466 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3467 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3468 | "`%s' expects a macro identifier as first parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3469 | free_tlist(origline); |
| 3470 | return DIRECTIVE_FOUND; |
| 3471 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3472 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3473 | last = tline; |
| 3474 | tline = expand_smacro(tline->next); |
| 3475 | last->next = NULL; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3476 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3477 | t = tline; |
| 3478 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3479 | t = t->next; |
| 3480 | /* t should now point to the string */ |
Cyrill Gorcunov | 4e1d5ab | 2010-07-23 18:51:51 +0400 | [diff] [blame] | 3481 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3482 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3483 | "`%s` requires string as second parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3484 | free_tlist(tline); |
| 3485 | free_tlist(origline); |
| 3486 | return DIRECTIVE_FOUND; |
| 3487 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3488 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3489 | macro_start = make_tok_num(nasm_unquote(t->text, NULL)); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3490 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3491 | /* |
| 3492 | * We now have a macro name, an implicit parameter count of |
| 3493 | * zero, and a numeric token to use as an expansion. Create |
| 3494 | * and store an SMacro. |
| 3495 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3496 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3497 | free_tlist(tline); |
| 3498 | free_tlist(origline); |
| 3499 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3500 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3501 | case PP_ISTRCAT: |
| 3502 | casesense = false; |
| 3503 | /* fall through */ |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3504 | case PP_STRCAT: |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3505 | tline = tline->next; |
| 3506 | skip_white_(tline); |
| 3507 | tline = expand_id(tline); |
| 3508 | if (!tline || (tline->type != TOK_ID && |
| 3509 | (tline->type != TOK_PREPROC_ID || |
| 3510 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3511 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3512 | "`%s' expects a macro identifier as first parameter", dname); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3513 | free_tlist(origline); |
| 3514 | return DIRECTIVE_FOUND; |
| 3515 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3516 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3517 | last = tline; |
| 3518 | tline = expand_smacro(tline->next); |
| 3519 | last->next = NULL; |
| 3520 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3521 | len = 0; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3522 | list_for_each(t, tline) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3523 | switch (t->type) { |
| 3524 | case TOK_WHITESPACE: |
| 3525 | break; |
| 3526 | case TOK_STRING: |
| 3527 | len += t->a.len = nasm_unquote(t->text, NULL); |
| 3528 | break; |
| 3529 | case TOK_OTHER: |
| 3530 | if (!strcmp(t->text, ",")) /* permit comma separators */ |
| 3531 | break; |
| 3532 | /* else fall through */ |
| 3533 | default: |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3534 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3535 | "non-string passed to `%s': %s", dname, t->text); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3536 | free_tlist(tline); |
| 3537 | free_tlist(origline); |
| 3538 | return DIRECTIVE_FOUND; |
| 3539 | } |
| 3540 | } |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3541 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3542 | p = pp = nasm_malloc(len); |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3543 | list_for_each(t, tline) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3544 | if (t->type == TOK_STRING) { |
| 3545 | memcpy(p, t->text, t->a.len); |
| 3546 | p += t->a.len; |
| 3547 | } |
| 3548 | } |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3549 | |
| 3550 | /* |
| 3551 | * We now have a macro name, an implicit parameter count of |
| 3552 | * zero, and a numeric token to use as an expansion. Create |
| 3553 | * and store an SMacro. |
| 3554 | */ |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3555 | macro_start = make_tok_qstr(pp); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3556 | nasm_free(pp); |
| 3557 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3558 | free_tlist(tline); |
| 3559 | free_tlist(origline); |
| 3560 | return DIRECTIVE_FOUND; |
| 3561 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3562 | case PP_ISUBSTR: |
| 3563 | casesense = false; |
| 3564 | /* fall through */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3565 | case PP_SUBSTR: |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3566 | { |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3567 | int64_t start, count; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3568 | size_t len; |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -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 | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3577 | "`%s' expects a macro identifier as first parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3578 | free_tlist(origline); |
| 3579 | return DIRECTIVE_FOUND; |
| 3580 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3581 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3582 | last = tline; |
| 3583 | tline = expand_smacro(tline->next); |
| 3584 | last->next = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3585 | |
Cyrill Gorcunov | 35519d6 | 2010-09-06 23:49:52 +0400 | [diff] [blame] | 3586 | if (tline) /* skip expanded id */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3587 | t = tline->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3588 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3589 | t = t->next; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3590 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3591 | /* t should now point to the string */ |
Cyrill Gorcunov | 35519d6 | 2010-09-06 23:49:52 +0400 | [diff] [blame] | 3592 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3593 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3594 | "`%s' requires string as second parameter", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3595 | free_tlist(tline); |
| 3596 | free_tlist(origline); |
| 3597 | return DIRECTIVE_FOUND; |
| 3598 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3599 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3600 | pps.tptr = t->next; |
| 3601 | pps.ntokens = -1; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3602 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3603 | evalresult = evaluate(ppscan, &pps, &tokval, NULL, pass, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3604 | if (!evalresult) { |
| 3605 | free_tlist(tline); |
| 3606 | free_tlist(origline); |
| 3607 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3608 | } else if (!is_simple(evalresult)) { |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3609 | nasm_error(ERR_NONFATAL, "non-constant value given to `%s'", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3610 | free_tlist(tline); |
| 3611 | free_tlist(origline); |
| 3612 | return DIRECTIVE_FOUND; |
| 3613 | } |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3614 | start = evalresult->value - 1; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3615 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3616 | while (tok_type_(pps.tptr, TOK_WHITESPACE)) |
| 3617 | pps.tptr = pps.tptr->next; |
| 3618 | if (!pps.tptr) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3619 | count = 1; /* Backwards compatibility: one character */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3620 | } else { |
| 3621 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3622 | evalresult = evaluate(ppscan, &pps, &tokval, NULL, pass, NULL); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3623 | if (!evalresult) { |
| 3624 | free_tlist(tline); |
| 3625 | free_tlist(origline); |
| 3626 | return DIRECTIVE_FOUND; |
| 3627 | } else if (!is_simple(evalresult)) { |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3628 | nasm_error(ERR_NONFATAL, "non-constant value given to `%s'", dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3629 | free_tlist(tline); |
| 3630 | free_tlist(origline); |
| 3631 | return DIRECTIVE_FOUND; |
| 3632 | } |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3633 | count = evalresult->value; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3634 | } |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3635 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3636 | len = nasm_unquote(t->text, NULL); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3637 | |
Cyrill Gorcunov | cff031e | 2010-09-07 20:31:11 +0400 | [diff] [blame] | 3638 | /* make start and count being in range */ |
| 3639 | if (start < 0) |
| 3640 | start = 0; |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3641 | if (count < 0) |
| 3642 | count = len + count + 1 - start; |
| 3643 | if (start + count > (int64_t)len) |
Cyrill Gorcunov | cff031e | 2010-09-07 20:31:11 +0400 | [diff] [blame] | 3644 | count = len - start; |
| 3645 | if (!len || count < 0 || start >=(int64_t)len) |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3646 | start = -1, count = 0; /* empty string */ |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3647 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3648 | macro_start = new_Token(NULL, TOK_STRING, NULL, 0); |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3649 | macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3650 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3651 | /* |
| 3652 | * We now have a macro name, an implicit parameter count of |
| 3653 | * zero, and a numeric token to use as an expansion. Create |
| 3654 | * and store an SMacro. |
| 3655 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3656 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3657 | free_tlist(tline); |
| 3658 | free_tlist(origline); |
| 3659 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3660 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3661 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3662 | case PP_IASSIGN: |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3663 | casesense = false; |
| 3664 | /* fall through */ |
| 3665 | case PP_ASSIGN: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3666 | tline = tline->next; |
| 3667 | skip_white_(tline); |
| 3668 | tline = expand_id(tline); |
| 3669 | if (!tline || (tline->type != TOK_ID && |
| 3670 | (tline->type != TOK_PREPROC_ID || |
| 3671 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3672 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3673 | "`%s' expects a macro identifier", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3674 | free_tlist(origline); |
| 3675 | return DIRECTIVE_FOUND; |
| 3676 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3677 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3678 | last = tline; |
| 3679 | tline = expand_smacro(tline->next); |
| 3680 | last->next = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3681 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3682 | pps.tptr = tline; |
| 3683 | pps.ntokens = -1; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3684 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3685 | evalresult = evaluate(ppscan, &pps, &tokval, NULL, pass, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3686 | free_tlist(tline); |
| 3687 | if (!evalresult) { |
| 3688 | free_tlist(origline); |
| 3689 | return DIRECTIVE_FOUND; |
| 3690 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3691 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3692 | if (tokval.t_type) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3693 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3694 | "trailing garbage after expression ignored"); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3695 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3696 | if (!is_simple(evalresult)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3697 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3698 | "non-constant value given to `%s'", dname); |
| 3699 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3700 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3701 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3702 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3703 | macro_start = make_tok_num(reloc_value(evalresult)); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3704 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3705 | /* |
| 3706 | * We now have a macro name, an implicit parameter count of |
| 3707 | * zero, and a numeric token to use as an expansion. Create |
| 3708 | * and store an SMacro. |
| 3709 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3710 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3711 | free_tlist(origline); |
| 3712 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3713 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3714 | case PP_LINE: |
| 3715 | /* |
| 3716 | * Syntax is `%line nnn[+mmm] [filename]' |
| 3717 | */ |
H. Peter Anvin (Intel) | 800c168 | 2018-12-14 12:22:11 -0800 | [diff] [blame] | 3718 | if (unlikely(pp_noline)) { |
| 3719 | free_tlist(origline); |
| 3720 | return DIRECTIVE_FOUND; |
| 3721 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3722 | tline = tline->next; |
| 3723 | skip_white_(tline); |
| 3724 | if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3725 | nasm_error(ERR_NONFATAL, "`%s' expects line number", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3726 | free_tlist(origline); |
| 3727 | return DIRECTIVE_FOUND; |
| 3728 | } |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 3729 | k = readnum(tline->text, &err); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3730 | m = 1; |
| 3731 | tline = tline->next; |
| 3732 | if (tok_is_(tline, "+")) { |
| 3733 | tline = tline->next; |
| 3734 | if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3735 | nasm_error(ERR_NONFATAL, "`%s' expects line increment", dname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3736 | free_tlist(origline); |
| 3737 | return DIRECTIVE_FOUND; |
| 3738 | } |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 3739 | m = readnum(tline->text, &err); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3740 | tline = tline->next; |
| 3741 | } |
| 3742 | skip_white_(tline); |
| 3743 | src_set_linnum(k); |
| 3744 | istk->lineinc = m; |
| 3745 | if (tline) { |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 3746 | char *fname = detoken(tline, false); |
| 3747 | src_set_fname(fname); |
| 3748 | nasm_free(fname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3749 | } |
| 3750 | free_tlist(origline); |
| 3751 | return DIRECTIVE_FOUND; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3752 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3753 | default: |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3754 | nasm_error(ERR_FATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3755 | "preprocessor directive `%s' not yet implemented", |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 3756 | dname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3757 | return DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3758 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3759 | } |
| 3760 | |
| 3761 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3762 | * Ensure that a macro parameter contains a condition code and |
| 3763 | * nothing else. Return the condition code index if so, or -1 |
| 3764 | * otherwise. |
| 3765 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3766 | static int find_cc(Token * t) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3767 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3768 | Token *tt; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3769 | |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3770 | if (!t) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3771 | return -1; /* Probably a %+ without a space */ |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3772 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3773 | skip_white_(t); |
Cyrill Gorcunov | 7524cfd | 2017-10-22 19:01:16 +0300 | [diff] [blame] | 3774 | if (!t) |
| 3775 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3776 | if (t->type != TOK_ID) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3777 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3778 | tt = t->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3779 | skip_white_(tt); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3780 | if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ","))) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3781 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3782 | |
Cyrill Gorcunov | 1945639 | 2012-05-02 00:18:56 +0400 | [diff] [blame] | 3783 | return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3784 | } |
| 3785 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3786 | /* |
| 3787 | * This routines walks over tokens strem and hadnles tokens |
| 3788 | * pasting, if @handle_explicit passed then explicit pasting |
| 3789 | * term is handled, otherwise -- implicit pastings only. |
| 3790 | */ |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 3791 | static bool paste_tokens(Token **head, const struct tokseq_match *m, |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3792 | size_t mnum, bool handle_explicit) |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3793 | { |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3794 | Token *tok, *next, **prev_next, **prev_nonspace; |
| 3795 | bool pasted = false; |
| 3796 | char *buf, *p; |
| 3797 | size_t len, i; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3798 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3799 | /* |
| 3800 | * The last token before pasting. We need it |
| 3801 | * to be able to connect new handled tokens. |
| 3802 | * In other words if there were a tokens stream |
| 3803 | * |
| 3804 | * A -> B -> C -> D |
| 3805 | * |
| 3806 | * and we've joined tokens B and C, the resulting |
| 3807 | * stream should be |
| 3808 | * |
| 3809 | * A -> BC -> D |
| 3810 | */ |
| 3811 | tok = *head; |
| 3812 | prev_next = NULL; |
| 3813 | |
| 3814 | if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE)) |
| 3815 | prev_nonspace = head; |
| 3816 | else |
| 3817 | prev_nonspace = NULL; |
| 3818 | |
| 3819 | while (tok && (next = tok->next)) { |
| 3820 | |
| 3821 | switch (tok->type) { |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3822 | case TOK_WHITESPACE: |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3823 | /* Zap redundant whitespaces */ |
| 3824 | while (tok_type_(next, TOK_WHITESPACE)) |
| 3825 | next = delete_Token(next); |
| 3826 | tok->next = next; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3827 | break; |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3828 | |
| 3829 | case TOK_PASTE: |
| 3830 | /* Explicit pasting */ |
| 3831 | if (!handle_explicit) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3832 | break; |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3833 | next = delete_Token(tok); |
| 3834 | |
| 3835 | while (tok_type_(next, TOK_WHITESPACE)) |
| 3836 | next = delete_Token(next); |
| 3837 | |
| 3838 | if (!pasted) |
| 3839 | pasted = true; |
| 3840 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3841 | /* Left pasting token is start of line */ |
| 3842 | if (!prev_nonspace) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3843 | nasm_error(ERR_FATAL, "No lvalue found on pasting"); |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3844 | |
Cyrill Gorcunov | 8b5c9fb | 2013-02-04 01:24:54 +0400 | [diff] [blame] | 3845 | /* |
| 3846 | * No ending token, this might happen in two |
| 3847 | * cases |
| 3848 | * |
| 3849 | * 1) There indeed no right token at all |
| 3850 | * 2) There is a bare "%define ID" statement, |
| 3851 | * and @ID does expand to whitespace. |
| 3852 | * |
| 3853 | * So technically we need to do a grammar analysis |
| 3854 | * in another stage of parsing, but for now lets don't |
| 3855 | * change the behaviour people used to. Simply allow |
| 3856 | * whitespace after paste token. |
| 3857 | */ |
| 3858 | if (!next) { |
| 3859 | /* |
| 3860 | * Zap ending space tokens and that's all. |
| 3861 | */ |
| 3862 | tok = (*prev_nonspace)->next; |
| 3863 | while (tok_type_(tok, TOK_WHITESPACE)) |
| 3864 | tok = delete_Token(tok); |
| 3865 | tok = *prev_nonspace; |
| 3866 | tok->next = NULL; |
| 3867 | break; |
| 3868 | } |
| 3869 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3870 | tok = *prev_nonspace; |
| 3871 | while (tok_type_(tok, TOK_WHITESPACE)) |
| 3872 | tok = delete_Token(tok); |
| 3873 | len = strlen(tok->text); |
| 3874 | len += strlen(next->text); |
| 3875 | |
| 3876 | p = buf = nasm_malloc(len + 1); |
| 3877 | strcpy(p, tok->text); |
| 3878 | p = strchr(p, '\0'); |
| 3879 | strcpy(p, next->text); |
| 3880 | |
| 3881 | delete_Token(tok); |
| 3882 | |
| 3883 | tok = tokenize(buf); |
| 3884 | nasm_free(buf); |
| 3885 | |
| 3886 | *prev_nonspace = tok; |
| 3887 | while (tok && tok->next) |
| 3888 | tok = tok->next; |
| 3889 | |
| 3890 | tok->next = delete_Token(next); |
| 3891 | |
| 3892 | /* Restart from pasted tokens head */ |
| 3893 | tok = *prev_nonspace; |
| 3894 | break; |
| 3895 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3896 | default: |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3897 | /* implicit pasting */ |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 3898 | for (i = 0; i < mnum; i++) { |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3899 | if (!(PP_CONCAT_MATCH(tok, m[i].mask_head))) |
| 3900 | continue; |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 3901 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3902 | len = 0; |
| 3903 | while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) { |
| 3904 | len += strlen(next->text); |
| 3905 | next = next->next; |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 3906 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3907 | |
Cyrill Gorcunov | 6f8109e | 2017-10-22 21:26:36 +0300 | [diff] [blame] | 3908 | /* No match or no text to process */ |
| 3909 | if (tok == next || len == 0) |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3910 | break; |
| 3911 | |
| 3912 | len += strlen(tok->text); |
| 3913 | p = buf = nasm_malloc(len + 1); |
| 3914 | |
Adam Majer | 1a06943 | 2017-07-25 11:12:35 +0200 | [diff] [blame] | 3915 | strcpy(p, tok->text); |
| 3916 | p = strchr(p, '\0'); |
| 3917 | tok = delete_Token(tok); |
| 3918 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3919 | while (tok != next) { |
Adam Majer | 1a06943 | 2017-07-25 11:12:35 +0200 | [diff] [blame] | 3920 | if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) { |
| 3921 | strcpy(p, tok->text); |
| 3922 | p = strchr(p, '\0'); |
| 3923 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3924 | tok = delete_Token(tok); |
| 3925 | } |
| 3926 | |
| 3927 | tok = tokenize(buf); |
| 3928 | nasm_free(buf); |
| 3929 | |
| 3930 | if (prev_next) |
| 3931 | *prev_next = tok; |
| 3932 | else |
| 3933 | *head = tok; |
| 3934 | |
| 3935 | /* |
| 3936 | * Connect pasted into original stream, |
| 3937 | * ie A -> new-tokens -> B |
| 3938 | */ |
| 3939 | while (tok && tok->next) |
| 3940 | tok = tok->next; |
| 3941 | tok->next = next; |
| 3942 | |
| 3943 | if (!pasted) |
| 3944 | pasted = true; |
| 3945 | |
| 3946 | /* Restart from pasted tokens head */ |
| 3947 | tok = prev_next ? *prev_next : *head; |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 3948 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3949 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3950 | break; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3951 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3952 | |
| 3953 | prev_next = &tok->next; |
| 3954 | |
| 3955 | if (tok->next && |
| 3956 | !tok_type_(tok->next, TOK_WHITESPACE) && |
| 3957 | !tok_type_(tok->next, TOK_PASTE)) |
| 3958 | prev_nonspace = prev_next; |
| 3959 | |
| 3960 | tok = tok->next; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3961 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3962 | |
| 3963 | return pasted; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3964 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3965 | |
| 3966 | /* |
| 3967 | * expands to a list of tokens from %{x:y} |
| 3968 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3969 | static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3970 | { |
| 3971 | Token *t = tline, **tt, *tm, *head; |
| 3972 | char *pos; |
| 3973 | int fst, lst, j, i; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3974 | |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3975 | pos = strchr(tline->text, ':'); |
| 3976 | nasm_assert(pos); |
| 3977 | |
| 3978 | lst = atoi(pos + 1); |
| 3979 | fst = atoi(tline->text + 1); |
| 3980 | |
| 3981 | /* |
| 3982 | * only macros params are accounted so |
| 3983 | * if someone passes %0 -- we reject such |
| 3984 | * value(s) |
| 3985 | */ |
| 3986 | if (lst == 0 || fst == 0) |
| 3987 | goto err; |
| 3988 | |
| 3989 | /* the values should be sane */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3990 | if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) || |
| 3991 | (lst > (int)mac->nparam || lst < (-(int)mac->nparam))) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3992 | goto err; |
| 3993 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3994 | fst = fst < 0 ? fst + (int)mac->nparam + 1: fst; |
| 3995 | lst = lst < 0 ? lst + (int)mac->nparam + 1: lst; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3996 | |
| 3997 | /* counted from zero */ |
| 3998 | fst--, lst--; |
| 3999 | |
| 4000 | /* |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 4001 | * It will be at least one token. Note we |
| 4002 | * need to scan params until separator, otherwise |
| 4003 | * only first token will be passed. |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4004 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4005 | tm = mac->params[(fst + mac->rotate) % mac->nparam]; |
Cyrill Gorcunov | 67f2ca2 | 2018-10-13 19:41:01 +0300 | [diff] [blame] | 4006 | if (!tm) |
| 4007 | goto err; |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 4008 | head = new_Token(NULL, tm->type, tm->text, 0); |
| 4009 | tt = &head->next, tm = tm->next; |
| 4010 | while (tok_isnt_(tm, ",")) { |
| 4011 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 4012 | *tt = t, tt = &t->next, tm = tm->next; |
| 4013 | } |
| 4014 | |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4015 | if (fst < lst) { |
| 4016 | for (i = fst + 1; i <= lst; i++) { |
| 4017 | t = new_Token(NULL, TOK_OTHER, ",", 0); |
| 4018 | *tt = t, tt = &t->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4019 | j = (i + mac->rotate) % mac->nparam; |
| 4020 | tm = mac->params[j]; |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 4021 | while (tok_isnt_(tm, ",")) { |
| 4022 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 4023 | *tt = t, tt = &t->next, tm = tm->next; |
| 4024 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4025 | } |
| 4026 | } else { |
| 4027 | for (i = fst - 1; i >= lst; i--) { |
| 4028 | t = new_Token(NULL, TOK_OTHER, ",", 0); |
| 4029 | *tt = t, tt = &t->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4030 | j = (i + mac->rotate) % mac->nparam; |
| 4031 | tm = mac->params[j]; |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 4032 | while (tok_isnt_(tm, ",")) { |
| 4033 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 4034 | *tt = t, tt = &t->next, tm = tm->next; |
| 4035 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4036 | } |
| 4037 | } |
| 4038 | |
| 4039 | *last = tt; |
| 4040 | return head; |
| 4041 | |
| 4042 | err: |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4043 | nasm_error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range", |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4044 | &tline->text[1]); |
| 4045 | return tline; |
| 4046 | } |
| 4047 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4048 | /* |
| 4049 | * Expand MMacro-local things: parameter references (%0, %n, %+n, |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 4050 | * %-n) and MMacro-local identifiers (%%foo) as well as |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4051 | * macro indirection (%[...]) and range (%{..:..}). |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4052 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4053 | static Token *expand_mmac_params(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4054 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4055 | Token *t, *tt, **tail, *thead; |
H. Peter Anvin | 6125b62 | 2009-04-08 14:02:25 -0700 | [diff] [blame] | 4056 | bool changed = false; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4057 | char *pos; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4058 | |
| 4059 | tail = &thead; |
| 4060 | thead = NULL; |
| 4061 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4062 | while (tline) { |
Cyrill Gorcunov | 661f723 | 2018-10-28 20:39:34 +0300 | [diff] [blame] | 4063 | if (tline->type == TOK_PREPROC_ID && tline->text && tline->text[0] && |
Cyrill Gorcunov | ca61119 | 2010-06-04 09:22:12 +0400 | [diff] [blame] | 4064 | (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) || |
| 4065 | (tline->text[1] >= '0' && tline->text[1] <= '9') || |
| 4066 | tline->text[1] == '%')) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4067 | char *text = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4068 | int type = 0, cc; /* type = 0 to placate optimisers */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4069 | char tmpbuf[30]; |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 4070 | unsigned int n; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4071 | int i; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4072 | MMacro *mac; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4073 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4074 | t = tline; |
| 4075 | tline = tline->next; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4076 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4077 | mac = istk->mstk; |
| 4078 | while (mac && !mac->name) /* avoid mistaking %reps for macros */ |
| 4079 | mac = mac->next_active; |
| 4080 | if (!mac) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4081 | nasm_error(ERR_NONFATAL, "`%s': not in a macro call", t->text); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4082 | } else { |
| 4083 | pos = strchr(t->text, ':'); |
| 4084 | if (!pos) { |
| 4085 | switch (t->text[1]) { |
| 4086 | /* |
| 4087 | * We have to make a substitution of one of the |
| 4088 | * forms %1, %-1, %+1, %%foo, %0. |
| 4089 | */ |
| 4090 | case '0': |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4091 | type = TOK_NUMBER; |
| 4092 | snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam); |
| 4093 | text = nasm_strdup(tmpbuf); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4094 | break; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4095 | case '%': |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4096 | type = TOK_ID; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4097 | snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".", |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4098 | mac->unique); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4099 | text = nasm_strcat(tmpbuf, t->text + 2); |
| 4100 | break; |
| 4101 | case '-': |
| 4102 | n = atoi(t->text + 2) - 1; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4103 | if (n >= mac->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4104 | tt = NULL; |
| 4105 | else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4106 | if (mac->nparam > 1) |
| 4107 | n = (n + mac->rotate) % mac->nparam; |
| 4108 | tt = mac->params[n]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4109 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4110 | cc = find_cc(tt); |
| 4111 | if (cc == -1) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4112 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4113 | "macro parameter %d is not a condition code", |
| 4114 | n + 1); |
| 4115 | text = NULL; |
| 4116 | } else { |
| 4117 | type = TOK_ID; |
| 4118 | if (inverse_ccs[cc] == -1) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4119 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4120 | "condition code `%s' is not invertible", |
| 4121 | conditions[cc]); |
| 4122 | text = NULL; |
| 4123 | } else |
| 4124 | text = nasm_strdup(conditions[inverse_ccs[cc]]); |
| 4125 | } |
| 4126 | break; |
| 4127 | case '+': |
| 4128 | n = atoi(t->text + 2) - 1; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4129 | if (n >= mac->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4130 | tt = NULL; |
| 4131 | else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4132 | if (mac->nparam > 1) |
| 4133 | n = (n + mac->rotate) % mac->nparam; |
| 4134 | tt = mac->params[n]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4135 | } |
| 4136 | cc = find_cc(tt); |
| 4137 | if (cc == -1) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4138 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4139 | "macro parameter %d is not a condition code", |
| 4140 | n + 1); |
| 4141 | text = NULL; |
| 4142 | } else { |
| 4143 | type = TOK_ID; |
| 4144 | text = nasm_strdup(conditions[cc]); |
| 4145 | } |
| 4146 | break; |
| 4147 | default: |
| 4148 | n = atoi(t->text + 1) - 1; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4149 | if (n >= mac->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4150 | tt = NULL; |
| 4151 | else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4152 | if (mac->nparam > 1) |
| 4153 | n = (n + mac->rotate) % mac->nparam; |
| 4154 | tt = mac->params[n]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4155 | } |
| 4156 | if (tt) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4157 | for (i = 0; i < mac->paramlen[n]; i++) { |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4158 | *tail = new_Token(NULL, tt->type, tt->text, 0); |
| 4159 | tail = &(*tail)->next; |
| 4160 | tt = tt->next; |
| 4161 | } |
| 4162 | } |
| 4163 | text = NULL; /* we've done it here */ |
| 4164 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4165 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4166 | } else { |
| 4167 | /* |
| 4168 | * seems we have a parameters range here |
| 4169 | */ |
| 4170 | Token *head, **last; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4171 | head = expand_mmac_params_range(mac, t, &last); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4172 | if (head != t) { |
| 4173 | *tail = head; |
| 4174 | *last = tline; |
| 4175 | tline = head; |
| 4176 | text = NULL; |
| 4177 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4178 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4179 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4180 | if (!text) { |
| 4181 | delete_Token(t); |
| 4182 | } else { |
| 4183 | *tail = t; |
| 4184 | tail = &t->next; |
| 4185 | t->type = type; |
| 4186 | nasm_free(t->text); |
| 4187 | t->text = text; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4188 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4189 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4190 | changed = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4191 | continue; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4192 | } else if (tline->type == TOK_INDIRECT) { |
| 4193 | t = tline; |
| 4194 | tline = tline->next; |
| 4195 | tt = tokenize(t->text); |
| 4196 | tt = expand_mmac_params(tt); |
| 4197 | tt = expand_smacro(tt); |
| 4198 | *tail = tt; |
| 4199 | while (tt) { |
| 4200 | tt->a.mac = NULL; /* Necessary? */ |
| 4201 | tail = &tt->next; |
| 4202 | tt = tt->next; |
| 4203 | } |
| 4204 | delete_Token(t); |
| 4205 | changed = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4206 | } else { |
| 4207 | t = *tail = tline; |
| 4208 | tline = tline->next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4209 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4210 | tail = &t->next; |
| 4211 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4212 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4213 | *tail = NULL; |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 4214 | |
Cyrill Gorcunov | c6a742c | 2011-06-27 01:23:09 +0400 | [diff] [blame] | 4215 | if (changed) { |
| 4216 | const struct tokseq_match t[] = { |
| 4217 | { |
| 4218 | PP_CONCAT_MASK(TOK_ID) | |
| 4219 | PP_CONCAT_MASK(TOK_FLOAT), /* head */ |
| 4220 | PP_CONCAT_MASK(TOK_ID) | |
| 4221 | PP_CONCAT_MASK(TOK_NUMBER) | |
| 4222 | PP_CONCAT_MASK(TOK_FLOAT) | |
| 4223 | PP_CONCAT_MASK(TOK_OTHER) /* tail */ |
| 4224 | }, |
| 4225 | { |
| 4226 | PP_CONCAT_MASK(TOK_NUMBER), /* head */ |
| 4227 | PP_CONCAT_MASK(TOK_NUMBER) /* tail */ |
| 4228 | } |
| 4229 | }; |
| 4230 | paste_tokens(&thead, t, ARRAY_SIZE(t), false); |
| 4231 | } |
H. Peter Anvin | 6125b62 | 2009-04-08 14:02:25 -0700 | [diff] [blame] | 4232 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4233 | return thead; |
| 4234 | } |
| 4235 | |
| 4236 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4237 | * Expand all single-line macro calls made in the given line. |
| 4238 | * Return the expanded version of the line. The original is deemed |
| 4239 | * to be destroyed in the process. (In reality we'll just move |
| 4240 | * Tokens from input to output a lot of the time, rather than |
| 4241 | * actually bothering to destroy and replicate.) |
| 4242 | */ |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4243 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4244 | static Token *expand_smacro(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4245 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4246 | Token *t, *tt, *mstart, **tail, *thead; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4247 | SMacro *head = NULL, *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4248 | Token **params; |
| 4249 | int *paramsize; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 4250 | Token *eparams; |
| 4251 | unsigned int nparam, sparam, i; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 4252 | int brackets; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4253 | Token *org_tline = tline; |
| 4254 | Context *ctx; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 4255 | const char *mname; |
H. Peter Anvin | 7956102 | 2018-06-15 17:51:39 -0700 | [diff] [blame] | 4256 | int64_t deadman = nasm_limit[LIMIT_MACROS]; |
H. Peter Anvin | 8287daf | 2009-07-07 16:00:58 -0700 | [diff] [blame] | 4257 | bool expanded; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4258 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4259 | /* |
| 4260 | * Trick: we should avoid changing the start token pointer since it can |
| 4261 | * be contained in "next" field of other token. Because of this |
| 4262 | * we allocate a copy of first token and work with it; at the end of |
| 4263 | * routine we copy it back |
| 4264 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4265 | if (org_tline) { |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4266 | tline = new_Token(org_tline->next, org_tline->type, |
| 4267 | org_tline->text, 0); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4268 | tline->a.mac = org_tline->a.mac; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4269 | nasm_free(org_tline->text); |
| 4270 | org_tline->text = NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4271 | } |
| 4272 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4273 | expanded = true; /* Always expand %+ at least once */ |
H. Peter Anvin | 8287daf | 2009-07-07 16:00:58 -0700 | [diff] [blame] | 4274 | |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4275 | again: |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4276 | thead = NULL; |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4277 | tail = &thead; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4278 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4279 | while (tline) { /* main token loop */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4280 | if (!--deadman) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4281 | nasm_error(ERR_NONFATAL, "interminable macro recursion"); |
Cyrill Gorcunov | bd38c8f | 2009-11-21 11:11:23 +0300 | [diff] [blame] | 4282 | goto err; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4283 | } |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4284 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4285 | if ((mname = tline->text)) { |
| 4286 | /* if this token is a local macro, look in local context */ |
Cyrill Gorcunov | c56d9ad | 2010-02-11 15:12:19 +0300 | [diff] [blame] | 4287 | if (tline->type == TOK_ID) { |
| 4288 | head = (SMacro *)hash_findix(&smacros, mname); |
| 4289 | } else if (tline->type == TOK_PREPROC_ID) { |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 4290 | ctx = get_ctx(mname, &mname); |
Cyrill Gorcunov | c56d9ad | 2010-02-11 15:12:19 +0300 | [diff] [blame] | 4291 | head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL; |
| 4292 | } else |
| 4293 | head = NULL; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 4294 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4295 | /* |
| 4296 | * We've hit an identifier. As in is_mmacro below, we first |
| 4297 | * check whether the identifier is a single-line macro at |
| 4298 | * all, then think about checking for parameters if |
| 4299 | * necessary. |
| 4300 | */ |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 4301 | list_for_each(m, head) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4302 | if (!mstrcmp(m->name, mname, m->casesense)) |
| 4303 | break; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 4304 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4305 | if (m) { |
| 4306 | mstart = tline; |
| 4307 | params = NULL; |
| 4308 | paramsize = NULL; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 4309 | eparams = NULL; |
| 4310 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4311 | if (m->nparam == 0) { |
| 4312 | /* |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 4313 | * Simple case: the macro is parameterless. |
| 4314 | * Nothing to parse; just drop the macro token itself. |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4315 | */ |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 4316 | tline = tline->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4317 | } else { |
| 4318 | /* |
| 4319 | * Complicated case: at least one macro with this name |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4320 | * exists and takes parameters. We must find the |
| 4321 | * parameters in the call, count them, find the SMacro |
| 4322 | * that corresponds to that form of the macro call, and |
| 4323 | * substitute for the parameters when we expand. What a |
| 4324 | * pain. |
| 4325 | */ |
| 4326 | /*tline = tline->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4327 | skip_white_(tline); */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4328 | do { |
| 4329 | t = tline->next; |
| 4330 | while (tok_type_(t, TOK_SMAC_END)) { |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4331 | t->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4332 | t->text = NULL; |
| 4333 | t = tline->next = delete_Token(t); |
| 4334 | } |
| 4335 | tline = t; |
| 4336 | } while (tok_type_(tline, TOK_WHITESPACE)); |
| 4337 | if (!tok_is_(tline, "(")) { |
| 4338 | /* |
| 4339 | * This macro wasn't called with parameters: ignore |
| 4340 | * the call. (Behaviour borrowed from gnu cpp.) |
| 4341 | */ |
| 4342 | tline = mstart; |
| 4343 | m = NULL; |
| 4344 | } else { |
| 4345 | int paren = 0; |
| 4346 | int white = 0; |
| 4347 | brackets = 0; |
| 4348 | nparam = 0; |
| 4349 | sparam = PARAM_DELTA; |
| 4350 | params = nasm_malloc(sparam * sizeof(Token *)); |
| 4351 | params[0] = tline->next; |
| 4352 | paramsize = nasm_malloc(sparam * sizeof(int)); |
| 4353 | paramsize[0] = 0; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4354 | while (true) { /* parameter loop */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4355 | /* |
| 4356 | * For some unusual expansions |
| 4357 | * which concatenates function call |
| 4358 | */ |
| 4359 | t = tline->next; |
| 4360 | while (tok_type_(t, TOK_SMAC_END)) { |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4361 | t->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4362 | t->text = NULL; |
| 4363 | t = tline->next = delete_Token(t); |
| 4364 | } |
| 4365 | tline = t; |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 4366 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4367 | if (!tline) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4368 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4369 | "macro call expects terminating `)'"); |
| 4370 | break; |
| 4371 | } |
| 4372 | if (tline->type == TOK_WHITESPACE |
| 4373 | && brackets <= 0) { |
| 4374 | if (paramsize[nparam]) |
| 4375 | white++; |
| 4376 | else |
| 4377 | params[nparam] = tline->next; |
| 4378 | continue; /* parameter loop */ |
| 4379 | } |
| 4380 | if (tline->type == TOK_OTHER |
| 4381 | && tline->text[1] == 0) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4382 | char ch = tline->text[0]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4383 | if (ch == ',' && !paren && brackets <= 0) { |
| 4384 | if (++nparam >= sparam) { |
| 4385 | sparam += PARAM_DELTA; |
| 4386 | params = nasm_realloc(params, |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4387 | sparam * sizeof(Token *)); |
| 4388 | paramsize = nasm_realloc(paramsize, |
| 4389 | sparam * sizeof(int)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4390 | } |
| 4391 | params[nparam] = tline->next; |
| 4392 | paramsize[nparam] = 0; |
| 4393 | white = 0; |
| 4394 | continue; /* parameter loop */ |
| 4395 | } |
| 4396 | if (ch == '{' && |
| 4397 | (brackets > 0 || (brackets == 0 && |
| 4398 | !paramsize[nparam]))) |
| 4399 | { |
| 4400 | if (!(brackets++)) { |
| 4401 | params[nparam] = tline->next; |
| 4402 | continue; /* parameter loop */ |
| 4403 | } |
| 4404 | } |
| 4405 | if (ch == '}' && brackets > 0) |
| 4406 | if (--brackets == 0) { |
| 4407 | brackets = -1; |
| 4408 | continue; /* parameter loop */ |
| 4409 | } |
| 4410 | if (ch == '(' && !brackets) |
| 4411 | paren++; |
| 4412 | if (ch == ')' && brackets <= 0) |
| 4413 | if (--paren < 0) |
| 4414 | break; |
| 4415 | } |
| 4416 | if (brackets < 0) { |
| 4417 | brackets = 0; |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4418 | nasm_error(ERR_NONFATAL, "braces do not " |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4419 | "enclose all of macro parameter"); |
| 4420 | } |
| 4421 | paramsize[nparam] += white + 1; |
| 4422 | white = 0; |
| 4423 | } /* parameter loop */ |
| 4424 | nparam++; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 4425 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4426 | while (m && (m->nparam != nparam || |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 4427 | mstrcmp(m->name, mname, m->casesense))) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4428 | m = m->next; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 4429 | if (!m) { |
H. Peter Anvin (Intel) | 77f53ba | 2018-12-12 14:38:50 -0800 | [diff] [blame] | 4430 | nasm_error(ERR_WARNING|ERR_PASS1|WARN_MNP, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4431 | "macro `%s' exists, " |
| 4432 | "but not taking %d parameters", |
| 4433 | mstart->text, nparam); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 4434 | } else if (m->eval_param) { |
| 4435 | struct ppscan pps; |
| 4436 | struct tokenval tokval; |
| 4437 | expr *evalresult; |
| 4438 | |
| 4439 | /* Evaluate parameters if applicable */ |
| 4440 | for (i = 0; i < nparam; i++) { |
| 4441 | if (!m->eval_param[i]) |
| 4442 | continue; |
| 4443 | |
| 4444 | pps.tptr = params[i]; |
| 4445 | pps.ntokens = paramsize[i]; |
| 4446 | tokval.t_type = TOKEN_INVALID; |
| 4447 | evalresult = evaluate(ppscan, &pps, &tokval, |
| 4448 | NULL, pass, NULL); |
| 4449 | if (!evalresult) |
| 4450 | continue; |
| 4451 | |
| 4452 | if (tokval.t_type) { |
| 4453 | nasm_error(ERR_NONFATAL, |
| 4454 | "invalid expression in parameter %d of macro `%s'", i, m->name); |
| 4455 | continue; |
| 4456 | } |
| 4457 | |
| 4458 | if (!is_simple(evalresult)) { |
| 4459 | nasm_error(ERR_NONFATAL, |
| 4460 | "non-constant expression in parameter %d of macro `%s'", i, m->name); |
| 4461 | continue; |
| 4462 | } |
| 4463 | params[i] = make_tok_num(reloc_value(evalresult)); |
| 4464 | params[i]->next = eparams; |
| 4465 | eparams = params[i]; |
| 4466 | paramsize[i] = 1; |
| 4467 | } |
| 4468 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4469 | } |
| 4470 | } |
| 4471 | if (m && m->in_progress) |
| 4472 | m = NULL; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 4473 | if (!m) { |
| 4474 | /* in progress or didn't find '(' or wrong nparam */ |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 4475 | /* |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4476 | * Design question: should we handle !tline, which |
| 4477 | * indicates missing ')' here, or expand those |
| 4478 | * macros anyway, which requires the (t) test a few |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 4479 | * lines down? |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4480 | */ |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 4481 | free_tlist(eparams); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4482 | nasm_free(params); |
| 4483 | nasm_free(paramsize); |
| 4484 | tline = mstart; |
| 4485 | } else { |
| 4486 | /* |
| 4487 | * Expand the macro: we are placed on the last token of the |
| 4488 | * call, so that we can easily split the call from the |
| 4489 | * following tokens. We also start by pushing an SMAC_END |
| 4490 | * token for the cycle removal. |
| 4491 | */ |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 4492 | Token *expansion; |
| 4493 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4494 | t = tline; |
| 4495 | if (t) { |
| 4496 | tline = t->next; |
| 4497 | t->next = NULL; |
| 4498 | } |
| 4499 | tt = new_Token(tline, TOK_SMAC_END, NULL, 0); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4500 | tt->a.mac = m; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4501 | m->in_progress = true; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 4502 | if (unlikely(m->magic)) |
| 4503 | expansion = m->e.magic(m, params, paramsize); |
| 4504 | else |
| 4505 | expansion = m->e.expansion; |
| 4506 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4507 | tline = tt; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 4508 | list_for_each(t, expansion) { |
| 4509 | if (is_smac_param(t->type)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4510 | Token *pcopy = tline, **ptail = &pcopy; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4511 | Token *ttt, *pt; |
| 4512 | int i; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4513 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 4514 | ttt = params[smac_nparam(t->type)]; |
| 4515 | i = paramsize[smac_nparam(t->type)]; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4516 | while (--i >= 0) { |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 4517 | nasm_assert(ttt); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4518 | pt = *ptail = new_Token(tline, ttt->type, |
| 4519 | ttt->text, 0); |
| 4520 | ptail = &pt->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4521 | ttt = ttt->next; |
| 4522 | } |
| 4523 | tline = pcopy; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4524 | } else if (t->type == TOK_PREPROC_Q) { |
| 4525 | tt = new_Token(tline, TOK_ID, mname, 0); |
| 4526 | tline = tt; |
| 4527 | } else if (t->type == TOK_PREPROC_QQ) { |
| 4528 | tt = new_Token(tline, TOK_ID, m->name, 0); |
| 4529 | tline = tt; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4530 | } else { |
| 4531 | tt = new_Token(tline, t->type, t->text, 0); |
| 4532 | tline = tt; |
| 4533 | } |
| 4534 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4535 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4536 | /* |
| 4537 | * Having done that, get rid of the macro call, and clean |
| 4538 | * up the parameters. |
| 4539 | */ |
| 4540 | nasm_free(params); |
| 4541 | nasm_free(paramsize); |
| 4542 | free_tlist(mstart); |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 4543 | free_tlist(eparams); |
| 4544 | if (m->magic) |
| 4545 | free_tlist(expansion); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4546 | expanded = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4547 | continue; /* main token loop */ |
| 4548 | } |
| 4549 | } |
| 4550 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4551 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4552 | if (tline->type == TOK_SMAC_END) { |
Cyrill Gorcunov | 980dd65 | 2018-10-14 19:25:32 +0300 | [diff] [blame] | 4553 | /* On error path it might already be dropped */ |
| 4554 | if (tline->a.mac) |
| 4555 | tline->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4556 | tline = delete_Token(tline); |
| 4557 | } else { |
| 4558 | t = *tail = tline; |
| 4559 | tline = tline->next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4560 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4561 | t->next = NULL; |
| 4562 | tail = &t->next; |
| 4563 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4564 | } |
| 4565 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4566 | /* |
| 4567 | * 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] | 4568 | * after expansion (they can't be produced by tokenize()). The successive |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4569 | * TOK_IDs should be concatenated. |
| 4570 | * Also we look for %+ tokens and concatenate the tokens before and after |
| 4571 | * them (without white spaces in between). |
| 4572 | */ |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4573 | if (expanded) { |
Cyrill Gorcunov | c6a742c | 2011-06-27 01:23:09 +0400 | [diff] [blame] | 4574 | const struct tokseq_match t[] = { |
| 4575 | { |
| 4576 | PP_CONCAT_MASK(TOK_ID) | |
| 4577 | PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */ |
| 4578 | PP_CONCAT_MASK(TOK_ID) | |
| 4579 | PP_CONCAT_MASK(TOK_PREPROC_ID) | |
| 4580 | PP_CONCAT_MASK(TOK_NUMBER) /* tail */ |
| 4581 | } |
| 4582 | }; |
| 4583 | if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) { |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4584 | /* |
| 4585 | * If we concatenated something, *and* we had previously expanded |
| 4586 | * an actual macro, scan the lines again for macros... |
| 4587 | */ |
| 4588 | tline = thead; |
| 4589 | expanded = false; |
| 4590 | goto again; |
| 4591 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4592 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4593 | |
Cyrill Gorcunov | bd38c8f | 2009-11-21 11:11:23 +0300 | [diff] [blame] | 4594 | err: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4595 | if (org_tline) { |
| 4596 | if (thead) { |
| 4597 | *org_tline = *thead; |
| 4598 | /* since we just gave text to org_line, don't free it */ |
| 4599 | thead->text = NULL; |
| 4600 | delete_Token(thead); |
| 4601 | } else { |
| 4602 | /* the expression expanded to empty line; |
| 4603 | we can't return NULL for some reasons |
| 4604 | we just set the line to a single WHITESPACE token. */ |
| 4605 | memset(org_tline, 0, sizeof(*org_tline)); |
| 4606 | org_tline->text = NULL; |
| 4607 | org_tline->type = TOK_WHITESPACE; |
| 4608 | } |
| 4609 | thead = org_tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4610 | } |
| 4611 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4612 | return thead; |
| 4613 | } |
| 4614 | |
| 4615 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4616 | * Similar to expand_smacro but used exclusively with macro identifiers |
| 4617 | * right before they are fetched in. The reason is that there can be |
| 4618 | * identifiers consisting of several subparts. We consider that if there |
| 4619 | * are more than one element forming the name, user wants a expansion, |
| 4620 | * otherwise it will be left as-is. Example: |
| 4621 | * |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4622 | * %define %$abc cde |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4623 | * |
| 4624 | * the identifier %$abc will be left as-is so that the handler for %define |
| 4625 | * will suck it and define the corresponding value. Other case: |
| 4626 | * |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4627 | * %define _%$abc cde |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4628 | * |
| 4629 | * In this case user wants name to be expanded *before* %define starts |
| 4630 | * working, so we'll expand %$abc into something (if it has a value; |
| 4631 | * otherwise it will be left as-is) then concatenate all successive |
| 4632 | * PP_IDs into one. |
| 4633 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4634 | static Token *expand_id(Token * tline) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4635 | { |
| 4636 | Token *cur, *oldnext = NULL; |
| 4637 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4638 | if (!tline || !tline->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4639 | return tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4640 | |
| 4641 | cur = tline; |
| 4642 | while (cur->next && |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4643 | (cur->next->type == TOK_ID || |
| 4644 | cur->next->type == TOK_PREPROC_ID |
| 4645 | || cur->next->type == TOK_NUMBER)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4646 | cur = cur->next; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4647 | |
| 4648 | /* If identifier consists of just one token, don't expand */ |
| 4649 | if (cur == tline) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4650 | return tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4651 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4652 | if (cur) { |
| 4653 | oldnext = cur->next; /* Detach the tail past identifier */ |
| 4654 | cur->next = NULL; /* so that expand_smacro stops here */ |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4655 | } |
| 4656 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4657 | tline = expand_smacro(tline); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4658 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4659 | if (cur) { |
| 4660 | /* expand_smacro possibly changhed tline; re-scan for EOL */ |
| 4661 | cur = tline; |
| 4662 | while (cur && cur->next) |
| 4663 | cur = cur->next; |
| 4664 | if (cur) |
| 4665 | cur->next = oldnext; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4666 | } |
| 4667 | |
| 4668 | return tline; |
| 4669 | } |
| 4670 | |
| 4671 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4672 | * Determine whether the given line constitutes a multi-line macro |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4673 | * 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] | 4674 | * to check for an initial label - that's taken care of in |
| 4675 | * expand_mmacro - but must check numbers of parameters. Guaranteed |
| 4676 | * to be called with tline->type == TOK_ID, so the putative macro |
| 4677 | * name is easy to find. |
| 4678 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4679 | static MMacro *is_mmacro(Token * tline, Token *** params_array) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4680 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4681 | MMacro *head, *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4682 | Token **params; |
| 4683 | int nparam; |
| 4684 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4685 | head = (MMacro *) hash_findix(&mmacros, tline->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4686 | |
| 4687 | /* |
| 4688 | * Efficiency: first we see if any macro exists with the given |
| 4689 | * name. If not, we can return NULL immediately. _Then_ we |
| 4690 | * count the parameters, and then we look further along the |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4691 | * list if necessary to find the proper MMacro. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4692 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4693 | list_for_each(m, head) |
| 4694 | if (!mstrcmp(m->name, tline->text, m->casesense)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4695 | break; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4696 | if (!m) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4697 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4698 | |
| 4699 | /* |
| 4700 | * OK, we have a potential macro. Count and demarcate the |
| 4701 | * parameters. |
| 4702 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4703 | count_mmac_params(tline->next, &nparam, ¶ms); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4704 | |
| 4705 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4706 | * 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] | 4707 | * structure that handles this number. |
| 4708 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4709 | while (m) { |
| 4710 | if (m->nparam_min <= nparam |
| 4711 | && (m->plus || nparam <= m->nparam_max)) { |
| 4712 | /* |
| 4713 | * This one is right. Just check if cycle removal |
| 4714 | * prohibits us using it before we actually celebrate... |
| 4715 | */ |
| 4716 | if (m->in_progress > m->max_depth) { |
| 4717 | if (m->max_depth > 0) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4718 | nasm_error(ERR_WARNING, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4719 | "reached maximum recursion depth of %i", |
| 4720 | m->max_depth); |
| 4721 | } |
| 4722 | nasm_free(params); |
| 4723 | return NULL; |
| 4724 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4725 | /* |
| 4726 | * It's right, and we can use it. Add its default |
| 4727 | * parameters to the end of our list if necessary. |
| 4728 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4729 | if (m->defaults && nparam < m->nparam_min + m->ndefs) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4730 | params = |
| 4731 | nasm_realloc(params, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4732 | ((m->nparam_min + m->ndefs + |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4733 | 1) * sizeof(*params))); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4734 | while (nparam < m->nparam_min + m->ndefs) { |
| 4735 | params[nparam] = m->defaults[nparam - m->nparam_min]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4736 | nparam++; |
| 4737 | } |
| 4738 | } |
| 4739 | /* |
| 4740 | * If we've gone over the maximum parameter count (and |
| 4741 | * we're in Plus mode), ignore parameters beyond |
| 4742 | * nparam_max. |
| 4743 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4744 | if (m->plus && nparam > m->nparam_max) |
| 4745 | nparam = m->nparam_max; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4746 | /* |
| 4747 | * Then terminate the parameter list, and leave. |
| 4748 | */ |
| 4749 | if (!params) { /* need this special case */ |
| 4750 | params = nasm_malloc(sizeof(*params)); |
| 4751 | nparam = 0; |
| 4752 | } |
| 4753 | params[nparam] = NULL; |
| 4754 | *params_array = params; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4755 | return m; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4756 | } |
| 4757 | /* |
| 4758 | * This one wasn't right: look for the next one with the |
| 4759 | * same name. |
| 4760 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4761 | list_for_each(m, m->next) |
| 4762 | if (!mstrcmp(m->name, tline->text, m->casesense)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4763 | break; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4764 | } |
| 4765 | |
| 4766 | /* |
| 4767 | * After all that, we didn't find one with the right number of |
| 4768 | * parameters. Issue a warning, and fail to expand the macro. |
| 4769 | */ |
H. Peter Anvin (Intel) | 77f53ba | 2018-12-12 14:38:50 -0800 | [diff] [blame] | 4770 | nasm_error(ERR_WARNING|ERR_PASS1|WARN_MNP, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4771 | "macro `%s' exists, but not taking %d parameters", |
| 4772 | tline->text, nparam); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4773 | nasm_free(params); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4774 | return NULL; |
| 4775 | } |
| 4776 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4777 | |
| 4778 | /* |
| 4779 | * Save MMacro invocation specific fields in |
| 4780 | * preparation for a recursive macro expansion |
| 4781 | */ |
| 4782 | static void push_mmacro(MMacro *m) |
| 4783 | { |
| 4784 | MMacroInvocation *i; |
| 4785 | |
| 4786 | i = nasm_malloc(sizeof(MMacroInvocation)); |
| 4787 | i->prev = m->prev; |
| 4788 | i->params = m->params; |
| 4789 | i->iline = m->iline; |
| 4790 | i->nparam = m->nparam; |
| 4791 | i->rotate = m->rotate; |
| 4792 | i->paramlen = m->paramlen; |
| 4793 | i->unique = m->unique; |
| 4794 | i->condcnt = m->condcnt; |
| 4795 | m->prev = i; |
| 4796 | } |
| 4797 | |
| 4798 | |
| 4799 | /* |
| 4800 | * Restore MMacro invocation specific fields that were |
| 4801 | * saved during a previous recursive macro expansion |
| 4802 | */ |
| 4803 | static void pop_mmacro(MMacro *m) |
| 4804 | { |
| 4805 | MMacroInvocation *i; |
| 4806 | |
| 4807 | if (m->prev) { |
| 4808 | i = m->prev; |
| 4809 | m->prev = i->prev; |
| 4810 | m->params = i->params; |
| 4811 | m->iline = i->iline; |
| 4812 | m->nparam = i->nparam; |
| 4813 | m->rotate = i->rotate; |
| 4814 | m->paramlen = i->paramlen; |
| 4815 | m->unique = i->unique; |
| 4816 | m->condcnt = i->condcnt; |
| 4817 | nasm_free(i); |
| 4818 | } |
| 4819 | } |
| 4820 | |
| 4821 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4822 | /* |
| 4823 | * Expand the multi-line macro call made by the given line, if |
| 4824 | * 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] | 4825 | * istk->expansion and return 1. Otherwise return 0. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4826 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4827 | static int expand_mmacro(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4828 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4829 | Token *startline = tline; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4830 | Token *label = NULL; |
| 4831 | int dont_prepend = 0; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4832 | Token **params, *t, *tt; |
| 4833 | MMacro *m; |
| 4834 | Line *l, *ll; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4835 | int i, nparam, *paramlen; |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 4836 | const char *mname; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4837 | |
| 4838 | t = tline; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4839 | skip_white_(t); |
H. Peter Anvin | ce2233b | 2008-05-25 21:57:00 -0700 | [diff] [blame] | 4840 | /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */ |
H. Peter Anvin | dce1e2f | 2002-04-30 21:06:37 +0000 | [diff] [blame] | 4841 | 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] | 4842 | return 0; |
| 4843 | m = is_mmacro(t, ¶ms); |
| 4844 | if (m) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4845 | mname = t->text; |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 4846 | } else { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4847 | Token *last; |
| 4848 | /* |
| 4849 | * We have an id which isn't a macro call. We'll assume |
| 4850 | * it might be a label; we'll also check to see if a |
| 4851 | * colon follows it. Then, if there's another id after |
| 4852 | * that lot, we'll check it again for macro-hood. |
| 4853 | */ |
| 4854 | label = last = t; |
| 4855 | t = t->next; |
| 4856 | if (tok_type_(t, TOK_WHITESPACE)) |
| 4857 | last = t, t = t->next; |
| 4858 | if (tok_is_(t, ":")) { |
| 4859 | dont_prepend = 1; |
| 4860 | last = t, t = t->next; |
| 4861 | if (tok_type_(t, TOK_WHITESPACE)) |
| 4862 | last = t, t = t->next; |
| 4863 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4864 | if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, ¶ms))) |
| 4865 | return 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4866 | last->next = NULL; |
Keith Kanios | 891775e | 2009-07-11 06:08:54 -0500 | [diff] [blame] | 4867 | mname = t->text; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4868 | tline = t; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4869 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4870 | |
| 4871 | /* |
| 4872 | * Fix up the parameters: this involves stripping leading and |
| 4873 | * trailing whitespace, then stripping braces if they are |
| 4874 | * present. |
| 4875 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4876 | for (nparam = 0; params[nparam]; nparam++) ; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4877 | paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4878 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4879 | for (i = 0; params[i]; i++) { |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4880 | int brace = 0; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4881 | int comma = (!m->plus || i < nparam - 1); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4882 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4883 | t = params[i]; |
| 4884 | skip_white_(t); |
| 4885 | if (tok_is_(t, "{")) |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4886 | t = t->next, brace++, comma = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4887 | params[i] = t; |
| 4888 | paramlen[i] = 0; |
| 4889 | while (t) { |
| 4890 | if (comma && t->type == TOK_OTHER && !strcmp(t->text, ",")) |
| 4891 | break; /* ... because we have hit a comma */ |
| 4892 | if (comma && t->type == TOK_WHITESPACE |
| 4893 | && tok_is_(t->next, ",")) |
| 4894 | break; /* ... or a space then a comma */ |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4895 | if (brace && t->type == TOK_OTHER) { |
| 4896 | if (t->text[0] == '{') |
| 4897 | brace++; /* ... or a nested opening brace */ |
| 4898 | else if (t->text[0] == '}') |
| 4899 | if (!--brace) |
| 4900 | break; /* ... or a brace */ |
| 4901 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4902 | t = t->next; |
| 4903 | paramlen[i]++; |
| 4904 | } |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4905 | if (brace) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4906 | nasm_error(ERR_NONFATAL, "macro params should be enclosed in braces"); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4907 | } |
| 4908 | |
| 4909 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4910 | * OK, we have a MMacro structure together with a set of |
| 4911 | * parameters. We must now go through the expansion and push |
| 4912 | * copies of each Line on to istk->expansion. Substitution of |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4913 | * parameter tokens and macro-local tokens doesn't get done |
| 4914 | * until the single-line macro substitution process; this is |
| 4915 | * because delaying them allows us to change the semantics |
| 4916 | * later through %rotate. |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4917 | * |
| 4918 | * First, push an end marker on to istk->expansion, mark this |
| 4919 | * macro as in progress, and set up its invocation-specific |
| 4920 | * variables. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4921 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4922 | ll = nasm_malloc(sizeof(Line)); |
| 4923 | ll->next = istk->expansion; |
| 4924 | ll->finishes = m; |
| 4925 | ll->first = NULL; |
| 4926 | istk->expansion = ll; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4927 | |
| 4928 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4929 | * Save the previous MMacro expansion in the case of |
| 4930 | * macro recursion |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4931 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4932 | if (m->max_depth && m->in_progress) |
| 4933 | push_mmacro(m); |
| 4934 | |
| 4935 | m->in_progress ++; |
| 4936 | m->params = params; |
| 4937 | m->iline = tline; |
| 4938 | m->nparam = nparam; |
| 4939 | m->rotate = 0; |
| 4940 | m->paramlen = paramlen; |
| 4941 | m->unique = unique++; |
| 4942 | m->lineno = 0; |
| 4943 | m->condcnt = 0; |
| 4944 | |
| 4945 | m->next_active = istk->mstk; |
| 4946 | istk->mstk = m; |
| 4947 | |
| 4948 | list_for_each(l, m->expansion) { |
| 4949 | Token **tail; |
| 4950 | |
| 4951 | ll = nasm_malloc(sizeof(Line)); |
| 4952 | ll->finishes = NULL; |
| 4953 | ll->next = istk->expansion; |
| 4954 | istk->expansion = ll; |
| 4955 | tail = &ll->first; |
| 4956 | |
| 4957 | list_for_each(t, l->first) { |
| 4958 | Token *x = t; |
| 4959 | switch (t->type) { |
| 4960 | case TOK_PREPROC_Q: |
| 4961 | tt = *tail = new_Token(NULL, TOK_ID, mname, 0); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4962 | break; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4963 | case TOK_PREPROC_QQ: |
| 4964 | tt = *tail = new_Token(NULL, TOK_ID, m->name, 0); |
| 4965 | break; |
| 4966 | case TOK_PREPROC_ID: |
| 4967 | if (t->text[1] == '0' && t->text[2] == '0') { |
| 4968 | dont_prepend = -1; |
| 4969 | x = label; |
| 4970 | if (!x) |
| 4971 | continue; |
| 4972 | } |
| 4973 | /* fall through */ |
| 4974 | default: |
| 4975 | tt = *tail = new_Token(NULL, x->type, x->text, 0); |
| 4976 | break; |
| 4977 | } |
| 4978 | tail = &tt->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4979 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4980 | *tail = NULL; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4981 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4982 | |
| 4983 | /* |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4984 | * If we had a label, push it on as the first line of |
| 4985 | * the macro expansion. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4986 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4987 | if (label) { |
| 4988 | if (dont_prepend < 0) |
| 4989 | free_tlist(startline); |
| 4990 | else { |
| 4991 | ll = nasm_malloc(sizeof(Line)); |
| 4992 | ll->finishes = NULL; |
| 4993 | ll->next = istk->expansion; |
| 4994 | istk->expansion = ll; |
| 4995 | ll->first = startline; |
| 4996 | if (!dont_prepend) { |
| 4997 | while (label->next) |
| 4998 | label = label->next; |
| 4999 | label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5000 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5001 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 5002 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5003 | |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 5004 | lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5005 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5006 | return 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5007 | } |
| 5008 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5009 | /* |
| 5010 | * This function adds macro names to error messages, and suppresses |
| 5011 | * them if necessary. |
| 5012 | */ |
| 5013 | 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] | 5014 | { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5015 | char buff[BUFSIZ]; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5016 | MMacro *mmac = NULL; |
| 5017 | int delta = 0; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 5018 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5019 | /* |
| 5020 | * If we're in a dead branch of IF or something like it, ignore the error. |
| 5021 | * However, because %else etc are evaluated in the state context |
| 5022 | * of the previous branch, errors might get lost: |
| 5023 | * %if 0 ... %else trailing garbage ... %endif |
| 5024 | * So %else etc should set the ERR_PP_PRECOND flag. |
| 5025 | */ |
| 5026 | if ((severity & ERR_MASK) < ERR_FATAL && |
| 5027 | istk && istk->conds && |
| 5028 | ((severity & ERR_PP_PRECOND) ? |
| 5029 | istk->conds->state == COND_NEVER : |
H. Peter Anvin | eb6653f | 2016-04-05 13:03:10 -0700 | [diff] [blame] | 5030 | !emitting(istk->conds->state))) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5031 | return; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 5032 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5033 | /* get %macro name */ |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5034 | if (!(severity & ERR_NOFILE) && istk && istk->mstk) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5035 | mmac = istk->mstk; |
| 5036 | /* but %rep blocks should be skipped */ |
| 5037 | while (mmac && !mmac->name) |
| 5038 | mmac = mmac->next_active, delta++; |
Cyrill Gorcunov | 9900c6b | 2011-10-09 18:58:46 +0400 | [diff] [blame] | 5039 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5040 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5041 | if (mmac) { |
| 5042 | vsnprintf(buff, sizeof(buff), fmt, arg); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 5043 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5044 | nasm_set_verror(real_verror); |
| 5045 | nasm_error(severity, "(%s:%d) %s", |
| 5046 | mmac->name, mmac->lineno - delta, buff); |
| 5047 | nasm_set_verror(pp_verror); |
| 5048 | } else { |
| 5049 | real_verror(severity, fmt, arg); |
| 5050 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 5051 | } |
| 5052 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 5053 | static Token *stdmac_file(const SMacro *s, Token **params, int *paramsize) |
| 5054 | { |
| 5055 | (void)s; |
| 5056 | (void)params; |
| 5057 | (void)paramsize; |
| 5058 | |
| 5059 | return make_tok_qstr(src_get_fname()); |
| 5060 | } |
| 5061 | |
| 5062 | static Token *stdmac_line(const SMacro *s, Token **params, int *paramsize) |
| 5063 | { |
| 5064 | (void)s; |
| 5065 | (void)params; |
| 5066 | (void)paramsize; |
| 5067 | |
| 5068 | return make_tok_num(src_get_linnum()); |
| 5069 | } |
| 5070 | |
| 5071 | static Token *stdmac_bits(const SMacro *s, Token **params, int *paramsize) |
| 5072 | { |
| 5073 | (void)s; |
| 5074 | (void)params; |
| 5075 | (void)paramsize; |
| 5076 | |
| 5077 | return make_tok_num(globalbits); |
| 5078 | } |
| 5079 | |
| 5080 | static Token *stdmac_ptr(const SMacro *s, Token **params, int *paramsize) |
| 5081 | { |
| 5082 | const char *name; |
| 5083 | |
| 5084 | (void)s; |
| 5085 | (void)params; |
| 5086 | (void)paramsize; |
| 5087 | |
| 5088 | switch (globalbits) { |
| 5089 | case 16: |
| 5090 | name = "word"; |
| 5091 | break; |
| 5092 | case 32: |
| 5093 | name = "dword"; |
| 5094 | break; |
| 5095 | case 64: |
| 5096 | name = "qword"; |
| 5097 | break; |
| 5098 | default: |
| 5099 | panic(); |
| 5100 | } |
| 5101 | return new_Token(NULL, TOK_ID, name, 0); |
| 5102 | } |
| 5103 | |
| 5104 | static Token *stdmac_pass(const SMacro *s, Token **params, int *paramsize) |
| 5105 | { |
| 5106 | (void)s; |
| 5107 | (void)params; |
| 5108 | (void)paramsize; |
| 5109 | |
| 5110 | return make_tok_num(pass); |
| 5111 | } |
| 5112 | |
| 5113 | /* Add magic standard macros */ |
| 5114 | struct magic_macros { |
| 5115 | const char *name; |
| 5116 | int nparams; |
| 5117 | Token *(*func)(const SMacro *s, Token **params, int *paramsize); |
| 5118 | }; |
| 5119 | static const struct magic_macros magic_macros[] = |
| 5120 | { |
| 5121 | { "__FILE__", 0, stdmac_file }, |
| 5122 | { "__LINE__", 0, stdmac_line }, |
| 5123 | { "__BITS__", 0, stdmac_bits }, |
| 5124 | { "__PTR__", 0, stdmac_ptr }, |
| 5125 | { "__PASS__", 0, stdmac_pass }, |
| 5126 | { NULL, 0, NULL } |
| 5127 | }; |
| 5128 | |
| 5129 | static void pp_add_magic_stdmac(void) |
| 5130 | { |
| 5131 | const struct magic_macros *m; |
| 5132 | SMacro *s; |
| 5133 | |
| 5134 | for (m = magic_macros; m->name; m++) { |
| 5135 | s = define_smacro(NULL, m->name, true, m->nparams, NULL); |
| 5136 | s->magic = true; |
| 5137 | s->e.magic = m->func; |
| 5138 | } |
| 5139 | } |
| 5140 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5141 | static void |
H. Peter Anvin | 81b62b9 | 2017-12-20 13:33:49 -0800 | [diff] [blame] | 5142 | pp_reset(const char *file, int apass, StrList **deplist) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5143 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5144 | cstk = NULL; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 5145 | nasm_new(istk); |
H. Peter Anvin | 3e83cec | 2016-05-25 04:28:46 -0700 | [diff] [blame] | 5146 | istk->fp = nasm_open_read(file, NF_TEXT); |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 5147 | src_set(0, file); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5148 | istk->lineinc = 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5149 | if (!istk->fp) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5150 | nasm_fatal(ERR_NOFILE, "unable to open input file `%s'", file); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5151 | defining = NULL; |
Charles Crayne | d4200be | 2008-07-12 16:42:33 -0700 | [diff] [blame] | 5152 | nested_mac_count = 0; |
| 5153 | nested_rep_count = 0; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 5154 | init_macros(); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5155 | unique = 0; |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 5156 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 5157 | pp_add_magic_stdmac(); |
| 5158 | |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 5159 | if (tasm_compatible_mode) |
| 5160 | pp_add_stdmac(nasm_stdmac_tasm); |
| 5161 | |
| 5162 | pp_add_stdmac(nasm_stdmac_nasm); |
| 5163 | pp_add_stdmac(nasm_stdmac_version); |
| 5164 | |
Cyrill Gorcunov | 15ce78f | 2017-01-06 20:21:28 +0300 | [diff] [blame] | 5165 | if (extrastdmac) |
| 5166 | pp_add_stdmac(extrastdmac); |
| 5167 | |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 5168 | stdmacpos = stdmacros[0]; |
| 5169 | stdmacnext = &stdmacros[1]; |
| 5170 | |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 5171 | do_predef = true; |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 5172 | |
| 5173 | /* |
| 5174 | * 0 for dependencies, 1 for preparatory passes, 2 for final pass. |
| 5175 | * The caller, however, will also pass in 3 for preprocess-only so |
| 5176 | * we can set __PASS__ accordingly. |
| 5177 | */ |
| 5178 | pass = apass > 2 ? 2 : apass; |
| 5179 | |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 5180 | dephead = deplist; |
H. Peter Anvin | 436e367 | 2016-10-04 01:12:28 -0700 | [diff] [blame] | 5181 | nasm_add_string_to_strlist(dephead, file); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5182 | } |
| 5183 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 5184 | static void pp_init(void) |
| 5185 | { |
| 5186 | hash_init(&FileHash, HASH_MEDIUM); |
| 5187 | } |
| 5188 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5189 | static char *pp_getline(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5190 | { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5191 | char *line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5192 | Token *tline; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5193 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5194 | real_verror = nasm_set_verror(pp_verror); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5195 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5196 | while (1) { |
| 5197 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5198 | * Fetch a tokenized line, either from the macro-expansion |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5199 | * buffer or from the input file. |
| 5200 | */ |
| 5201 | tline = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5202 | while (istk->expansion && istk->expansion->finishes) { |
| 5203 | Line *l = istk->expansion; |
| 5204 | if (!l->finishes->name && l->finishes->in_progress > 1) { |
| 5205 | Line *ll; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5206 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5207 | /* |
| 5208 | * This is a macro-end marker for a macro with no |
| 5209 | * name, which means it's not really a macro at all |
| 5210 | * but a %rep block, and the `in_progress' field is |
| 5211 | * more than 1, meaning that we still need to |
| 5212 | * repeat. (1 means the natural last repetition; 0 |
| 5213 | * means termination by %exitrep.) We have |
| 5214 | * therefore expanded up to the %endrep, and must |
| 5215 | * push the whole block on to the expansion buffer |
| 5216 | * again. We don't bother to remove the macro-end |
| 5217 | * marker: we'd only have to generate another one |
| 5218 | * if we did. |
| 5219 | */ |
| 5220 | l->finishes->in_progress--; |
| 5221 | list_for_each(l, l->finishes->expansion) { |
| 5222 | Token *t, *tt, **tail; |
| 5223 | |
| 5224 | ll = nasm_malloc(sizeof(Line)); |
| 5225 | ll->next = istk->expansion; |
| 5226 | ll->finishes = NULL; |
| 5227 | ll->first = NULL; |
| 5228 | tail = &ll->first; |
| 5229 | |
| 5230 | list_for_each(t, l->first) { |
| 5231 | if (t->text || t->type == TOK_WHITESPACE) { |
| 5232 | tt = *tail = new_Token(NULL, t->type, t->text, 0); |
| 5233 | tail = &tt->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5234 | } |
| 5235 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5236 | |
| 5237 | istk->expansion = ll; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5238 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5239 | } else { |
| 5240 | /* |
| 5241 | * Check whether a `%rep' was started and not ended |
| 5242 | * within this macro expansion. This can happen and |
| 5243 | * should be detected. It's a fatal error because |
| 5244 | * I'm too confused to work out how to recover |
| 5245 | * sensibly from it. |
| 5246 | */ |
| 5247 | if (defining) { |
| 5248 | if (defining->name) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5249 | nasm_panic(0, "defining with name in expansion"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5250 | else if (istk->mstk->name) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5251 | nasm_fatal(0, "`%%rep' without `%%endrep' within" |
| 5252 | " expansion of macro `%s'", |
| 5253 | istk->mstk->name); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5254 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5255 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5256 | /* |
| 5257 | * FIXME: investigate the relationship at this point between |
| 5258 | * istk->mstk and l->finishes |
| 5259 | */ |
| 5260 | { |
| 5261 | MMacro *m = istk->mstk; |
| 5262 | istk->mstk = m->next_active; |
| 5263 | if (m->name) { |
| 5264 | /* |
| 5265 | * This was a real macro call, not a %rep, and |
| 5266 | * therefore the parameter information needs to |
| 5267 | * be freed. |
| 5268 | */ |
| 5269 | if (m->prev) { |
| 5270 | pop_mmacro(m); |
| 5271 | l->finishes->in_progress --; |
| 5272 | } else { |
| 5273 | nasm_free(m->params); |
| 5274 | free_tlist(m->iline); |
| 5275 | nasm_free(m->paramlen); |
| 5276 | l->finishes->in_progress = 0; |
| 5277 | } |
Adam Majer | 91e7240 | 2017-07-25 10:42:01 +0200 | [diff] [blame] | 5278 | } |
| 5279 | |
| 5280 | /* |
| 5281 | * FIXME It is incorrect to always free_mmacro here. |
| 5282 | * It leads to usage-after-free. |
| 5283 | * |
| 5284 | * https://bugzilla.nasm.us/show_bug.cgi?id=3392414 |
| 5285 | */ |
| 5286 | #if 0 |
| 5287 | else |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5288 | free_mmacro(m); |
Adam Majer | 91e7240 | 2017-07-25 10:42:01 +0200 | [diff] [blame] | 5289 | #endif |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5290 | } |
| 5291 | istk->expansion = l->next; |
| 5292 | nasm_free(l); |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 5293 | lfmt->downlevel(LIST_MACRO); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5294 | } |
| 5295 | } |
| 5296 | while (1) { /* until we get a line we can use */ |
| 5297 | |
| 5298 | if (istk->expansion) { /* from a macro expansion */ |
| 5299 | char *p; |
| 5300 | Line *l = istk->expansion; |
| 5301 | if (istk->mstk) |
| 5302 | istk->mstk->lineno++; |
| 5303 | tline = l->first; |
| 5304 | istk->expansion = l->next; |
| 5305 | nasm_free(l); |
| 5306 | p = detoken(tline, false); |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 5307 | lfmt->line(LIST_MACRO, p); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5308 | nasm_free(p); |
| 5309 | break; |
| 5310 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5311 | line = read_line(); |
| 5312 | if (line) { /* from the current input file */ |
| 5313 | line = prepreproc(line); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5314 | tline = tokenize(line); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5315 | nasm_free(line); |
| 5316 | break; |
| 5317 | } |
| 5318 | /* |
| 5319 | * The current file has ended; work down the istk |
| 5320 | */ |
| 5321 | { |
| 5322 | Include *i = istk; |
| 5323 | fclose(i->fp); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5324 | if (i->conds) { |
| 5325 | /* nasm_error can't be conditionally suppressed */ |
H. Peter Anvin | 4108706 | 2016-03-03 14:27:34 -0800 | [diff] [blame] | 5326 | nasm_fatal(0, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5327 | "expected `%%endif' before end of file"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5328 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5329 | /* 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] | 5330 | if (i->next) |
| 5331 | src_set(i->lineno, i->fname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5332 | istk = i->next; |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 5333 | lfmt->downlevel(LIST_INCLUDE); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5334 | nasm_free(i); |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5335 | if (!istk) { |
| 5336 | line = NULL; |
| 5337 | goto done; |
| 5338 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5339 | if (istk->expansion && istk->expansion->finishes) |
| 5340 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5341 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5342 | } |
| 5343 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5344 | /* |
| 5345 | * We must expand MMacro parameters and MMacro-local labels |
| 5346 | * _before_ we plunge into directive processing, to cope |
| 5347 | * with things like `%define something %1' such as STRUC |
| 5348 | * uses. Unless we're _defining_ a MMacro, in which case |
| 5349 | * those tokens should be left alone to go into the |
| 5350 | * definition; and unless we're in a non-emitting |
| 5351 | * condition, in which case we don't want to meddle with |
| 5352 | * anything. |
| 5353 | */ |
| 5354 | if (!defining && !(istk->conds && !emitting(istk->conds->state)) |
| 5355 | && !(istk->mstk && !istk->mstk->in_progress)) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5356 | tline = expand_mmac_params(tline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5357 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5358 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5359 | /* |
| 5360 | * Check the line to see if it's a preprocessor directive. |
| 5361 | */ |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 5362 | if (do_directive(tline, &line) == DIRECTIVE_FOUND) { |
| 5363 | if (line) |
| 5364 | break; /* Directive generated output */ |
| 5365 | else |
| 5366 | continue; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5367 | } else if (defining) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5368 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5369 | * We're defining a multi-line macro. We emit nothing |
| 5370 | * at all, and just |
| 5371 | * shove the tokenized line on to the macro definition. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5372 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5373 | Line *l = nasm_malloc(sizeof(Line)); |
| 5374 | l->next = defining->expansion; |
| 5375 | l->first = tline; |
| 5376 | l->finishes = NULL; |
| 5377 | defining->expansion = l; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5378 | continue; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5379 | } else if (istk->conds && !emitting(istk->conds->state)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5380 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5381 | * We're in a non-emitting branch of a condition block. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5382 | * Emit nothing at all, not even a blank line: when we |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5383 | * emerge from the condition we'll give a line-number |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5384 | * directive so we keep our place correctly. |
| 5385 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5386 | free_tlist(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5387 | continue; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5388 | } else if (istk->mstk && !istk->mstk->in_progress) { |
| 5389 | /* |
| 5390 | * We're in a %rep block which has been terminated, so |
| 5391 | * we're walking through to the %endrep without |
| 5392 | * emitting anything. Emit nothing at all, not even a |
| 5393 | * blank line: when we emerge from the %rep block we'll |
| 5394 | * give a line-number directive so we keep our place |
| 5395 | * correctly. |
| 5396 | */ |
| 5397 | free_tlist(tline); |
| 5398 | continue; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5399 | } else { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5400 | tline = expand_smacro(tline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5401 | if (!expand_mmacro(tline)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5402 | /* |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5403 | * De-tokenize the line again, and emit it. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5404 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5405 | line = detoken(tline, true); |
| 5406 | free_tlist(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5407 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5408 | } else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5409 | continue; /* expand_mmacro calls free_tlist */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5410 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5411 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5412 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5413 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5414 | done: |
| 5415 | nasm_set_verror(real_verror); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5416 | return line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5417 | } |
| 5418 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5419 | static void pp_cleanup(int pass) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5420 | { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5421 | real_verror = nasm_set_verror(pp_verror); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5422 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5423 | if (defining) { |
| 5424 | if (defining->name) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5425 | nasm_error(ERR_NONFATAL, |
| 5426 | "end of file while still defining macro `%s'", |
| 5427 | defining->name); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5428 | } else { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5429 | nasm_error(ERR_NONFATAL, "end of file while still in %%rep"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5430 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5431 | |
| 5432 | free_mmacro(defining); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5433 | defining = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5434 | } |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5435 | |
| 5436 | nasm_set_verror(real_verror); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5437 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5438 | while (cstk) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5439 | ctx_pop(); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 5440 | free_macros(); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5441 | while (istk) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5442 | Include *i = istk; |
| 5443 | istk = istk->next; |
| 5444 | fclose(i->fp); |
Cyrill Gorcunov | 8dcfd88 | 2011-03-03 09:18:56 +0300 | [diff] [blame] | 5445 | nasm_free(i); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5446 | } |
| 5447 | while (cstk) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5448 | ctx_pop(); |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 5449 | src_set_fname(NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5450 | if (pass == 0) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5451 | IncPath *i; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5452 | free_llist(predef); |
Cyrill Gorcunov | dae24d7 | 2014-06-28 10:17:39 +0400 | [diff] [blame] | 5453 | predef = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5454 | delete_Blocks(); |
Cyrill Gorcunov | dae24d7 | 2014-06-28 10:17:39 +0400 | [diff] [blame] | 5455 | freeTokens = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5456 | while ((i = ipath)) { |
| 5457 | ipath = i->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5458 | if (i->path) |
| 5459 | nasm_free(i->path); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5460 | nasm_free(i); |
| 5461 | } |
H. Peter Anvin | 11dfa1a | 2008-07-02 18:11:04 -0700 | [diff] [blame] | 5462 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5463 | } |
| 5464 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5465 | static void pp_include_path(char *path) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5466 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5467 | IncPath *i; |
H. Peter Anvin | 37a321f | 2007-09-24 13:41:58 -0700 | [diff] [blame] | 5468 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5469 | i = nasm_malloc(sizeof(IncPath)); |
| 5470 | i->path = path ? nasm_strdup(path) : NULL; |
| 5471 | i->next = NULL; |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5472 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 5473 | if (ipath) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5474 | IncPath *j = ipath; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 5475 | while (j->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5476 | j = j->next; |
| 5477 | j->next = i; |
| 5478 | } else { |
| 5479 | ipath = i; |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5480 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5481 | } |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5482 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5483 | static void pp_pre_include(char *fname) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5484 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5485 | Token *inc, *space, *name; |
| 5486 | Line *l; |
| 5487 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5488 | name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0); |
| 5489 | space = new_Token(name, TOK_WHITESPACE, NULL, 0); |
| 5490 | inc = new_Token(space, TOK_PREPROC_ID, "%include", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5491 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5492 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5493 | l->next = predef; |
| 5494 | l->first = inc; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5495 | l->finishes = NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5496 | predef = l; |
| 5497 | } |
| 5498 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5499 | static void pp_pre_define(char *definition) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5500 | { |
| 5501 | Token *def, *space; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5502 | Line *l; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5503 | char *equals; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5504 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5505 | real_verror = nasm_set_verror(pp_verror); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5506 | |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5507 | equals = strchr(definition, '='); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5508 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 5509 | def = new_Token(space, TOK_PREPROC_ID, "%define", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5510 | if (equals) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5511 | *equals = ' '; |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5512 | space->next = tokenize(definition); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5513 | if (equals) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5514 | *equals = '='; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5515 | |
Cyrill Gorcunov | 6d42e9b | 2015-02-08 11:07:17 +0300 | [diff] [blame] | 5516 | if (space->next->type != TOK_PREPROC_ID && |
| 5517 | space->next->type != TOK_ID) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5518 | nasm_error(ERR_WARNING, "pre-defining non ID `%s\'\n", definition); |
Cyrill Gorcunov | 6d42e9b | 2015-02-08 11:07:17 +0300 | [diff] [blame] | 5519 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5520 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5521 | l->next = predef; |
| 5522 | l->first = def; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5523 | l->finishes = NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5524 | predef = l; |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5525 | |
| 5526 | nasm_set_verror(real_verror); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5527 | } |
| 5528 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5529 | static void pp_pre_undefine(char *definition) |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5530 | { |
| 5531 | Token *def, *space; |
| 5532 | Line *l; |
| 5533 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5534 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 5535 | def = new_Token(space, TOK_PREPROC_ID, "%undef", 0); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5536 | space->next = tokenize(definition); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5537 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5538 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5539 | l->next = predef; |
| 5540 | l->first = def; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5541 | l->finishes = NULL; |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5542 | predef = l; |
| 5543 | } |
| 5544 | |
H. Peter Anvin | 0599034 | 2018-06-11 13:32:42 -0700 | [diff] [blame] | 5545 | /* Insert an early preprocessor command that doesn't need special handling */ |
| 5546 | static void pp_pre_command(const char *what, char *string) |
| 5547 | { |
| 5548 | char *cmd; |
| 5549 | Token *def, *space; |
| 5550 | Line *l; |
| 5551 | |
| 5552 | def = tokenize(string); |
| 5553 | if (what) { |
| 5554 | cmd = nasm_strcat(what[0] == '%' ? "" : "%", what); |
| 5555 | space = new_Token(def, TOK_WHITESPACE, NULL, 0); |
| 5556 | def = new_Token(space, TOK_PREPROC_ID, cmd, 0); |
| 5557 | } |
| 5558 | |
| 5559 | l = nasm_malloc(sizeof(Line)); |
| 5560 | l->next = predef; |
| 5561 | l->first = def; |
| 5562 | l->finishes = NULL; |
| 5563 | predef = l; |
| 5564 | } |
| 5565 | |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 5566 | static void pp_add_stdmac(macros_t *macros) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5567 | { |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 5568 | macros_t **mp; |
| 5569 | |
| 5570 | /* Find the end of the list and avoid duplicates */ |
| 5571 | for (mp = stdmacros; *mp; mp++) { |
| 5572 | if (*mp == macros) |
| 5573 | return; /* Nothing to do */ |
| 5574 | } |
| 5575 | |
| 5576 | nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]); |
| 5577 | |
| 5578 | *mp = macros; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 5579 | } |
| 5580 | |
Cyrill Gorcunov | 15ce78f | 2017-01-06 20:21:28 +0300 | [diff] [blame] | 5581 | static void pp_extra_stdmac(macros_t *macros) |
| 5582 | { |
| 5583 | extrastdmac = macros; |
| 5584 | } |
| 5585 | |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 5586 | static Token *make_tok_num(int64_t val) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5587 | { |
Cyrill Gorcunov | ce65274 | 2013-05-06 23:43:43 +0400 | [diff] [blame] | 5588 | char numbuf[32]; |
H. Peter Anvin | 8b26247 | 2019-02-26 14:00:54 -0800 | [diff] [blame^] | 5589 | int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val); |
| 5590 | return new_Token(NULL, TOK_NUMBER, numbuf, len); |
| 5591 | } |
| 5592 | |
| 5593 | static Token *make_tok_qstr(const char *str) |
| 5594 | { |
| 5595 | Token *t = new_Token(NULL, TOK_STRING, NULL, 0); |
| 5596 | t->text = nasm_quote_cstr(str); |
| 5597 | return t; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5598 | } |
| 5599 | |
H. Peter Anvin | 3736895 | 2016-05-09 14:10:32 -0700 | [diff] [blame] | 5600 | static void pp_list_one_macro(MMacro *m, int severity) |
| 5601 | { |
| 5602 | if (!m) |
| 5603 | return; |
| 5604 | |
| 5605 | /* We need to print the next_active list in reverse order */ |
| 5606 | pp_list_one_macro(m->next_active, severity); |
| 5607 | |
| 5608 | if (m->name && !m->nolist) { |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 5609 | src_set(m->xline + m->lineno, m->fname); |
H. Peter Anvin | e2f5edb | 2018-12-11 00:06:29 -0800 | [diff] [blame] | 5610 | nasm_error(severity, "... from macro `%s' defined", m->name); |
H. Peter Anvin | 3736895 | 2016-05-09 14:10:32 -0700 | [diff] [blame] | 5611 | } |
| 5612 | } |
| 5613 | |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5614 | static void pp_error_list_macros(int severity) |
| 5615 | { |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5616 | int32_t saved_line; |
| 5617 | const char *saved_fname = NULL; |
| 5618 | |
H. Peter Anvin | e2f5edb | 2018-12-11 00:06:29 -0800 | [diff] [blame] | 5619 | severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE; |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 5620 | src_get(&saved_line, &saved_fname); |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5621 | |
Cyrill Gorcunov | 771d04e | 2016-05-10 23:27:03 +0300 | [diff] [blame] | 5622 | if (istk) |
| 5623 | pp_list_one_macro(istk->mstk, severity); |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5624 | |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 5625 | src_set(saved_line, saved_fname); |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5626 | } |
| 5627 | |
H. Peter Anvin | e746971 | 2016-02-18 02:20:59 -0800 | [diff] [blame] | 5628 | const struct preproc_ops nasmpp = { |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 5629 | pp_init, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5630 | pp_reset, |
| 5631 | pp_getline, |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5632 | pp_cleanup, |
Cyrill Gorcunov | 15ce78f | 2017-01-06 20:21:28 +0300 | [diff] [blame] | 5633 | pp_extra_stdmac, |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5634 | pp_pre_define, |
| 5635 | pp_pre_undefine, |
| 5636 | pp_pre_include, |
H. Peter Anvin | 0599034 | 2018-06-11 13:32:42 -0700 | [diff] [blame] | 5637 | pp_pre_command, |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5638 | pp_include_path, |
| 5639 | pp_error_list_macros, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5640 | }; |