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 | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 109 | SMacro *next; |
| 110 | char *name; |
| 111 | bool casesense; |
| 112 | bool in_progress; |
| 113 | unsigned int nparam; |
| 114 | Token *expansion; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 118 | * Store the definition of a multi-line macro. This is also used to |
| 119 | * store the interiors of `%rep...%endrep' blocks, which are |
| 120 | * effectively self-re-invoking multi-line macros which simply |
| 121 | * don't have a name or bother to appear in the hash tables. %rep |
| 122 | * blocks are signified by having a NULL `name' field. |
| 123 | * |
| 124 | * In a MMacro describing a `%rep' block, the `in_progress' field |
| 125 | * isn't merely boolean, but gives the number of repeats left to |
| 126 | * run. |
| 127 | * |
| 128 | * The `next' field is used for storing MMacros in hash tables; the |
| 129 | * `next_active' field is for stacking them on istk entries. |
| 130 | * |
| 131 | * When a MMacro is being expanded, `params', `iline', `nparam', |
| 132 | * `paramlen', `rotate' and `unique' are local to the invocation. |
| 133 | */ |
| 134 | struct MMacro { |
| 135 | MMacro *next; |
| 136 | MMacroInvocation *prev; /* previous invocation */ |
| 137 | char *name; |
| 138 | int nparam_min, nparam_max; |
| 139 | bool casesense; |
| 140 | bool plus; /* is the last parameter greedy? */ |
| 141 | bool nolist; /* is this macro listing-inhibited? */ |
| 142 | int64_t in_progress; /* is this macro currently being expanded? */ |
| 143 | int32_t max_depth; /* maximum number of recursive expansions allowed */ |
| 144 | Token *dlist; /* All defaults as one list */ |
| 145 | Token **defaults; /* Parameter default pointers */ |
| 146 | int ndefs; /* number of default parameters */ |
| 147 | Line *expansion; |
| 148 | |
| 149 | MMacro *next_active; |
| 150 | MMacro *rep_nest; /* used for nesting %rep */ |
| 151 | Token **params; /* actual parameters */ |
| 152 | Token *iline; /* invocation line */ |
| 153 | unsigned int nparam, rotate; |
| 154 | int *paramlen; |
| 155 | uint64_t unique; |
| 156 | int lineno; /* Current line number on expansion */ |
| 157 | uint64_t condcnt; /* number of if blocks... */ |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 158 | |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 159 | const char *fname; /* File where defined */ |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 160 | int32_t xline; /* First line in macro */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | |
| 164 | /* Store the definition of a multi-line macro, as defined in a |
| 165 | * previous recursive macro expansion. |
| 166 | */ |
| 167 | struct MMacroInvocation { |
| 168 | MMacroInvocation *prev; /* previous invocation */ |
| 169 | Token **params; /* actual parameters */ |
| 170 | Token *iline; /* invocation line */ |
| 171 | unsigned int nparam, rotate; |
| 172 | int *paramlen; |
| 173 | uint64_t unique; |
| 174 | uint64_t condcnt; |
| 175 | }; |
| 176 | |
| 177 | |
| 178 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 179 | * The context stack is composed of a linked list of these. |
| 180 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 181 | struct Context { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 182 | Context *next; |
| 183 | char *name; |
| 184 | struct hash_table localmac; |
| 185 | uint32_t number; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | /* |
| 189 | * This is the internal form which we break input lines up into. |
| 190 | * Typically stored in linked lists. |
| 191 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 192 | * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not |
| 193 | * necessarily used as-is, but is intended to denote the number of |
| 194 | * the substituted parameter. So in the definition |
| 195 | * |
| 196 | * %define a(x,y) ( (x) & ~(y) ) |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 197 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 198 | * the token representing `x' will have its type changed to |
| 199 | * TOK_SMAC_PARAM, but the one representing `y' will be |
| 200 | * TOK_SMAC_PARAM+1. |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 201 | * |
| 202 | * TOK_INTERNAL_STRING is a dirty hack: it's a single string token |
| 203 | * which doesn't need quotes around it. Used in the pre-include |
| 204 | * mechanism as an alternative to trying to find a sensible type of |
| 205 | * quote to use on the filename we were passed. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 206 | */ |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 207 | enum pp_token_type { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 208 | TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID, |
| 209 | TOK_PREPROC_ID, TOK_STRING, |
| 210 | TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER, |
H. Peter Anvin | 6c81f0a | 2008-05-25 21:46:17 -0700 | [diff] [blame] | 211 | TOK_INTERNAL_STRING, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 212 | TOK_PREPROC_Q, TOK_PREPROC_QQ, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 213 | TOK_PASTE, /* %+ */ |
| 214 | TOK_INDIRECT, /* %[...] */ |
| 215 | TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */ |
| 216 | TOK_MAX = INT_MAX /* Keep compiler from reducing the range */ |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 217 | }; |
| 218 | |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 219 | #define PP_CONCAT_MASK(x) (1 << (x)) |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 220 | #define PP_CONCAT_MATCH(t, mask) (PP_CONCAT_MASK((t)->type) & mask) |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 221 | |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 222 | struct tokseq_match { |
| 223 | int mask_head; |
| 224 | int mask_tail; |
| 225 | }; |
| 226 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 227 | struct Token { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 228 | Token *next; |
| 229 | char *text; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 230 | union { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 231 | SMacro *mac; /* associated macro for TOK_SMAC_END */ |
| 232 | size_t len; /* scratch length field */ |
| 233 | } a; /* Auxiliary data */ |
| 234 | enum pp_token_type type; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 235 | }; |
| 236 | |
| 237 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 238 | * Multi-line macro definitions are stored as a linked list of |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 239 | * these, which is essentially a container to allow several linked |
| 240 | * lists of Tokens. |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 241 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 242 | * Note that in this module, linked lists are treated as stacks |
| 243 | * wherever possible. For this reason, Lines are _pushed_ on to the |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 244 | * `expansion' field in MMacro structures, so that the linked list, |
| 245 | * if walked, would give the macro lines in reverse order; this |
| 246 | * means that we can walk the list when expanding a macro, and thus |
| 247 | * push the lines on to the `expansion' field in _istk_ in reverse |
| 248 | * order (so that when popped back off they are in the right |
| 249 | * order). It may seem cockeyed, and it relies on my design having |
| 250 | * an even number of steps in, but it works... |
| 251 | * |
| 252 | * Some of these structures, rather than being actual lines, are |
| 253 | * markers delimiting the end of the expansion of a given macro. |
| 254 | * This is for use in the cycle-tracking and %rep-handling code. |
| 255 | * Such structures have `finishes' non-NULL, and `first' NULL. All |
| 256 | * others have `finishes' NULL, but `first' may still be NULL if |
| 257 | * the line is blank. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 258 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 259 | struct Line { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 260 | Line *next; |
| 261 | MMacro *finishes; |
| 262 | Token *first; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 263 | }; |
| 264 | |
| 265 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 266 | * To handle an arbitrary level of file inclusion, we maintain a |
| 267 | * stack (ie linked list) of these things. |
| 268 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 269 | struct Include { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 270 | Include *next; |
| 271 | FILE *fp; |
| 272 | Cond *conds; |
| 273 | Line *expansion; |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 274 | const char *fname; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 275 | int lineno, lineinc; |
| 276 | MMacro *mstk; /* stack of active macros/reps */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 277 | }; |
| 278 | |
| 279 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 280 | * Include search path. This is simply a list of strings which get |
| 281 | * prepended, in turn, to the name of an include file, in an |
| 282 | * attempt to find the file if it's not in the current directory. |
| 283 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 284 | struct IncPath { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 285 | IncPath *next; |
| 286 | char *path; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 287 | }; |
| 288 | |
| 289 | /* |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 290 | * File real name hash, so we don't have to re-search the include |
| 291 | * path for every pass (and potentially more than that if a file |
| 292 | * is used more than once.) |
| 293 | */ |
| 294 | struct hash_table FileHash; |
| 295 | |
| 296 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 297 | * Conditional assembly: we maintain a separate stack of these for |
| 298 | * each level of file inclusion. (The only reason we keep the |
| 299 | * stacks separate is to ensure that a stray `%endif' in a file |
| 300 | * included from within the true branch of a `%if' won't terminate |
| 301 | * it and cause confusion: instead, rightly, it'll cause an error.) |
| 302 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 303 | struct Cond { |
| 304 | Cond *next; |
| 305 | int state; |
| 306 | }; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 307 | enum { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 308 | /* |
| 309 | * These states are for use just after %if or %elif: IF_TRUE |
| 310 | * means the condition has evaluated to truth so we are |
| 311 | * currently emitting, whereas IF_FALSE means we are not |
| 312 | * currently emitting but will start doing so if a %else comes |
| 313 | * up. In these states, all directives are admissible: %elif, |
| 314 | * %else and %endif. (And of course %if.) |
| 315 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 316 | COND_IF_TRUE, COND_IF_FALSE, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 317 | /* |
| 318 | * These states come up after a %else: ELSE_TRUE means we're |
| 319 | * emitting, and ELSE_FALSE means we're not. In ELSE_* states, |
| 320 | * any %elif or %else will cause an error. |
| 321 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 322 | COND_ELSE_TRUE, COND_ELSE_FALSE, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 323 | /* |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 324 | * These states mean that we're not emitting now, and also that |
| 325 | * nothing until %endif will be emitted at all. COND_DONE is |
| 326 | * used when we've had our moment of emission |
| 327 | * and have now started seeing %elifs. COND_NEVER is used when |
| 328 | * the condition construct in question is contained within a |
| 329 | * non-emitting branch of a larger condition construct, |
| 330 | * or if there is an error. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 331 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 332 | COND_DONE, COND_NEVER |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 333 | }; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 334 | #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE ) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 335 | |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 336 | /* |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 337 | * These defines are used as the possible return values for do_directive |
| 338 | */ |
| 339 | #define NO_DIRECTIVE_FOUND 0 |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 340 | #define DIRECTIVE_FOUND 1 |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 341 | |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 342 | /* max reps */ |
| 343 | #define REP_LIMIT ((INT64_C(1) << 62)) |
| 344 | |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 345 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 346 | * Condition codes. Note that we use c_ prefix not C_ because C_ is |
| 347 | * used in nasm.h for the "real" condition codes. At _this_ level, |
| 348 | * we treat CXZ and ECXZ as condition codes, albeit non-invertible |
| 349 | * ones, so we need a different enum... |
| 350 | */ |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 351 | static const char * const conditions[] = { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 352 | "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le", |
| 353 | "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no", |
H. Peter Anvin | ce9be34 | 2007-09-12 00:22:29 +0000 | [diff] [blame] | 354 | "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z" |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 355 | }; |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 356 | enum pp_conds { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 357 | c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE, |
| 358 | 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] | 359 | c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z, |
| 360 | c_none = -1 |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 361 | }; |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 362 | static const enum pp_conds inverse_ccs[] = { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 363 | c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE, |
| 364 | 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] | 365 | 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] | 366 | }; |
| 367 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 368 | /* |
| 369 | * Directive names. |
| 370 | */ |
| 371 | /* If this is a an IF, ELIF, ELSE or ENDIF keyword */ |
| 372 | static int is_condition(enum preproc_token arg) |
| 373 | { |
| 374 | return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF); |
| 375 | } |
| 376 | |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 377 | /* For TASM compatibility we need to be able to recognise TASM compatible |
| 378 | * conditional compilation directives. Using the NASM pre-processor does |
| 379 | * not work, so we look for them specifically from the following list and |
| 380 | * then jam in the equivalent NASM directive into the input stream. |
| 381 | */ |
| 382 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 383 | enum { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 384 | TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI, |
| 385 | TM_IFNDEF, TM_INCLUDE, TM_LOCAL |
| 386 | }; |
| 387 | |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 388 | static const char * const tasm_directives[] = { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 389 | "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi", |
| 390 | "ifndef", "include", "local" |
| 391 | }; |
| 392 | |
| 393 | static int StackSize = 4; |
H. Peter Anvin | 6c8b2be | 2016-05-24 23:46:50 -0700 | [diff] [blame] | 394 | static const char *StackPointer = "ebp"; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 395 | static int ArgOffset = 8; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 396 | static int LocalOffset = 0; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 397 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 398 | static Context *cstk; |
| 399 | static Include *istk; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 400 | static IncPath *ipath = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 401 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 402 | static int pass; /* HACK: pass 0 = generate dependencies only */ |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 403 | static StrList **dephead; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 404 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 405 | static uint64_t unique; /* unique identifier numbers */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 406 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 407 | static Line *predef = NULL; |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 408 | static bool do_predef; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 409 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 410 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 411 | * The current set of multi-line macros we have defined. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 412 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 413 | static struct hash_table mmacros; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 414 | |
| 415 | /* |
| 416 | * The current set of single-line macros we have defined. |
| 417 | */ |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 418 | static struct hash_table smacros; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 419 | |
| 420 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 421 | * The multi-line macro we are currently defining, or the %rep |
| 422 | * block we are currently reading, if any. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 423 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 424 | static MMacro *defining; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 425 | |
Charles Crayne | d4200be | 2008-07-12 16:42:33 -0700 | [diff] [blame] | 426 | static uint64_t nested_mac_count; |
| 427 | static uint64_t nested_rep_count; |
| 428 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 429 | /* |
| 430 | * The number of macro parameters to allocate space for at a time. |
| 431 | */ |
| 432 | #define PARAM_DELTA 16 |
| 433 | |
| 434 | /* |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 435 | * The standard macro set: defined in macros.c in a set of arrays. |
| 436 | * This gives our position in any macro set, while we are processing it. |
| 437 | * The stdmacset is an array of such macro sets. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 438 | */ |
H. Peter Anvin | a70547f | 2008-07-19 21:44:26 -0700 | [diff] [blame] | 439 | static macros_t *stdmacpos; |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 440 | static macros_t **stdmacnext; |
| 441 | static macros_t *stdmacros[8]; |
Cyrill Gorcunov | 15ce78f | 2017-01-06 20:21:28 +0300 | [diff] [blame] | 442 | static macros_t *extrastdmac; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 443 | |
| 444 | /* |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 445 | * Tokens are allocated in blocks to improve speed |
| 446 | */ |
| 447 | #define TOKEN_BLOCKSIZE 4096 |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 448 | static Token *freeTokens = NULL; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 449 | struct Blocks { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 450 | Blocks *next; |
| 451 | void *chunk; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 452 | }; |
| 453 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 454 | static Blocks blocks = { NULL, NULL }; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 455 | |
| 456 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 457 | * Forward declarations. |
| 458 | */ |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 459 | static void pp_add_stdmac(macros_t *macros); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 460 | static Token *expand_mmac_params(Token * tline); |
| 461 | static Token *expand_smacro(Token * tline); |
| 462 | static Token *expand_id(Token * tline); |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 463 | static Context *get_ctx(const char *name, const char **namep); |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 464 | static void make_tok_num(Token * tok, int64_t val); |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 465 | static void pp_verror(int severity, const char *fmt, va_list ap); |
| 466 | static vefunc real_verror; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 467 | static void *new_Block(size_t size); |
| 468 | static void delete_Blocks(void); |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 469 | static Token *new_Token(Token * next, enum pp_token_type type, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 470 | const char *text, int txtlen); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 471 | static Token *delete_Token(Token * t); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 472 | |
| 473 | /* |
| 474 | * Macros for safe checking of token pointers, avoid *(NULL) |
| 475 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 476 | #define tok_type_(x,t) ((x) && (x)->type == (t)) |
| 477 | #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next |
| 478 | #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v))) |
| 479 | #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] | 480 | |
Cyrill Gorcunov | 194ba89 | 2011-06-30 01:16:35 +0400 | [diff] [blame] | 481 | /* |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 482 | * nasm_unquote with error if the string contains NUL characters. |
| 483 | * If the string contains NUL characters, issue an error and return |
| 484 | * the C len, i.e. truncate at the NUL. |
| 485 | */ |
| 486 | static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive) |
| 487 | { |
| 488 | size_t len = nasm_unquote(qstr, NULL); |
| 489 | size_t clen = strlen(qstr); |
| 490 | |
| 491 | if (len != clen) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 492 | nasm_error(ERR_NONFATAL, "NUL character in `%s' directive", |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 493 | pp_directives[directive]); |
| 494 | |
| 495 | return clen; |
| 496 | } |
| 497 | |
| 498 | /* |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 499 | * In-place reverse a list of tokens. |
| 500 | */ |
| 501 | static Token *reverse_tokens(Token *t) |
| 502 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 503 | Token *prev = NULL; |
| 504 | Token *next; |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 505 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 506 | while (t) { |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 507 | next = t->next; |
| 508 | t->next = prev; |
| 509 | prev = t; |
| 510 | t = next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 511 | } |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 512 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 513 | return prev; |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | /* |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 517 | * Handle TASM specific directives, which do not contain a % in |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 518 | * front of them. We do it here because I could not find any other |
| 519 | * place to do it for the moment, and it is a hack (ideally it would |
| 520 | * be nice to be able to use the NASM pre-processor to do it). |
| 521 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 522 | static char *check_tasm_directive(char *line) |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 523 | { |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 524 | int32_t i, j, k, m, len; |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 525 | char *p, *q, *oldline, oldchar; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 526 | |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 527 | p = nasm_skip_spaces(line); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 528 | |
| 529 | /* Binary search for the directive name */ |
| 530 | i = -1; |
Cyrill Gorcunov | a731924 | 2010-06-03 22:04:36 +0400 | [diff] [blame] | 531 | j = ARRAY_SIZE(tasm_directives); |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 532 | q = nasm_skip_word(p); |
| 533 | len = q - p; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 534 | if (len) { |
| 535 | oldchar = p[len]; |
| 536 | p[len] = 0; |
| 537 | while (j - i > 1) { |
| 538 | k = (j + i) / 2; |
| 539 | m = nasm_stricmp(p, tasm_directives[k]); |
| 540 | if (m == 0) { |
| 541 | /* We have found a directive, so jam a % in front of it |
| 542 | * so that NASM will then recognise it as one if it's own. |
| 543 | */ |
| 544 | p[len] = oldchar; |
| 545 | len = strlen(p); |
| 546 | oldline = line; |
| 547 | line = nasm_malloc(len + 2); |
| 548 | line[0] = '%'; |
| 549 | if (k == TM_IFDIFI) { |
H. Peter Anvin | 18f4879 | 2009-06-27 15:56:27 -0700 | [diff] [blame] | 550 | /* |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 551 | * NASM does not recognise IFDIFI, so we convert |
| 552 | * it to %if 0. This is not used in NASM |
| 553 | * compatible code, but does need to parse for the |
| 554 | * TASM macro package. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 555 | */ |
H. Peter Anvin | 18f4879 | 2009-06-27 15:56:27 -0700 | [diff] [blame] | 556 | strcpy(line + 1, "if 0"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 557 | } else { |
| 558 | memcpy(line + 1, p, len + 1); |
| 559 | } |
| 560 | nasm_free(oldline); |
| 561 | return line; |
| 562 | } else if (m < 0) { |
| 563 | j = k; |
| 564 | } else |
| 565 | i = k; |
| 566 | } |
| 567 | p[len] = oldchar; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 568 | } |
| 569 | return line; |
| 570 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 571 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 572 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 573 | * The pre-preprocessing stage... This function translates line |
| 574 | * number indications as they emerge from GNU cpp (`# lineno "file" |
| 575 | * flags') into NASM preprocessor line number indications (`%line |
| 576 | * lineno file'). |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 577 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 578 | static char *prepreproc(char *line) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 579 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 580 | int lineno, fnlen; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 581 | char *fname, *oldline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 582 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 583 | if (line[0] == '#' && line[1] == ' ') { |
| 584 | oldline = line; |
| 585 | fname = oldline + 2; |
| 586 | lineno = atoi(fname); |
| 587 | fname += strspn(fname, "0123456789 "); |
| 588 | if (*fname == '"') |
| 589 | fname++; |
| 590 | fnlen = strcspn(fname, "\""); |
| 591 | line = nasm_malloc(20 + fnlen); |
| 592 | snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname); |
| 593 | nasm_free(oldline); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 594 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 595 | if (tasm_compatible_mode) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 596 | return check_tasm_directive(line); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 597 | return line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 601 | * Free a linked list of tokens. |
| 602 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 603 | static void free_tlist(Token * list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 604 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 605 | while (list) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 606 | list = delete_Token(list); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | /* |
| 610 | * Free a linked list of lines. |
| 611 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 612 | static void free_llist(Line * list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 613 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 614 | Line *l, *tmp; |
| 615 | list_for_each_safe(l, tmp, list) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 616 | free_tlist(l->first); |
| 617 | nasm_free(l); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 618 | } |
| 619 | } |
| 620 | |
| 621 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 622 | * Free an MMacro |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 623 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 624 | static void free_mmacro(MMacro * m) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 625 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 626 | nasm_free(m->name); |
| 627 | free_tlist(m->dlist); |
| 628 | nasm_free(m->defaults); |
| 629 | free_llist(m->expansion); |
| 630 | nasm_free(m); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | /* |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 634 | * Free all currently defined macros, and free the hash tables |
| 635 | */ |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 636 | static void free_smacro_table(struct hash_table *smt) |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 637 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 638 | SMacro *s, *tmp; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 639 | const char *key; |
| 640 | struct hash_tbl_node *it = NULL; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 641 | |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 642 | while ((s = hash_iterate(smt, &it, &key)) != NULL) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 643 | nasm_free((void *)key); |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 644 | list_for_each_safe(s, tmp, s) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 645 | nasm_free(s->name); |
| 646 | free_tlist(s->expansion); |
| 647 | nasm_free(s); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 648 | } |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 649 | } |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 650 | hash_free(smt); |
| 651 | } |
| 652 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 653 | static void free_mmacro_table(struct hash_table *mmt) |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 654 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 655 | MMacro *m, *tmp; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 656 | const char *key; |
| 657 | struct hash_tbl_node *it = NULL; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 658 | |
| 659 | it = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 660 | while ((m = hash_iterate(mmt, &it, &key)) != NULL) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 661 | nasm_free((void *)key); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 662 | list_for_each_safe(m ,tmp, m) |
| 663 | free_mmacro(m); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 664 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 665 | hash_free(mmt); |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | static void free_macros(void) |
| 669 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 670 | free_smacro_table(&smacros); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 671 | free_mmacro_table(&mmacros); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | /* |
| 675 | * Initialize the hash tables |
| 676 | */ |
| 677 | static void init_macros(void) |
| 678 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 679 | hash_init(&smacros, HASH_LARGE); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 680 | hash_init(&mmacros, HASH_LARGE); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 684 | * Pop the context stack. |
| 685 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 686 | static void ctx_pop(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 687 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 688 | Context *c = cstk; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 689 | |
| 690 | cstk = cstk->next; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 691 | free_smacro_table(&c->localmac); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 692 | nasm_free(c->name); |
| 693 | nasm_free(c); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 694 | } |
| 695 | |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 696 | /* |
| 697 | * Search for a key in the hash index; adding it if necessary |
| 698 | * (in which case we initialize the data pointer to NULL.) |
| 699 | */ |
| 700 | static void ** |
| 701 | hash_findi_add(struct hash_table *hash, const char *str) |
| 702 | { |
| 703 | struct hash_insert hi; |
| 704 | void **r; |
| 705 | char *strx; |
| 706 | |
| 707 | r = hash_findi(hash, str, &hi); |
| 708 | if (r) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 709 | return r; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 710 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 711 | strx = nasm_strdup(str); /* Use a more efficient allocator here? */ |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 712 | return hash_add(&hi, strx, NULL); |
| 713 | } |
| 714 | |
| 715 | /* |
| 716 | * Like hash_findi, but returns the data element rather than a pointer |
| 717 | * to it. Used only when not adding a new element, hence no third |
| 718 | * argument. |
| 719 | */ |
| 720 | static void * |
| 721 | hash_findix(struct hash_table *hash, const char *str) |
| 722 | { |
| 723 | void **p; |
| 724 | |
| 725 | p = hash_findi(hash, str, NULL); |
| 726 | return p ? *p : NULL; |
| 727 | } |
| 728 | |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 729 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 730 | * read line from standart macros set, |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 731 | * if there no more left -- return NULL |
| 732 | */ |
| 733 | static char *line_from_stdmac(void) |
| 734 | { |
| 735 | unsigned char c; |
| 736 | const unsigned char *p = stdmacpos; |
| 737 | char *line, *q; |
| 738 | size_t len = 0; |
| 739 | |
| 740 | if (!stdmacpos) |
| 741 | return NULL; |
| 742 | |
| 743 | while ((c = *p++)) { |
| 744 | if (c >= 0x80) |
| 745 | len += pp_directives_len[c - 0x80] + 1; |
| 746 | else |
| 747 | len++; |
| 748 | } |
| 749 | |
| 750 | line = nasm_malloc(len + 1); |
| 751 | q = line; |
| 752 | while ((c = *stdmacpos++)) { |
| 753 | if (c >= 0x80) { |
| 754 | memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]); |
| 755 | q += pp_directives_len[c - 0x80]; |
| 756 | *q++ = ' '; |
| 757 | } else { |
| 758 | *q++ = c; |
| 759 | } |
| 760 | } |
| 761 | stdmacpos = p; |
| 762 | *q = '\0'; |
| 763 | |
| 764 | if (!*stdmacpos) { |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 765 | /* This was the last of this particular macro set */ |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 766 | stdmacpos = NULL; |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 767 | if (*stdmacnext) { |
| 768 | stdmacpos = *stdmacnext++; |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 769 | } else if (do_predef) { |
| 770 | Line *pd, *l; |
| 771 | Token *head, **tail, *t; |
| 772 | |
| 773 | /* |
| 774 | * Nasty hack: here we push the contents of |
| 775 | * `predef' on to the top-level expansion stack, |
| 776 | * since this is the most convenient way to |
| 777 | * implement the pre-include and pre-define |
| 778 | * features. |
| 779 | */ |
| 780 | list_for_each(pd, predef) { |
| 781 | head = NULL; |
| 782 | tail = &head; |
| 783 | list_for_each(t, pd->first) { |
| 784 | *tail = new_Token(NULL, t->type, t->text, 0); |
| 785 | tail = &(*tail)->next; |
| 786 | } |
| 787 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 788 | l = nasm_malloc(sizeof(Line)); |
| 789 | l->next = istk->expansion; |
| 790 | l->first = head; |
| 791 | l->finishes = NULL; |
| 792 | |
| 793 | istk->expansion = l; |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 794 | } |
| 795 | do_predef = false; |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | return line; |
| 800 | } |
| 801 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 802 | static char *read_line(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 803 | { |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 804 | unsigned int size, c, next; |
| 805 | const unsigned int delta = 512; |
| 806 | const unsigned int pad = 8; |
| 807 | unsigned int nr_cont = 0; |
| 808 | bool cont = false; |
| 809 | char *buffer, *p; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 810 | |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 811 | /* Standart macros set (predefined) goes first */ |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 812 | p = line_from_stdmac(); |
| 813 | if (p) |
| 814 | return p; |
H. Peter Anvin | 72edbb8 | 2008-06-19 16:00:04 -0700 | [diff] [blame] | 815 | |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 816 | size = delta; |
| 817 | p = buffer = nasm_malloc(size); |
| 818 | |
| 819 | for (;;) { |
| 820 | c = fgetc(istk->fp); |
| 821 | if ((int)(c) == EOF) { |
| 822 | p[0] = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 823 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 824 | } |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 825 | |
| 826 | switch (c) { |
| 827 | case '\r': |
| 828 | next = fgetc(istk->fp); |
| 829 | if (next != '\n') |
| 830 | ungetc(next, istk->fp); |
| 831 | if (cont) { |
| 832 | cont = false; |
| 833 | continue; |
| 834 | } |
| 835 | break; |
| 836 | |
| 837 | case '\n': |
| 838 | if (cont) { |
| 839 | cont = false; |
| 840 | continue; |
| 841 | } |
| 842 | break; |
| 843 | |
| 844 | case '\\': |
| 845 | next = fgetc(istk->fp); |
| 846 | ungetc(next, istk->fp); |
Cyrill Gorcunov | 490f85e | 2012-12-27 20:02:17 +0400 | [diff] [blame] | 847 | if (next == '\r' || next == '\n') { |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 848 | cont = true; |
| 849 | nr_cont++; |
| 850 | continue; |
| 851 | } |
| 852 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 853 | } |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 854 | |
| 855 | if (c == '\r' || c == '\n') { |
| 856 | *p++ = 0; |
| 857 | break; |
| 858 | } |
| 859 | |
| 860 | if (p >= (buffer + size - pad)) { |
| 861 | buffer = nasm_realloc(buffer, size + delta); |
| 862 | p = buffer + size - pad; |
| 863 | size += delta; |
| 864 | } |
| 865 | |
| 866 | *p++ = (unsigned char)c; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 867 | } |
| 868 | |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 869 | if (p == buffer) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 870 | nasm_free(buffer); |
| 871 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 874 | src_set_linnum(src_get_linnum() + istk->lineinc + |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 875 | (nr_cont * istk->lineinc)); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 876 | |
| 877 | /* |
| 878 | * Handle spurious ^Z, which may be inserted into source files |
| 879 | * by some file transfer utilities. |
| 880 | */ |
| 881 | buffer[strcspn(buffer, "\032")] = '\0'; |
| 882 | |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 883 | lfmt->line(LIST_READ, buffer); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 884 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 885 | return buffer; |
| 886 | } |
| 887 | |
| 888 | /* |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 889 | * 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] | 890 | * don't need to parse the value out of e.g. numeric tokens: we |
| 891 | * simply split one string into many. |
| 892 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 893 | static Token *tokenize(char *line) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 894 | { |
H. Peter Anvin | ca544db | 2008-10-19 19:30:11 -0700 | [diff] [blame] | 895 | char c, *p = line; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 896 | enum pp_token_type type; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 897 | Token *list = NULL; |
| 898 | Token *t, **tail = &list; |
| 899 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 900 | while (*line) { |
| 901 | p = line; |
| 902 | if (*p == '%') { |
| 903 | p++; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 904 | if (*p == '+' && !nasm_isdigit(p[1])) { |
| 905 | p++; |
| 906 | type = TOK_PASTE; |
| 907 | } else if (nasm_isdigit(*p) || |
| 908 | ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 909 | do { |
| 910 | p++; |
| 911 | } |
H. Peter Anvin | bda7a6e | 2008-06-21 10:23:17 -0700 | [diff] [blame] | 912 | while (nasm_isdigit(*p)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 913 | type = TOK_PREPROC_ID; |
| 914 | } else if (*p == '{') { |
| 915 | p++; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 916 | while (*p) { |
| 917 | if (*p == '}') |
| 918 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 919 | p[-1] = *p; |
| 920 | p++; |
| 921 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 922 | if (*p != '}') |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 923 | nasm_error(ERR_WARNING | ERR_PASS1, |
| 924 | "unterminated %%{ construct"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 925 | p[-1] = '\0'; |
| 926 | if (*p) |
| 927 | p++; |
| 928 | type = TOK_PREPROC_ID; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 929 | } else if (*p == '[') { |
| 930 | int lvl = 1; |
| 931 | line += 2; /* Skip the leading %[ */ |
| 932 | p++; |
| 933 | while (lvl && (c = *p++)) { |
| 934 | switch (c) { |
| 935 | case ']': |
| 936 | lvl--; |
| 937 | break; |
| 938 | case '%': |
| 939 | if (*p == '[') |
| 940 | lvl++; |
| 941 | break; |
| 942 | case '\'': |
| 943 | case '\"': |
| 944 | case '`': |
Cyrill Gorcunov | 3144e84 | 2017-10-22 10:50:55 +0300 | [diff] [blame] | 945 | p = nasm_skip_string(p - 1); |
| 946 | if (*p) |
| 947 | p++; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 948 | break; |
| 949 | default: |
| 950 | break; |
| 951 | } |
| 952 | } |
| 953 | p--; |
| 954 | if (*p) |
| 955 | *p++ = '\0'; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 956 | if (lvl) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 957 | nasm_error(ERR_NONFATAL|ERR_PASS1, |
| 958 | "unterminated %%[ construct"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 959 | type = TOK_INDIRECT; |
| 960 | } else if (*p == '?') { |
| 961 | type = TOK_PREPROC_Q; /* %? */ |
| 962 | p++; |
| 963 | if (*p == '?') { |
| 964 | type = TOK_PREPROC_QQ; /* %?? */ |
| 965 | p++; |
| 966 | } |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 967 | } else if (*p == '!') { |
| 968 | type = TOK_PREPROC_ID; |
| 969 | p++; |
| 970 | if (isidchar(*p)) { |
| 971 | do { |
| 972 | p++; |
| 973 | } |
| 974 | while (isidchar(*p)); |
| 975 | } else if (*p == '\'' || *p == '\"' || *p == '`') { |
| 976 | p = nasm_skip_string(p); |
| 977 | if (*p) |
| 978 | p++; |
| 979 | else |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 980 | nasm_error(ERR_NONFATAL|ERR_PASS1, |
| 981 | "unterminated %%! string"); |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 982 | } else { |
| 983 | /* %! without string or identifier */ |
| 984 | type = TOK_OTHER; /* Legacy behavior... */ |
| 985 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 986 | } else if (isidchar(*p) || |
| 987 | ((*p == '!' || *p == '%' || *p == '$') && |
| 988 | isidchar(p[1]))) { |
| 989 | do { |
| 990 | p++; |
| 991 | } |
| 992 | while (isidchar(*p)); |
| 993 | type = TOK_PREPROC_ID; |
| 994 | } else { |
| 995 | type = TOK_OTHER; |
| 996 | if (*p == '%') |
| 997 | p++; |
| 998 | } |
| 999 | } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) { |
| 1000 | type = TOK_ID; |
| 1001 | p++; |
| 1002 | while (*p && isidchar(*p)) |
| 1003 | p++; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 1004 | } else if (*p == '\'' || *p == '"' || *p == '`') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1005 | /* |
| 1006 | * A string token. |
| 1007 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1008 | type = TOK_STRING; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1009 | p = nasm_skip_string(p); |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1010 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1011 | if (*p) { |
| 1012 | p++; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1013 | } else { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1014 | nasm_error(ERR_WARNING|ERR_PASS1, "unterminated string"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1015 | /* Handling unterminated strings by UNV */ |
| 1016 | /* type = -1; */ |
| 1017 | } |
Victor van den Elzen | fb5f251 | 2009-04-17 16:17:59 +0200 | [diff] [blame] | 1018 | } else if (p[0] == '$' && p[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1019 | type = TOK_OTHER; /* TOKEN_BASE */ |
Victor van den Elzen | fb5f251 | 2009-04-17 16:17:59 +0200 | [diff] [blame] | 1020 | p += 2; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1021 | } else if (isnumstart(*p)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1022 | bool is_hex = false; |
| 1023 | bool is_float = false; |
| 1024 | bool has_e = false; |
| 1025 | char c, *r; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1026 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1027 | /* |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1028 | * A numeric token. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1029 | */ |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1030 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1031 | if (*p == '$') { |
| 1032 | p++; |
| 1033 | is_hex = true; |
| 1034 | } |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1035 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1036 | for (;;) { |
| 1037 | c = *p++; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1038 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1039 | if (!is_hex && (c == 'e' || c == 'E')) { |
| 1040 | has_e = true; |
| 1041 | if (*p == '+' || *p == '-') { |
| 1042 | /* |
| 1043 | * e can only be followed by +/- if it is either a |
| 1044 | * prefixed hex number or a floating-point number |
| 1045 | */ |
| 1046 | p++; |
| 1047 | is_float = true; |
| 1048 | } |
| 1049 | } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') { |
| 1050 | is_hex = true; |
| 1051 | } else if (c == 'P' || c == 'p') { |
| 1052 | is_float = true; |
| 1053 | if (*p == '+' || *p == '-') |
| 1054 | p++; |
Martin Lindhe | b150e38 | 2016-11-16 15:36:53 +0100 | [diff] [blame] | 1055 | } else if (isnumchar(c)) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1056 | ; /* just advance */ |
| 1057 | else if (c == '.') { |
| 1058 | /* |
| 1059 | * we need to deal with consequences of the legacy |
| 1060 | * parser, like "1.nolist" being two tokens |
| 1061 | * (TOK_NUMBER, TOK_ID) here; at least give it |
| 1062 | * a shot for now. In the future, we probably need |
| 1063 | * a flex-based scanner with proper pattern matching |
| 1064 | * to do it as well as it can be done. Nothing in |
| 1065 | * the world is going to help the person who wants |
| 1066 | * 0x123.p16 interpreted as two tokens, though. |
| 1067 | */ |
| 1068 | r = p; |
| 1069 | while (*r == '_') |
| 1070 | r++; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1071 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1072 | if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) || |
| 1073 | (!is_hex && (*r == 'e' || *r == 'E')) || |
| 1074 | (*r == 'p' || *r == 'P')) { |
| 1075 | p = r; |
| 1076 | is_float = true; |
| 1077 | } else |
| 1078 | break; /* Terminate the token */ |
| 1079 | } else |
| 1080 | break; |
| 1081 | } |
| 1082 | p--; /* Point to first character beyond number */ |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1083 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1084 | if (p == line+1 && *line == '$') { |
| 1085 | type = TOK_OTHER; /* TOKEN_HERE */ |
| 1086 | } else { |
| 1087 | if (has_e && !is_hex) { |
| 1088 | /* 1e13 is floating-point, but 1e13h is not */ |
| 1089 | is_float = true; |
| 1090 | } |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 1091 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1092 | type = is_float ? TOK_FLOAT : TOK_NUMBER; |
| 1093 | } |
H. Peter Anvin | bda7a6e | 2008-06-21 10:23:17 -0700 | [diff] [blame] | 1094 | } else if (nasm_isspace(*p)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1095 | type = TOK_WHITESPACE; |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 1096 | p = nasm_skip_spaces(p); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1097 | /* |
| 1098 | * Whitespace just before end-of-line is discarded by |
| 1099 | * pretending it's a comment; whitespace just before a |
| 1100 | * comment gets lumped into the comment. |
| 1101 | */ |
| 1102 | if (!*p || *p == ';') { |
| 1103 | type = TOK_COMMENT; |
| 1104 | while (*p) |
| 1105 | p++; |
| 1106 | } |
| 1107 | } else if (*p == ';') { |
| 1108 | type = TOK_COMMENT; |
| 1109 | while (*p) |
| 1110 | p++; |
| 1111 | } else { |
| 1112 | /* |
| 1113 | * Anything else is an operator of some kind. We check |
| 1114 | * for all the double-character operators (>>, <<, //, |
| 1115 | * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1116 | * else is a single-character operator. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1117 | */ |
| 1118 | type = TOK_OTHER; |
| 1119 | if ((p[0] == '>' && p[1] == '>') || |
| 1120 | (p[0] == '<' && p[1] == '<') || |
| 1121 | (p[0] == '/' && p[1] == '/') || |
| 1122 | (p[0] == '<' && p[1] == '=') || |
| 1123 | (p[0] == '>' && p[1] == '=') || |
| 1124 | (p[0] == '=' && p[1] == '=') || |
| 1125 | (p[0] == '!' && p[1] == '=') || |
| 1126 | (p[0] == '<' && p[1] == '>') || |
| 1127 | (p[0] == '&' && p[1] == '&') || |
| 1128 | (p[0] == '|' && p[1] == '|') || |
| 1129 | (p[0] == '^' && p[1] == '^')) { |
| 1130 | p++; |
| 1131 | } |
| 1132 | p++; |
| 1133 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1134 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1135 | /* Handling unterminated string by UNV */ |
| 1136 | /*if (type == -1) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1137 | { |
| 1138 | *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1); |
| 1139 | t->text[p-line] = *line; |
| 1140 | tail = &t->next; |
| 1141 | } |
| 1142 | else */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1143 | if (type != TOK_COMMENT) { |
| 1144 | *tail = t = new_Token(NULL, type, line, p - line); |
| 1145 | tail = &t->next; |
| 1146 | } |
| 1147 | line = p; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1148 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1149 | return list; |
| 1150 | } |
| 1151 | |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1152 | /* |
| 1153 | * this function allocates a new managed block of memory and |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1154 | * returns a pointer to the block. The managed blocks are |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1155 | * deleted only all at once by the delete_Blocks function. |
| 1156 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1157 | static void *new_Block(size_t size) |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1158 | { |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1159 | Blocks *b = &blocks; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1160 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1161 | /* first, get to the end of the linked list */ |
| 1162 | while (b->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1163 | b = b->next; |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1164 | /* now allocate the requested chunk */ |
| 1165 | b->chunk = nasm_malloc(size); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1166 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1167 | /* now allocate a new block for the next request */ |
Cyrill Gorcunov | c31767c | 2014-06-28 02:22:17 +0400 | [diff] [blame] | 1168 | b->next = nasm_zalloc(sizeof(Blocks)); |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1169 | return b->chunk; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | /* |
| 1173 | * this function deletes all managed blocks of memory |
| 1174 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1175 | static void delete_Blocks(void) |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1176 | { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1177 | Blocks *a, *b = &blocks; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1178 | |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1179 | /* |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1180 | * keep in mind that the first block, pointed to by blocks |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1181 | * is a static and not dynamically allocated, so we don't |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1182 | * free it. |
| 1183 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1184 | while (b) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1185 | if (b->chunk) |
| 1186 | nasm_free(b->chunk); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1187 | a = b; |
| 1188 | b = b->next; |
| 1189 | if (a != &blocks) |
| 1190 | nasm_free(a); |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1191 | } |
Cyrill Gorcunov | dae24d7 | 2014-06-28 10:17:39 +0400 | [diff] [blame] | 1192 | memset(&blocks, 0, sizeof(blocks)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1193 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1194 | |
| 1195 | /* |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1196 | * 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] | 1197 | * 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] | 1198 | * also the a.mac and next elements to NULL. |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1199 | */ |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1200 | static Token *new_Token(Token * next, enum pp_token_type type, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1201 | const char *text, int txtlen) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1202 | { |
| 1203 | Token *t; |
| 1204 | int i; |
| 1205 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1206 | if (!freeTokens) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1207 | freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token)); |
| 1208 | for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++) |
| 1209 | freeTokens[i].next = &freeTokens[i + 1]; |
| 1210 | freeTokens[i].next = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1211 | } |
| 1212 | t = freeTokens; |
| 1213 | freeTokens = t->next; |
| 1214 | t->next = next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 1215 | t->a.mac = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1216 | t->type = type; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1217 | if (type == TOK_WHITESPACE || !text) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1218 | t->text = NULL; |
| 1219 | } else { |
| 1220 | if (txtlen == 0) |
| 1221 | txtlen = strlen(text); |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1222 | t->text = nasm_malloc(txtlen+1); |
| 1223 | memcpy(t->text, text, txtlen); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1224 | t->text[txtlen] = '\0'; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1225 | } |
| 1226 | return t; |
| 1227 | } |
| 1228 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1229 | static Token *delete_Token(Token * t) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1230 | { |
| 1231 | Token *next = t->next; |
| 1232 | nasm_free(t->text); |
H. Peter Anvin | 788e6c1 | 2002-04-30 21:02:01 +0000 | [diff] [blame] | 1233 | t->next = freeTokens; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1234 | freeTokens = t; |
| 1235 | return next; |
| 1236 | } |
| 1237 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1238 | /* |
| 1239 | * Convert a line of tokens back into text. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1240 | * If expand_locals is not zero, identifiers of the form "%$*xxx" |
| 1241 | * will be transformed into ..@ctxnum.xxx |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1242 | */ |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 1243 | static char *detoken(Token * tlist, bool expand_locals) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1244 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1245 | Token *t; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1246 | char *line, *p; |
H. Peter Anvin | b4daadc | 2008-01-21 16:31:57 -0800 | [diff] [blame] | 1247 | const char *q; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1248 | int len = 0; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1249 | |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1250 | list_for_each(t, tlist) { |
Cyrill Gorcunov | 9b7ee09 | 2017-10-22 21:42:59 +0300 | [diff] [blame] | 1251 | if (t->type == TOK_PREPROC_ID && t->text && |
| 1252 | t->text[0] && t->text[1] == '!') { |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1253 | char *v; |
| 1254 | char *q = t->text; |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1255 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1256 | v = t->text + 2; |
| 1257 | if (*v == '\'' || *v == '\"' || *v == '`') { |
| 1258 | size_t len = nasm_unquote(v, NULL); |
| 1259 | size_t clen = strlen(v); |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1260 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1261 | if (len != clen) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1262 | nasm_error(ERR_NONFATAL | ERR_PASS1, |
| 1263 | "NUL character in %%! string"); |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1264 | v = NULL; |
| 1265 | } |
| 1266 | } |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1267 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1268 | if (v) { |
| 1269 | char *p = getenv(v); |
| 1270 | if (!p) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1271 | nasm_error(ERR_NONFATAL | ERR_PASS1, |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1272 | "nonexistent environment variable `%s'", v); |
Cyrill Gorcunov | bbb7a1a | 2016-06-19 12:15:24 +0300 | [diff] [blame] | 1273 | /* |
| 1274 | * FIXME We better should investigate if accessing |
| 1275 | * ->text[1] without ->text[0] is safe enough. |
| 1276 | */ |
| 1277 | t->text = nasm_zalloc(2); |
| 1278 | } else |
| 1279 | t->text = nasm_strdup(p); |
Cyrill Gorcunov | 7500487 | 2017-07-26 01:21:16 +0300 | [diff] [blame] | 1280 | nasm_free(q); |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1281 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1282 | } |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1283 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1284 | /* Expand local macros here and not during preprocessing */ |
| 1285 | if (expand_locals && |
| 1286 | t->type == TOK_PREPROC_ID && t->text && |
| 1287 | t->text[0] == '%' && t->text[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1288 | const char *q; |
| 1289 | char *p; |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1290 | Context *ctx = get_ctx(t->text, &q); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1291 | if (ctx) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1292 | char buffer[40]; |
Keith Kanios | 93f2e9a | 2007-04-14 00:10:59 +0000 | [diff] [blame] | 1293 | snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1294 | p = nasm_strcat(buffer, q); |
| 1295 | nasm_free(t->text); |
| 1296 | t->text = p; |
| 1297 | } |
| 1298 | } |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1299 | if (t->type == TOK_WHITESPACE) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1300 | len++; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1301 | else if (t->text) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1302 | len += strlen(t->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1303 | } |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1304 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1305 | p = line = nasm_malloc(len + 1); |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1306 | |
| 1307 | list_for_each(t, tlist) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1308 | if (t->type == TOK_WHITESPACE) { |
H. Peter Anvin | b4daadc | 2008-01-21 16:31:57 -0800 | [diff] [blame] | 1309 | *p++ = ' '; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1310 | } else if (t->text) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1311 | q = t->text; |
| 1312 | while (*q) |
| 1313 | *p++ = *q++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1314 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1315 | } |
| 1316 | *p = '\0'; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1317 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1318 | return line; |
| 1319 | } |
| 1320 | |
| 1321 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1322 | * A scanner, suitable for use by the expression evaluator, which |
| 1323 | * operates on a line of Tokens. Expects a pointer to a pointer to |
| 1324 | * the first token in the line to be passed in as its private_data |
| 1325 | * field. |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1326 | * |
| 1327 | * FIX: This really needs to be unified with stdscan. |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1328 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1329 | static int ppscan(void *private_data, struct tokenval *tokval) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1330 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1331 | Token **tlineptr = private_data; |
| 1332 | Token *tline; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1333 | char ourcopy[MAX_KEYWORD+1], *p, *r, *s; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1334 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1335 | do { |
| 1336 | tline = *tlineptr; |
| 1337 | *tlineptr = tline ? tline->next : NULL; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 1338 | } while (tline && (tline->type == TOK_WHITESPACE || |
| 1339 | tline->type == TOK_COMMENT)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1340 | |
| 1341 | if (!tline) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1342 | return tokval->t_type = TOKEN_EOS; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1343 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1344 | tokval->t_charptr = tline->text; |
| 1345 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1346 | if (tline->text[0] == '$' && !tline->text[1]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1347 | return tokval->t_type = TOKEN_HERE; |
H. Peter Anvin | 7cf897e | 2002-05-30 21:30:33 +0000 | [diff] [blame] | 1348 | if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1349 | return tokval->t_type = TOKEN_BASE; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1350 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1351 | if (tline->type == TOK_ID) { |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1352 | p = tokval->t_charptr = tline->text; |
| 1353 | if (p[0] == '$') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1354 | tokval->t_charptr++; |
| 1355 | return tokval->t_type = TOKEN_ID; |
| 1356 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1357 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1358 | for (r = p, s = ourcopy; *r; r++) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1359 | if (r >= p+MAX_KEYWORD) |
| 1360 | return tokval->t_type = TOKEN_ID; /* Not a keyword */ |
H. Peter Anvin | ac8f8fc | 2008-06-11 15:49:41 -0700 | [diff] [blame] | 1361 | *s++ = nasm_tolower(*r); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1362 | } |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1363 | *s = '\0'; |
| 1364 | /* right, so we have an identifier sitting in temp storage. now, |
| 1365 | * is it actually a register or instruction name, or what? */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1366 | return nasm_token_hash(ourcopy, tokval); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1369 | if (tline->type == TOK_NUMBER) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1370 | bool rn_error; |
| 1371 | tokval->t_integer = readnum(tline->text, &rn_error); |
| 1372 | tokval->t_charptr = tline->text; |
| 1373 | if (rn_error) |
| 1374 | return tokval->t_type = TOKEN_ERRNUM; |
| 1375 | else |
| 1376 | return tokval->t_type = TOKEN_NUM; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1377 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1378 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1379 | if (tline->type == TOK_FLOAT) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1380 | return tokval->t_type = TOKEN_FLOAT; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1383 | if (tline->type == TOK_STRING) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1384 | char bq, *ep; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1385 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1386 | bq = tline->text[0]; |
H. Peter Anvin | 1162704 | 2008-06-09 20:45:19 -0700 | [diff] [blame] | 1387 | tokval->t_charptr = tline->text; |
| 1388 | tokval->t_inttwo = nasm_unquote(tline->text, &ep); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 1389 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1390 | if (ep[0] != bq || ep[1] != '\0') |
| 1391 | return tokval->t_type = TOKEN_ERRSTR; |
| 1392 | else |
| 1393 | return tokval->t_type = TOKEN_STR; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1394 | } |
| 1395 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1396 | if (tline->type == TOK_OTHER) { |
| 1397 | if (!strcmp(tline->text, "<<")) |
| 1398 | return tokval->t_type = TOKEN_SHL; |
| 1399 | if (!strcmp(tline->text, ">>")) |
| 1400 | return tokval->t_type = TOKEN_SHR; |
| 1401 | if (!strcmp(tline->text, "//")) |
| 1402 | return tokval->t_type = TOKEN_SDIV; |
| 1403 | if (!strcmp(tline->text, "%%")) |
| 1404 | return tokval->t_type = TOKEN_SMOD; |
| 1405 | if (!strcmp(tline->text, "==")) |
| 1406 | return tokval->t_type = TOKEN_EQ; |
| 1407 | if (!strcmp(tline->text, "<>")) |
| 1408 | return tokval->t_type = TOKEN_NE; |
| 1409 | if (!strcmp(tline->text, "!=")) |
| 1410 | return tokval->t_type = TOKEN_NE; |
| 1411 | if (!strcmp(tline->text, "<=")) |
| 1412 | return tokval->t_type = TOKEN_LE; |
| 1413 | if (!strcmp(tline->text, ">=")) |
| 1414 | return tokval->t_type = TOKEN_GE; |
| 1415 | if (!strcmp(tline->text, "&&")) |
| 1416 | return tokval->t_type = TOKEN_DBL_AND; |
| 1417 | if (!strcmp(tline->text, "^^")) |
| 1418 | return tokval->t_type = TOKEN_DBL_XOR; |
| 1419 | if (!strcmp(tline->text, "||")) |
| 1420 | return tokval->t_type = TOKEN_DBL_OR; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1421 | } |
| 1422 | |
| 1423 | /* |
| 1424 | * We have no other options: just return the first character of |
| 1425 | * the token text. |
| 1426 | */ |
| 1427 | return tokval->t_type = tline->text[0]; |
| 1428 | } |
| 1429 | |
| 1430 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1431 | * Compare a string to the name of an existing macro; this is a |
| 1432 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 1433 | * depending on the value of the `casesense' parameter. |
| 1434 | */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 1435 | static int mstrcmp(const char *p, const char *q, bool casesense) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1436 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1437 | return casesense ? strcmp(p, q) : nasm_stricmp(p, q); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | /* |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1441 | * Compare a string to the name of an existing macro; this is a |
| 1442 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 1443 | * depending on the value of the `casesense' parameter. |
| 1444 | */ |
| 1445 | static int mmemcmp(const char *p, const char *q, size_t l, bool casesense) |
| 1446 | { |
| 1447 | return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l); |
| 1448 | } |
| 1449 | |
| 1450 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1451 | * Return the Context structure associated with a %$ token. Return |
| 1452 | * NULL, having _already_ reported an error condition, if the |
| 1453 | * context stack isn't deep enough for the supplied number of $ |
| 1454 | * signs. |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1455 | * |
| 1456 | * If "namep" is non-NULL, set it to the pointer to the macro name |
| 1457 | * tail, i.e. the part beyond %$... |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1458 | */ |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1459 | static Context *get_ctx(const char *name, const char **namep) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1460 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1461 | Context *ctx; |
| 1462 | int i; |
| 1463 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1464 | if (namep) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1465 | *namep = name; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1466 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1467 | if (!name || name[0] != '%' || name[1] != '$') |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1468 | return NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1469 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1470 | if (!cstk) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1471 | nasm_error(ERR_NONFATAL, "`%s': context stack is empty", name); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1472 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1473 | } |
| 1474 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1475 | name += 2; |
| 1476 | ctx = cstk; |
| 1477 | i = 0; |
| 1478 | while (ctx && *name == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1479 | name++; |
| 1480 | i++; |
| 1481 | ctx = ctx->next; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1482 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1483 | if (!ctx) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1484 | nasm_error(ERR_NONFATAL, "`%s': context stack is only" |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1485 | " %d level%s deep", name, i, (i == 1 ? "" : "s")); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1486 | return NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1487 | } |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1488 | |
| 1489 | if (namep) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1490 | *namep = name; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1491 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1492 | return ctx; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1496 | * Open an include file. This routine must always return a valid |
| 1497 | * file pointer if it returns - it's responsible for throwing an |
| 1498 | * ERR_FATAL and bombing out completely if not. It should also try |
| 1499 | * the include path one by one until it finds the file or reaches |
| 1500 | * the end of the path. |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1501 | * |
| 1502 | * Note: for INC_PROBE the function returns NULL at all times; |
| 1503 | * instead look for the |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1504 | */ |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1505 | enum incopen_mode { |
| 1506 | INC_NEEDED, /* File must exist */ |
| 1507 | INC_OPTIONAL, /* Missing is OK */ |
| 1508 | INC_PROBE /* Only an existence probe */ |
| 1509 | }; |
| 1510 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1511 | /* This is conducts a full pathname search */ |
| 1512 | static FILE *inc_fopen_search(const char *file, StrList **slpath, |
| 1513 | enum incopen_mode omode, enum file_flags fmode) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1514 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1515 | FILE *fp; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1516 | char *prefix = ""; |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1517 | const IncPath *ip = ipath; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1518 | int len = strlen(file); |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1519 | size_t prefix_len = 0; |
| 1520 | StrList *sl; |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 1521 | size_t path_len; |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1522 | bool found; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1523 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1524 | while (1) { |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 1525 | path_len = prefix_len + len + 1; |
| 1526 | |
| 1527 | sl = nasm_malloc(path_len + sizeof sl->next); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1528 | memcpy(sl->str, prefix, prefix_len); |
| 1529 | memcpy(sl->str+prefix_len, file, len+1); |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1530 | sl->next = NULL; |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 1531 | |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1532 | if (omode == INC_PROBE) { |
| 1533 | fp = NULL; |
| 1534 | found = nasm_file_exists(sl->str); |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1535 | } else { |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1536 | fp = nasm_open_read(sl->str, fmode); |
| 1537 | found = (fp != NULL); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1538 | } |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1539 | if (found) { |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1540 | *slpath = sl; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1541 | return fp; |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1542 | } |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 1543 | |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1544 | nasm_free(sl); |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 1545 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1546 | if (!ip) |
| 1547 | return NULL; |
| 1548 | |
| 1549 | prefix = ip->path; |
| 1550 | prefix_len = strlen(prefix); |
| 1551 | ip = ip->next; |
| 1552 | } |
| 1553 | } |
| 1554 | |
| 1555 | /* |
| 1556 | * Open a file, or test for the presence of one (depending on omode), |
| 1557 | * considering the include path. |
| 1558 | */ |
| 1559 | static FILE *inc_fopen(const char *file, |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 1560 | StrList **dhead, |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 1561 | const char **found_path, |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1562 | enum incopen_mode omode, |
| 1563 | enum file_flags fmode) |
| 1564 | { |
| 1565 | StrList *sl; |
| 1566 | struct hash_insert hi; |
| 1567 | void **hp; |
| 1568 | char *path; |
| 1569 | FILE *fp = NULL; |
| 1570 | |
| 1571 | hp = hash_find(&FileHash, file, &hi); |
| 1572 | if (hp) { |
| 1573 | path = *hp; |
Martin Storsjö | f283c8f | 2017-08-13 17:28:46 +0300 | [diff] [blame] | 1574 | if (path || omode != INC_NEEDED) { |
H. Peter Anvin | 97fda4c | 2017-08-16 15:52:51 -0700 | [diff] [blame] | 1575 | nasm_add_string_to_strlist(dhead, path ? path : file); |
Martin Storsjö | f283c8f | 2017-08-13 17:28:46 +0300 | [diff] [blame] | 1576 | } |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1577 | } else { |
| 1578 | /* Need to do the actual path search */ |
| 1579 | size_t file_len; |
| 1580 | |
| 1581 | sl = NULL; |
| 1582 | fp = inc_fopen_search(file, &sl, omode, fmode); |
| 1583 | |
| 1584 | file_len = strlen(file); |
| 1585 | |
| 1586 | if (!sl) { |
| 1587 | /* Store negative result for this file */ |
| 1588 | sl = nasm_malloc(file_len + 1 + sizeof sl->next); |
| 1589 | memcpy(sl->str, file, file_len+1); |
| 1590 | sl->next = NULL; |
| 1591 | file = sl->str; |
| 1592 | path = NULL; |
| 1593 | } else { |
| 1594 | path = sl->str; |
| 1595 | file = strchr(path, '\0') - file_len; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1596 | } |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1597 | |
| 1598 | hash_add(&hi, file, path); /* Positive or negative result */ |
| 1599 | |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 1600 | /* |
| 1601 | * Add file to dependency path. The in_list() is needed |
| 1602 | * in case the file was already added with %depend. |
| 1603 | */ |
| 1604 | if (path || omode != INC_NEEDED) |
H. Peter Anvin | 436e367 | 2016-10-04 01:12:28 -0700 | [diff] [blame] | 1605 | nasm_add_to_strlist(dhead, sl); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1606 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1607 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1608 | if (!path) { |
| 1609 | if (omode == INC_NEEDED) |
| 1610 | nasm_fatal(0, "unable to open include file `%s'", file); |
| 1611 | |
| 1612 | if (found_path) |
| 1613 | *found_path = NULL; |
| 1614 | |
| 1615 | return NULL; |
| 1616 | } |
| 1617 | |
| 1618 | if (!fp && omode != INC_PROBE) |
Cyrill Gorcunov | 4ff8c63 | 2017-01-06 00:36:23 +0300 | [diff] [blame] | 1619 | fp = nasm_open_read(path, fmode); |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1620 | |
| 1621 | if (found_path) |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 1622 | *found_path = path; |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1623 | |
| 1624 | return fp; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1625 | } |
| 1626 | |
| 1627 | /* |
Fabian Giesen | 0bbc38d | 2016-04-28 13:48:14 -0700 | [diff] [blame] | 1628 | * Opens an include or input file. Public version, for use by modules |
| 1629 | * that get a file:lineno pair and need to look at the file again |
| 1630 | * (e.g. the CodeView debug backend). Returns NULL on failure. |
| 1631 | */ |
H. Peter Anvin | 3e83cec | 2016-05-25 04:28:46 -0700 | [diff] [blame] | 1632 | FILE *pp_input_fopen(const char *filename, enum file_flags mode) |
Fabian Giesen | 0bbc38d | 2016-04-28 13:48:14 -0700 | [diff] [blame] | 1633 | { |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 1634 | return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode); |
Fabian Giesen | 0bbc38d | 2016-04-28 13:48:14 -0700 | [diff] [blame] | 1635 | } |
| 1636 | |
| 1637 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1638 | * 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] | 1639 | * 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] | 1640 | * return true if _any_ single-line macro of that name is defined. |
| 1641 | * Otherwise, will return true if a single-line macro with either |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1642 | * `nparam' or no parameters is defined. |
| 1643 | * |
| 1644 | * If a macro with precisely the right number of parameters is |
H. Peter Anvin | ef7468f | 2002-04-30 20:57:59 +0000 | [diff] [blame] | 1645 | * defined, or nparam is -1, the address of the definition structure |
| 1646 | * 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] | 1647 | * is NULL, no action will be taken regarding its contents, and no |
| 1648 | * error will occur. |
| 1649 | * |
| 1650 | * Note that this is also called with nparam zero to resolve |
| 1651 | * `ifdef'. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1652 | * |
| 1653 | * If you already know which context macro belongs to, you can pass |
| 1654 | * the context pointer as first parameter; if you won't but name begins |
| 1655 | * with %$ the context will be automatically computed. If all_contexts |
| 1656 | * is true, macro will be searched in outer contexts as well. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1657 | */ |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1658 | static bool |
H. Peter Anvin | b2a5fda | 2008-06-19 21:42:42 -0700 | [diff] [blame] | 1659 | smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn, |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1660 | bool nocase) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1661 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1662 | struct hash_table *smtbl; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1663 | SMacro *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1664 | |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1665 | if (ctx) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1666 | smtbl = &ctx->localmac; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1667 | } else if (name[0] == '%' && name[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1668 | if (cstk) |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1669 | ctx = get_ctx(name, &name); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1670 | if (!ctx) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1671 | return false; /* got to return _something_ */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1672 | smtbl = &ctx->localmac; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1673 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1674 | smtbl = &smacros; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1675 | } |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1676 | m = (SMacro *) hash_findix(smtbl, name); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1677 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1678 | while (m) { |
| 1679 | if (!mstrcmp(m->name, name, m->casesense && nocase) && |
Charles Crayne | 192d5b5 | 2007-10-18 19:02:42 -0700 | [diff] [blame] | 1680 | (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1681 | if (defn) { |
Charles Crayne | 192d5b5 | 2007-10-18 19:02:42 -0700 | [diff] [blame] | 1682 | if (nparam == (int) m->nparam || nparam == -1) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1683 | *defn = m; |
| 1684 | else |
| 1685 | *defn = NULL; |
| 1686 | } |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1687 | return true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1688 | } |
| 1689 | m = m->next; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1690 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1691 | |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1692 | return false; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1693 | } |
| 1694 | |
| 1695 | /* |
| 1696 | * Count and mark off the parameters in a multi-line macro call. |
| 1697 | * This is called both from within the multi-line macro expansion |
| 1698 | * code, and also to mark off the default parameters when provided |
| 1699 | * in a %macro definition line. |
| 1700 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1701 | static void count_mmac_params(Token * t, int *nparam, Token *** params) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1702 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1703 | int paramsize, brace; |
| 1704 | |
| 1705 | *nparam = paramsize = 0; |
| 1706 | *params = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1707 | while (t) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1708 | /* +1: we need space for the final NULL */ |
H. Peter Anvin | 91fb6f1 | 2008-09-01 10:56:33 -0700 | [diff] [blame] | 1709 | if (*nparam+1 >= paramsize) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1710 | paramsize += PARAM_DELTA; |
| 1711 | *params = nasm_realloc(*params, sizeof(**params) * paramsize); |
| 1712 | } |
| 1713 | skip_white_(t); |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1714 | brace = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1715 | if (tok_is_(t, "{")) |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1716 | brace++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1717 | (*params)[(*nparam)++] = t; |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1718 | if (brace) { |
| 1719 | while (brace && (t = t->next) != NULL) { |
| 1720 | if (tok_is_(t, "{")) |
| 1721 | brace++; |
| 1722 | else if (tok_is_(t, "}")) |
| 1723 | brace--; |
| 1724 | } |
| 1725 | |
| 1726 | if (t) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1727 | /* |
| 1728 | * Now we've found the closing brace, look further |
| 1729 | * for the comma. |
| 1730 | */ |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1731 | t = t->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1732 | skip_white_(t); |
| 1733 | if (tok_isnt_(t, ",")) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1734 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1735 | "braces do not enclose all of macro parameter"); |
| 1736 | while (tok_isnt_(t, ",")) |
| 1737 | t = t->next; |
| 1738 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1739 | } |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1740 | } else { |
| 1741 | while (tok_isnt_(t, ",")) |
| 1742 | t = t->next; |
| 1743 | } |
| 1744 | if (t) { /* got a comma/brace */ |
| 1745 | t = t->next; /* eat the comma */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1746 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1747 | } |
| 1748 | } |
| 1749 | |
| 1750 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1751 | * Determine whether one of the various `if' conditions is true or |
| 1752 | * not. |
| 1753 | * |
| 1754 | * We must free the tline we get passed. |
| 1755 | */ |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1756 | static bool if_condition(Token * tline, enum preproc_token ct) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1757 | { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1758 | enum pp_conditional i = PP_COND(ct); |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1759 | bool j; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1760 | Token *t, *tt, **tptr, *origline; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1761 | struct tokenval tokval; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1762 | expr *evalresult; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1763 | enum pp_token_type needtype; |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1764 | char *p; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1765 | |
| 1766 | origline = tline; |
| 1767 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1768 | switch (i) { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1769 | case PPC_IFCTX: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1770 | j = false; /* have we matched yet? */ |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1771 | while (true) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1772 | skip_white_(tline); |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1773 | if (!tline) |
| 1774 | break; |
| 1775 | if (tline->type != TOK_ID) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1776 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1777 | "`%s' expects context identifiers", pp_directives[ct]); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1778 | free_tlist(origline); |
| 1779 | return -1; |
| 1780 | } |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1781 | if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1782 | j = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1783 | tline = tline->next; |
| 1784 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1785 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1786 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1787 | case PPC_IFDEF: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1788 | j = false; /* have we matched yet? */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1789 | while (tline) { |
| 1790 | skip_white_(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1791 | if (!tline || (tline->type != TOK_ID && |
| 1792 | (tline->type != TOK_PREPROC_ID || |
| 1793 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1794 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1795 | "`%s' expects macro identifiers", pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1796 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1797 | } |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1798 | if (smacro_defined(NULL, tline->text, 0, NULL, true)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1799 | j = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1800 | tline = tline->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1801 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1802 | break; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1803 | |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1804 | case PPC_IFENV: |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1805 | tline = expand_smacro(tline); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1806 | j = false; /* have we matched yet? */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1807 | while (tline) { |
| 1808 | skip_white_(tline); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1809 | if (!tline || (tline->type != TOK_ID && |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1810 | tline->type != TOK_STRING && |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1811 | (tline->type != TOK_PREPROC_ID || |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1812 | tline->text[1] != '!'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1813 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1814 | "`%s' expects environment variable names", |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1815 | pp_directives[ct]); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1816 | goto fail; |
| 1817 | } |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1818 | p = tline->text; |
| 1819 | if (tline->type == TOK_PREPROC_ID) |
| 1820 | p += 2; /* Skip leading %! */ |
| 1821 | if (*p == '\'' || *p == '\"' || *p == '`') |
| 1822 | nasm_unquote_cstr(p, ct); |
| 1823 | if (getenv(p)) |
| 1824 | j = true; |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1825 | tline = tline->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1826 | } |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1827 | break; |
| 1828 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1829 | case PPC_IFIDN: |
| 1830 | case PPC_IFIDNI: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1831 | tline = expand_smacro(tline); |
| 1832 | t = tt = tline; |
| 1833 | while (tok_isnt_(tt, ",")) |
| 1834 | tt = tt->next; |
| 1835 | if (!tt) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1836 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1837 | "`%s' expects two comma-separated arguments", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1838 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1839 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1840 | } |
| 1841 | tt = tt->next; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1842 | j = true; /* assume equality unless proved not */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1843 | while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) { |
| 1844 | if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1845 | nasm_error(ERR_NONFATAL, "`%s': more than one comma on line", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1846 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1847 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1848 | } |
| 1849 | if (t->type == TOK_WHITESPACE) { |
| 1850 | t = t->next; |
| 1851 | continue; |
| 1852 | } |
| 1853 | if (tt->type == TOK_WHITESPACE) { |
| 1854 | tt = tt->next; |
| 1855 | continue; |
| 1856 | } |
| 1857 | if (tt->type != t->type) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1858 | j = false; /* found mismatching tokens */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1859 | break; |
| 1860 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1861 | /* When comparing strings, need to unquote them first */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1862 | if (t->type == TOK_STRING) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1863 | size_t l1 = nasm_unquote(t->text, NULL); |
| 1864 | size_t l2 = nasm_unquote(tt->text, NULL); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 1865 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1866 | if (l1 != l2) { |
| 1867 | j = false; |
| 1868 | break; |
| 1869 | } |
| 1870 | if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) { |
| 1871 | j = false; |
| 1872 | break; |
| 1873 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1874 | } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1875 | j = false; /* found mismatching tokens */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1876 | break; |
| 1877 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1878 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1879 | t = t->next; |
| 1880 | tt = tt->next; |
| 1881 | } |
| 1882 | if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1883 | j = false; /* trailing gunk on one end or other */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1884 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1885 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1886 | case PPC_IFMACRO: |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1887 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1888 | bool found = false; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1889 | MMacro searching, *mmac; |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 1890 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1891 | skip_white_(tline); |
| 1892 | tline = expand_id(tline); |
| 1893 | if (!tok_type_(tline, TOK_ID)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1894 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1895 | "`%s' expects a macro name", pp_directives[ct]); |
| 1896 | goto fail; |
| 1897 | } |
| 1898 | searching.name = nasm_strdup(tline->text); |
| 1899 | searching.casesense = true; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1900 | searching.plus = false; |
| 1901 | searching.nolist = false; |
| 1902 | searching.in_progress = 0; |
| 1903 | searching.max_depth = 0; |
| 1904 | searching.rep_nest = NULL; |
| 1905 | searching.nparam_min = 0; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1906 | searching.nparam_max = INT_MAX; |
| 1907 | tline = expand_smacro(tline->next); |
| 1908 | skip_white_(tline); |
| 1909 | if (!tline) { |
| 1910 | } else if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1911 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1912 | "`%s' expects a parameter count or nothing", |
| 1913 | pp_directives[ct]); |
| 1914 | } else { |
| 1915 | searching.nparam_min = searching.nparam_max = |
| 1916 | readnum(tline->text, &j); |
| 1917 | if (j) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1918 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1919 | "unable to parse parameter count `%s'", |
| 1920 | tline->text); |
| 1921 | } |
| 1922 | if (tline && tok_is_(tline->next, "-")) { |
| 1923 | tline = tline->next->next; |
| 1924 | if (tok_is_(tline, "*")) |
| 1925 | searching.nparam_max = INT_MAX; |
| 1926 | else if (!tok_type_(tline, TOK_NUMBER)) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1927 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1928 | "`%s' expects a parameter count after `-'", |
| 1929 | pp_directives[ct]); |
| 1930 | else { |
| 1931 | searching.nparam_max = readnum(tline->text, &j); |
| 1932 | if (j) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1933 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1934 | "unable to parse parameter count `%s'", |
| 1935 | tline->text); |
Cyrill Gorcunov | c9244ea | 2017-10-22 15:25:48 +0300 | [diff] [blame] | 1936 | if (searching.nparam_min > searching.nparam_max) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1937 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1938 | "minimum parameter count exceeds maximum"); |
Cyrill Gorcunov | c9244ea | 2017-10-22 15:25:48 +0300 | [diff] [blame] | 1939 | searching.nparam_max = searching.nparam_min; |
| 1940 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1941 | } |
| 1942 | } |
| 1943 | if (tline && tok_is_(tline->next, "+")) { |
| 1944 | tline = tline->next; |
| 1945 | searching.plus = true; |
| 1946 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1947 | mmac = (MMacro *) hash_findix(&mmacros, searching.name); |
| 1948 | while (mmac) { |
| 1949 | if (!strcmp(mmac->name, searching.name) && |
| 1950 | (mmac->nparam_min <= searching.nparam_max |
| 1951 | || searching.plus) |
| 1952 | && (searching.nparam_min <= mmac->nparam_max |
| 1953 | || mmac->plus)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1954 | found = true; |
| 1955 | break; |
| 1956 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1957 | mmac = mmac->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1958 | } |
| 1959 | if (tline && tline->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1960 | nasm_error(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1961 | "trailing garbage after %%ifmacro ignored"); |
| 1962 | nasm_free(searching.name); |
| 1963 | j = found; |
| 1964 | break; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1965 | } |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 1966 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1967 | case PPC_IFID: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1968 | needtype = TOK_ID; |
| 1969 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1970 | case PPC_IFNUM: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1971 | needtype = TOK_NUMBER; |
| 1972 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1973 | case PPC_IFSTR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1974 | needtype = TOK_STRING; |
| 1975 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1976 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1977 | iftype: |
| 1978 | t = tline = expand_smacro(tline); |
H. Peter Anvin | d85d250 | 2008-05-04 17:53:31 -0700 | [diff] [blame] | 1979 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1980 | while (tok_type_(t, TOK_WHITESPACE) || |
| 1981 | (needtype == TOK_NUMBER && |
| 1982 | tok_type_(t, TOK_OTHER) && |
| 1983 | (t->text[0] == '-' || t->text[0] == '+') && |
| 1984 | !t->text[1])) |
| 1985 | t = t->next; |
H. Peter Anvin | d85d250 | 2008-05-04 17:53:31 -0700 | [diff] [blame] | 1986 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1987 | j = tok_type_(t, needtype); |
| 1988 | break; |
H. Peter Anvin | cbf768d | 2008-02-16 16:41:25 -0800 | [diff] [blame] | 1989 | |
| 1990 | case PPC_IFTOKEN: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1991 | t = tline = expand_smacro(tline); |
| 1992 | while (tok_type_(t, TOK_WHITESPACE)) |
| 1993 | t = t->next; |
H. Peter Anvin | cbf768d | 2008-02-16 16:41:25 -0800 | [diff] [blame] | 1994 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1995 | j = false; |
| 1996 | if (t) { |
| 1997 | t = t->next; /* Skip the actual token */ |
| 1998 | while (tok_type_(t, TOK_WHITESPACE)) |
| 1999 | t = t->next; |
| 2000 | j = !t; /* Should be nothing left */ |
| 2001 | } |
| 2002 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2003 | |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 2004 | case PPC_IFEMPTY: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2005 | t = tline = expand_smacro(tline); |
| 2006 | while (tok_type_(t, TOK_WHITESPACE)) |
| 2007 | t = t->next; |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 2008 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2009 | j = !t; /* Should be empty */ |
| 2010 | break; |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 2011 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2012 | case PPC_IF: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2013 | t = tline = expand_smacro(tline); |
| 2014 | tptr = &t; |
| 2015 | tokval.t_type = TOKEN_INVALID; |
| 2016 | evalresult = evaluate(ppscan, tptr, &tokval, |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2017 | NULL, pass | CRITICAL, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2018 | if (!evalresult) |
| 2019 | return -1; |
| 2020 | if (tokval.t_type) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2021 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2022 | "trailing garbage after expression ignored"); |
| 2023 | if (!is_simple(evalresult)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2024 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2025 | "non-constant value given to `%s'", pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2026 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2027 | } |
Chuck Crayne | 60ae75d | 2007-05-02 01:59:16 +0000 | [diff] [blame] | 2028 | j = reloc_value(evalresult) != 0; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2029 | break; |
H. Peter Anvin | 95e2882 | 2007-09-12 04:20:08 +0000 | [diff] [blame] | 2030 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2031 | default: |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2032 | nasm_error(ERR_FATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2033 | "preprocessor directive `%s' not yet implemented", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2034 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2035 | goto fail; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2036 | } |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2037 | |
| 2038 | free_tlist(origline); |
| 2039 | return j ^ PP_NEGATIVE(ct); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2040 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2041 | fail: |
| 2042 | free_tlist(origline); |
| 2043 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2044 | } |
| 2045 | |
| 2046 | /* |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2047 | * Common code for defining an smacro |
| 2048 | */ |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 2049 | static bool define_smacro(Context *ctx, const char *mname, bool casesense, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2050 | int nparam, Token *expansion) |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2051 | { |
| 2052 | SMacro *smac, **smhead; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2053 | struct hash_table *smtbl; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2054 | |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2055 | if (smacro_defined(ctx, mname, nparam, &smac, casesense)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2056 | if (!smac) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2057 | nasm_error(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2058 | "single-line macro `%s' defined both with and" |
| 2059 | " without parameters", mname); |
| 2060 | /* |
| 2061 | * Some instances of the old code considered this a failure, |
| 2062 | * some others didn't. What is the right thing to do here? |
| 2063 | */ |
| 2064 | free_tlist(expansion); |
| 2065 | return false; /* Failure */ |
| 2066 | } else { |
| 2067 | /* |
| 2068 | * We're redefining, so we have to take over an |
| 2069 | * existing SMacro structure. This means freeing |
| 2070 | * what was already in it. |
| 2071 | */ |
| 2072 | nasm_free(smac->name); |
| 2073 | free_tlist(smac->expansion); |
| 2074 | } |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2075 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2076 | smtbl = ctx ? &ctx->localmac : &smacros; |
| 2077 | smhead = (SMacro **) hash_findi_add(smtbl, mname); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2078 | smac = nasm_malloc(sizeof(SMacro)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2079 | smac->next = *smhead; |
| 2080 | *smhead = smac; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2081 | } |
| 2082 | smac->name = nasm_strdup(mname); |
| 2083 | smac->casesense = casesense; |
| 2084 | smac->nparam = nparam; |
| 2085 | smac->expansion = expansion; |
| 2086 | smac->in_progress = false; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2087 | return true; /* Success */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2088 | } |
| 2089 | |
| 2090 | /* |
| 2091 | * Undefine an smacro |
| 2092 | */ |
| 2093 | static void undef_smacro(Context *ctx, const char *mname) |
| 2094 | { |
| 2095 | SMacro **smhead, *s, **sp; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2096 | struct hash_table *smtbl; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2097 | |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2098 | smtbl = ctx ? &ctx->localmac : &smacros; |
| 2099 | smhead = (SMacro **)hash_findi(smtbl, mname, NULL); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2100 | |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2101 | if (smhead) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2102 | /* |
| 2103 | * We now have a macro name... go hunt for it. |
| 2104 | */ |
| 2105 | sp = smhead; |
| 2106 | while ((s = *sp) != NULL) { |
| 2107 | if (!mstrcmp(s->name, mname, s->casesense)) { |
| 2108 | *sp = s->next; |
| 2109 | nasm_free(s->name); |
| 2110 | free_tlist(s->expansion); |
| 2111 | nasm_free(s); |
| 2112 | } else { |
| 2113 | sp = &s->next; |
| 2114 | } |
| 2115 | } |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2116 | } |
| 2117 | } |
| 2118 | |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2119 | /* |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2120 | * Parse a mmacro specification. |
| 2121 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2122 | 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] | 2123 | { |
| 2124 | bool err; |
| 2125 | |
| 2126 | tline = tline->next; |
| 2127 | skip_white_(tline); |
| 2128 | tline = expand_id(tline); |
| 2129 | if (!tok_type_(tline, TOK_ID)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2130 | nasm_error(ERR_NONFATAL, "`%s' expects a macro name", directive); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2131 | return false; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2132 | } |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2133 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2134 | def->prev = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2135 | def->name = nasm_strdup(tline->text); |
| 2136 | def->plus = false; |
| 2137 | def->nolist = false; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2138 | def->in_progress = 0; |
| 2139 | def->rep_nest = NULL; |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2140 | def->nparam_min = 0; |
| 2141 | def->nparam_max = 0; |
| 2142 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2143 | tline = expand_smacro(tline->next); |
| 2144 | skip_white_(tline); |
| 2145 | if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2146 | nasm_error(ERR_NONFATAL, "`%s' expects a parameter count", directive); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2147 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2148 | def->nparam_min = def->nparam_max = |
| 2149 | readnum(tline->text, &err); |
| 2150 | if (err) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2151 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2152 | "unable to parse parameter count `%s'", tline->text); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2153 | } |
| 2154 | if (tline && tok_is_(tline->next, "-")) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2155 | tline = tline->next->next; |
| 2156 | if (tok_is_(tline, "*")) { |
| 2157 | def->nparam_max = INT_MAX; |
| 2158 | } else if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2159 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2160 | "`%s' expects a parameter count after `-'", directive); |
| 2161 | } else { |
| 2162 | def->nparam_max = readnum(tline->text, &err); |
| 2163 | if (err) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2164 | nasm_error(ERR_NONFATAL, "unable to parse parameter count `%s'", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2165 | tline->text); |
| 2166 | } |
| 2167 | if (def->nparam_min > def->nparam_max) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2168 | nasm_error(ERR_NONFATAL, "minimum parameter count exceeds maximum"); |
Cyrill Gorcunov | c9244ea | 2017-10-22 15:25:48 +0300 | [diff] [blame] | 2169 | def->nparam_max = def->nparam_min; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2170 | } |
| 2171 | } |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2172 | } |
| 2173 | if (tline && tok_is_(tline->next, "+")) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2174 | tline = tline->next; |
| 2175 | def->plus = true; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2176 | } |
| 2177 | if (tline && tok_type_(tline->next, TOK_ID) && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2178 | !nasm_stricmp(tline->next->text, ".nolist")) { |
| 2179 | tline = tline->next; |
| 2180 | def->nolist = true; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2181 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2182 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2183 | /* |
| 2184 | * Handle default parameters. |
| 2185 | */ |
| 2186 | if (tline && tline->next) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2187 | def->dlist = tline->next; |
| 2188 | tline->next = NULL; |
| 2189 | count_mmac_params(def->dlist, &def->ndefs, &def->defaults); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2190 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2191 | def->dlist = NULL; |
| 2192 | def->defaults = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2193 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2194 | def->expansion = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2195 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2196 | if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2197 | !def->plus) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2198 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2199 | "too many default macro parameters"); |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2200 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2201 | return true; |
| 2202 | } |
| 2203 | |
| 2204 | |
| 2205 | /* |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2206 | * Decode a size directive |
| 2207 | */ |
| 2208 | static int parse_size(const char *str) { |
| 2209 | static const char *size_names[] = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2210 | { "byte", "dword", "oword", "qword", "tword", "word", "yword" }; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2211 | static const int sizes[] = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2212 | { 0, 1, 4, 16, 8, 10, 2, 32 }; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2213 | |
Cyrill Gorcunov | a731924 | 2010-06-03 22:04:36 +0400 | [diff] [blame] | 2214 | return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1]; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2215 | } |
| 2216 | |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2217 | /* |
| 2218 | * Process a preprocessor %pragma directive. Currently there are none. |
| 2219 | * Gets passed the token list starting with the "preproc" token from |
| 2220 | * "%pragma preproc". |
| 2221 | */ |
| 2222 | static void do_pragma_preproc(Token *tline) |
| 2223 | { |
| 2224 | /* Skip to the real stuff */ |
| 2225 | tline = tline->next; |
| 2226 | skip_white_(tline); |
| 2227 | if (!tline) |
| 2228 | return; |
| 2229 | |
| 2230 | (void)tline; /* Nothing else to do at present */ |
| 2231 | } |
| 2232 | |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2233 | /** |
| 2234 | * find and process preprocessor directive in passed line |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2235 | * Find out if a line contains a preprocessor directive, and deal |
| 2236 | * with it if so. |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2237 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2238 | * If a directive _is_ found, it is the responsibility of this routine |
| 2239 | * (and not the caller) to free_tlist() the line. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2240 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2241 | * @param tline a pointer to the current tokeninzed line linked list |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2242 | * @param output if this directive generated output |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2243 | * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2244 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2245 | */ |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2246 | static int do_directive(Token *tline, char **output) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2247 | { |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2248 | enum preproc_token i; |
| 2249 | int j; |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 2250 | bool err; |
| 2251 | int nparam; |
| 2252 | bool nolist; |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 2253 | bool casesense; |
H. Peter Anvin | 8cfdb9d | 2007-09-14 18:36:01 -0700 | [diff] [blame] | 2254 | int k, m; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2255 | int offset; |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 2256 | char *p, *pp; |
| 2257 | const char *found_path; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 2258 | const char *mname; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2259 | Include *inc; |
| 2260 | Context *ctx; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2261 | Cond *cond; |
| 2262 | MMacro *mmac, **mmhead; |
Jin Kyu Song | c9486b9 | 2013-10-28 17:07:57 -0700 | [diff] [blame] | 2263 | Token *t = NULL, *tt, *param_start, *macro_start, *last, **tptr, *origline; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2264 | Line *l; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2265 | struct tokenval tokval; |
| 2266 | expr *evalresult; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2267 | MMacro *tmp_defining; /* Used when manipulating rep_nest */ |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 2268 | int64_t count; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 2269 | size_t len; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2270 | int severity; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2271 | |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2272 | *output = NULL; /* No output generated */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2273 | origline = tline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2274 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2275 | skip_white_(tline); |
H. Peter Anvin | f2936d7 | 2008-06-04 15:11:23 -0700 | [diff] [blame] | 2276 | if (!tline || !tok_type_(tline, TOK_PREPROC_ID) || |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2277 | (tline->text[1] == '%' || tline->text[1] == '$' |
| 2278 | || tline->text[1] == '!')) |
| 2279 | return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2280 | |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2281 | i = pp_token_hash(tline->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2282 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2283 | /* |
| 2284 | * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO |
| 2285 | * since they are known to be buggy at moment, we need to fix them |
| 2286 | * in future release (2.09-2.10) |
| 2287 | */ |
Philipp Kloke | b432f57 | 2013-03-31 12:01:23 +0200 | [diff] [blame] | 2288 | if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2289 | nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'", |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2290 | tline->text); |
| 2291 | return NO_DIRECTIVE_FOUND; |
| 2292 | } |
| 2293 | |
| 2294 | /* |
| 2295 | * If we're in a non-emitting branch of a condition construct, |
| 2296 | * or walking to the end of an already terminated %rep block, |
| 2297 | * we should ignore all directives except for condition |
| 2298 | * directives. |
| 2299 | */ |
| 2300 | if (((istk->conds && !emitting(istk->conds->state)) || |
| 2301 | (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) { |
| 2302 | return NO_DIRECTIVE_FOUND; |
| 2303 | } |
| 2304 | |
| 2305 | /* |
| 2306 | * If we're defining a macro or reading a %rep block, we should |
| 2307 | * ignore all directives except for %macro/%imacro (which nest), |
| 2308 | * %endm/%endmacro, and (only if we're in a %rep block) %endrep. |
| 2309 | * If we're in a %rep block, another %rep nests, so should be let through. |
| 2310 | */ |
| 2311 | if (defining && i != PP_MACRO && i != PP_IMACRO && |
| 2312 | i != PP_RMACRO && i != PP_IRMACRO && |
| 2313 | i != PP_ENDMACRO && i != PP_ENDM && |
| 2314 | (defining->name || (i != PP_ENDREP && i != PP_REP))) { |
| 2315 | return NO_DIRECTIVE_FOUND; |
| 2316 | } |
| 2317 | |
| 2318 | if (defining) { |
| 2319 | if (i == PP_MACRO || i == PP_IMACRO || |
| 2320 | i == PP_RMACRO || i == PP_IRMACRO) { |
| 2321 | nested_mac_count++; |
| 2322 | return NO_DIRECTIVE_FOUND; |
| 2323 | } else if (nested_mac_count > 0) { |
| 2324 | if (i == PP_ENDMACRO) { |
| 2325 | nested_mac_count--; |
| 2326 | return NO_DIRECTIVE_FOUND; |
| 2327 | } |
| 2328 | } |
| 2329 | if (!defining->name) { |
| 2330 | if (i == PP_REP) { |
| 2331 | nested_rep_count++; |
| 2332 | return NO_DIRECTIVE_FOUND; |
| 2333 | } else if (nested_rep_count > 0) { |
| 2334 | if (i == PP_ENDREP) { |
| 2335 | nested_rep_count--; |
| 2336 | return NO_DIRECTIVE_FOUND; |
| 2337 | } |
| 2338 | } |
| 2339 | } |
| 2340 | } |
| 2341 | |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2342 | switch (i) { |
| 2343 | case PP_INVALID: |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2344 | nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'", |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2345 | tline->text); |
| 2346 | return NO_DIRECTIVE_FOUND; /* didn't get it */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2347 | |
H. Peter Anvin | 3f87a2a | 2016-10-04 14:07:19 -0700 | [diff] [blame] | 2348 | case PP_PRAGMA: |
| 2349 | /* |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2350 | * %pragma namespace options... |
| 2351 | * |
| 2352 | * The namespace "preproc" is reserved for the preprocessor; |
| 2353 | * all other namespaces generate a [pragma] assembly directive. |
| 2354 | * |
| 2355 | * Invalid %pragmas are ignored and may have different |
| 2356 | * meaning in future versions of NASM. |
H. Peter Anvin | 3f87a2a | 2016-10-04 14:07:19 -0700 | [diff] [blame] | 2357 | */ |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2358 | tline = tline->next; |
| 2359 | skip_white_(tline); |
| 2360 | tline = expand_smacro(tline); |
| 2361 | if (tok_type_(tline, TOK_ID)) { |
| 2362 | if (!nasm_stricmp(tline->text, "preproc")) { |
| 2363 | /* Preprocessor pragma */ |
| 2364 | do_pragma_preproc(tline); |
| 2365 | } else { |
| 2366 | /* Build the assembler directive */ |
| 2367 | t = new_Token(NULL, TOK_OTHER, "[", 1); |
| 2368 | t->next = new_Token(NULL, TOK_ID, "pragma", 6); |
| 2369 | t->next->next = new_Token(tline, TOK_WHITESPACE, NULL, 0); |
| 2370 | tline = t; |
| 2371 | for (t = tline; t->next; t = t->next) |
| 2372 | ; |
| 2373 | t->next = new_Token(NULL, TOK_OTHER, "]", 1); |
| 2374 | /* true here can be revisited in the future */ |
| 2375 | *output = detoken(tline, true); |
| 2376 | } |
| 2377 | } |
H. Peter Anvin | 3f87a2a | 2016-10-04 14:07:19 -0700 | [diff] [blame] | 2378 | free_tlist(origline); |
| 2379 | return DIRECTIVE_FOUND; |
| 2380 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2381 | case PP_STACKSIZE: |
| 2382 | /* Directive to tell NASM what the default stack size is. The |
| 2383 | * default is for a 16-bit stack, and this can be overriden with |
| 2384 | * %stacksize large. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2385 | */ |
| 2386 | tline = tline->next; |
| 2387 | if (tline && tline->type == TOK_WHITESPACE) |
| 2388 | tline = tline->next; |
| 2389 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2390 | nasm_error(ERR_NONFATAL, "`%%stacksize' missing size parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2391 | free_tlist(origline); |
| 2392 | return DIRECTIVE_FOUND; |
| 2393 | } |
| 2394 | if (nasm_stricmp(tline->text, "flat") == 0) { |
| 2395 | /* All subsequent ARG directives are for a 32-bit stack */ |
| 2396 | StackSize = 4; |
| 2397 | StackPointer = "ebp"; |
| 2398 | ArgOffset = 8; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2399 | LocalOffset = 0; |
Charles Crayne | 7eaf919 | 2007-11-08 22:11:14 -0800 | [diff] [blame] | 2400 | } else if (nasm_stricmp(tline->text, "flat64") == 0) { |
| 2401 | /* All subsequent ARG directives are for a 64-bit stack */ |
| 2402 | StackSize = 8; |
| 2403 | StackPointer = "rbp"; |
Per Jessen | 53252e0 | 2010-02-11 00:16:59 +0300 | [diff] [blame] | 2404 | ArgOffset = 16; |
Charles Crayne | 7eaf919 | 2007-11-08 22:11:14 -0800 | [diff] [blame] | 2405 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2406 | } else if (nasm_stricmp(tline->text, "large") == 0) { |
| 2407 | /* All subsequent ARG directives are for a 16-bit stack, |
| 2408 | * far function call. |
| 2409 | */ |
| 2410 | StackSize = 2; |
| 2411 | StackPointer = "bp"; |
| 2412 | ArgOffset = 4; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2413 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2414 | } else if (nasm_stricmp(tline->text, "small") == 0) { |
| 2415 | /* All subsequent ARG directives are for a 16-bit stack, |
| 2416 | * far function call. We don't support near functions. |
| 2417 | */ |
| 2418 | StackSize = 2; |
| 2419 | StackPointer = "bp"; |
| 2420 | ArgOffset = 6; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2421 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2422 | } else { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2423 | nasm_error(ERR_NONFATAL, "`%%stacksize' invalid size type"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2424 | free_tlist(origline); |
| 2425 | return DIRECTIVE_FOUND; |
| 2426 | } |
| 2427 | free_tlist(origline); |
| 2428 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2429 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2430 | case PP_ARG: |
| 2431 | /* TASM like ARG directive to define arguments to functions, in |
| 2432 | * the following form: |
| 2433 | * |
| 2434 | * ARG arg1:WORD, arg2:DWORD, arg4:QWORD |
| 2435 | */ |
| 2436 | offset = ArgOffset; |
| 2437 | do { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 2438 | char *arg, directive[256]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2439 | int size = StackSize; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2440 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2441 | /* Find the argument name */ |
| 2442 | tline = tline->next; |
| 2443 | if (tline && tline->type == TOK_WHITESPACE) |
| 2444 | tline = tline->next; |
| 2445 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2446 | nasm_error(ERR_NONFATAL, "`%%arg' missing argument parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2447 | free_tlist(origline); |
| 2448 | return DIRECTIVE_FOUND; |
| 2449 | } |
| 2450 | arg = tline->text; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2451 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2452 | /* Find the argument size type */ |
| 2453 | tline = tline->next; |
| 2454 | if (!tline || tline->type != TOK_OTHER |
| 2455 | || tline->text[0] != ':') { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2456 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2457 | "Syntax error processing `%%arg' directive"); |
| 2458 | free_tlist(origline); |
| 2459 | return DIRECTIVE_FOUND; |
| 2460 | } |
| 2461 | tline = tline->next; |
| 2462 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2463 | nasm_error(ERR_NONFATAL, "`%%arg' missing size type parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2464 | free_tlist(origline); |
| 2465 | return DIRECTIVE_FOUND; |
| 2466 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2467 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2468 | /* Allow macro expansion of type parameter */ |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2469 | tt = tokenize(tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2470 | tt = expand_smacro(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2471 | size = parse_size(tt->text); |
| 2472 | if (!size) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2473 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2474 | "Invalid size type for `%%arg' missing directive"); |
| 2475 | free_tlist(tt); |
| 2476 | free_tlist(origline); |
| 2477 | return DIRECTIVE_FOUND; |
| 2478 | } |
| 2479 | free_tlist(tt); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2480 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2481 | /* Round up to even stack slots */ |
| 2482 | size = ALIGN(size, StackSize); |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2483 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2484 | /* Now define the macro for the argument */ |
| 2485 | snprintf(directive, sizeof(directive), "%%define %s (%s+%d)", |
| 2486 | arg, StackPointer, offset); |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2487 | do_directive(tokenize(directive), output); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2488 | offset += size; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2489 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2490 | /* Move to the next argument in the list */ |
| 2491 | tline = tline->next; |
| 2492 | if (tline && tline->type == TOK_WHITESPACE) |
| 2493 | tline = tline->next; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2494 | } while (tline && tline->type == TOK_OTHER && tline->text[0] == ','); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2495 | ArgOffset = offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2496 | free_tlist(origline); |
| 2497 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2498 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2499 | case PP_LOCAL: |
| 2500 | /* TASM like LOCAL directive to define local variables for a |
| 2501 | * function, in the following form: |
| 2502 | * |
| 2503 | * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize |
| 2504 | * |
| 2505 | * The '= LocalSize' at the end is ignored by NASM, but is |
| 2506 | * required by TASM to define the local parameter size (and used |
| 2507 | * by the TASM macro package). |
| 2508 | */ |
| 2509 | offset = LocalOffset; |
| 2510 | do { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 2511 | char *local, directive[256]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2512 | int size = StackSize; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2513 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2514 | /* Find the argument name */ |
| 2515 | tline = tline->next; |
| 2516 | if (tline && tline->type == TOK_WHITESPACE) |
| 2517 | tline = tline->next; |
| 2518 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2519 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2520 | "`%%local' missing argument parameter"); |
| 2521 | free_tlist(origline); |
| 2522 | return DIRECTIVE_FOUND; |
| 2523 | } |
| 2524 | local = tline->text; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2525 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2526 | /* Find the argument size type */ |
| 2527 | tline = tline->next; |
| 2528 | if (!tline || tline->type != TOK_OTHER |
| 2529 | || tline->text[0] != ':') { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2530 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2531 | "Syntax error processing `%%local' directive"); |
| 2532 | free_tlist(origline); |
| 2533 | return DIRECTIVE_FOUND; |
| 2534 | } |
| 2535 | tline = tline->next; |
| 2536 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2537 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2538 | "`%%local' missing size type parameter"); |
| 2539 | free_tlist(origline); |
| 2540 | return DIRECTIVE_FOUND; |
| 2541 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2542 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2543 | /* Allow macro expansion of type parameter */ |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2544 | tt = tokenize(tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2545 | tt = expand_smacro(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2546 | size = parse_size(tt->text); |
| 2547 | if (!size) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2548 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2549 | "Invalid size type for `%%local' missing directive"); |
| 2550 | free_tlist(tt); |
| 2551 | free_tlist(origline); |
| 2552 | return DIRECTIVE_FOUND; |
| 2553 | } |
| 2554 | free_tlist(tt); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2555 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2556 | /* Round up to even stack slots */ |
| 2557 | size = ALIGN(size, StackSize); |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2558 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2559 | offset += size; /* Negative offset, increment before */ |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2560 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2561 | /* Now define the macro for the argument */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2562 | snprintf(directive, sizeof(directive), "%%define %s (%s-%d)", |
| 2563 | local, StackPointer, offset); |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2564 | do_directive(tokenize(directive), output); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2565 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2566 | /* Now define the assign to setup the enter_c macro correctly */ |
| 2567 | snprintf(directive, sizeof(directive), |
| 2568 | "%%assign %%$localsize %%$localsize+%d", size); |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2569 | do_directive(tokenize(directive), output); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2570 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2571 | /* Move to the next argument in the list */ |
| 2572 | tline = tline->next; |
| 2573 | if (tline && tline->type == TOK_WHITESPACE) |
| 2574 | tline = tline->next; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2575 | } while (tline && tline->type == TOK_OTHER && tline->text[0] == ','); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2576 | LocalOffset = offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2577 | free_tlist(origline); |
| 2578 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2579 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2580 | case PP_CLEAR: |
| 2581 | if (tline->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2582 | nasm_error(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2583 | "trailing garbage after `%%clear' ignored"); |
| 2584 | free_macros(); |
| 2585 | init_macros(); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2586 | free_tlist(origline); |
| 2587 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2588 | |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2589 | case PP_DEPEND: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2590 | t = tline->next = expand_smacro(tline->next); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2591 | skip_white_(t); |
| 2592 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2593 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2594 | nasm_error(ERR_NONFATAL, "`%%depend' expects a file name"); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2595 | free_tlist(origline); |
| 2596 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2597 | } |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2598 | if (t->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2599 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2600 | "trailing garbage after `%%depend' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2601 | p = t->text; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2602 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2603 | nasm_unquote_cstr(p, i); |
H. Peter Anvin | 436e367 | 2016-10-04 01:12:28 -0700 | [diff] [blame] | 2604 | nasm_add_string_to_strlist(dephead, p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2605 | free_tlist(origline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2606 | return DIRECTIVE_FOUND; |
| 2607 | |
| 2608 | case PP_INCLUDE: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2609 | t = tline->next = expand_smacro(tline->next); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2610 | skip_white_(t); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2611 | |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2612 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2613 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2614 | nasm_error(ERR_NONFATAL, "`%%include' expects a file name"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2615 | free_tlist(origline); |
| 2616 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2617 | } |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2618 | if (t->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2619 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2620 | "trailing garbage after `%%include' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2621 | p = t->text; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2622 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2623 | nasm_unquote_cstr(p, i); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2624 | inc = nasm_malloc(sizeof(Include)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2625 | inc->next = istk; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2626 | inc->conds = NULL; |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 2627 | found_path = NULL; |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 2628 | inc->fp = inc_fopen(p, dephead, &found_path, |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 2629 | pass == 0 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2630 | if (!inc->fp) { |
| 2631 | /* -MG given but file not found */ |
| 2632 | nasm_free(inc); |
| 2633 | } else { |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 2634 | inc->fname = src_set_fname(found_path ? found_path : p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2635 | inc->lineno = src_set_linnum(0); |
| 2636 | inc->lineinc = 1; |
| 2637 | inc->expansion = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2638 | inc->mstk = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2639 | istk = inc; |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 2640 | lfmt->uplevel(LIST_INCLUDE); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2641 | } |
| 2642 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2643 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2644 | |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2645 | case PP_USE: |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2646 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2647 | static macros_t *use_pkg; |
| 2648 | const char *pkg_macro = NULL; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2649 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2650 | tline = tline->next; |
| 2651 | skip_white_(tline); |
| 2652 | tline = expand_id(tline); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2653 | |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2654 | if (!tline || (tline->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2655 | tline->type != TOK_INTERNAL_STRING && |
| 2656 | tline->type != TOK_ID)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2657 | nasm_error(ERR_NONFATAL, "`%%use' expects a package name"); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2658 | free_tlist(origline); |
| 2659 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2660 | } |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2661 | if (tline->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2662 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2663 | "trailing garbage after `%%use' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2664 | if (tline->type == TOK_STRING) |
| 2665 | nasm_unquote_cstr(tline->text, i); |
| 2666 | use_pkg = nasm_stdmac_find_package(tline->text); |
| 2667 | if (!use_pkg) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2668 | nasm_error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2669 | else |
| 2670 | 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] | 2671 | if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2672 | /* Not already included, go ahead and include it */ |
| 2673 | stdmacpos = use_pkg; |
| 2674 | } |
| 2675 | free_tlist(origline); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2676 | return DIRECTIVE_FOUND; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2677 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2678 | case PP_PUSH: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2679 | case PP_REPL: |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2680 | case PP_POP: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2681 | tline = tline->next; |
| 2682 | skip_white_(tline); |
| 2683 | tline = expand_id(tline); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2684 | if (tline) { |
| 2685 | if (!tok_type_(tline, TOK_ID)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2686 | nasm_error(ERR_NONFATAL, "`%s' expects a context identifier", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2687 | pp_directives[i]); |
| 2688 | free_tlist(origline); |
| 2689 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2690 | } |
| 2691 | if (tline->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2692 | nasm_error(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2693 | "trailing garbage after `%s' ignored", |
| 2694 | pp_directives[i]); |
| 2695 | p = nasm_strdup(tline->text); |
| 2696 | } else { |
| 2697 | p = NULL; /* Anonymous */ |
| 2698 | } |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2699 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2700 | if (i == PP_PUSH) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2701 | ctx = nasm_malloc(sizeof(Context)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2702 | ctx->next = cstk; |
| 2703 | hash_init(&ctx->localmac, HASH_SMALL); |
| 2704 | ctx->name = p; |
| 2705 | ctx->number = unique++; |
| 2706 | cstk = ctx; |
| 2707 | } else { |
| 2708 | /* %pop or %repl */ |
| 2709 | if (!cstk) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2710 | nasm_error(ERR_NONFATAL, "`%s': context stack is empty", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2711 | pp_directives[i]); |
| 2712 | } else if (i == PP_POP) { |
| 2713 | if (p && (!cstk->name || nasm_stricmp(p, cstk->name))) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2714 | nasm_error(ERR_NONFATAL, "`%%pop' in wrong context: %s, " |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2715 | "expected %s", |
| 2716 | cstk->name ? cstk->name : "anonymous", p); |
| 2717 | else |
| 2718 | ctx_pop(); |
| 2719 | } else { |
| 2720 | /* i == PP_REPL */ |
| 2721 | nasm_free(cstk->name); |
| 2722 | cstk->name = p; |
| 2723 | p = NULL; |
| 2724 | } |
| 2725 | nasm_free(p); |
| 2726 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2727 | free_tlist(origline); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2728 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2729 | case PP_FATAL: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2730 | severity = ERR_FATAL; |
| 2731 | goto issue_error; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2732 | case PP_ERROR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2733 | severity = ERR_NONFATAL; |
| 2734 | goto issue_error; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2735 | case PP_WARNING: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2736 | severity = ERR_WARNING|ERR_WARN_USER; |
| 2737 | goto issue_error; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2738 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2739 | issue_error: |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2740 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2741 | /* Only error out if this is the final pass */ |
| 2742 | if (pass != 2 && i != PP_FATAL) |
| 2743 | return DIRECTIVE_FOUND; |
| 2744 | |
| 2745 | tline->next = expand_smacro(tline->next); |
| 2746 | tline = tline->next; |
| 2747 | skip_white_(tline); |
| 2748 | t = tline ? tline->next : NULL; |
| 2749 | skip_white_(t); |
| 2750 | if (tok_type_(tline, TOK_STRING) && !t) { |
| 2751 | /* The line contains only a quoted string */ |
| 2752 | p = tline->text; |
| 2753 | nasm_unquote(p, NULL); /* Ignore NUL character truncation */ |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2754 | nasm_error(severity, "%s", p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2755 | } else { |
| 2756 | /* Not a quoted string, or more than a quoted string */ |
| 2757 | p = detoken(tline, false); |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2758 | nasm_error(severity, "%s", p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2759 | nasm_free(p); |
| 2760 | } |
| 2761 | free_tlist(origline); |
| 2762 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2763 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2764 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2765 | CASE_PP_IF: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2766 | if (istk->conds && !emitting(istk->conds->state)) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2767 | j = COND_NEVER; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2768 | else { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2769 | j = if_condition(tline->next, i); |
| 2770 | tline->next = NULL; /* it got freed */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2771 | j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2772 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2773 | cond = nasm_malloc(sizeof(Cond)); |
| 2774 | cond->next = istk->conds; |
| 2775 | cond->state = j; |
| 2776 | istk->conds = cond; |
| 2777 | if(istk->mstk) |
| 2778 | istk->mstk->condcnt ++; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2779 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2780 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2781 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2782 | CASE_PP_ELIF: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2783 | if (!istk->conds) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2784 | nasm_error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2785 | switch(istk->conds->state) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2786 | case COND_IF_TRUE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2787 | istk->conds->state = COND_DONE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2788 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2789 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2790 | case COND_DONE: |
| 2791 | case COND_NEVER: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2792 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2793 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2794 | case COND_ELSE_TRUE: |
| 2795 | case COND_ELSE_FALSE: |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2796 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND, |
| 2797 | "`%%elif' after `%%else' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2798 | istk->conds->state = COND_NEVER; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2799 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2800 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2801 | case COND_IF_FALSE: |
| 2802 | /* |
| 2803 | * IMPORTANT: In the case of %if, we will already have |
| 2804 | * called expand_mmac_params(); however, if we're |
| 2805 | * processing an %elif we must have been in a |
| 2806 | * non-emitting mode, which would have inhibited |
| 2807 | * the normal invocation of expand_mmac_params(). |
| 2808 | * Therefore, we have to do it explicitly here. |
| 2809 | */ |
| 2810 | j = if_condition(expand_mmac_params(tline->next), i); |
| 2811 | tline->next = NULL; /* it got freed */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2812 | istk->conds->state = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2813 | j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; |
| 2814 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2815 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2816 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2817 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2818 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2819 | case PP_ELSE: |
| 2820 | if (tline->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2821 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND, |
| 2822 | "trailing garbage after `%%else' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2823 | if (!istk->conds) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2824 | nasm_fatal(0, "`%%else: no matching `%%if'"); |
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: |
| 2827 | case COND_DONE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2828 | istk->conds->state = COND_ELSE_FALSE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2829 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2830 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 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 | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2834 | case COND_IF_FALSE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2835 | istk->conds->state = COND_ELSE_TRUE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2836 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2837 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2838 | case COND_ELSE_TRUE: |
| 2839 | case COND_ELSE_FALSE: |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2840 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2841 | "`%%else' after `%%else' ignored."); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2842 | istk->conds->state = COND_NEVER; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2843 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2844 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2845 | free_tlist(origline); |
| 2846 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2847 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2848 | case PP_ENDIF: |
| 2849 | if (tline->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2850 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND, |
| 2851 | "trailing garbage after `%%endif' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2852 | if (!istk->conds) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2853 | nasm_error(ERR_FATAL, "`%%endif': no matching `%%if'"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2854 | cond = istk->conds; |
| 2855 | istk->conds = cond->next; |
| 2856 | nasm_free(cond); |
| 2857 | if(istk->mstk) |
| 2858 | istk->mstk->condcnt --; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2859 | free_tlist(origline); |
| 2860 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2861 | |
H. Peter Anvin | db8f96e | 2009-07-15 09:07:29 -0400 | [diff] [blame] | 2862 | case PP_RMACRO: |
| 2863 | case PP_IRMACRO: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2864 | case PP_MACRO: |
| 2865 | case PP_IMACRO: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2866 | if (defining) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2867 | nasm_error(ERR_FATAL, "`%s': already defining a macro", |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2868 | pp_directives[i]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2869 | return DIRECTIVE_FOUND; |
| 2870 | } |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 2871 | defining = nasm_zalloc(sizeof(MMacro)); |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame^] | 2872 | defining->max_depth = ((i == PP_RMACRO) || (i == PP_IRMACRO)) |
| 2873 | ? nasm_limit[LIMIT_MACROS] : 0; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2874 | defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO); |
| 2875 | if (!parse_mmacro_spec(tline, defining, pp_directives[i])) { |
| 2876 | nasm_free(defining); |
| 2877 | defining = NULL; |
| 2878 | return DIRECTIVE_FOUND; |
| 2879 | } |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2880 | |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 2881 | src_get(&defining->xline, &defining->fname); |
| 2882 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2883 | mmac = (MMacro *) hash_findix(&mmacros, defining->name); |
| 2884 | while (mmac) { |
| 2885 | if (!strcmp(mmac->name, defining->name) && |
| 2886 | (mmac->nparam_min <= defining->nparam_max |
| 2887 | || defining->plus) |
| 2888 | && (defining->nparam_min <= mmac->nparam_max |
| 2889 | || mmac->plus)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2890 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2891 | "redefining multi-line macro `%s'", defining->name); |
| 2892 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2893 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2894 | mmac = mmac->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2895 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2896 | free_tlist(origline); |
| 2897 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2898 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2899 | case PP_ENDM: |
| 2900 | case PP_ENDMACRO: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2901 | if (! (defining && defining->name)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2902 | nasm_error(ERR_NONFATAL, "`%s': not defining a macro", tline->text); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2903 | return DIRECTIVE_FOUND; |
| 2904 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2905 | mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name); |
| 2906 | defining->next = *mmhead; |
| 2907 | *mmhead = defining; |
| 2908 | defining = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2909 | free_tlist(origline); |
| 2910 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2911 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2912 | case PP_EXITMACRO: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2913 | /* |
| 2914 | * We must search along istk->expansion until we hit a |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2915 | * macro-end marker for a macro with a name. Then we |
| 2916 | * bypass all lines between exitmacro and endmacro. |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2917 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2918 | list_for_each(l, istk->expansion) |
| 2919 | if (l->finishes && l->finishes->name) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2920 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2921 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2922 | if (l) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2923 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2924 | * Remove all conditional entries relative to this |
| 2925 | * macro invocation. (safe to do in this context) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2926 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2927 | for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) { |
| 2928 | cond = istk->conds; |
| 2929 | istk->conds = cond->next; |
| 2930 | nasm_free(cond); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2931 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2932 | istk->expansion = l; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2933 | } else { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2934 | nasm_error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2935 | } |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 2936 | free_tlist(origline); |
| 2937 | return DIRECTIVE_FOUND; |
| 2938 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2939 | case PP_UNMACRO: |
| 2940 | case PP_UNIMACRO: |
| 2941 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2942 | MMacro **mmac_p; |
| 2943 | MMacro spec; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2944 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2945 | spec.casesense = (i == PP_UNMACRO); |
| 2946 | if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) { |
| 2947 | return DIRECTIVE_FOUND; |
| 2948 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2949 | mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL); |
| 2950 | while (mmac_p && *mmac_p) { |
| 2951 | mmac = *mmac_p; |
| 2952 | if (mmac->casesense == spec.casesense && |
| 2953 | !mstrcmp(mmac->name, spec.name, spec.casesense) && |
| 2954 | mmac->nparam_min == spec.nparam_min && |
| 2955 | mmac->nparam_max == spec.nparam_max && |
| 2956 | mmac->plus == spec.plus) { |
| 2957 | *mmac_p = mmac->next; |
| 2958 | free_mmacro(mmac); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2959 | } else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2960 | mmac_p = &mmac->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2961 | } |
| 2962 | } |
| 2963 | free_tlist(origline); |
| 2964 | free_tlist(spec.dlist); |
| 2965 | return DIRECTIVE_FOUND; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2966 | } |
| 2967 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2968 | case PP_ROTATE: |
| 2969 | if (tline->next && tline->next->type == TOK_WHITESPACE) |
| 2970 | tline = tline->next; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2971 | if (!tline->next) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2972 | free_tlist(origline); |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2973 | nasm_error(ERR_NONFATAL, "`%%rotate' missing rotate count"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2974 | return DIRECTIVE_FOUND; |
| 2975 | } |
| 2976 | t = expand_smacro(tline->next); |
| 2977 | tline->next = NULL; |
| 2978 | free_tlist(origline); |
| 2979 | tline = t; |
| 2980 | tptr = &t; |
| 2981 | tokval.t_type = TOKEN_INVALID; |
| 2982 | evalresult = |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2983 | evaluate(ppscan, tptr, &tokval, NULL, pass, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2984 | free_tlist(tline); |
| 2985 | if (!evalresult) |
| 2986 | return DIRECTIVE_FOUND; |
| 2987 | if (tokval.t_type) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2988 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2989 | "trailing garbage after expression ignored"); |
| 2990 | if (!is_simple(evalresult)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2991 | nasm_error(ERR_NONFATAL, "non-constant value given to `%%rotate'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2992 | return DIRECTIVE_FOUND; |
| 2993 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2994 | mmac = istk->mstk; |
| 2995 | while (mmac && !mmac->name) /* avoid mistaking %reps for macros */ |
| 2996 | mmac = mmac->next_active; |
| 2997 | if (!mmac) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2998 | nasm_error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2999 | } else if (mmac->nparam == 0) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3000 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3001 | "`%%rotate' invoked within macro without parameters"); |
| 3002 | } else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3003 | int rotate = mmac->rotate + reloc_value(evalresult); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3004 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3005 | rotate %= (int)mmac->nparam; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3006 | if (rotate < 0) |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3007 | rotate += mmac->nparam; |
| 3008 | |
| 3009 | mmac->rotate = rotate; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3010 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3011 | return DIRECTIVE_FOUND; |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 3012 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3013 | case PP_REP: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 3014 | nolist = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3015 | do { |
| 3016 | tline = tline->next; |
| 3017 | } while (tok_type_(tline, TOK_WHITESPACE)); |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 3018 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3019 | if (tok_type_(tline, TOK_ID) && |
| 3020 | nasm_stricmp(tline->text, ".nolist") == 0) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 3021 | nolist = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3022 | do { |
| 3023 | tline = tline->next; |
| 3024 | } while (tok_type_(tline, TOK_WHITESPACE)); |
| 3025 | } |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 3026 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3027 | if (tline) { |
| 3028 | t = expand_smacro(tline); |
| 3029 | tptr = &t; |
| 3030 | tokval.t_type = TOKEN_INVALID; |
| 3031 | evalresult = |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3032 | evaluate(ppscan, tptr, &tokval, NULL, pass, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3033 | if (!evalresult) { |
| 3034 | free_tlist(origline); |
| 3035 | return DIRECTIVE_FOUND; |
| 3036 | } |
| 3037 | if (tokval.t_type) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3038 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3039 | "trailing garbage after expression ignored"); |
| 3040 | if (!is_simple(evalresult)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3041 | nasm_error(ERR_NONFATAL, "non-constant value given to `%%rep'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3042 | return DIRECTIVE_FOUND; |
| 3043 | } |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 3044 | count = reloc_value(evalresult); |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame^] | 3045 | if (count > nasm_limit[LIMIT_REP]) { |
| 3046 | nasm_error(ERR_NONFATAL, |
| 3047 | "`%%rep' count %"PRId64" exceeds limit (currently %d)", |
| 3048 | count, nasm_limit[LIMIT_REP]); |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 3049 | count = 0; |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame^] | 3050 | } else if (count < 0) { |
| 3051 | nasm_error(ERR_WARNING|ERR_PASS2|ERR_WARN_NEG_REP, |
| 3052 | "negative `%%rep' count: %"PRId64, count); |
| 3053 | count = 0; |
| 3054 | } else { |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 3055 | count++; |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame^] | 3056 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3057 | } else { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3058 | nasm_error(ERR_NONFATAL, "`%%rep' expects a repeat count"); |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 3059 | count = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3060 | } |
| 3061 | free_tlist(origline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3062 | |
| 3063 | tmp_defining = defining; |
| 3064 | defining = nasm_malloc(sizeof(MMacro)); |
| 3065 | defining->prev = NULL; |
| 3066 | defining->name = NULL; /* flags this macro as a %rep block */ |
| 3067 | defining->casesense = false; |
| 3068 | defining->plus = false; |
| 3069 | defining->nolist = nolist; |
| 3070 | defining->in_progress = count; |
| 3071 | defining->max_depth = 0; |
| 3072 | defining->nparam_min = defining->nparam_max = 0; |
| 3073 | defining->defaults = NULL; |
| 3074 | defining->dlist = NULL; |
| 3075 | defining->expansion = NULL; |
| 3076 | defining->next_active = istk->mstk; |
| 3077 | defining->rep_nest = tmp_defining; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3078 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3079 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3080 | case PP_ENDREP: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3081 | if (!defining || defining->name) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3082 | nasm_error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3083 | return DIRECTIVE_FOUND; |
| 3084 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3085 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3086 | /* |
| 3087 | * Now we have a "macro" defined - although it has no name |
| 3088 | * and we won't be entering it in the hash tables - we must |
| 3089 | * push a macro-end marker for it on to istk->expansion. |
| 3090 | * After that, it will take care of propagating itself (a |
| 3091 | * macro-end marker line for a macro which is really a %rep |
| 3092 | * block will cause the macro to be re-expanded, complete |
| 3093 | * with another macro-end marker to ensure the process |
| 3094 | * continues) until the whole expansion is forcibly removed |
| 3095 | * from istk->expansion by a %exitrep. |
| 3096 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3097 | l = nasm_malloc(sizeof(Line)); |
| 3098 | l->next = istk->expansion; |
| 3099 | l->finishes = defining; |
| 3100 | l->first = NULL; |
| 3101 | istk->expansion = l; |
| 3102 | |
| 3103 | istk->mstk = defining; |
| 3104 | |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 3105 | lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3106 | tmp_defining = defining; |
| 3107 | defining = defining->rep_nest; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3108 | free_tlist(origline); |
| 3109 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3110 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3111 | case PP_EXITREP: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3112 | /* |
| 3113 | * We must search along istk->expansion until we hit a |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3114 | * macro-end marker for a macro with no name. Then we set |
| 3115 | * its `in_progress' flag to 0. |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3116 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3117 | list_for_each(l, istk->expansion) |
| 3118 | if (l->finishes && !l->finishes->name) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3119 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3120 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3121 | if (l) |
| 3122 | l->finishes->in_progress = 1; |
| 3123 | else |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3124 | nasm_error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3125 | free_tlist(origline); |
| 3126 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3127 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3128 | case PP_XDEFINE: |
| 3129 | case PP_IXDEFINE: |
| 3130 | case PP_DEFINE: |
| 3131 | case PP_IDEFINE: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3132 | casesense = (i == PP_DEFINE || i == PP_XDEFINE); |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3133 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3134 | tline = tline->next; |
| 3135 | skip_white_(tline); |
| 3136 | tline = expand_id(tline); |
| 3137 | if (!tline || (tline->type != TOK_ID && |
| 3138 | (tline->type != TOK_PREPROC_ID || |
| 3139 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3140 | nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3141 | pp_directives[i]); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3142 | free_tlist(origline); |
| 3143 | return DIRECTIVE_FOUND; |
| 3144 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3145 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3146 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3147 | last = tline; |
| 3148 | param_start = tline = tline->next; |
| 3149 | nparam = 0; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3150 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3151 | /* Expand the macro definition now for %xdefine and %ixdefine */ |
| 3152 | if ((i == PP_XDEFINE) || (i == PP_IXDEFINE)) |
| 3153 | tline = expand_smacro(tline); |
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 | if (tok_is_(tline, "(")) { |
| 3156 | /* |
| 3157 | * This macro has parameters. |
| 3158 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3159 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3160 | tline = tline->next; |
| 3161 | while (1) { |
| 3162 | skip_white_(tline); |
| 3163 | if (!tline) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3164 | nasm_error(ERR_NONFATAL, "parameter identifier expected"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3165 | free_tlist(origline); |
| 3166 | return DIRECTIVE_FOUND; |
| 3167 | } |
| 3168 | if (tline->type != TOK_ID) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3169 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3170 | "`%s': parameter identifier expected", |
| 3171 | tline->text); |
| 3172 | free_tlist(origline); |
| 3173 | return DIRECTIVE_FOUND; |
| 3174 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3175 | tline->type = TOK_SMAC_PARAM + nparam++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3176 | tline = tline->next; |
| 3177 | skip_white_(tline); |
| 3178 | if (tok_is_(tline, ",")) { |
| 3179 | tline = tline->next; |
H. Peter Anvin | ca348b6 | 2008-07-23 10:49:26 -0400 | [diff] [blame] | 3180 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3181 | if (!tok_is_(tline, ")")) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3182 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3183 | "`)' expected to terminate macro template"); |
| 3184 | free_tlist(origline); |
| 3185 | return DIRECTIVE_FOUND; |
| 3186 | } |
| 3187 | break; |
| 3188 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3189 | } |
| 3190 | last = tline; |
| 3191 | tline = tline->next; |
| 3192 | } |
| 3193 | if (tok_type_(tline, TOK_WHITESPACE)) |
| 3194 | last = tline, tline = tline->next; |
| 3195 | macro_start = NULL; |
| 3196 | last->next = NULL; |
| 3197 | t = tline; |
| 3198 | while (t) { |
| 3199 | if (t->type == TOK_ID) { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3200 | list_for_each(tt, param_start) |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3201 | if (tt->type >= TOK_SMAC_PARAM && |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3202 | !strcmp(tt->text, t->text)) |
| 3203 | t->type = tt->type; |
| 3204 | } |
| 3205 | tt = t->next; |
| 3206 | t->next = macro_start; |
| 3207 | macro_start = t; |
| 3208 | t = tt; |
| 3209 | } |
| 3210 | /* |
| 3211 | * Good. We now have a macro name, a parameter count, and a |
| 3212 | * token list (in reverse order) for an expansion. We ought |
| 3213 | * to be OK just to create an SMacro, store it, and let |
| 3214 | * free_tlist have the rest of the line (which we have |
| 3215 | * carefully re-terminated after chopping off the expansion |
| 3216 | * from the end). |
| 3217 | */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 3218 | define_smacro(ctx, mname, casesense, nparam, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3219 | free_tlist(origline); |
| 3220 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3221 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3222 | case PP_UNDEF: |
| 3223 | tline = tline->next; |
| 3224 | skip_white_(tline); |
| 3225 | tline = expand_id(tline); |
| 3226 | if (!tline || (tline->type != TOK_ID && |
| 3227 | (tline->type != TOK_PREPROC_ID || |
| 3228 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3229 | nasm_error(ERR_NONFATAL, "`%%undef' expects a macro identifier"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3230 | free_tlist(origline); |
| 3231 | return DIRECTIVE_FOUND; |
| 3232 | } |
| 3233 | if (tline->next) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3234 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3235 | "trailing garbage after macro name ignored"); |
| 3236 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3237 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3238 | /* Find the context that symbol belongs to */ |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3239 | ctx = get_ctx(tline->text, &mname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3240 | undef_smacro(ctx, mname); |
| 3241 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3242 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3243 | |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3244 | case PP_DEFSTR: |
| 3245 | case PP_IDEFSTR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3246 | casesense = (i == PP_DEFSTR); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3247 | |
| 3248 | tline = tline->next; |
| 3249 | skip_white_(tline); |
| 3250 | tline = expand_id(tline); |
| 3251 | if (!tline || (tline->type != TOK_ID && |
| 3252 | (tline->type != TOK_PREPROC_ID || |
| 3253 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3254 | nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3255 | pp_directives[i]); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3256 | free_tlist(origline); |
| 3257 | return DIRECTIVE_FOUND; |
| 3258 | } |
| 3259 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3260 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3261 | last = tline; |
| 3262 | tline = expand_smacro(tline->next); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3263 | last->next = NULL; |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3264 | |
| 3265 | while (tok_type_(tline, TOK_WHITESPACE)) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3266 | tline = delete_Token(tline); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3267 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3268 | p = detoken(tline, false); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3269 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3270 | macro_start->next = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3271 | macro_start->text = nasm_quote(p, strlen(p)); |
| 3272 | macro_start->type = TOK_STRING; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3273 | macro_start->a.mac = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3274 | nasm_free(p); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3275 | |
| 3276 | /* |
| 3277 | * We now have a macro name, an implicit parameter count of |
| 3278 | * zero, and a string token to use as an expansion. Create |
| 3279 | * and store an SMacro. |
| 3280 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3281 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3282 | free_tlist(origline); |
| 3283 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3284 | |
H. Peter Anvin | 2f55bda | 2009-07-14 15:04:04 -0400 | [diff] [blame] | 3285 | case PP_DEFTOK: |
| 3286 | case PP_IDEFTOK: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3287 | casesense = (i == PP_DEFTOK); |
| 3288 | |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3289 | tline = tline->next; |
| 3290 | skip_white_(tline); |
| 3291 | tline = expand_id(tline); |
| 3292 | if (!tline || (tline->type != TOK_ID && |
| 3293 | (tline->type != TOK_PREPROC_ID || |
| 3294 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3295 | nasm_error(ERR_NONFATAL, |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3296 | "`%s' expects a macro identifier as first parameter", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3297 | pp_directives[i]); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3298 | free_tlist(origline); |
| 3299 | return DIRECTIVE_FOUND; |
| 3300 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3301 | ctx = get_ctx(tline->text, &mname); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3302 | last = tline; |
| 3303 | tline = expand_smacro(tline->next); |
| 3304 | last->next = NULL; |
| 3305 | |
| 3306 | t = tline; |
| 3307 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3308 | t = t->next; |
| 3309 | /* t should now point to the string */ |
Cyrill Gorcunov | 6908e58 | 2010-09-06 19:36:15 +0400 | [diff] [blame] | 3310 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3311 | nasm_error(ERR_NONFATAL, |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3312 | "`%s` requires string as second parameter", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3313 | pp_directives[i]); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3314 | free_tlist(tline); |
| 3315 | free_tlist(origline); |
| 3316 | return DIRECTIVE_FOUND; |
| 3317 | } |
| 3318 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 3319 | /* |
| 3320 | * Convert the string to a token stream. Note that smacros |
| 3321 | * are stored with the token stream reversed, so we have to |
| 3322 | * reverse the output of tokenize(). |
| 3323 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3324 | nasm_unquote_cstr(t->text, i); |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 3325 | macro_start = reverse_tokens(tokenize(t->text)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3326 | |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3327 | /* |
| 3328 | * We now have a macro name, an implicit parameter count of |
| 3329 | * zero, and a numeric token to use as an expansion. Create |
| 3330 | * and store an SMacro. |
| 3331 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3332 | define_smacro(ctx, mname, casesense, 0, macro_start); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3333 | free_tlist(tline); |
| 3334 | free_tlist(origline); |
| 3335 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3336 | |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3337 | case PP_PATHSEARCH: |
| 3338 | { |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 3339 | const char *found_path; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3340 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3341 | casesense = true; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3342 | |
| 3343 | tline = tline->next; |
| 3344 | skip_white_(tline); |
| 3345 | tline = expand_id(tline); |
| 3346 | if (!tline || (tline->type != TOK_ID && |
| 3347 | (tline->type != TOK_PREPROC_ID || |
| 3348 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3349 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3350 | "`%%pathsearch' expects a macro identifier as first parameter"); |
| 3351 | free_tlist(origline); |
| 3352 | return DIRECTIVE_FOUND; |
| 3353 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3354 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3355 | last = tline; |
| 3356 | tline = expand_smacro(tline->next); |
| 3357 | last->next = NULL; |
| 3358 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3359 | t = tline; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3360 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3361 | t = t->next; |
| 3362 | |
| 3363 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3364 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3365 | nasm_error(ERR_NONFATAL, "`%%pathsearch' expects a file name"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3366 | free_tlist(tline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3367 | free_tlist(origline); |
| 3368 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 3369 | } |
| 3370 | if (t->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3371 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3372 | "trailing garbage after `%%pathsearch' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3373 | p = t->text; |
H. Peter Anvin | 427cc91 | 2008-06-01 21:43:03 -0700 | [diff] [blame] | 3374 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3375 | nasm_unquote(p, NULL); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3376 | |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 3377 | inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY); |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 3378 | if (!found_path) |
| 3379 | found_path = p; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3380 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3381 | macro_start->next = NULL; |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 3382 | macro_start->text = nasm_quote(found_path, strlen(found_path)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3383 | macro_start->type = TOK_STRING; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3384 | macro_start->a.mac = NULL; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3385 | |
| 3386 | /* |
| 3387 | * We now have a macro name, an implicit parameter count of |
| 3388 | * zero, and a string token to use as an expansion. Create |
| 3389 | * and store an SMacro. |
| 3390 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3391 | define_smacro(ctx, mname, casesense, 0, macro_start); |
| 3392 | free_tlist(tline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3393 | free_tlist(origline); |
| 3394 | return DIRECTIVE_FOUND; |
| 3395 | } |
| 3396 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3397 | case PP_STRLEN: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3398 | casesense = true; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 3399 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3400 | tline = tline->next; |
| 3401 | skip_white_(tline); |
| 3402 | tline = expand_id(tline); |
| 3403 | if (!tline || (tline->type != TOK_ID && |
| 3404 | (tline->type != TOK_PREPROC_ID || |
| 3405 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3406 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3407 | "`%%strlen' expects a macro identifier as first parameter"); |
| 3408 | free_tlist(origline); |
| 3409 | return DIRECTIVE_FOUND; |
| 3410 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3411 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3412 | last = tline; |
| 3413 | tline = expand_smacro(tline->next); |
| 3414 | last->next = NULL; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3415 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3416 | t = tline; |
| 3417 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3418 | t = t->next; |
| 3419 | /* t should now point to the string */ |
Cyrill Gorcunov | 4e1d5ab | 2010-07-23 18:51:51 +0400 | [diff] [blame] | 3420 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3421 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3422 | "`%%strlen` requires string as second parameter"); |
| 3423 | free_tlist(tline); |
| 3424 | free_tlist(origline); |
| 3425 | return DIRECTIVE_FOUND; |
| 3426 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3427 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3428 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3429 | macro_start->next = NULL; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 3430 | make_tok_num(macro_start, nasm_unquote(t->text, NULL)); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3431 | macro_start->a.mac = NULL; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3432 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3433 | /* |
| 3434 | * We now have a macro name, an implicit parameter count of |
| 3435 | * zero, and a numeric token to use as an expansion. Create |
| 3436 | * and store an SMacro. |
| 3437 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3438 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3439 | free_tlist(tline); |
| 3440 | free_tlist(origline); |
| 3441 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3442 | |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3443 | case PP_STRCAT: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3444 | casesense = true; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3445 | |
| 3446 | tline = tline->next; |
| 3447 | skip_white_(tline); |
| 3448 | tline = expand_id(tline); |
| 3449 | if (!tline || (tline->type != TOK_ID && |
| 3450 | (tline->type != TOK_PREPROC_ID || |
| 3451 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3452 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3453 | "`%%strcat' expects a macro identifier as first parameter"); |
| 3454 | free_tlist(origline); |
| 3455 | return DIRECTIVE_FOUND; |
| 3456 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3457 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3458 | last = tline; |
| 3459 | tline = expand_smacro(tline->next); |
| 3460 | last->next = NULL; |
| 3461 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3462 | len = 0; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3463 | list_for_each(t, tline) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3464 | switch (t->type) { |
| 3465 | case TOK_WHITESPACE: |
| 3466 | break; |
| 3467 | case TOK_STRING: |
| 3468 | len += t->a.len = nasm_unquote(t->text, NULL); |
| 3469 | break; |
| 3470 | case TOK_OTHER: |
| 3471 | if (!strcmp(t->text, ",")) /* permit comma separators */ |
| 3472 | break; |
| 3473 | /* else fall through */ |
| 3474 | default: |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3475 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3476 | "non-string passed to `%%strcat' (%d)", t->type); |
| 3477 | free_tlist(tline); |
| 3478 | free_tlist(origline); |
| 3479 | return DIRECTIVE_FOUND; |
| 3480 | } |
| 3481 | } |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3482 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3483 | p = pp = nasm_malloc(len); |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3484 | list_for_each(t, tline) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3485 | if (t->type == TOK_STRING) { |
| 3486 | memcpy(p, t->text, t->a.len); |
| 3487 | p += t->a.len; |
| 3488 | } |
| 3489 | } |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3490 | |
| 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 | macro_start = new_Token(NULL, TOK_STRING, NULL, 0); |
| 3497 | macro_start->text = nasm_quote(pp, len); |
| 3498 | nasm_free(pp); |
| 3499 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3500 | free_tlist(tline); |
| 3501 | free_tlist(origline); |
| 3502 | return DIRECTIVE_FOUND; |
| 3503 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3504 | case PP_SUBSTR: |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3505 | { |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3506 | int64_t start, count; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3507 | size_t len; |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 3508 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3509 | casesense = true; |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3510 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3511 | tline = tline->next; |
| 3512 | skip_white_(tline); |
| 3513 | tline = expand_id(tline); |
| 3514 | if (!tline || (tline->type != TOK_ID && |
| 3515 | (tline->type != TOK_PREPROC_ID || |
| 3516 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3517 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3518 | "`%%substr' expects a macro identifier as first parameter"); |
| 3519 | free_tlist(origline); |
| 3520 | return DIRECTIVE_FOUND; |
| 3521 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3522 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3523 | last = tline; |
| 3524 | tline = expand_smacro(tline->next); |
| 3525 | last->next = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3526 | |
Cyrill Gorcunov | 35519d6 | 2010-09-06 23:49:52 +0400 | [diff] [blame] | 3527 | if (tline) /* skip expanded id */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3528 | t = tline->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3529 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3530 | t = t->next; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3531 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3532 | /* t should now point to the string */ |
Cyrill Gorcunov | 35519d6 | 2010-09-06 23:49:52 +0400 | [diff] [blame] | 3533 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3534 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3535 | "`%%substr` requires string as second parameter"); |
| 3536 | free_tlist(tline); |
| 3537 | free_tlist(origline); |
| 3538 | return DIRECTIVE_FOUND; |
| 3539 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3540 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3541 | tt = t->next; |
| 3542 | tptr = &tt; |
| 3543 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3544 | evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3545 | if (!evalresult) { |
| 3546 | free_tlist(tline); |
| 3547 | free_tlist(origline); |
| 3548 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3549 | } else if (!is_simple(evalresult)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3550 | nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3551 | free_tlist(tline); |
| 3552 | free_tlist(origline); |
| 3553 | return DIRECTIVE_FOUND; |
| 3554 | } |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3555 | start = evalresult->value - 1; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3556 | |
| 3557 | while (tok_type_(tt, TOK_WHITESPACE)) |
| 3558 | tt = tt->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3559 | if (!tt) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3560 | count = 1; /* Backwards compatibility: one character */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3561 | } else { |
| 3562 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3563 | evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3564 | if (!evalresult) { |
| 3565 | free_tlist(tline); |
| 3566 | free_tlist(origline); |
| 3567 | return DIRECTIVE_FOUND; |
| 3568 | } else if (!is_simple(evalresult)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3569 | nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3570 | free_tlist(tline); |
| 3571 | free_tlist(origline); |
| 3572 | return DIRECTIVE_FOUND; |
| 3573 | } |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3574 | count = evalresult->value; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3575 | } |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3576 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3577 | len = nasm_unquote(t->text, NULL); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3578 | |
Cyrill Gorcunov | cff031e | 2010-09-07 20:31:11 +0400 | [diff] [blame] | 3579 | /* make start and count being in range */ |
| 3580 | if (start < 0) |
| 3581 | start = 0; |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3582 | if (count < 0) |
| 3583 | count = len + count + 1 - start; |
| 3584 | if (start + count > (int64_t)len) |
Cyrill Gorcunov | cff031e | 2010-09-07 20:31:11 +0400 | [diff] [blame] | 3585 | count = len - start; |
| 3586 | if (!len || count < 0 || start >=(int64_t)len) |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3587 | start = -1, count = 0; /* empty string */ |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3588 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3589 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3590 | macro_start->next = NULL; |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3591 | macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3592 | macro_start->type = TOK_STRING; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3593 | macro_start->a.mac = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3594 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3595 | /* |
| 3596 | * We now have a macro name, an implicit parameter count of |
| 3597 | * zero, and a numeric token to use as an expansion. Create |
| 3598 | * and store an SMacro. |
| 3599 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3600 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3601 | free_tlist(tline); |
| 3602 | free_tlist(origline); |
| 3603 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3604 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3605 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3606 | case PP_ASSIGN: |
| 3607 | case PP_IASSIGN: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3608 | casesense = (i == PP_ASSIGN); |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3609 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3610 | tline = tline->next; |
| 3611 | skip_white_(tline); |
| 3612 | tline = expand_id(tline); |
| 3613 | if (!tline || (tline->type != TOK_ID && |
| 3614 | (tline->type != TOK_PREPROC_ID || |
| 3615 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3616 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3617 | "`%%%sassign' expects a macro identifier", |
| 3618 | (i == PP_IASSIGN ? "i" : "")); |
| 3619 | free_tlist(origline); |
| 3620 | return DIRECTIVE_FOUND; |
| 3621 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3622 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3623 | last = tline; |
| 3624 | tline = expand_smacro(tline->next); |
| 3625 | last->next = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3626 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3627 | t = tline; |
| 3628 | tptr = &t; |
| 3629 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3630 | evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3631 | free_tlist(tline); |
| 3632 | if (!evalresult) { |
| 3633 | free_tlist(origline); |
| 3634 | return DIRECTIVE_FOUND; |
| 3635 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3636 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3637 | if (tokval.t_type) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3638 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3639 | "trailing garbage after expression ignored"); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3640 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3641 | if (!is_simple(evalresult)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3642 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3643 | "non-constant value given to `%%%sassign'", |
| 3644 | (i == PP_IASSIGN ? "i" : "")); |
| 3645 | free_tlist(origline); |
| 3646 | return DIRECTIVE_FOUND; |
| 3647 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3648 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3649 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3650 | macro_start->next = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3651 | make_tok_num(macro_start, reloc_value(evalresult)); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3652 | macro_start->a.mac = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3653 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3654 | /* |
| 3655 | * We now have a macro name, an implicit parameter count of |
| 3656 | * zero, and a numeric token to use as an expansion. Create |
| 3657 | * and store an SMacro. |
| 3658 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3659 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3660 | free_tlist(origline); |
| 3661 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3662 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3663 | case PP_LINE: |
| 3664 | /* |
| 3665 | * Syntax is `%line nnn[+mmm] [filename]' |
| 3666 | */ |
| 3667 | tline = tline->next; |
| 3668 | skip_white_(tline); |
| 3669 | if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3670 | nasm_error(ERR_NONFATAL, "`%%line' expects line number"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3671 | free_tlist(origline); |
| 3672 | return DIRECTIVE_FOUND; |
| 3673 | } |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 3674 | k = readnum(tline->text, &err); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3675 | m = 1; |
| 3676 | tline = tline->next; |
| 3677 | if (tok_is_(tline, "+")) { |
| 3678 | tline = tline->next; |
| 3679 | if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3680 | nasm_error(ERR_NONFATAL, "`%%line' expects line increment"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3681 | free_tlist(origline); |
| 3682 | return DIRECTIVE_FOUND; |
| 3683 | } |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 3684 | m = readnum(tline->text, &err); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3685 | tline = tline->next; |
| 3686 | } |
| 3687 | skip_white_(tline); |
| 3688 | src_set_linnum(k); |
| 3689 | istk->lineinc = m; |
| 3690 | if (tline) { |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 3691 | char *fname = detoken(tline, false); |
| 3692 | src_set_fname(fname); |
| 3693 | nasm_free(fname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3694 | } |
| 3695 | free_tlist(origline); |
| 3696 | return DIRECTIVE_FOUND; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3697 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3698 | default: |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3699 | nasm_error(ERR_FATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3700 | "preprocessor directive `%s' not yet implemented", |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 3701 | pp_directives[i]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3702 | return DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3703 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3704 | } |
| 3705 | |
| 3706 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3707 | * Ensure that a macro parameter contains a condition code and |
| 3708 | * nothing else. Return the condition code index if so, or -1 |
| 3709 | * otherwise. |
| 3710 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3711 | static int find_cc(Token * t) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3712 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3713 | Token *tt; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3714 | |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3715 | if (!t) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3716 | return -1; /* Probably a %+ without a space */ |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3717 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3718 | skip_white_(t); |
Cyrill Gorcunov | 7524cfd | 2017-10-22 19:01:16 +0300 | [diff] [blame] | 3719 | if (!t) |
| 3720 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3721 | if (t->type != TOK_ID) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3722 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3723 | tt = t->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3724 | skip_white_(tt); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3725 | if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ","))) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3726 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3727 | |
Cyrill Gorcunov | 1945639 | 2012-05-02 00:18:56 +0400 | [diff] [blame] | 3728 | return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3729 | } |
| 3730 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3731 | /* |
| 3732 | * This routines walks over tokens strem and hadnles tokens |
| 3733 | * pasting, if @handle_explicit passed then explicit pasting |
| 3734 | * term is handled, otherwise -- implicit pastings only. |
| 3735 | */ |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 3736 | static bool paste_tokens(Token **head, const struct tokseq_match *m, |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3737 | size_t mnum, bool handle_explicit) |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3738 | { |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3739 | Token *tok, *next, **prev_next, **prev_nonspace; |
| 3740 | bool pasted = false; |
| 3741 | char *buf, *p; |
| 3742 | size_t len, i; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3743 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3744 | /* |
| 3745 | * The last token before pasting. We need it |
| 3746 | * to be able to connect new handled tokens. |
| 3747 | * In other words if there were a tokens stream |
| 3748 | * |
| 3749 | * A -> B -> C -> D |
| 3750 | * |
| 3751 | * and we've joined tokens B and C, the resulting |
| 3752 | * stream should be |
| 3753 | * |
| 3754 | * A -> BC -> D |
| 3755 | */ |
| 3756 | tok = *head; |
| 3757 | prev_next = NULL; |
| 3758 | |
| 3759 | if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE)) |
| 3760 | prev_nonspace = head; |
| 3761 | else |
| 3762 | prev_nonspace = NULL; |
| 3763 | |
| 3764 | while (tok && (next = tok->next)) { |
| 3765 | |
| 3766 | switch (tok->type) { |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3767 | case TOK_WHITESPACE: |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3768 | /* Zap redundant whitespaces */ |
| 3769 | while (tok_type_(next, TOK_WHITESPACE)) |
| 3770 | next = delete_Token(next); |
| 3771 | tok->next = next; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3772 | break; |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3773 | |
| 3774 | case TOK_PASTE: |
| 3775 | /* Explicit pasting */ |
| 3776 | if (!handle_explicit) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3777 | break; |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3778 | next = delete_Token(tok); |
| 3779 | |
| 3780 | while (tok_type_(next, TOK_WHITESPACE)) |
| 3781 | next = delete_Token(next); |
| 3782 | |
| 3783 | if (!pasted) |
| 3784 | pasted = true; |
| 3785 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3786 | /* Left pasting token is start of line */ |
| 3787 | if (!prev_nonspace) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3788 | nasm_error(ERR_FATAL, "No lvalue found on pasting"); |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3789 | |
Cyrill Gorcunov | 8b5c9fb | 2013-02-04 01:24:54 +0400 | [diff] [blame] | 3790 | /* |
| 3791 | * No ending token, this might happen in two |
| 3792 | * cases |
| 3793 | * |
| 3794 | * 1) There indeed no right token at all |
| 3795 | * 2) There is a bare "%define ID" statement, |
| 3796 | * and @ID does expand to whitespace. |
| 3797 | * |
| 3798 | * So technically we need to do a grammar analysis |
| 3799 | * in another stage of parsing, but for now lets don't |
| 3800 | * change the behaviour people used to. Simply allow |
| 3801 | * whitespace after paste token. |
| 3802 | */ |
| 3803 | if (!next) { |
| 3804 | /* |
| 3805 | * Zap ending space tokens and that's all. |
| 3806 | */ |
| 3807 | tok = (*prev_nonspace)->next; |
| 3808 | while (tok_type_(tok, TOK_WHITESPACE)) |
| 3809 | tok = delete_Token(tok); |
| 3810 | tok = *prev_nonspace; |
| 3811 | tok->next = NULL; |
| 3812 | break; |
| 3813 | } |
| 3814 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3815 | tok = *prev_nonspace; |
| 3816 | while (tok_type_(tok, TOK_WHITESPACE)) |
| 3817 | tok = delete_Token(tok); |
| 3818 | len = strlen(tok->text); |
| 3819 | len += strlen(next->text); |
| 3820 | |
| 3821 | p = buf = nasm_malloc(len + 1); |
| 3822 | strcpy(p, tok->text); |
| 3823 | p = strchr(p, '\0'); |
| 3824 | strcpy(p, next->text); |
| 3825 | |
| 3826 | delete_Token(tok); |
| 3827 | |
| 3828 | tok = tokenize(buf); |
| 3829 | nasm_free(buf); |
| 3830 | |
| 3831 | *prev_nonspace = tok; |
| 3832 | while (tok && tok->next) |
| 3833 | tok = tok->next; |
| 3834 | |
| 3835 | tok->next = delete_Token(next); |
| 3836 | |
| 3837 | /* Restart from pasted tokens head */ |
| 3838 | tok = *prev_nonspace; |
| 3839 | break; |
| 3840 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3841 | default: |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3842 | /* implicit pasting */ |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 3843 | for (i = 0; i < mnum; i++) { |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3844 | if (!(PP_CONCAT_MATCH(tok, m[i].mask_head))) |
| 3845 | continue; |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 3846 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3847 | len = 0; |
| 3848 | while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) { |
| 3849 | len += strlen(next->text); |
| 3850 | next = next->next; |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 3851 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3852 | |
Cyrill Gorcunov | 6f8109e | 2017-10-22 21:26:36 +0300 | [diff] [blame] | 3853 | /* No match or no text to process */ |
| 3854 | if (tok == next || len == 0) |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3855 | break; |
| 3856 | |
| 3857 | len += strlen(tok->text); |
| 3858 | p = buf = nasm_malloc(len + 1); |
| 3859 | |
Adam Majer | 1a06943 | 2017-07-25 11:12:35 +0200 | [diff] [blame] | 3860 | strcpy(p, tok->text); |
| 3861 | p = strchr(p, '\0'); |
| 3862 | tok = delete_Token(tok); |
| 3863 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3864 | while (tok != next) { |
Adam Majer | 1a06943 | 2017-07-25 11:12:35 +0200 | [diff] [blame] | 3865 | if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) { |
| 3866 | strcpy(p, tok->text); |
| 3867 | p = strchr(p, '\0'); |
| 3868 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3869 | tok = delete_Token(tok); |
| 3870 | } |
| 3871 | |
| 3872 | tok = tokenize(buf); |
| 3873 | nasm_free(buf); |
| 3874 | |
| 3875 | if (prev_next) |
| 3876 | *prev_next = tok; |
| 3877 | else |
| 3878 | *head = tok; |
| 3879 | |
| 3880 | /* |
| 3881 | * Connect pasted into original stream, |
| 3882 | * ie A -> new-tokens -> B |
| 3883 | */ |
| 3884 | while (tok && tok->next) |
| 3885 | tok = tok->next; |
| 3886 | tok->next = next; |
| 3887 | |
| 3888 | if (!pasted) |
| 3889 | pasted = true; |
| 3890 | |
| 3891 | /* Restart from pasted tokens head */ |
| 3892 | tok = prev_next ? *prev_next : *head; |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 3893 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3894 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3895 | break; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3896 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3897 | |
| 3898 | prev_next = &tok->next; |
| 3899 | |
| 3900 | if (tok->next && |
| 3901 | !tok_type_(tok->next, TOK_WHITESPACE) && |
| 3902 | !tok_type_(tok->next, TOK_PASTE)) |
| 3903 | prev_nonspace = prev_next; |
| 3904 | |
| 3905 | tok = tok->next; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3906 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3907 | |
| 3908 | return pasted; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3909 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3910 | |
| 3911 | /* |
| 3912 | * expands to a list of tokens from %{x:y} |
| 3913 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3914 | static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3915 | { |
| 3916 | Token *t = tline, **tt, *tm, *head; |
| 3917 | char *pos; |
| 3918 | int fst, lst, j, i; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3919 | |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3920 | pos = strchr(tline->text, ':'); |
| 3921 | nasm_assert(pos); |
| 3922 | |
| 3923 | lst = atoi(pos + 1); |
| 3924 | fst = atoi(tline->text + 1); |
| 3925 | |
| 3926 | /* |
| 3927 | * only macros params are accounted so |
| 3928 | * if someone passes %0 -- we reject such |
| 3929 | * value(s) |
| 3930 | */ |
| 3931 | if (lst == 0 || fst == 0) |
| 3932 | goto err; |
| 3933 | |
| 3934 | /* the values should be sane */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3935 | if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) || |
| 3936 | (lst > (int)mac->nparam || lst < (-(int)mac->nparam))) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3937 | goto err; |
| 3938 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3939 | fst = fst < 0 ? fst + (int)mac->nparam + 1: fst; |
| 3940 | lst = lst < 0 ? lst + (int)mac->nparam + 1: lst; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3941 | |
| 3942 | /* counted from zero */ |
| 3943 | fst--, lst--; |
| 3944 | |
| 3945 | /* |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3946 | * It will be at least one token. Note we |
| 3947 | * need to scan params until separator, otherwise |
| 3948 | * only first token will be passed. |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3949 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3950 | tm = mac->params[(fst + mac->rotate) % mac->nparam]; |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3951 | head = new_Token(NULL, tm->type, tm->text, 0); |
| 3952 | tt = &head->next, tm = tm->next; |
| 3953 | while (tok_isnt_(tm, ",")) { |
| 3954 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 3955 | *tt = t, tt = &t->next, tm = tm->next; |
| 3956 | } |
| 3957 | |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3958 | if (fst < lst) { |
| 3959 | for (i = fst + 1; i <= lst; i++) { |
| 3960 | t = new_Token(NULL, TOK_OTHER, ",", 0); |
| 3961 | *tt = t, tt = &t->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3962 | j = (i + mac->rotate) % mac->nparam; |
| 3963 | tm = mac->params[j]; |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3964 | while (tok_isnt_(tm, ",")) { |
| 3965 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 3966 | *tt = t, tt = &t->next, tm = tm->next; |
| 3967 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3968 | } |
| 3969 | } else { |
| 3970 | for (i = fst - 1; i >= lst; i--) { |
| 3971 | t = new_Token(NULL, TOK_OTHER, ",", 0); |
| 3972 | *tt = t, tt = &t->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3973 | j = (i + mac->rotate) % mac->nparam; |
| 3974 | tm = mac->params[j]; |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3975 | while (tok_isnt_(tm, ",")) { |
| 3976 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 3977 | *tt = t, tt = &t->next, tm = tm->next; |
| 3978 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3979 | } |
| 3980 | } |
| 3981 | |
| 3982 | *last = tt; |
| 3983 | return head; |
| 3984 | |
| 3985 | err: |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3986 | nasm_error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range", |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3987 | &tline->text[1]); |
| 3988 | return tline; |
| 3989 | } |
| 3990 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3991 | /* |
| 3992 | * Expand MMacro-local things: parameter references (%0, %n, %+n, |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 3993 | * %-n) and MMacro-local identifiers (%%foo) as well as |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3994 | * macro indirection (%[...]) and range (%{..:..}). |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3995 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3996 | static Token *expand_mmac_params(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3997 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3998 | Token *t, *tt, **tail, *thead; |
H. Peter Anvin | 6125b62 | 2009-04-08 14:02:25 -0700 | [diff] [blame] | 3999 | bool changed = false; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4000 | char *pos; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4001 | |
| 4002 | tail = &thead; |
| 4003 | thead = NULL; |
| 4004 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4005 | while (tline) { |
| 4006 | if (tline->type == TOK_PREPROC_ID && |
Cyrill Gorcunov | ca61119 | 2010-06-04 09:22:12 +0400 | [diff] [blame] | 4007 | (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) || |
| 4008 | (tline->text[1] >= '0' && tline->text[1] <= '9') || |
| 4009 | tline->text[1] == '%')) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4010 | char *text = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4011 | int type = 0, cc; /* type = 0 to placate optimisers */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4012 | char tmpbuf[30]; |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 4013 | unsigned int n; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4014 | int i; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4015 | MMacro *mac; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4016 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4017 | t = tline; |
| 4018 | tline = tline->next; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4019 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4020 | mac = istk->mstk; |
| 4021 | while (mac && !mac->name) /* avoid mistaking %reps for macros */ |
| 4022 | mac = mac->next_active; |
| 4023 | if (!mac) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4024 | nasm_error(ERR_NONFATAL, "`%s': not in a macro call", t->text); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4025 | } else { |
| 4026 | pos = strchr(t->text, ':'); |
| 4027 | if (!pos) { |
| 4028 | switch (t->text[1]) { |
| 4029 | /* |
| 4030 | * We have to make a substitution of one of the |
| 4031 | * forms %1, %-1, %+1, %%foo, %0. |
| 4032 | */ |
| 4033 | case '0': |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4034 | type = TOK_NUMBER; |
| 4035 | snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam); |
| 4036 | text = nasm_strdup(tmpbuf); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4037 | break; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4038 | case '%': |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4039 | type = TOK_ID; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4040 | snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".", |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4041 | mac->unique); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4042 | text = nasm_strcat(tmpbuf, t->text + 2); |
| 4043 | break; |
| 4044 | case '-': |
| 4045 | n = atoi(t->text + 2) - 1; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4046 | if (n >= mac->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4047 | tt = NULL; |
| 4048 | else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4049 | if (mac->nparam > 1) |
| 4050 | n = (n + mac->rotate) % mac->nparam; |
| 4051 | tt = mac->params[n]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4052 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4053 | cc = find_cc(tt); |
| 4054 | if (cc == -1) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4055 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4056 | "macro parameter %d is not a condition code", |
| 4057 | n + 1); |
| 4058 | text = NULL; |
| 4059 | } else { |
| 4060 | type = TOK_ID; |
| 4061 | if (inverse_ccs[cc] == -1) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4062 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4063 | "condition code `%s' is not invertible", |
| 4064 | conditions[cc]); |
| 4065 | text = NULL; |
| 4066 | } else |
| 4067 | text = nasm_strdup(conditions[inverse_ccs[cc]]); |
| 4068 | } |
| 4069 | break; |
| 4070 | case '+': |
| 4071 | n = atoi(t->text + 2) - 1; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4072 | if (n >= mac->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4073 | tt = NULL; |
| 4074 | else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4075 | if (mac->nparam > 1) |
| 4076 | n = (n + mac->rotate) % mac->nparam; |
| 4077 | tt = mac->params[n]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4078 | } |
| 4079 | cc = find_cc(tt); |
| 4080 | if (cc == -1) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4081 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4082 | "macro parameter %d is not a condition code", |
| 4083 | n + 1); |
| 4084 | text = NULL; |
| 4085 | } else { |
| 4086 | type = TOK_ID; |
| 4087 | text = nasm_strdup(conditions[cc]); |
| 4088 | } |
| 4089 | break; |
| 4090 | default: |
| 4091 | n = atoi(t->text + 1) - 1; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4092 | if (n >= mac->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4093 | tt = NULL; |
| 4094 | else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4095 | if (mac->nparam > 1) |
| 4096 | n = (n + mac->rotate) % mac->nparam; |
| 4097 | tt = mac->params[n]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4098 | } |
| 4099 | if (tt) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4100 | for (i = 0; i < mac->paramlen[n]; i++) { |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4101 | *tail = new_Token(NULL, tt->type, tt->text, 0); |
| 4102 | tail = &(*tail)->next; |
| 4103 | tt = tt->next; |
| 4104 | } |
| 4105 | } |
| 4106 | text = NULL; /* we've done it here */ |
| 4107 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4108 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4109 | } else { |
| 4110 | /* |
| 4111 | * seems we have a parameters range here |
| 4112 | */ |
| 4113 | Token *head, **last; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4114 | head = expand_mmac_params_range(mac, t, &last); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4115 | if (head != t) { |
| 4116 | *tail = head; |
| 4117 | *last = tline; |
| 4118 | tline = head; |
| 4119 | text = NULL; |
| 4120 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4121 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4122 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4123 | if (!text) { |
| 4124 | delete_Token(t); |
| 4125 | } else { |
| 4126 | *tail = t; |
| 4127 | tail = &t->next; |
| 4128 | t->type = type; |
| 4129 | nasm_free(t->text); |
| 4130 | t->text = text; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4131 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4132 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4133 | changed = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4134 | continue; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4135 | } else if (tline->type == TOK_INDIRECT) { |
| 4136 | t = tline; |
| 4137 | tline = tline->next; |
| 4138 | tt = tokenize(t->text); |
| 4139 | tt = expand_mmac_params(tt); |
| 4140 | tt = expand_smacro(tt); |
| 4141 | *tail = tt; |
| 4142 | while (tt) { |
| 4143 | tt->a.mac = NULL; /* Necessary? */ |
| 4144 | tail = &tt->next; |
| 4145 | tt = tt->next; |
| 4146 | } |
| 4147 | delete_Token(t); |
| 4148 | changed = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4149 | } else { |
| 4150 | t = *tail = tline; |
| 4151 | tline = tline->next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4152 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4153 | tail = &t->next; |
| 4154 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4155 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4156 | *tail = NULL; |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 4157 | |
Cyrill Gorcunov | c6a742c | 2011-06-27 01:23:09 +0400 | [diff] [blame] | 4158 | if (changed) { |
| 4159 | const struct tokseq_match t[] = { |
| 4160 | { |
| 4161 | PP_CONCAT_MASK(TOK_ID) | |
| 4162 | PP_CONCAT_MASK(TOK_FLOAT), /* head */ |
| 4163 | PP_CONCAT_MASK(TOK_ID) | |
| 4164 | PP_CONCAT_MASK(TOK_NUMBER) | |
| 4165 | PP_CONCAT_MASK(TOK_FLOAT) | |
| 4166 | PP_CONCAT_MASK(TOK_OTHER) /* tail */ |
| 4167 | }, |
| 4168 | { |
| 4169 | PP_CONCAT_MASK(TOK_NUMBER), /* head */ |
| 4170 | PP_CONCAT_MASK(TOK_NUMBER) /* tail */ |
| 4171 | } |
| 4172 | }; |
| 4173 | paste_tokens(&thead, t, ARRAY_SIZE(t), false); |
| 4174 | } |
H. Peter Anvin | 6125b62 | 2009-04-08 14:02:25 -0700 | [diff] [blame] | 4175 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4176 | return thead; |
| 4177 | } |
| 4178 | |
| 4179 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4180 | * Expand all single-line macro calls made in the given line. |
| 4181 | * Return the expanded version of the line. The original is deemed |
| 4182 | * to be destroyed in the process. (In reality we'll just move |
| 4183 | * Tokens from input to output a lot of the time, rather than |
| 4184 | * actually bothering to destroy and replicate.) |
| 4185 | */ |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4186 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4187 | static Token *expand_smacro(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4188 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4189 | Token *t, *tt, *mstart, **tail, *thead; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4190 | SMacro *head = NULL, *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4191 | Token **params; |
| 4192 | int *paramsize; |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 4193 | unsigned int nparam, sparam; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 4194 | int brackets; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4195 | Token *org_tline = tline; |
| 4196 | Context *ctx; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 4197 | const char *mname; |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame^] | 4198 | int deadman = nasm_limit[LIMIT_MACROS]; |
H. Peter Anvin | 8287daf | 2009-07-07 16:00:58 -0700 | [diff] [blame] | 4199 | bool expanded; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4200 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4201 | /* |
| 4202 | * Trick: we should avoid changing the start token pointer since it can |
| 4203 | * be contained in "next" field of other token. Because of this |
| 4204 | * we allocate a copy of first token and work with it; at the end of |
| 4205 | * routine we copy it back |
| 4206 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4207 | if (org_tline) { |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4208 | tline = new_Token(org_tline->next, org_tline->type, |
| 4209 | org_tline->text, 0); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4210 | tline->a.mac = org_tline->a.mac; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4211 | nasm_free(org_tline->text); |
| 4212 | org_tline->text = NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4213 | } |
| 4214 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4215 | expanded = true; /* Always expand %+ at least once */ |
H. Peter Anvin | 8287daf | 2009-07-07 16:00:58 -0700 | [diff] [blame] | 4216 | |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4217 | again: |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4218 | thead = NULL; |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4219 | tail = &thead; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4220 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4221 | while (tline) { /* main token loop */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4222 | if (!--deadman) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4223 | nasm_error(ERR_NONFATAL, "interminable macro recursion"); |
Cyrill Gorcunov | bd38c8f | 2009-11-21 11:11:23 +0300 | [diff] [blame] | 4224 | goto err; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4225 | } |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4226 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4227 | if ((mname = tline->text)) { |
| 4228 | /* if this token is a local macro, look in local context */ |
Cyrill Gorcunov | c56d9ad | 2010-02-11 15:12:19 +0300 | [diff] [blame] | 4229 | if (tline->type == TOK_ID) { |
| 4230 | head = (SMacro *)hash_findix(&smacros, mname); |
| 4231 | } else if (tline->type == TOK_PREPROC_ID) { |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 4232 | ctx = get_ctx(mname, &mname); |
Cyrill Gorcunov | c56d9ad | 2010-02-11 15:12:19 +0300 | [diff] [blame] | 4233 | head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL; |
| 4234 | } else |
| 4235 | head = NULL; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 4236 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4237 | /* |
| 4238 | * We've hit an identifier. As in is_mmacro below, we first |
| 4239 | * check whether the identifier is a single-line macro at |
| 4240 | * all, then think about checking for parameters if |
| 4241 | * necessary. |
| 4242 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4243 | list_for_each(m, head) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4244 | if (!mstrcmp(m->name, mname, m->casesense)) |
| 4245 | break; |
| 4246 | if (m) { |
| 4247 | mstart = tline; |
| 4248 | params = NULL; |
| 4249 | paramsize = NULL; |
| 4250 | if (m->nparam == 0) { |
| 4251 | /* |
| 4252 | * Simple case: the macro is parameterless. Discard the |
| 4253 | * one token that the macro call took, and push the |
| 4254 | * expansion back on the to-do stack. |
| 4255 | */ |
| 4256 | if (!m->expansion) { |
| 4257 | if (!strcmp("__FILE__", m->name)) { |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 4258 | const char *file = src_get_fname(); |
| 4259 | /* nasm_free(tline->text); here? */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4260 | tline->text = nasm_quote(file, strlen(file)); |
| 4261 | tline->type = TOK_STRING; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4262 | continue; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4263 | } |
| 4264 | if (!strcmp("__LINE__", m->name)) { |
| 4265 | nasm_free(tline->text); |
| 4266 | make_tok_num(tline, src_get_linnum()); |
| 4267 | continue; |
| 4268 | } |
| 4269 | if (!strcmp("__BITS__", m->name)) { |
| 4270 | nasm_free(tline->text); |
| 4271 | make_tok_num(tline, globalbits); |
| 4272 | continue; |
| 4273 | } |
| 4274 | tline = delete_Token(tline); |
| 4275 | continue; |
| 4276 | } |
| 4277 | } else { |
| 4278 | /* |
| 4279 | * Complicated case: at least one macro with this name |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4280 | * exists and takes parameters. We must find the |
| 4281 | * parameters in the call, count them, find the SMacro |
| 4282 | * that corresponds to that form of the macro call, and |
| 4283 | * substitute for the parameters when we expand. What a |
| 4284 | * pain. |
| 4285 | */ |
| 4286 | /*tline = tline->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4287 | skip_white_(tline); */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4288 | do { |
| 4289 | t = tline->next; |
| 4290 | while (tok_type_(t, TOK_SMAC_END)) { |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4291 | t->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4292 | t->text = NULL; |
| 4293 | t = tline->next = delete_Token(t); |
| 4294 | } |
| 4295 | tline = t; |
| 4296 | } while (tok_type_(tline, TOK_WHITESPACE)); |
| 4297 | if (!tok_is_(tline, "(")) { |
| 4298 | /* |
| 4299 | * This macro wasn't called with parameters: ignore |
| 4300 | * the call. (Behaviour borrowed from gnu cpp.) |
| 4301 | */ |
| 4302 | tline = mstart; |
| 4303 | m = NULL; |
| 4304 | } else { |
| 4305 | int paren = 0; |
| 4306 | int white = 0; |
| 4307 | brackets = 0; |
| 4308 | nparam = 0; |
| 4309 | sparam = PARAM_DELTA; |
| 4310 | params = nasm_malloc(sparam * sizeof(Token *)); |
| 4311 | params[0] = tline->next; |
| 4312 | paramsize = nasm_malloc(sparam * sizeof(int)); |
| 4313 | paramsize[0] = 0; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4314 | while (true) { /* parameter loop */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4315 | /* |
| 4316 | * For some unusual expansions |
| 4317 | * which concatenates function call |
| 4318 | */ |
| 4319 | t = tline->next; |
| 4320 | while (tok_type_(t, TOK_SMAC_END)) { |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4321 | t->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4322 | t->text = NULL; |
| 4323 | t = tline->next = delete_Token(t); |
| 4324 | } |
| 4325 | tline = t; |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 4326 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4327 | if (!tline) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4328 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4329 | "macro call expects terminating `)'"); |
| 4330 | break; |
| 4331 | } |
| 4332 | if (tline->type == TOK_WHITESPACE |
| 4333 | && brackets <= 0) { |
| 4334 | if (paramsize[nparam]) |
| 4335 | white++; |
| 4336 | else |
| 4337 | params[nparam] = tline->next; |
| 4338 | continue; /* parameter loop */ |
| 4339 | } |
| 4340 | if (tline->type == TOK_OTHER |
| 4341 | && tline->text[1] == 0) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4342 | char ch = tline->text[0]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4343 | if (ch == ',' && !paren && brackets <= 0) { |
| 4344 | if (++nparam >= sparam) { |
| 4345 | sparam += PARAM_DELTA; |
| 4346 | params = nasm_realloc(params, |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4347 | sparam * sizeof(Token *)); |
| 4348 | paramsize = nasm_realloc(paramsize, |
| 4349 | sparam * sizeof(int)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4350 | } |
| 4351 | params[nparam] = tline->next; |
| 4352 | paramsize[nparam] = 0; |
| 4353 | white = 0; |
| 4354 | continue; /* parameter loop */ |
| 4355 | } |
| 4356 | if (ch == '{' && |
| 4357 | (brackets > 0 || (brackets == 0 && |
| 4358 | !paramsize[nparam]))) |
| 4359 | { |
| 4360 | if (!(brackets++)) { |
| 4361 | params[nparam] = tline->next; |
| 4362 | continue; /* parameter loop */ |
| 4363 | } |
| 4364 | } |
| 4365 | if (ch == '}' && brackets > 0) |
| 4366 | if (--brackets == 0) { |
| 4367 | brackets = -1; |
| 4368 | continue; /* parameter loop */ |
| 4369 | } |
| 4370 | if (ch == '(' && !brackets) |
| 4371 | paren++; |
| 4372 | if (ch == ')' && brackets <= 0) |
| 4373 | if (--paren < 0) |
| 4374 | break; |
| 4375 | } |
| 4376 | if (brackets < 0) { |
| 4377 | brackets = 0; |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4378 | nasm_error(ERR_NONFATAL, "braces do not " |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4379 | "enclose all of macro parameter"); |
| 4380 | } |
| 4381 | paramsize[nparam] += white + 1; |
| 4382 | white = 0; |
| 4383 | } /* parameter loop */ |
| 4384 | nparam++; |
| 4385 | while (m && (m->nparam != nparam || |
| 4386 | mstrcmp(m->name, mname, |
| 4387 | m->casesense))) |
| 4388 | m = m->next; |
| 4389 | if (!m) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4390 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4391 | "macro `%s' exists, " |
| 4392 | "but not taking %d parameters", |
| 4393 | mstart->text, nparam); |
| 4394 | } |
| 4395 | } |
| 4396 | if (m && m->in_progress) |
| 4397 | m = NULL; |
| 4398 | if (!m) { /* in progess or didn't find '(' or wrong nparam */ |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 4399 | /* |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4400 | * Design question: should we handle !tline, which |
| 4401 | * indicates missing ')' here, or expand those |
| 4402 | * macros anyway, which requires the (t) test a few |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 4403 | * lines down? |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4404 | */ |
| 4405 | nasm_free(params); |
| 4406 | nasm_free(paramsize); |
| 4407 | tline = mstart; |
| 4408 | } else { |
| 4409 | /* |
| 4410 | * Expand the macro: we are placed on the last token of the |
| 4411 | * call, so that we can easily split the call from the |
| 4412 | * following tokens. We also start by pushing an SMAC_END |
| 4413 | * token for the cycle removal. |
| 4414 | */ |
| 4415 | t = tline; |
| 4416 | if (t) { |
| 4417 | tline = t->next; |
| 4418 | t->next = NULL; |
| 4419 | } |
| 4420 | tt = new_Token(tline, TOK_SMAC_END, NULL, 0); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4421 | tt->a.mac = m; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4422 | m->in_progress = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4423 | tline = tt; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 4424 | list_for_each(t, m->expansion) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4425 | if (t->type >= TOK_SMAC_PARAM) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4426 | Token *pcopy = tline, **ptail = &pcopy; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4427 | Token *ttt, *pt; |
| 4428 | int i; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4429 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4430 | ttt = params[t->type - TOK_SMAC_PARAM]; |
| 4431 | i = paramsize[t->type - TOK_SMAC_PARAM]; |
| 4432 | while (--i >= 0) { |
| 4433 | pt = *ptail = new_Token(tline, ttt->type, |
| 4434 | ttt->text, 0); |
| 4435 | ptail = &pt->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4436 | ttt = ttt->next; |
Cyrill Gorcunov | 59ce1c6 | 2017-10-22 18:42:07 +0300 | [diff] [blame] | 4437 | if (!ttt && i > 0) { |
| 4438 | /* |
| 4439 | * FIXME: Need to handle more gracefully, |
| 4440 | * exiting early on agruments analysis. |
| 4441 | */ |
| 4442 | nasm_error(ERR_FATAL, |
| 4443 | "macro `%s' expects %d args", |
| 4444 | mstart->text, |
| 4445 | (int)paramsize[t->type - TOK_SMAC_PARAM]); |
| 4446 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4447 | } |
| 4448 | tline = pcopy; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4449 | } else if (t->type == TOK_PREPROC_Q) { |
| 4450 | tt = new_Token(tline, TOK_ID, mname, 0); |
| 4451 | tline = tt; |
| 4452 | } else if (t->type == TOK_PREPROC_QQ) { |
| 4453 | tt = new_Token(tline, TOK_ID, m->name, 0); |
| 4454 | tline = tt; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4455 | } else { |
| 4456 | tt = new_Token(tline, t->type, t->text, 0); |
| 4457 | tline = tt; |
| 4458 | } |
| 4459 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4460 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4461 | /* |
| 4462 | * Having done that, get rid of the macro call, and clean |
| 4463 | * up the parameters. |
| 4464 | */ |
| 4465 | nasm_free(params); |
| 4466 | nasm_free(paramsize); |
| 4467 | free_tlist(mstart); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4468 | expanded = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4469 | continue; /* main token loop */ |
| 4470 | } |
| 4471 | } |
| 4472 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4473 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4474 | if (tline->type == TOK_SMAC_END) { |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4475 | tline->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4476 | tline = delete_Token(tline); |
| 4477 | } else { |
| 4478 | t = *tail = tline; |
| 4479 | tline = tline->next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4480 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4481 | t->next = NULL; |
| 4482 | tail = &t->next; |
| 4483 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4484 | } |
| 4485 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4486 | /* |
| 4487 | * 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] | 4488 | * after expansion (they can't be produced by tokenize()). The successive |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4489 | * TOK_IDs should be concatenated. |
| 4490 | * Also we look for %+ tokens and concatenate the tokens before and after |
| 4491 | * them (without white spaces in between). |
| 4492 | */ |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4493 | if (expanded) { |
Cyrill Gorcunov | c6a742c | 2011-06-27 01:23:09 +0400 | [diff] [blame] | 4494 | const struct tokseq_match t[] = { |
| 4495 | { |
| 4496 | PP_CONCAT_MASK(TOK_ID) | |
| 4497 | PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */ |
| 4498 | PP_CONCAT_MASK(TOK_ID) | |
| 4499 | PP_CONCAT_MASK(TOK_PREPROC_ID) | |
| 4500 | PP_CONCAT_MASK(TOK_NUMBER) /* tail */ |
| 4501 | } |
| 4502 | }; |
| 4503 | if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) { |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4504 | /* |
| 4505 | * If we concatenated something, *and* we had previously expanded |
| 4506 | * an actual macro, scan the lines again for macros... |
| 4507 | */ |
| 4508 | tline = thead; |
| 4509 | expanded = false; |
| 4510 | goto again; |
| 4511 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4512 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4513 | |
Cyrill Gorcunov | bd38c8f | 2009-11-21 11:11:23 +0300 | [diff] [blame] | 4514 | err: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4515 | if (org_tline) { |
| 4516 | if (thead) { |
| 4517 | *org_tline = *thead; |
| 4518 | /* since we just gave text to org_line, don't free it */ |
| 4519 | thead->text = NULL; |
| 4520 | delete_Token(thead); |
| 4521 | } else { |
| 4522 | /* the expression expanded to empty line; |
| 4523 | we can't return NULL for some reasons |
| 4524 | we just set the line to a single WHITESPACE token. */ |
| 4525 | memset(org_tline, 0, sizeof(*org_tline)); |
| 4526 | org_tline->text = NULL; |
| 4527 | org_tline->type = TOK_WHITESPACE; |
| 4528 | } |
| 4529 | thead = org_tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4530 | } |
| 4531 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4532 | return thead; |
| 4533 | } |
| 4534 | |
| 4535 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4536 | * Similar to expand_smacro but used exclusively with macro identifiers |
| 4537 | * right before they are fetched in. The reason is that there can be |
| 4538 | * identifiers consisting of several subparts. We consider that if there |
| 4539 | * are more than one element forming the name, user wants a expansion, |
| 4540 | * otherwise it will be left as-is. Example: |
| 4541 | * |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4542 | * %define %$abc cde |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4543 | * |
| 4544 | * the identifier %$abc will be left as-is so that the handler for %define |
| 4545 | * will suck it and define the corresponding value. Other case: |
| 4546 | * |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4547 | * %define _%$abc cde |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4548 | * |
| 4549 | * In this case user wants name to be expanded *before* %define starts |
| 4550 | * working, so we'll expand %$abc into something (if it has a value; |
| 4551 | * otherwise it will be left as-is) then concatenate all successive |
| 4552 | * PP_IDs into one. |
| 4553 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4554 | static Token *expand_id(Token * tline) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4555 | { |
| 4556 | Token *cur, *oldnext = NULL; |
| 4557 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4558 | if (!tline || !tline->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4559 | return tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4560 | |
| 4561 | cur = tline; |
| 4562 | while (cur->next && |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4563 | (cur->next->type == TOK_ID || |
| 4564 | cur->next->type == TOK_PREPROC_ID |
| 4565 | || cur->next->type == TOK_NUMBER)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4566 | cur = cur->next; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4567 | |
| 4568 | /* If identifier consists of just one token, don't expand */ |
| 4569 | if (cur == tline) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4570 | return tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4571 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4572 | if (cur) { |
| 4573 | oldnext = cur->next; /* Detach the tail past identifier */ |
| 4574 | cur->next = NULL; /* so that expand_smacro stops here */ |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4575 | } |
| 4576 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4577 | tline = expand_smacro(tline); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4578 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4579 | if (cur) { |
| 4580 | /* expand_smacro possibly changhed tline; re-scan for EOL */ |
| 4581 | cur = tline; |
| 4582 | while (cur && cur->next) |
| 4583 | cur = cur->next; |
| 4584 | if (cur) |
| 4585 | cur->next = oldnext; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4586 | } |
| 4587 | |
| 4588 | return tline; |
| 4589 | } |
| 4590 | |
| 4591 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4592 | * Determine whether the given line constitutes a multi-line macro |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4593 | * 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] | 4594 | * to check for an initial label - that's taken care of in |
| 4595 | * expand_mmacro - but must check numbers of parameters. Guaranteed |
| 4596 | * to be called with tline->type == TOK_ID, so the putative macro |
| 4597 | * name is easy to find. |
| 4598 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4599 | static MMacro *is_mmacro(Token * tline, Token *** params_array) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4600 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4601 | MMacro *head, *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4602 | Token **params; |
| 4603 | int nparam; |
| 4604 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4605 | head = (MMacro *) hash_findix(&mmacros, tline->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4606 | |
| 4607 | /* |
| 4608 | * Efficiency: first we see if any macro exists with the given |
| 4609 | * name. If not, we can return NULL immediately. _Then_ we |
| 4610 | * count the parameters, and then we look further along the |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4611 | * list if necessary to find the proper MMacro. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4612 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4613 | list_for_each(m, head) |
| 4614 | if (!mstrcmp(m->name, tline->text, m->casesense)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4615 | break; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4616 | if (!m) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4617 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4618 | |
| 4619 | /* |
| 4620 | * OK, we have a potential macro. Count and demarcate the |
| 4621 | * parameters. |
| 4622 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4623 | count_mmac_params(tline->next, &nparam, ¶ms); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4624 | |
| 4625 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4626 | * 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] | 4627 | * structure that handles this number. |
| 4628 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4629 | while (m) { |
| 4630 | if (m->nparam_min <= nparam |
| 4631 | && (m->plus || nparam <= m->nparam_max)) { |
| 4632 | /* |
| 4633 | * This one is right. Just check if cycle removal |
| 4634 | * prohibits us using it before we actually celebrate... |
| 4635 | */ |
| 4636 | if (m->in_progress > m->max_depth) { |
| 4637 | if (m->max_depth > 0) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4638 | nasm_error(ERR_WARNING, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4639 | "reached maximum recursion depth of %i", |
| 4640 | m->max_depth); |
| 4641 | } |
| 4642 | nasm_free(params); |
| 4643 | return NULL; |
| 4644 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4645 | /* |
| 4646 | * It's right, and we can use it. Add its default |
| 4647 | * parameters to the end of our list if necessary. |
| 4648 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4649 | if (m->defaults && nparam < m->nparam_min + m->ndefs) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4650 | params = |
| 4651 | nasm_realloc(params, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4652 | ((m->nparam_min + m->ndefs + |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4653 | 1) * sizeof(*params))); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4654 | while (nparam < m->nparam_min + m->ndefs) { |
| 4655 | params[nparam] = m->defaults[nparam - m->nparam_min]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4656 | nparam++; |
| 4657 | } |
| 4658 | } |
| 4659 | /* |
| 4660 | * If we've gone over the maximum parameter count (and |
| 4661 | * we're in Plus mode), ignore parameters beyond |
| 4662 | * nparam_max. |
| 4663 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4664 | if (m->plus && nparam > m->nparam_max) |
| 4665 | nparam = m->nparam_max; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4666 | /* |
| 4667 | * Then terminate the parameter list, and leave. |
| 4668 | */ |
| 4669 | if (!params) { /* need this special case */ |
| 4670 | params = nasm_malloc(sizeof(*params)); |
| 4671 | nparam = 0; |
| 4672 | } |
| 4673 | params[nparam] = NULL; |
| 4674 | *params_array = params; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4675 | return m; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4676 | } |
| 4677 | /* |
| 4678 | * This one wasn't right: look for the next one with the |
| 4679 | * same name. |
| 4680 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4681 | list_for_each(m, m->next) |
| 4682 | if (!mstrcmp(m->name, tline->text, m->casesense)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4683 | break; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4684 | } |
| 4685 | |
| 4686 | /* |
| 4687 | * After all that, we didn't find one with the right number of |
| 4688 | * parameters. Issue a warning, and fail to expand the macro. |
| 4689 | */ |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4690 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4691 | "macro `%s' exists, but not taking %d parameters", |
| 4692 | tline->text, nparam); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4693 | nasm_free(params); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4694 | return NULL; |
| 4695 | } |
| 4696 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4697 | |
| 4698 | /* |
| 4699 | * Save MMacro invocation specific fields in |
| 4700 | * preparation for a recursive macro expansion |
| 4701 | */ |
| 4702 | static void push_mmacro(MMacro *m) |
| 4703 | { |
| 4704 | MMacroInvocation *i; |
| 4705 | |
| 4706 | i = nasm_malloc(sizeof(MMacroInvocation)); |
| 4707 | i->prev = m->prev; |
| 4708 | i->params = m->params; |
| 4709 | i->iline = m->iline; |
| 4710 | i->nparam = m->nparam; |
| 4711 | i->rotate = m->rotate; |
| 4712 | i->paramlen = m->paramlen; |
| 4713 | i->unique = m->unique; |
| 4714 | i->condcnt = m->condcnt; |
| 4715 | m->prev = i; |
| 4716 | } |
| 4717 | |
| 4718 | |
| 4719 | /* |
| 4720 | * Restore MMacro invocation specific fields that were |
| 4721 | * saved during a previous recursive macro expansion |
| 4722 | */ |
| 4723 | static void pop_mmacro(MMacro *m) |
| 4724 | { |
| 4725 | MMacroInvocation *i; |
| 4726 | |
| 4727 | if (m->prev) { |
| 4728 | i = m->prev; |
| 4729 | m->prev = i->prev; |
| 4730 | m->params = i->params; |
| 4731 | m->iline = i->iline; |
| 4732 | m->nparam = i->nparam; |
| 4733 | m->rotate = i->rotate; |
| 4734 | m->paramlen = i->paramlen; |
| 4735 | m->unique = i->unique; |
| 4736 | m->condcnt = i->condcnt; |
| 4737 | nasm_free(i); |
| 4738 | } |
| 4739 | } |
| 4740 | |
| 4741 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4742 | /* |
| 4743 | * Expand the multi-line macro call made by the given line, if |
| 4744 | * there is one to be expanded. If there is, push the expansion on |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4745 | * istk->expansion and return 1. Otherwise return 0. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4746 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4747 | static int expand_mmacro(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4748 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4749 | Token *startline = tline; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4750 | Token *label = NULL; |
| 4751 | int dont_prepend = 0; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4752 | Token **params, *t, *tt; |
| 4753 | MMacro *m; |
| 4754 | Line *l, *ll; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4755 | int i, nparam, *paramlen; |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 4756 | const char *mname; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4757 | |
| 4758 | t = tline; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4759 | skip_white_(t); |
H. Peter Anvin | ce2233b | 2008-05-25 21:57:00 -0700 | [diff] [blame] | 4760 | /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */ |
H. Peter Anvin | dce1e2f | 2002-04-30 21:06:37 +0000 | [diff] [blame] | 4761 | if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID)) |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4762 | return 0; |
| 4763 | m = is_mmacro(t, ¶ms); |
| 4764 | if (m) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4765 | mname = t->text; |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 4766 | } else { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4767 | Token *last; |
| 4768 | /* |
| 4769 | * We have an id which isn't a macro call. We'll assume |
| 4770 | * it might be a label; we'll also check to see if a |
| 4771 | * colon follows it. Then, if there's another id after |
| 4772 | * that lot, we'll check it again for macro-hood. |
| 4773 | */ |
| 4774 | label = last = t; |
| 4775 | t = t->next; |
| 4776 | if (tok_type_(t, TOK_WHITESPACE)) |
| 4777 | last = t, t = t->next; |
| 4778 | if (tok_is_(t, ":")) { |
| 4779 | dont_prepend = 1; |
| 4780 | last = t, t = t->next; |
| 4781 | if (tok_type_(t, TOK_WHITESPACE)) |
| 4782 | last = t, t = t->next; |
| 4783 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4784 | if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, ¶ms))) |
| 4785 | return 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4786 | last->next = NULL; |
Keith Kanios | 891775e | 2009-07-11 06:08:54 -0500 | [diff] [blame] | 4787 | mname = t->text; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4788 | tline = t; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4789 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4790 | |
| 4791 | /* |
| 4792 | * Fix up the parameters: this involves stripping leading and |
| 4793 | * trailing whitespace, then stripping braces if they are |
| 4794 | * present. |
| 4795 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4796 | for (nparam = 0; params[nparam]; nparam++) ; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4797 | paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4798 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4799 | for (i = 0; params[i]; i++) { |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4800 | int brace = 0; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4801 | int comma = (!m->plus || i < nparam - 1); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4802 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4803 | t = params[i]; |
| 4804 | skip_white_(t); |
| 4805 | if (tok_is_(t, "{")) |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4806 | t = t->next, brace++, comma = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4807 | params[i] = t; |
| 4808 | paramlen[i] = 0; |
| 4809 | while (t) { |
| 4810 | if (comma && t->type == TOK_OTHER && !strcmp(t->text, ",")) |
| 4811 | break; /* ... because we have hit a comma */ |
| 4812 | if (comma && t->type == TOK_WHITESPACE |
| 4813 | && tok_is_(t->next, ",")) |
| 4814 | break; /* ... or a space then a comma */ |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4815 | if (brace && t->type == TOK_OTHER) { |
| 4816 | if (t->text[0] == '{') |
| 4817 | brace++; /* ... or a nested opening brace */ |
| 4818 | else if (t->text[0] == '}') |
| 4819 | if (!--brace) |
| 4820 | break; /* ... or a brace */ |
| 4821 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4822 | t = t->next; |
| 4823 | paramlen[i]++; |
| 4824 | } |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4825 | if (brace) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4826 | nasm_error(ERR_NONFATAL, "macro params should be enclosed in braces"); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4827 | } |
| 4828 | |
| 4829 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4830 | * OK, we have a MMacro structure together with a set of |
| 4831 | * parameters. We must now go through the expansion and push |
| 4832 | * copies of each Line on to istk->expansion. Substitution of |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4833 | * parameter tokens and macro-local tokens doesn't get done |
| 4834 | * until the single-line macro substitution process; this is |
| 4835 | * because delaying them allows us to change the semantics |
| 4836 | * later through %rotate. |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4837 | * |
| 4838 | * First, push an end marker on to istk->expansion, mark this |
| 4839 | * macro as in progress, and set up its invocation-specific |
| 4840 | * variables. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4841 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4842 | ll = nasm_malloc(sizeof(Line)); |
| 4843 | ll->next = istk->expansion; |
| 4844 | ll->finishes = m; |
| 4845 | ll->first = NULL; |
| 4846 | istk->expansion = ll; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4847 | |
| 4848 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4849 | * Save the previous MMacro expansion in the case of |
| 4850 | * macro recursion |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4851 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4852 | if (m->max_depth && m->in_progress) |
| 4853 | push_mmacro(m); |
| 4854 | |
| 4855 | m->in_progress ++; |
| 4856 | m->params = params; |
| 4857 | m->iline = tline; |
| 4858 | m->nparam = nparam; |
| 4859 | m->rotate = 0; |
| 4860 | m->paramlen = paramlen; |
| 4861 | m->unique = unique++; |
| 4862 | m->lineno = 0; |
| 4863 | m->condcnt = 0; |
| 4864 | |
| 4865 | m->next_active = istk->mstk; |
| 4866 | istk->mstk = m; |
| 4867 | |
| 4868 | list_for_each(l, m->expansion) { |
| 4869 | Token **tail; |
| 4870 | |
| 4871 | ll = nasm_malloc(sizeof(Line)); |
| 4872 | ll->finishes = NULL; |
| 4873 | ll->next = istk->expansion; |
| 4874 | istk->expansion = ll; |
| 4875 | tail = &ll->first; |
| 4876 | |
| 4877 | list_for_each(t, l->first) { |
| 4878 | Token *x = t; |
| 4879 | switch (t->type) { |
| 4880 | case TOK_PREPROC_Q: |
| 4881 | tt = *tail = new_Token(NULL, TOK_ID, mname, 0); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4882 | break; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4883 | case TOK_PREPROC_QQ: |
| 4884 | tt = *tail = new_Token(NULL, TOK_ID, m->name, 0); |
| 4885 | break; |
| 4886 | case TOK_PREPROC_ID: |
| 4887 | if (t->text[1] == '0' && t->text[2] == '0') { |
| 4888 | dont_prepend = -1; |
| 4889 | x = label; |
| 4890 | if (!x) |
| 4891 | continue; |
| 4892 | } |
| 4893 | /* fall through */ |
| 4894 | default: |
| 4895 | tt = *tail = new_Token(NULL, x->type, x->text, 0); |
| 4896 | break; |
| 4897 | } |
| 4898 | tail = &tt->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4899 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4900 | *tail = NULL; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4901 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4902 | |
| 4903 | /* |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4904 | * If we had a label, push it on as the first line of |
| 4905 | * the macro expansion. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4906 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4907 | if (label) { |
| 4908 | if (dont_prepend < 0) |
| 4909 | free_tlist(startline); |
| 4910 | else { |
| 4911 | ll = nasm_malloc(sizeof(Line)); |
| 4912 | ll->finishes = NULL; |
| 4913 | ll->next = istk->expansion; |
| 4914 | istk->expansion = ll; |
| 4915 | ll->first = startline; |
| 4916 | if (!dont_prepend) { |
| 4917 | while (label->next) |
| 4918 | label = label->next; |
| 4919 | label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4920 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4921 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4922 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4923 | |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 4924 | lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4925 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4926 | return 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4927 | } |
| 4928 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4929 | /* |
| 4930 | * This function adds macro names to error messages, and suppresses |
| 4931 | * them if necessary. |
| 4932 | */ |
| 4933 | 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] | 4934 | { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4935 | char buff[BUFSIZ]; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4936 | MMacro *mmac = NULL; |
| 4937 | int delta = 0; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4938 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4939 | /* |
| 4940 | * If we're in a dead branch of IF or something like it, ignore the error. |
| 4941 | * However, because %else etc are evaluated in the state context |
| 4942 | * of the previous branch, errors might get lost: |
| 4943 | * %if 0 ... %else trailing garbage ... %endif |
| 4944 | * So %else etc should set the ERR_PP_PRECOND flag. |
| 4945 | */ |
| 4946 | if ((severity & ERR_MASK) < ERR_FATAL && |
| 4947 | istk && istk->conds && |
| 4948 | ((severity & ERR_PP_PRECOND) ? |
| 4949 | istk->conds->state == COND_NEVER : |
H. Peter Anvin | eb6653f | 2016-04-05 13:03:10 -0700 | [diff] [blame] | 4950 | !emitting(istk->conds->state))) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4951 | return; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4952 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4953 | /* get %macro name */ |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4954 | if (!(severity & ERR_NOFILE) && istk && istk->mstk) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4955 | mmac = istk->mstk; |
| 4956 | /* but %rep blocks should be skipped */ |
| 4957 | while (mmac && !mmac->name) |
| 4958 | mmac = mmac->next_active, delta++; |
Cyrill Gorcunov | 9900c6b | 2011-10-09 18:58:46 +0400 | [diff] [blame] | 4959 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4960 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4961 | if (mmac) { |
| 4962 | vsnprintf(buff, sizeof(buff), fmt, arg); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4963 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4964 | nasm_set_verror(real_verror); |
| 4965 | nasm_error(severity, "(%s:%d) %s", |
| 4966 | mmac->name, mmac->lineno - delta, buff); |
| 4967 | nasm_set_verror(pp_verror); |
| 4968 | } else { |
| 4969 | real_verror(severity, fmt, arg); |
| 4970 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4971 | } |
| 4972 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4973 | static void |
H. Peter Anvin | 81b62b9 | 2017-12-20 13:33:49 -0800 | [diff] [blame] | 4974 | pp_reset(const char *file, int apass, StrList **deplist) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4975 | { |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 4976 | Token *t; |
| 4977 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4978 | cstk = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4979 | istk = nasm_malloc(sizeof(Include)); |
| 4980 | istk->next = NULL; |
| 4981 | istk->conds = NULL; |
| 4982 | istk->expansion = NULL; |
| 4983 | istk->mstk = NULL; |
H. Peter Anvin | 3e83cec | 2016-05-25 04:28:46 -0700 | [diff] [blame] | 4984 | istk->fp = nasm_open_read(file, NF_TEXT); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4985 | istk->fname = NULL; |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 4986 | src_set(0, file); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4987 | istk->lineinc = 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4988 | if (!istk->fp) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4989 | nasm_fatal(ERR_NOFILE, "unable to open input file `%s'", file); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4990 | defining = NULL; |
Charles Crayne | d4200be | 2008-07-12 16:42:33 -0700 | [diff] [blame] | 4991 | nested_mac_count = 0; |
| 4992 | nested_rep_count = 0; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 4993 | init_macros(); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4994 | unique = 0; |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 4995 | |
| 4996 | if (tasm_compatible_mode) |
| 4997 | pp_add_stdmac(nasm_stdmac_tasm); |
| 4998 | |
| 4999 | pp_add_stdmac(nasm_stdmac_nasm); |
| 5000 | pp_add_stdmac(nasm_stdmac_version); |
| 5001 | |
Cyrill Gorcunov | 15ce78f | 2017-01-06 20:21:28 +0300 | [diff] [blame] | 5002 | if (extrastdmac) |
| 5003 | pp_add_stdmac(extrastdmac); |
| 5004 | |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 5005 | stdmacpos = stdmacros[0]; |
| 5006 | stdmacnext = &stdmacros[1]; |
| 5007 | |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 5008 | do_predef = true; |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 5009 | |
| 5010 | /* |
| 5011 | * 0 for dependencies, 1 for preparatory passes, 2 for final pass. |
| 5012 | * The caller, however, will also pass in 3 for preprocess-only so |
| 5013 | * we can set __PASS__ accordingly. |
| 5014 | */ |
| 5015 | pass = apass > 2 ? 2 : apass; |
| 5016 | |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 5017 | dephead = deplist; |
H. Peter Anvin | 436e367 | 2016-10-04 01:12:28 -0700 | [diff] [blame] | 5018 | nasm_add_string_to_strlist(dephead, file); |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 5019 | |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 5020 | /* |
| 5021 | * Define the __PASS__ macro. This is defined here unlike |
| 5022 | * all the other builtins, because it is special -- it varies between |
| 5023 | * passes. |
| 5024 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5025 | t = nasm_malloc(sizeof(*t)); |
| 5026 | t->next = NULL; |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 5027 | make_tok_num(t, apass); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5028 | t->a.mac = NULL; |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 5029 | define_smacro(NULL, "__PASS__", true, 0, t); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5030 | } |
| 5031 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 5032 | static void pp_init(void) |
| 5033 | { |
| 5034 | hash_init(&FileHash, HASH_MEDIUM); |
| 5035 | } |
| 5036 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5037 | static char *pp_getline(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5038 | { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5039 | char *line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5040 | Token *tline; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5041 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5042 | real_verror = nasm_set_verror(pp_verror); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5043 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5044 | while (1) { |
| 5045 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5046 | * Fetch a tokenized line, either from the macro-expansion |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5047 | * buffer or from the input file. |
| 5048 | */ |
| 5049 | tline = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5050 | while (istk->expansion && istk->expansion->finishes) { |
| 5051 | Line *l = istk->expansion; |
| 5052 | if (!l->finishes->name && l->finishes->in_progress > 1) { |
| 5053 | Line *ll; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5054 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5055 | /* |
| 5056 | * This is a macro-end marker for a macro with no |
| 5057 | * name, which means it's not really a macro at all |
| 5058 | * but a %rep block, and the `in_progress' field is |
| 5059 | * more than 1, meaning that we still need to |
| 5060 | * repeat. (1 means the natural last repetition; 0 |
| 5061 | * means termination by %exitrep.) We have |
| 5062 | * therefore expanded up to the %endrep, and must |
| 5063 | * push the whole block on to the expansion buffer |
| 5064 | * again. We don't bother to remove the macro-end |
| 5065 | * marker: we'd only have to generate another one |
| 5066 | * if we did. |
| 5067 | */ |
| 5068 | l->finishes->in_progress--; |
| 5069 | list_for_each(l, l->finishes->expansion) { |
| 5070 | Token *t, *tt, **tail; |
| 5071 | |
| 5072 | ll = nasm_malloc(sizeof(Line)); |
| 5073 | ll->next = istk->expansion; |
| 5074 | ll->finishes = NULL; |
| 5075 | ll->first = NULL; |
| 5076 | tail = &ll->first; |
| 5077 | |
| 5078 | list_for_each(t, l->first) { |
| 5079 | if (t->text || t->type == TOK_WHITESPACE) { |
| 5080 | tt = *tail = new_Token(NULL, t->type, t->text, 0); |
| 5081 | tail = &tt->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5082 | } |
| 5083 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5084 | |
| 5085 | istk->expansion = ll; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5086 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5087 | } else { |
| 5088 | /* |
| 5089 | * Check whether a `%rep' was started and not ended |
| 5090 | * within this macro expansion. This can happen and |
| 5091 | * should be detected. It's a fatal error because |
| 5092 | * I'm too confused to work out how to recover |
| 5093 | * sensibly from it. |
| 5094 | */ |
| 5095 | if (defining) { |
| 5096 | if (defining->name) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5097 | nasm_panic(0, "defining with name in expansion"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5098 | else if (istk->mstk->name) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5099 | nasm_fatal(0, "`%%rep' without `%%endrep' within" |
| 5100 | " expansion of macro `%s'", |
| 5101 | istk->mstk->name); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5102 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5103 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5104 | /* |
| 5105 | * FIXME: investigate the relationship at this point between |
| 5106 | * istk->mstk and l->finishes |
| 5107 | */ |
| 5108 | { |
| 5109 | MMacro *m = istk->mstk; |
| 5110 | istk->mstk = m->next_active; |
| 5111 | if (m->name) { |
| 5112 | /* |
| 5113 | * This was a real macro call, not a %rep, and |
| 5114 | * therefore the parameter information needs to |
| 5115 | * be freed. |
| 5116 | */ |
| 5117 | if (m->prev) { |
| 5118 | pop_mmacro(m); |
| 5119 | l->finishes->in_progress --; |
| 5120 | } else { |
| 5121 | nasm_free(m->params); |
| 5122 | free_tlist(m->iline); |
| 5123 | nasm_free(m->paramlen); |
| 5124 | l->finishes->in_progress = 0; |
| 5125 | } |
Adam Majer | 91e7240 | 2017-07-25 10:42:01 +0200 | [diff] [blame] | 5126 | } |
| 5127 | |
| 5128 | /* |
| 5129 | * FIXME It is incorrect to always free_mmacro here. |
| 5130 | * It leads to usage-after-free. |
| 5131 | * |
| 5132 | * https://bugzilla.nasm.us/show_bug.cgi?id=3392414 |
| 5133 | */ |
| 5134 | #if 0 |
| 5135 | else |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5136 | free_mmacro(m); |
Adam Majer | 91e7240 | 2017-07-25 10:42:01 +0200 | [diff] [blame] | 5137 | #endif |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5138 | } |
| 5139 | istk->expansion = l->next; |
| 5140 | nasm_free(l); |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 5141 | lfmt->downlevel(LIST_MACRO); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5142 | } |
| 5143 | } |
| 5144 | while (1) { /* until we get a line we can use */ |
| 5145 | |
| 5146 | if (istk->expansion) { /* from a macro expansion */ |
| 5147 | char *p; |
| 5148 | Line *l = istk->expansion; |
| 5149 | if (istk->mstk) |
| 5150 | istk->mstk->lineno++; |
| 5151 | tline = l->first; |
| 5152 | istk->expansion = l->next; |
| 5153 | nasm_free(l); |
| 5154 | p = detoken(tline, false); |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 5155 | lfmt->line(LIST_MACRO, p); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5156 | nasm_free(p); |
| 5157 | break; |
| 5158 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5159 | line = read_line(); |
| 5160 | if (line) { /* from the current input file */ |
| 5161 | line = prepreproc(line); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5162 | tline = tokenize(line); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5163 | nasm_free(line); |
| 5164 | break; |
| 5165 | } |
| 5166 | /* |
| 5167 | * The current file has ended; work down the istk |
| 5168 | */ |
| 5169 | { |
| 5170 | Include *i = istk; |
| 5171 | fclose(i->fp); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5172 | if (i->conds) { |
| 5173 | /* nasm_error can't be conditionally suppressed */ |
H. Peter Anvin | 4108706 | 2016-03-03 14:27:34 -0800 | [diff] [blame] | 5174 | nasm_fatal(0, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5175 | "expected `%%endif' before end of file"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5176 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5177 | /* 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] | 5178 | if (i->next) |
| 5179 | src_set(i->lineno, i->fname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5180 | istk = i->next; |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 5181 | lfmt->downlevel(LIST_INCLUDE); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5182 | nasm_free(i); |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5183 | if (!istk) { |
| 5184 | line = NULL; |
| 5185 | goto done; |
| 5186 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5187 | if (istk->expansion && istk->expansion->finishes) |
| 5188 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5189 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5190 | } |
| 5191 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5192 | /* |
| 5193 | * We must expand MMacro parameters and MMacro-local labels |
| 5194 | * _before_ we plunge into directive processing, to cope |
| 5195 | * with things like `%define something %1' such as STRUC |
| 5196 | * uses. Unless we're _defining_ a MMacro, in which case |
| 5197 | * those tokens should be left alone to go into the |
| 5198 | * definition; and unless we're in a non-emitting |
| 5199 | * condition, in which case we don't want to meddle with |
| 5200 | * anything. |
| 5201 | */ |
| 5202 | if (!defining && !(istk->conds && !emitting(istk->conds->state)) |
| 5203 | && !(istk->mstk && !istk->mstk->in_progress)) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5204 | tline = expand_mmac_params(tline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5205 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5206 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5207 | /* |
| 5208 | * Check the line to see if it's a preprocessor directive. |
| 5209 | */ |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 5210 | if (do_directive(tline, &line) == DIRECTIVE_FOUND) { |
| 5211 | if (line) |
| 5212 | break; /* Directive generated output */ |
| 5213 | else |
| 5214 | continue; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5215 | } else if (defining) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5216 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5217 | * We're defining a multi-line macro. We emit nothing |
| 5218 | * at all, and just |
| 5219 | * shove the tokenized line on to the macro definition. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5220 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5221 | Line *l = nasm_malloc(sizeof(Line)); |
| 5222 | l->next = defining->expansion; |
| 5223 | l->first = tline; |
| 5224 | l->finishes = NULL; |
| 5225 | defining->expansion = l; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5226 | continue; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5227 | } else if (istk->conds && !emitting(istk->conds->state)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5228 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5229 | * We're in a non-emitting branch of a condition block. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5230 | * Emit nothing at all, not even a blank line: when we |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5231 | * emerge from the condition we'll give a line-number |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5232 | * directive so we keep our place correctly. |
| 5233 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5234 | free_tlist(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5235 | continue; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5236 | } else if (istk->mstk && !istk->mstk->in_progress) { |
| 5237 | /* |
| 5238 | * We're in a %rep block which has been terminated, so |
| 5239 | * we're walking through to the %endrep without |
| 5240 | * emitting anything. Emit nothing at all, not even a |
| 5241 | * blank line: when we emerge from the %rep block we'll |
| 5242 | * give a line-number directive so we keep our place |
| 5243 | * correctly. |
| 5244 | */ |
| 5245 | free_tlist(tline); |
| 5246 | continue; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5247 | } else { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5248 | tline = expand_smacro(tline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5249 | if (!expand_mmacro(tline)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5250 | /* |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5251 | * De-tokenize the line again, and emit it. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5252 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5253 | line = detoken(tline, true); |
| 5254 | free_tlist(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5255 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5256 | } else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5257 | continue; /* expand_mmacro calls free_tlist */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5258 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5259 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5260 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5261 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5262 | done: |
| 5263 | nasm_set_verror(real_verror); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5264 | return line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5265 | } |
| 5266 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5267 | static void pp_cleanup(int pass) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5268 | { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5269 | real_verror = nasm_set_verror(pp_verror); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5270 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5271 | if (defining) { |
| 5272 | if (defining->name) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5273 | nasm_error(ERR_NONFATAL, |
| 5274 | "end of file while still defining macro `%s'", |
| 5275 | defining->name); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5276 | } else { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5277 | nasm_error(ERR_NONFATAL, "end of file while still in %%rep"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5278 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5279 | |
| 5280 | free_mmacro(defining); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5281 | defining = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5282 | } |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5283 | |
| 5284 | nasm_set_verror(real_verror); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5285 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5286 | while (cstk) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5287 | ctx_pop(); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 5288 | free_macros(); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5289 | while (istk) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5290 | Include *i = istk; |
| 5291 | istk = istk->next; |
| 5292 | fclose(i->fp); |
Cyrill Gorcunov | 8dcfd88 | 2011-03-03 09:18:56 +0300 | [diff] [blame] | 5293 | nasm_free(i); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5294 | } |
| 5295 | while (cstk) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5296 | ctx_pop(); |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 5297 | src_set_fname(NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5298 | if (pass == 0) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5299 | IncPath *i; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5300 | free_llist(predef); |
Cyrill Gorcunov | dae24d7 | 2014-06-28 10:17:39 +0400 | [diff] [blame] | 5301 | predef = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5302 | delete_Blocks(); |
Cyrill Gorcunov | dae24d7 | 2014-06-28 10:17:39 +0400 | [diff] [blame] | 5303 | freeTokens = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5304 | while ((i = ipath)) { |
| 5305 | ipath = i->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5306 | if (i->path) |
| 5307 | nasm_free(i->path); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5308 | nasm_free(i); |
| 5309 | } |
H. Peter Anvin | 11dfa1a | 2008-07-02 18:11:04 -0700 | [diff] [blame] | 5310 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5311 | } |
| 5312 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5313 | static void pp_include_path(char *path) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5314 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5315 | IncPath *i; |
H. Peter Anvin | 37a321f | 2007-09-24 13:41:58 -0700 | [diff] [blame] | 5316 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5317 | i = nasm_malloc(sizeof(IncPath)); |
| 5318 | i->path = path ? nasm_strdup(path) : NULL; |
| 5319 | i->next = NULL; |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5320 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 5321 | if (ipath) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5322 | IncPath *j = ipath; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 5323 | while (j->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5324 | j = j->next; |
| 5325 | j->next = i; |
| 5326 | } else { |
| 5327 | ipath = i; |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5328 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5329 | } |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5330 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5331 | static void pp_pre_include(char *fname) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5332 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5333 | Token *inc, *space, *name; |
| 5334 | Line *l; |
| 5335 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5336 | name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0); |
| 5337 | space = new_Token(name, TOK_WHITESPACE, NULL, 0); |
| 5338 | inc = new_Token(space, TOK_PREPROC_ID, "%include", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5339 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5340 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5341 | l->next = predef; |
| 5342 | l->first = inc; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5343 | l->finishes = NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5344 | predef = l; |
| 5345 | } |
| 5346 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5347 | static void pp_pre_define(char *definition) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5348 | { |
| 5349 | Token *def, *space; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5350 | Line *l; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5351 | char *equals; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5352 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5353 | real_verror = nasm_set_verror(pp_verror); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5354 | |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5355 | equals = strchr(definition, '='); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5356 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 5357 | def = new_Token(space, TOK_PREPROC_ID, "%define", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5358 | if (equals) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5359 | *equals = ' '; |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5360 | space->next = tokenize(definition); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5361 | if (equals) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5362 | *equals = '='; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5363 | |
Cyrill Gorcunov | 6d42e9b | 2015-02-08 11:07:17 +0300 | [diff] [blame] | 5364 | if (space->next->type != TOK_PREPROC_ID && |
| 5365 | space->next->type != TOK_ID) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5366 | nasm_error(ERR_WARNING, "pre-defining non ID `%s\'\n", definition); |
Cyrill Gorcunov | 6d42e9b | 2015-02-08 11:07:17 +0300 | [diff] [blame] | 5367 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5368 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5369 | l->next = predef; |
| 5370 | l->first = def; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5371 | l->finishes = NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5372 | predef = l; |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5373 | |
| 5374 | nasm_set_verror(real_verror); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5375 | } |
| 5376 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5377 | static void pp_pre_undefine(char *definition) |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5378 | { |
| 5379 | Token *def, *space; |
| 5380 | Line *l; |
| 5381 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5382 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 5383 | def = new_Token(space, TOK_PREPROC_ID, "%undef", 0); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5384 | space->next = tokenize(definition); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5385 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5386 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5387 | l->next = predef; |
| 5388 | l->first = def; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5389 | l->finishes = NULL; |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5390 | predef = l; |
| 5391 | } |
| 5392 | |
H. Peter Anvin | 0599034 | 2018-06-11 13:32:42 -0700 | [diff] [blame] | 5393 | /* Insert an early preprocessor command that doesn't need special handling */ |
| 5394 | static void pp_pre_command(const char *what, char *string) |
| 5395 | { |
| 5396 | char *cmd; |
| 5397 | Token *def, *space; |
| 5398 | Line *l; |
| 5399 | |
| 5400 | def = tokenize(string); |
| 5401 | if (what) { |
| 5402 | cmd = nasm_strcat(what[0] == '%' ? "" : "%", what); |
| 5403 | space = new_Token(def, TOK_WHITESPACE, NULL, 0); |
| 5404 | def = new_Token(space, TOK_PREPROC_ID, cmd, 0); |
| 5405 | } |
| 5406 | |
| 5407 | l = nasm_malloc(sizeof(Line)); |
| 5408 | l->next = predef; |
| 5409 | l->first = def; |
| 5410 | l->finishes = NULL; |
| 5411 | predef = l; |
| 5412 | } |
| 5413 | |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 5414 | static void pp_add_stdmac(macros_t *macros) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5415 | { |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 5416 | macros_t **mp; |
| 5417 | |
| 5418 | /* Find the end of the list and avoid duplicates */ |
| 5419 | for (mp = stdmacros; *mp; mp++) { |
| 5420 | if (*mp == macros) |
| 5421 | return; /* Nothing to do */ |
| 5422 | } |
| 5423 | |
| 5424 | nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]); |
| 5425 | |
| 5426 | *mp = macros; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 5427 | } |
| 5428 | |
Cyrill Gorcunov | 15ce78f | 2017-01-06 20:21:28 +0300 | [diff] [blame] | 5429 | static void pp_extra_stdmac(macros_t *macros) |
| 5430 | { |
| 5431 | extrastdmac = macros; |
| 5432 | } |
| 5433 | |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 5434 | static void make_tok_num(Token * tok, int64_t val) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5435 | { |
Cyrill Gorcunov | ce65274 | 2013-05-06 23:43:43 +0400 | [diff] [blame] | 5436 | char numbuf[32]; |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 5437 | snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5438 | tok->text = nasm_strdup(numbuf); |
| 5439 | tok->type = TOK_NUMBER; |
| 5440 | } |
| 5441 | |
H. Peter Anvin | 3736895 | 2016-05-09 14:10:32 -0700 | [diff] [blame] | 5442 | static void pp_list_one_macro(MMacro *m, int severity) |
| 5443 | { |
| 5444 | if (!m) |
| 5445 | return; |
| 5446 | |
| 5447 | /* We need to print the next_active list in reverse order */ |
| 5448 | pp_list_one_macro(m->next_active, severity); |
| 5449 | |
| 5450 | if (m->name && !m->nolist) { |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 5451 | src_set(m->xline + m->lineno, m->fname); |
H. Peter Anvin | 3736895 | 2016-05-09 14:10:32 -0700 | [diff] [blame] | 5452 | nasm_error(severity, "... from macro `%s' defined here", m->name); |
| 5453 | } |
| 5454 | } |
| 5455 | |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5456 | static void pp_error_list_macros(int severity) |
| 5457 | { |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5458 | int32_t saved_line; |
| 5459 | const char *saved_fname = NULL; |
| 5460 | |
| 5461 | severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY; |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 5462 | src_get(&saved_line, &saved_fname); |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5463 | |
Cyrill Gorcunov | 771d04e | 2016-05-10 23:27:03 +0300 | [diff] [blame] | 5464 | if (istk) |
| 5465 | pp_list_one_macro(istk->mstk, severity); |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5466 | |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 5467 | src_set(saved_line, saved_fname); |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5468 | } |
| 5469 | |
H. Peter Anvin | e746971 | 2016-02-18 02:20:59 -0800 | [diff] [blame] | 5470 | const struct preproc_ops nasmpp = { |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 5471 | pp_init, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5472 | pp_reset, |
| 5473 | pp_getline, |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5474 | pp_cleanup, |
Cyrill Gorcunov | 15ce78f | 2017-01-06 20:21:28 +0300 | [diff] [blame] | 5475 | pp_extra_stdmac, |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5476 | pp_pre_define, |
| 5477 | pp_pre_undefine, |
| 5478 | pp_pre_include, |
H. Peter Anvin | 0599034 | 2018-06-11 13:32:42 -0700 | [diff] [blame] | 5479 | pp_pre_command, |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5480 | pp_include_path, |
| 5481 | pp_error_list_macros, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5482 | }; |