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 | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3 | * Copyright 1996-2016 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 | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 75 | #include "preproc.h" |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 76 | #include "hashtbl.h" |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 77 | #include "quote.h" |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 78 | #include "stdscan.h" |
H. Peter Anvin | dbb640b | 2009-07-18 18:57:16 -0700 | [diff] [blame] | 79 | #include "eval.h" |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 80 | #include "tokens.h" |
H. Peter Anvin | a4835d4 | 2008-05-20 14:21:29 -0700 | [diff] [blame] | 81 | #include "tables.h" |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 82 | #include "listing.h" |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 83 | |
| 84 | typedef struct SMacro SMacro; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 85 | typedef struct MMacro MMacro; |
| 86 | typedef struct MMacroInvocation MMacroInvocation; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 87 | typedef struct Context Context; |
| 88 | typedef struct Token Token; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 89 | typedef struct Blocks Blocks; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 90 | typedef struct Line Line; |
| 91 | typedef struct Include Include; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 92 | typedef struct Cond Cond; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 93 | typedef struct IncPath IncPath; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 94 | |
| 95 | /* |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 96 | * Note on the storage of both SMacro and MMacros: the hash table |
| 97 | * indexes them case-insensitively, and we then have to go through a |
| 98 | * linked list of potential case aliases (and, for MMacros, parameter |
| 99 | * ranges); this is to preserve the matching semantics of the earlier |
| 100 | * code. If the number of case aliases for a specific macro is a |
| 101 | * performance issue, you may want to reconsider your coding style. |
| 102 | */ |
| 103 | |
| 104 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 105 | * Store the definition of a single-line macro. |
| 106 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 107 | struct SMacro { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 108 | SMacro *next; |
| 109 | char *name; |
| 110 | bool casesense; |
| 111 | bool in_progress; |
| 112 | unsigned int nparam; |
| 113 | Token *expansion; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 117 | * Store the definition of a multi-line macro. This is also used to |
| 118 | * store the interiors of `%rep...%endrep' blocks, which are |
| 119 | * effectively self-re-invoking multi-line macros which simply |
| 120 | * don't have a name or bother to appear in the hash tables. %rep |
| 121 | * blocks are signified by having a NULL `name' field. |
| 122 | * |
| 123 | * In a MMacro describing a `%rep' block, the `in_progress' field |
| 124 | * isn't merely boolean, but gives the number of repeats left to |
| 125 | * run. |
| 126 | * |
| 127 | * The `next' field is used for storing MMacros in hash tables; the |
| 128 | * `next_active' field is for stacking them on istk entries. |
| 129 | * |
| 130 | * When a MMacro is being expanded, `params', `iline', `nparam', |
| 131 | * `paramlen', `rotate' and `unique' are local to the invocation. |
| 132 | */ |
| 133 | struct MMacro { |
| 134 | MMacro *next; |
| 135 | MMacroInvocation *prev; /* previous invocation */ |
| 136 | char *name; |
| 137 | int nparam_min, nparam_max; |
| 138 | bool casesense; |
| 139 | bool plus; /* is the last parameter greedy? */ |
| 140 | bool nolist; /* is this macro listing-inhibited? */ |
| 141 | int64_t in_progress; /* is this macro currently being expanded? */ |
| 142 | int32_t max_depth; /* maximum number of recursive expansions allowed */ |
| 143 | Token *dlist; /* All defaults as one list */ |
| 144 | Token **defaults; /* Parameter default pointers */ |
| 145 | int ndefs; /* number of default parameters */ |
| 146 | Line *expansion; |
| 147 | |
| 148 | MMacro *next_active; |
| 149 | MMacro *rep_nest; /* used for nesting %rep */ |
| 150 | Token **params; /* actual parameters */ |
| 151 | Token *iline; /* invocation line */ |
| 152 | unsigned int nparam, rotate; |
| 153 | int *paramlen; |
| 154 | uint64_t unique; |
| 155 | int lineno; /* Current line number on expansion */ |
| 156 | uint64_t condcnt; /* number of if blocks... */ |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 157 | |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 158 | const char *fname; /* File where defined */ |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 159 | int32_t xline; /* First line in macro */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | |
| 163 | /* Store the definition of a multi-line macro, as defined in a |
| 164 | * previous recursive macro expansion. |
| 165 | */ |
| 166 | struct MMacroInvocation { |
| 167 | MMacroInvocation *prev; /* previous invocation */ |
| 168 | Token **params; /* actual parameters */ |
| 169 | Token *iline; /* invocation line */ |
| 170 | unsigned int nparam, rotate; |
| 171 | int *paramlen; |
| 172 | uint64_t unique; |
| 173 | uint64_t condcnt; |
| 174 | }; |
| 175 | |
| 176 | |
| 177 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 178 | * The context stack is composed of a linked list of these. |
| 179 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 180 | struct Context { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 181 | Context *next; |
| 182 | char *name; |
| 183 | struct hash_table localmac; |
| 184 | uint32_t number; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | /* |
| 188 | * This is the internal form which we break input lines up into. |
| 189 | * Typically stored in linked lists. |
| 190 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 191 | * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not |
| 192 | * necessarily used as-is, but is intended to denote the number of |
| 193 | * the substituted parameter. So in the definition |
| 194 | * |
| 195 | * %define a(x,y) ( (x) & ~(y) ) |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 196 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 197 | * the token representing `x' will have its type changed to |
| 198 | * TOK_SMAC_PARAM, but the one representing `y' will be |
| 199 | * TOK_SMAC_PARAM+1. |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 200 | * |
| 201 | * TOK_INTERNAL_STRING is a dirty hack: it's a single string token |
| 202 | * which doesn't need quotes around it. Used in the pre-include |
| 203 | * mechanism as an alternative to trying to find a sensible type of |
| 204 | * quote to use on the filename we were passed. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 205 | */ |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 206 | enum pp_token_type { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 207 | TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID, |
| 208 | TOK_PREPROC_ID, TOK_STRING, |
| 209 | TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER, |
H. Peter Anvin | 6c81f0a | 2008-05-25 21:46:17 -0700 | [diff] [blame] | 210 | TOK_INTERNAL_STRING, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 211 | TOK_PREPROC_Q, TOK_PREPROC_QQ, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 212 | TOK_PASTE, /* %+ */ |
| 213 | TOK_INDIRECT, /* %[...] */ |
| 214 | TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */ |
| 215 | TOK_MAX = INT_MAX /* Keep compiler from reducing the range */ |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 216 | }; |
| 217 | |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 218 | #define PP_CONCAT_MASK(x) (1 << (x)) |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 219 | #define PP_CONCAT_MATCH(t, mask) (PP_CONCAT_MASK((t)->type) & mask) |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 220 | |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 221 | struct tokseq_match { |
| 222 | int mask_head; |
| 223 | int mask_tail; |
| 224 | }; |
| 225 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 226 | struct Token { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 227 | Token *next; |
| 228 | char *text; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 229 | union { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 230 | SMacro *mac; /* associated macro for TOK_SMAC_END */ |
| 231 | size_t len; /* scratch length field */ |
| 232 | } a; /* Auxiliary data */ |
| 233 | enum pp_token_type type; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 234 | }; |
| 235 | |
| 236 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 237 | * Multi-line macro definitions are stored as a linked list of |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 238 | * these, which is essentially a container to allow several linked |
| 239 | * lists of Tokens. |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 240 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 241 | * Note that in this module, linked lists are treated as stacks |
| 242 | * wherever possible. For this reason, Lines are _pushed_ on to the |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 243 | * `expansion' field in MMacro structures, so that the linked list, |
| 244 | * if walked, would give the macro lines in reverse order; this |
| 245 | * means that we can walk the list when expanding a macro, and thus |
| 246 | * push the lines on to the `expansion' field in _istk_ in reverse |
| 247 | * order (so that when popped back off they are in the right |
| 248 | * order). It may seem cockeyed, and it relies on my design having |
| 249 | * an even number of steps in, but it works... |
| 250 | * |
| 251 | * Some of these structures, rather than being actual lines, are |
| 252 | * markers delimiting the end of the expansion of a given macro. |
| 253 | * This is for use in the cycle-tracking and %rep-handling code. |
| 254 | * Such structures have `finishes' non-NULL, and `first' NULL. All |
| 255 | * others have `finishes' NULL, but `first' may still be NULL if |
| 256 | * the line is blank. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 257 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 258 | struct Line { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 259 | Line *next; |
| 260 | MMacro *finishes; |
| 261 | Token *first; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 262 | }; |
| 263 | |
| 264 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 265 | * To handle an arbitrary level of file inclusion, we maintain a |
| 266 | * stack (ie linked list) of these things. |
| 267 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 268 | struct Include { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 269 | Include *next; |
| 270 | FILE *fp; |
| 271 | Cond *conds; |
| 272 | Line *expansion; |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 273 | const char *fname; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 274 | int lineno, lineinc; |
| 275 | MMacro *mstk; /* stack of active macros/reps */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 276 | }; |
| 277 | |
| 278 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 279 | * Include search path. This is simply a list of strings which get |
| 280 | * prepended, in turn, to the name of an include file, in an |
| 281 | * attempt to find the file if it's not in the current directory. |
| 282 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 283 | struct IncPath { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 284 | IncPath *next; |
| 285 | char *path; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 286 | }; |
| 287 | |
| 288 | /* |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 289 | * File real name hash, so we don't have to re-search the include |
| 290 | * path for every pass (and potentially more than that if a file |
| 291 | * is used more than once.) |
| 292 | */ |
| 293 | struct hash_table FileHash; |
| 294 | |
| 295 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 296 | * Conditional assembly: we maintain a separate stack of these for |
| 297 | * each level of file inclusion. (The only reason we keep the |
| 298 | * stacks separate is to ensure that a stray `%endif' in a file |
| 299 | * included from within the true branch of a `%if' won't terminate |
| 300 | * it and cause confusion: instead, rightly, it'll cause an error.) |
| 301 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 302 | struct Cond { |
| 303 | Cond *next; |
| 304 | int state; |
| 305 | }; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 306 | enum { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 307 | /* |
| 308 | * These states are for use just after %if or %elif: IF_TRUE |
| 309 | * means the condition has evaluated to truth so we are |
| 310 | * currently emitting, whereas IF_FALSE means we are not |
| 311 | * currently emitting but will start doing so if a %else comes |
| 312 | * up. In these states, all directives are admissible: %elif, |
| 313 | * %else and %endif. (And of course %if.) |
| 314 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 315 | COND_IF_TRUE, COND_IF_FALSE, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 316 | /* |
| 317 | * These states come up after a %else: ELSE_TRUE means we're |
| 318 | * emitting, and ELSE_FALSE means we're not. In ELSE_* states, |
| 319 | * any %elif or %else will cause an error. |
| 320 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 321 | COND_ELSE_TRUE, COND_ELSE_FALSE, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 322 | /* |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 323 | * These states mean that we're not emitting now, and also that |
| 324 | * nothing until %endif will be emitted at all. COND_DONE is |
| 325 | * used when we've had our moment of emission |
| 326 | * and have now started seeing %elifs. COND_NEVER is used when |
| 327 | * the condition construct in question is contained within a |
| 328 | * non-emitting branch of a larger condition construct, |
| 329 | * or if there is an error. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 330 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 331 | COND_DONE, COND_NEVER |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 332 | }; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 333 | #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE ) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 334 | |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 335 | /* |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 336 | * These defines are used as the possible return values for do_directive |
| 337 | */ |
| 338 | #define NO_DIRECTIVE_FOUND 0 |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 339 | #define DIRECTIVE_FOUND 1 |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 340 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 341 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 342 | * This define sets the upper limit for smacro and recursive mmacro |
| 343 | * expansions |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 344 | */ |
| 345 | #define DEADMAN_LIMIT (1 << 20) |
| 346 | |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 347 | /* max reps */ |
| 348 | #define REP_LIMIT ((INT64_C(1) << 62)) |
| 349 | |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 350 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 351 | * Condition codes. Note that we use c_ prefix not C_ because C_ is |
| 352 | * used in nasm.h for the "real" condition codes. At _this_ level, |
| 353 | * we treat CXZ and ECXZ as condition codes, albeit non-invertible |
| 354 | * ones, so we need a different enum... |
| 355 | */ |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 356 | static const char * const conditions[] = { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 357 | "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le", |
| 358 | "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no", |
H. Peter Anvin | ce9be34 | 2007-09-12 00:22:29 +0000 | [diff] [blame] | 359 | "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z" |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 360 | }; |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 361 | enum pp_conds { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 362 | c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE, |
| 363 | 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] | 364 | c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z, |
| 365 | c_none = -1 |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 366 | }; |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 367 | static const enum pp_conds inverse_ccs[] = { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 368 | c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE, |
| 369 | 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] | 370 | 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] | 371 | }; |
| 372 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 373 | /* |
| 374 | * Directive names. |
| 375 | */ |
| 376 | /* If this is a an IF, ELIF, ELSE or ENDIF keyword */ |
| 377 | static int is_condition(enum preproc_token arg) |
| 378 | { |
| 379 | return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF); |
| 380 | } |
| 381 | |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 382 | /* For TASM compatibility we need to be able to recognise TASM compatible |
| 383 | * conditional compilation directives. Using the NASM pre-processor does |
| 384 | * not work, so we look for them specifically from the following list and |
| 385 | * then jam in the equivalent NASM directive into the input stream. |
| 386 | */ |
| 387 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 388 | enum { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 389 | TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI, |
| 390 | TM_IFNDEF, TM_INCLUDE, TM_LOCAL |
| 391 | }; |
| 392 | |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 393 | static const char * const tasm_directives[] = { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 394 | "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi", |
| 395 | "ifndef", "include", "local" |
| 396 | }; |
| 397 | |
| 398 | static int StackSize = 4; |
H. Peter Anvin | 6c8b2be | 2016-05-24 23:46:50 -0700 | [diff] [blame] | 399 | static const char *StackPointer = "ebp"; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 400 | static int ArgOffset = 8; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 401 | static int LocalOffset = 0; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 402 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 403 | static Context *cstk; |
| 404 | static Include *istk; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 405 | static IncPath *ipath = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 406 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 407 | static int pass; /* HACK: pass 0 = generate dependencies only */ |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 408 | static StrList **dephead; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 409 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 410 | static uint64_t unique; /* unique identifier numbers */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 411 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 412 | static Line *predef = NULL; |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 413 | static bool do_predef; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 414 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 415 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 416 | * The current set of multi-line macros we have defined. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 417 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 418 | static struct hash_table mmacros; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 419 | |
| 420 | /* |
| 421 | * The current set of single-line macros we have defined. |
| 422 | */ |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 423 | static struct hash_table smacros; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 424 | |
| 425 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 426 | * The multi-line macro we are currently defining, or the %rep |
| 427 | * block we are currently reading, if any. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 428 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 429 | static MMacro *defining; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 430 | |
Charles Crayne | d4200be | 2008-07-12 16:42:33 -0700 | [diff] [blame] | 431 | static uint64_t nested_mac_count; |
| 432 | static uint64_t nested_rep_count; |
| 433 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 434 | /* |
| 435 | * The number of macro parameters to allocate space for at a time. |
| 436 | */ |
| 437 | #define PARAM_DELTA 16 |
| 438 | |
| 439 | /* |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 440 | * The standard macro set: defined in macros.c in a set of arrays. |
| 441 | * This gives our position in any macro set, while we are processing it. |
| 442 | * The stdmacset is an array of such macro sets. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 443 | */ |
H. Peter Anvin | a70547f | 2008-07-19 21:44:26 -0700 | [diff] [blame] | 444 | static macros_t *stdmacpos; |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 445 | static macros_t **stdmacnext; |
| 446 | static macros_t *stdmacros[8]; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 447 | |
| 448 | /* |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 449 | * Tokens are allocated in blocks to improve speed |
| 450 | */ |
| 451 | #define TOKEN_BLOCKSIZE 4096 |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 452 | static Token *freeTokens = NULL; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 453 | struct Blocks { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 454 | Blocks *next; |
| 455 | void *chunk; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 456 | }; |
| 457 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 458 | static Blocks blocks = { NULL, NULL }; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 459 | |
| 460 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 461 | * Forward declarations. |
| 462 | */ |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 463 | static void pp_add_stdmac(macros_t *macros); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 464 | static Token *expand_mmac_params(Token * tline); |
| 465 | static Token *expand_smacro(Token * tline); |
| 466 | static Token *expand_id(Token * tline); |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 467 | static Context *get_ctx(const char *name, const char **namep); |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 468 | static void make_tok_num(Token * tok, int64_t val); |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 469 | static void pp_verror(int severity, const char *fmt, va_list ap); |
| 470 | static vefunc real_verror; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 471 | static void *new_Block(size_t size); |
| 472 | static void delete_Blocks(void); |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 473 | static Token *new_Token(Token * next, enum pp_token_type type, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 474 | const char *text, int txtlen); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 475 | static Token *delete_Token(Token * t); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 476 | |
| 477 | /* |
| 478 | * Macros for safe checking of token pointers, avoid *(NULL) |
| 479 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 480 | #define tok_type_(x,t) ((x) && (x)->type == (t)) |
| 481 | #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next |
| 482 | #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v))) |
| 483 | #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v)))) |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 484 | |
Cyrill Gorcunov | 194ba89 | 2011-06-30 01:16:35 +0400 | [diff] [blame] | 485 | /* |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 486 | * nasm_unquote with error if the string contains NUL characters. |
| 487 | * If the string contains NUL characters, issue an error and return |
| 488 | * the C len, i.e. truncate at the NUL. |
| 489 | */ |
| 490 | static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive) |
| 491 | { |
| 492 | size_t len = nasm_unquote(qstr, NULL); |
| 493 | size_t clen = strlen(qstr); |
| 494 | |
| 495 | if (len != clen) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 496 | nasm_error(ERR_NONFATAL, "NUL character in `%s' directive", |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 497 | pp_directives[directive]); |
| 498 | |
| 499 | return clen; |
| 500 | } |
| 501 | |
| 502 | /* |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 503 | * In-place reverse a list of tokens. |
| 504 | */ |
| 505 | static Token *reverse_tokens(Token *t) |
| 506 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 507 | Token *prev = NULL; |
| 508 | Token *next; |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 509 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 510 | while (t) { |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 511 | next = t->next; |
| 512 | t->next = prev; |
| 513 | prev = t; |
| 514 | t = next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 515 | } |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 516 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 517 | return prev; |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | /* |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 521 | * Handle TASM specific directives, which do not contain a % in |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 522 | * front of them. We do it here because I could not find any other |
| 523 | * place to do it for the moment, and it is a hack (ideally it would |
| 524 | * be nice to be able to use the NASM pre-processor to do it). |
| 525 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 526 | static char *check_tasm_directive(char *line) |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 527 | { |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 528 | int32_t i, j, k, m, len; |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 529 | char *p, *q, *oldline, oldchar; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 530 | |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 531 | p = nasm_skip_spaces(line); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 532 | |
| 533 | /* Binary search for the directive name */ |
| 534 | i = -1; |
Cyrill Gorcunov | a731924 | 2010-06-03 22:04:36 +0400 | [diff] [blame] | 535 | j = ARRAY_SIZE(tasm_directives); |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 536 | q = nasm_skip_word(p); |
| 537 | len = q - p; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 538 | if (len) { |
| 539 | oldchar = p[len]; |
| 540 | p[len] = 0; |
| 541 | while (j - i > 1) { |
| 542 | k = (j + i) / 2; |
| 543 | m = nasm_stricmp(p, tasm_directives[k]); |
| 544 | if (m == 0) { |
| 545 | /* We have found a directive, so jam a % in front of it |
| 546 | * so that NASM will then recognise it as one if it's own. |
| 547 | */ |
| 548 | p[len] = oldchar; |
| 549 | len = strlen(p); |
| 550 | oldline = line; |
| 551 | line = nasm_malloc(len + 2); |
| 552 | line[0] = '%'; |
| 553 | if (k == TM_IFDIFI) { |
H. Peter Anvin | 18f4879 | 2009-06-27 15:56:27 -0700 | [diff] [blame] | 554 | /* |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 555 | * NASM does not recognise IFDIFI, so we convert |
| 556 | * it to %if 0. This is not used in NASM |
| 557 | * compatible code, but does need to parse for the |
| 558 | * TASM macro package. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 559 | */ |
H. Peter Anvin | 18f4879 | 2009-06-27 15:56:27 -0700 | [diff] [blame] | 560 | strcpy(line + 1, "if 0"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 561 | } else { |
| 562 | memcpy(line + 1, p, len + 1); |
| 563 | } |
| 564 | nasm_free(oldline); |
| 565 | return line; |
| 566 | } else if (m < 0) { |
| 567 | j = k; |
| 568 | } else |
| 569 | i = k; |
| 570 | } |
| 571 | p[len] = oldchar; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 572 | } |
| 573 | return line; |
| 574 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 575 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 576 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 577 | * The pre-preprocessing stage... This function translates line |
| 578 | * number indications as they emerge from GNU cpp (`# lineno "file" |
| 579 | * flags') into NASM preprocessor line number indications (`%line |
| 580 | * lineno file'). |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 581 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 582 | static char *prepreproc(char *line) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 583 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 584 | int lineno, fnlen; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 585 | char *fname, *oldline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 586 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 587 | if (line[0] == '#' && line[1] == ' ') { |
| 588 | oldline = line; |
| 589 | fname = oldline + 2; |
| 590 | lineno = atoi(fname); |
| 591 | fname += strspn(fname, "0123456789 "); |
| 592 | if (*fname == '"') |
| 593 | fname++; |
| 594 | fnlen = strcspn(fname, "\""); |
| 595 | line = nasm_malloc(20 + fnlen); |
| 596 | snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname); |
| 597 | nasm_free(oldline); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 598 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 599 | if (tasm_compatible_mode) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 600 | return check_tasm_directive(line); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 601 | return line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 605 | * Free a linked list of tokens. |
| 606 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 607 | static void free_tlist(Token * list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 608 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 609 | while (list) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 610 | list = delete_Token(list); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | /* |
| 614 | * Free a linked list of lines. |
| 615 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 616 | static void free_llist(Line * list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 617 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 618 | Line *l, *tmp; |
| 619 | list_for_each_safe(l, tmp, list) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 620 | free_tlist(l->first); |
| 621 | nasm_free(l); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 622 | } |
| 623 | } |
| 624 | |
| 625 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 626 | * Free an MMacro |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 627 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 628 | static void free_mmacro(MMacro * m) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 629 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 630 | nasm_free(m->name); |
| 631 | free_tlist(m->dlist); |
| 632 | nasm_free(m->defaults); |
| 633 | free_llist(m->expansion); |
| 634 | nasm_free(m); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | /* |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 638 | * Free all currently defined macros, and free the hash tables |
| 639 | */ |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 640 | static void free_smacro_table(struct hash_table *smt) |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 641 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 642 | SMacro *s, *tmp; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 643 | const char *key; |
| 644 | struct hash_tbl_node *it = NULL; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 645 | |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 646 | while ((s = hash_iterate(smt, &it, &key)) != NULL) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 647 | nasm_free((void *)key); |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 648 | list_for_each_safe(s, tmp, s) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 649 | nasm_free(s->name); |
| 650 | free_tlist(s->expansion); |
| 651 | nasm_free(s); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 652 | } |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 653 | } |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 654 | hash_free(smt); |
| 655 | } |
| 656 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 657 | static void free_mmacro_table(struct hash_table *mmt) |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 658 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 659 | MMacro *m, *tmp; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 660 | const char *key; |
| 661 | struct hash_tbl_node *it = NULL; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 662 | |
| 663 | it = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 664 | while ((m = hash_iterate(mmt, &it, &key)) != NULL) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 665 | nasm_free((void *)key); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 666 | list_for_each_safe(m ,tmp, m) |
| 667 | free_mmacro(m); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 668 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 669 | hash_free(mmt); |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | static void free_macros(void) |
| 673 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 674 | free_smacro_table(&smacros); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 675 | free_mmacro_table(&mmacros); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | /* |
| 679 | * Initialize the hash tables |
| 680 | */ |
| 681 | static void init_macros(void) |
| 682 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 683 | hash_init(&smacros, HASH_LARGE); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 684 | hash_init(&mmacros, HASH_LARGE); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 688 | * Pop the context stack. |
| 689 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 690 | static void ctx_pop(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 691 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 692 | Context *c = cstk; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 693 | |
| 694 | cstk = cstk->next; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 695 | free_smacro_table(&c->localmac); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 696 | nasm_free(c->name); |
| 697 | nasm_free(c); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 698 | } |
| 699 | |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 700 | /* |
| 701 | * Search for a key in the hash index; adding it if necessary |
| 702 | * (in which case we initialize the data pointer to NULL.) |
| 703 | */ |
| 704 | static void ** |
| 705 | hash_findi_add(struct hash_table *hash, const char *str) |
| 706 | { |
| 707 | struct hash_insert hi; |
| 708 | void **r; |
| 709 | char *strx; |
| 710 | |
| 711 | r = hash_findi(hash, str, &hi); |
| 712 | if (r) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 713 | return r; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 714 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 715 | strx = nasm_strdup(str); /* Use a more efficient allocator here? */ |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 716 | return hash_add(&hi, strx, NULL); |
| 717 | } |
| 718 | |
| 719 | /* |
| 720 | * Like hash_findi, but returns the data element rather than a pointer |
| 721 | * to it. Used only when not adding a new element, hence no third |
| 722 | * argument. |
| 723 | */ |
| 724 | static void * |
| 725 | hash_findix(struct hash_table *hash, const char *str) |
| 726 | { |
| 727 | void **p; |
| 728 | |
| 729 | p = hash_findi(hash, str, NULL); |
| 730 | return p ? *p : NULL; |
| 731 | } |
| 732 | |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 733 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 734 | * read line from standart macros set, |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 735 | * if there no more left -- return NULL |
| 736 | */ |
| 737 | static char *line_from_stdmac(void) |
| 738 | { |
| 739 | unsigned char c; |
| 740 | const unsigned char *p = stdmacpos; |
| 741 | char *line, *q; |
| 742 | size_t len = 0; |
| 743 | |
| 744 | if (!stdmacpos) |
| 745 | return NULL; |
| 746 | |
| 747 | while ((c = *p++)) { |
| 748 | if (c >= 0x80) |
| 749 | len += pp_directives_len[c - 0x80] + 1; |
| 750 | else |
| 751 | len++; |
| 752 | } |
| 753 | |
| 754 | line = nasm_malloc(len + 1); |
| 755 | q = line; |
| 756 | while ((c = *stdmacpos++)) { |
| 757 | if (c >= 0x80) { |
| 758 | memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]); |
| 759 | q += pp_directives_len[c - 0x80]; |
| 760 | *q++ = ' '; |
| 761 | } else { |
| 762 | *q++ = c; |
| 763 | } |
| 764 | } |
| 765 | stdmacpos = p; |
| 766 | *q = '\0'; |
| 767 | |
| 768 | if (!*stdmacpos) { |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 769 | /* This was the last of this particular macro set */ |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 770 | stdmacpos = NULL; |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 771 | if (*stdmacnext) { |
| 772 | stdmacpos = *stdmacnext++; |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 773 | } else if (do_predef) { |
| 774 | Line *pd, *l; |
| 775 | Token *head, **tail, *t; |
| 776 | |
| 777 | /* |
| 778 | * Nasty hack: here we push the contents of |
| 779 | * `predef' on to the top-level expansion stack, |
| 780 | * since this is the most convenient way to |
| 781 | * implement the pre-include and pre-define |
| 782 | * features. |
| 783 | */ |
| 784 | list_for_each(pd, predef) { |
| 785 | head = NULL; |
| 786 | tail = &head; |
| 787 | list_for_each(t, pd->first) { |
| 788 | *tail = new_Token(NULL, t->type, t->text, 0); |
| 789 | tail = &(*tail)->next; |
| 790 | } |
| 791 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 792 | l = nasm_malloc(sizeof(Line)); |
| 793 | l->next = istk->expansion; |
| 794 | l->first = head; |
| 795 | l->finishes = NULL; |
| 796 | |
| 797 | istk->expansion = l; |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 798 | } |
| 799 | do_predef = false; |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | return line; |
| 804 | } |
| 805 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 806 | static char *read_line(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 807 | { |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 808 | unsigned int size, c, next; |
| 809 | const unsigned int delta = 512; |
| 810 | const unsigned int pad = 8; |
| 811 | unsigned int nr_cont = 0; |
| 812 | bool cont = false; |
| 813 | char *buffer, *p; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 814 | |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 815 | /* Standart macros set (predefined) goes first */ |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 816 | p = line_from_stdmac(); |
| 817 | if (p) |
| 818 | return p; |
H. Peter Anvin | 72edbb8 | 2008-06-19 16:00:04 -0700 | [diff] [blame] | 819 | |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 820 | size = delta; |
| 821 | p = buffer = nasm_malloc(size); |
| 822 | |
| 823 | for (;;) { |
| 824 | c = fgetc(istk->fp); |
| 825 | if ((int)(c) == EOF) { |
| 826 | p[0] = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 827 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 828 | } |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 829 | |
| 830 | switch (c) { |
| 831 | case '\r': |
| 832 | next = fgetc(istk->fp); |
| 833 | if (next != '\n') |
| 834 | ungetc(next, istk->fp); |
| 835 | if (cont) { |
| 836 | cont = false; |
| 837 | continue; |
| 838 | } |
| 839 | break; |
| 840 | |
| 841 | case '\n': |
| 842 | if (cont) { |
| 843 | cont = false; |
| 844 | continue; |
| 845 | } |
| 846 | break; |
| 847 | |
| 848 | case '\\': |
| 849 | next = fgetc(istk->fp); |
| 850 | ungetc(next, istk->fp); |
Cyrill Gorcunov | 490f85e | 2012-12-27 20:02:17 +0400 | [diff] [blame] | 851 | if (next == '\r' || next == '\n') { |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 852 | cont = true; |
| 853 | nr_cont++; |
| 854 | continue; |
| 855 | } |
| 856 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 857 | } |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 858 | |
| 859 | if (c == '\r' || c == '\n') { |
| 860 | *p++ = 0; |
| 861 | break; |
| 862 | } |
| 863 | |
| 864 | if (p >= (buffer + size - pad)) { |
| 865 | buffer = nasm_realloc(buffer, size + delta); |
| 866 | p = buffer + size - pad; |
| 867 | size += delta; |
| 868 | } |
| 869 | |
| 870 | *p++ = (unsigned char)c; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 871 | } |
| 872 | |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 873 | if (p == buffer) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 874 | nasm_free(buffer); |
| 875 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 876 | } |
| 877 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 878 | src_set_linnum(src_get_linnum() + istk->lineinc + |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 879 | (nr_cont * istk->lineinc)); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 880 | |
| 881 | /* |
| 882 | * Handle spurious ^Z, which may be inserted into source files |
| 883 | * by some file transfer utilities. |
| 884 | */ |
| 885 | buffer[strcspn(buffer, "\032")] = '\0'; |
| 886 | |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 887 | lfmt->line(LIST_READ, buffer); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 888 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 889 | return buffer; |
| 890 | } |
| 891 | |
| 892 | /* |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 893 | * 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] | 894 | * don't need to parse the value out of e.g. numeric tokens: we |
| 895 | * simply split one string into many. |
| 896 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 897 | static Token *tokenize(char *line) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 898 | { |
H. Peter Anvin | ca544db | 2008-10-19 19:30:11 -0700 | [diff] [blame] | 899 | char c, *p = line; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 900 | enum pp_token_type type; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 901 | Token *list = NULL; |
| 902 | Token *t, **tail = &list; |
| 903 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 904 | while (*line) { |
| 905 | p = line; |
| 906 | if (*p == '%') { |
| 907 | p++; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 908 | if (*p == '+' && !nasm_isdigit(p[1])) { |
| 909 | p++; |
| 910 | type = TOK_PASTE; |
| 911 | } else if (nasm_isdigit(*p) || |
| 912 | ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 913 | do { |
| 914 | p++; |
| 915 | } |
H. Peter Anvin | bda7a6e | 2008-06-21 10:23:17 -0700 | [diff] [blame] | 916 | while (nasm_isdigit(*p)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 917 | type = TOK_PREPROC_ID; |
| 918 | } else if (*p == '{') { |
| 919 | p++; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 920 | while (*p) { |
| 921 | if (*p == '}') |
| 922 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 923 | p[-1] = *p; |
| 924 | p++; |
| 925 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 926 | if (*p != '}') |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 927 | nasm_error(ERR_WARNING | ERR_PASS1, |
| 928 | "unterminated %%{ construct"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 929 | p[-1] = '\0'; |
| 930 | if (*p) |
| 931 | p++; |
| 932 | type = TOK_PREPROC_ID; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 933 | } else if (*p == '[') { |
| 934 | int lvl = 1; |
| 935 | line += 2; /* Skip the leading %[ */ |
| 936 | p++; |
| 937 | while (lvl && (c = *p++)) { |
| 938 | switch (c) { |
| 939 | case ']': |
| 940 | lvl--; |
| 941 | break; |
| 942 | case '%': |
| 943 | if (*p == '[') |
| 944 | lvl++; |
| 945 | break; |
| 946 | case '\'': |
| 947 | case '\"': |
| 948 | case '`': |
Cyrill Gorcunov | c6360a7 | 2010-07-13 13:32:19 +0400 | [diff] [blame] | 949 | p = nasm_skip_string(p - 1) + 1; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 950 | break; |
| 951 | default: |
| 952 | break; |
| 953 | } |
| 954 | } |
| 955 | p--; |
| 956 | if (*p) |
| 957 | *p++ = '\0'; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 958 | if (lvl) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 959 | nasm_error(ERR_NONFATAL|ERR_PASS1, |
| 960 | "unterminated %%[ construct"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 961 | type = TOK_INDIRECT; |
| 962 | } else if (*p == '?') { |
| 963 | type = TOK_PREPROC_Q; /* %? */ |
| 964 | p++; |
| 965 | if (*p == '?') { |
| 966 | type = TOK_PREPROC_QQ; /* %?? */ |
| 967 | p++; |
| 968 | } |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 969 | } else if (*p == '!') { |
| 970 | type = TOK_PREPROC_ID; |
| 971 | p++; |
| 972 | if (isidchar(*p)) { |
| 973 | do { |
| 974 | p++; |
| 975 | } |
| 976 | while (isidchar(*p)); |
| 977 | } else if (*p == '\'' || *p == '\"' || *p == '`') { |
| 978 | p = nasm_skip_string(p); |
| 979 | if (*p) |
| 980 | p++; |
| 981 | else |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 982 | nasm_error(ERR_NONFATAL|ERR_PASS1, |
| 983 | "unterminated %%! string"); |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 984 | } else { |
| 985 | /* %! without string or identifier */ |
| 986 | type = TOK_OTHER; /* Legacy behavior... */ |
| 987 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 988 | } else if (isidchar(*p) || |
| 989 | ((*p == '!' || *p == '%' || *p == '$') && |
| 990 | isidchar(p[1]))) { |
| 991 | do { |
| 992 | p++; |
| 993 | } |
| 994 | while (isidchar(*p)); |
| 995 | type = TOK_PREPROC_ID; |
| 996 | } else { |
| 997 | type = TOK_OTHER; |
| 998 | if (*p == '%') |
| 999 | p++; |
| 1000 | } |
| 1001 | } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) { |
| 1002 | type = TOK_ID; |
| 1003 | p++; |
| 1004 | while (*p && isidchar(*p)) |
| 1005 | p++; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 1006 | } else if (*p == '\'' || *p == '"' || *p == '`') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1007 | /* |
| 1008 | * A string token. |
| 1009 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1010 | type = TOK_STRING; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1011 | p = nasm_skip_string(p); |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1012 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1013 | if (*p) { |
| 1014 | p++; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1015 | } else { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1016 | nasm_error(ERR_WARNING|ERR_PASS1, "unterminated string"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1017 | /* Handling unterminated strings by UNV */ |
| 1018 | /* type = -1; */ |
| 1019 | } |
Victor van den Elzen | fb5f251 | 2009-04-17 16:17:59 +0200 | [diff] [blame] | 1020 | } else if (p[0] == '$' && p[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1021 | type = TOK_OTHER; /* TOKEN_BASE */ |
Victor van den Elzen | fb5f251 | 2009-04-17 16:17:59 +0200 | [diff] [blame] | 1022 | p += 2; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1023 | } else if (isnumstart(*p)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1024 | bool is_hex = false; |
| 1025 | bool is_float = false; |
| 1026 | bool has_e = false; |
| 1027 | char c, *r; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1028 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1029 | /* |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1030 | * A numeric token. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1031 | */ |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1032 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1033 | if (*p == '$') { |
| 1034 | p++; |
| 1035 | is_hex = true; |
| 1036 | } |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1037 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1038 | for (;;) { |
| 1039 | c = *p++; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1040 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1041 | if (!is_hex && (c == 'e' || c == 'E')) { |
| 1042 | has_e = true; |
| 1043 | if (*p == '+' || *p == '-') { |
| 1044 | /* |
| 1045 | * e can only be followed by +/- if it is either a |
| 1046 | * prefixed hex number or a floating-point number |
| 1047 | */ |
| 1048 | p++; |
| 1049 | is_float = true; |
| 1050 | } |
| 1051 | } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') { |
| 1052 | is_hex = true; |
| 1053 | } else if (c == 'P' || c == 'p') { |
| 1054 | is_float = true; |
| 1055 | if (*p == '+' || *p == '-') |
| 1056 | p++; |
| 1057 | } else if (isnumchar(c) || c == '_') |
| 1058 | ; /* just advance */ |
| 1059 | else if (c == '.') { |
| 1060 | /* |
| 1061 | * we need to deal with consequences of the legacy |
| 1062 | * parser, like "1.nolist" being two tokens |
| 1063 | * (TOK_NUMBER, TOK_ID) here; at least give it |
| 1064 | * a shot for now. In the future, we probably need |
| 1065 | * a flex-based scanner with proper pattern matching |
| 1066 | * to do it as well as it can be done. Nothing in |
| 1067 | * the world is going to help the person who wants |
| 1068 | * 0x123.p16 interpreted as two tokens, though. |
| 1069 | */ |
| 1070 | r = p; |
| 1071 | while (*r == '_') |
| 1072 | r++; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1073 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1074 | if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) || |
| 1075 | (!is_hex && (*r == 'e' || *r == 'E')) || |
| 1076 | (*r == 'p' || *r == 'P')) { |
| 1077 | p = r; |
| 1078 | is_float = true; |
| 1079 | } else |
| 1080 | break; /* Terminate the token */ |
| 1081 | } else |
| 1082 | break; |
| 1083 | } |
| 1084 | p--; /* Point to first character beyond number */ |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1085 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1086 | if (p == line+1 && *line == '$') { |
| 1087 | type = TOK_OTHER; /* TOKEN_HERE */ |
| 1088 | } else { |
| 1089 | if (has_e && !is_hex) { |
| 1090 | /* 1e13 is floating-point, but 1e13h is not */ |
| 1091 | is_float = true; |
| 1092 | } |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 1093 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1094 | type = is_float ? TOK_FLOAT : TOK_NUMBER; |
| 1095 | } |
H. Peter Anvin | bda7a6e | 2008-06-21 10:23:17 -0700 | [diff] [blame] | 1096 | } else if (nasm_isspace(*p)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1097 | type = TOK_WHITESPACE; |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 1098 | p = nasm_skip_spaces(p); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1099 | /* |
| 1100 | * Whitespace just before end-of-line is discarded by |
| 1101 | * pretending it's a comment; whitespace just before a |
| 1102 | * comment gets lumped into the comment. |
| 1103 | */ |
| 1104 | if (!*p || *p == ';') { |
| 1105 | type = TOK_COMMENT; |
| 1106 | while (*p) |
| 1107 | p++; |
| 1108 | } |
| 1109 | } else if (*p == ';') { |
| 1110 | type = TOK_COMMENT; |
| 1111 | while (*p) |
| 1112 | p++; |
| 1113 | } else { |
| 1114 | /* |
| 1115 | * Anything else is an operator of some kind. We check |
| 1116 | * for all the double-character operators (>>, <<, //, |
| 1117 | * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1118 | * else is a single-character operator. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1119 | */ |
| 1120 | type = TOK_OTHER; |
| 1121 | if ((p[0] == '>' && p[1] == '>') || |
| 1122 | (p[0] == '<' && p[1] == '<') || |
| 1123 | (p[0] == '/' && p[1] == '/') || |
| 1124 | (p[0] == '<' && p[1] == '=') || |
| 1125 | (p[0] == '>' && p[1] == '=') || |
| 1126 | (p[0] == '=' && p[1] == '=') || |
| 1127 | (p[0] == '!' && p[1] == '=') || |
| 1128 | (p[0] == '<' && p[1] == '>') || |
| 1129 | (p[0] == '&' && p[1] == '&') || |
| 1130 | (p[0] == '|' && p[1] == '|') || |
| 1131 | (p[0] == '^' && p[1] == '^')) { |
| 1132 | p++; |
| 1133 | } |
| 1134 | p++; |
| 1135 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1136 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1137 | /* Handling unterminated string by UNV */ |
| 1138 | /*if (type == -1) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1139 | { |
| 1140 | *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1); |
| 1141 | t->text[p-line] = *line; |
| 1142 | tail = &t->next; |
| 1143 | } |
| 1144 | else */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1145 | if (type != TOK_COMMENT) { |
| 1146 | *tail = t = new_Token(NULL, type, line, p - line); |
| 1147 | tail = &t->next; |
| 1148 | } |
| 1149 | line = p; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1150 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1151 | return list; |
| 1152 | } |
| 1153 | |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1154 | /* |
| 1155 | * this function allocates a new managed block of memory and |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1156 | * returns a pointer to the block. The managed blocks are |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1157 | * deleted only all at once by the delete_Blocks function. |
| 1158 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1159 | static void *new_Block(size_t size) |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1160 | { |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1161 | Blocks *b = &blocks; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1162 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1163 | /* first, get to the end of the linked list */ |
| 1164 | while (b->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1165 | b = b->next; |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1166 | /* now allocate the requested chunk */ |
| 1167 | b->chunk = nasm_malloc(size); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1168 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1169 | /* now allocate a new block for the next request */ |
Cyrill Gorcunov | c31767c | 2014-06-28 02:22:17 +0400 | [diff] [blame] | 1170 | b->next = nasm_zalloc(sizeof(Blocks)); |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1171 | return b->chunk; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | /* |
| 1175 | * this function deletes all managed blocks of memory |
| 1176 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1177 | static void delete_Blocks(void) |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1178 | { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1179 | Blocks *a, *b = &blocks; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1180 | |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1181 | /* |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1182 | * keep in mind that the first block, pointed to by blocks |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1183 | * is a static and not dynamically allocated, so we don't |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1184 | * free it. |
| 1185 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1186 | while (b) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1187 | if (b->chunk) |
| 1188 | nasm_free(b->chunk); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1189 | a = b; |
| 1190 | b = b->next; |
| 1191 | if (a != &blocks) |
| 1192 | nasm_free(a); |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1193 | } |
Cyrill Gorcunov | dae24d7 | 2014-06-28 10:17:39 +0400 | [diff] [blame] | 1194 | memset(&blocks, 0, sizeof(blocks)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1195 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1196 | |
| 1197 | /* |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1198 | * this function creates a new Token and passes a pointer to it |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1199 | * 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] | 1200 | * also the a.mac and next elements to NULL. |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1201 | */ |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1202 | static Token *new_Token(Token * next, enum pp_token_type type, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1203 | const char *text, int txtlen) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1204 | { |
| 1205 | Token *t; |
| 1206 | int i; |
| 1207 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1208 | if (!freeTokens) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1209 | freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token)); |
| 1210 | for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++) |
| 1211 | freeTokens[i].next = &freeTokens[i + 1]; |
| 1212 | freeTokens[i].next = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1213 | } |
| 1214 | t = freeTokens; |
| 1215 | freeTokens = t->next; |
| 1216 | t->next = next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 1217 | t->a.mac = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1218 | t->type = type; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1219 | if (type == TOK_WHITESPACE || !text) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1220 | t->text = NULL; |
| 1221 | } else { |
| 1222 | if (txtlen == 0) |
| 1223 | txtlen = strlen(text); |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1224 | t->text = nasm_malloc(txtlen+1); |
| 1225 | memcpy(t->text, text, txtlen); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1226 | t->text[txtlen] = '\0'; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1227 | } |
| 1228 | return t; |
| 1229 | } |
| 1230 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1231 | static Token *delete_Token(Token * t) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1232 | { |
| 1233 | Token *next = t->next; |
| 1234 | nasm_free(t->text); |
H. Peter Anvin | 788e6c1 | 2002-04-30 21:02:01 +0000 | [diff] [blame] | 1235 | t->next = freeTokens; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1236 | freeTokens = t; |
| 1237 | return next; |
| 1238 | } |
| 1239 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1240 | /* |
| 1241 | * Convert a line of tokens back into text. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1242 | * If expand_locals is not zero, identifiers of the form "%$*xxx" |
| 1243 | * will be transformed into ..@ctxnum.xxx |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1244 | */ |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 1245 | static char *detoken(Token * tlist, bool expand_locals) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1246 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1247 | Token *t; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1248 | char *line, *p; |
H. Peter Anvin | b4daadc | 2008-01-21 16:31:57 -0800 | [diff] [blame] | 1249 | const char *q; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1250 | int len = 0; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1251 | |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1252 | list_for_each(t, tlist) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1253 | if (t->type == TOK_PREPROC_ID && t->text[1] == '!') { |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1254 | char *v; |
| 1255 | char *q = t->text; |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1256 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1257 | v = t->text + 2; |
| 1258 | if (*v == '\'' || *v == '\"' || *v == '`') { |
| 1259 | size_t len = nasm_unquote(v, NULL); |
| 1260 | size_t clen = strlen(v); |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1261 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1262 | if (len != clen) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1263 | nasm_error(ERR_NONFATAL | ERR_PASS1, |
| 1264 | "NUL character in %%! string"); |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1265 | v = NULL; |
| 1266 | } |
| 1267 | } |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1268 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1269 | if (v) { |
| 1270 | char *p = getenv(v); |
| 1271 | if (!p) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1272 | nasm_error(ERR_NONFATAL | ERR_PASS1, |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1273 | "nonexistent environment variable `%s'", v); |
Cyrill Gorcunov | bbb7a1a | 2016-06-19 12:15:24 +0300 | [diff] [blame] | 1274 | /* |
| 1275 | * FIXME We better should investigate if accessing |
| 1276 | * ->text[1] without ->text[0] is safe enough. |
| 1277 | */ |
| 1278 | t->text = nasm_zalloc(2); |
| 1279 | } else |
| 1280 | t->text = nasm_strdup(p); |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1281 | } |
| 1282 | nasm_free(q); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1283 | } |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1284 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1285 | /* Expand local macros here and not during preprocessing */ |
| 1286 | if (expand_locals && |
| 1287 | t->type == TOK_PREPROC_ID && t->text && |
| 1288 | t->text[0] == '%' && t->text[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1289 | const char *q; |
| 1290 | char *p; |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1291 | Context *ctx = get_ctx(t->text, &q); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1292 | if (ctx) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1293 | char buffer[40]; |
Keith Kanios | 93f2e9a | 2007-04-14 00:10:59 +0000 | [diff] [blame] | 1294 | snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1295 | p = nasm_strcat(buffer, q); |
| 1296 | nasm_free(t->text); |
| 1297 | t->text = p; |
| 1298 | } |
| 1299 | } |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1300 | if (t->type == TOK_WHITESPACE) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1301 | len++; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1302 | else if (t->text) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1303 | len += strlen(t->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1304 | } |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1305 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1306 | p = line = nasm_malloc(len + 1); |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1307 | |
| 1308 | list_for_each(t, tlist) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1309 | if (t->type == TOK_WHITESPACE) { |
H. Peter Anvin | b4daadc | 2008-01-21 16:31:57 -0800 | [diff] [blame] | 1310 | *p++ = ' '; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1311 | } else if (t->text) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1312 | q = t->text; |
| 1313 | while (*q) |
| 1314 | *p++ = *q++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1315 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1316 | } |
| 1317 | *p = '\0'; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1318 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1319 | return line; |
| 1320 | } |
| 1321 | |
| 1322 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1323 | * A scanner, suitable for use by the expression evaluator, which |
| 1324 | * operates on a line of Tokens. Expects a pointer to a pointer to |
| 1325 | * the first token in the line to be passed in as its private_data |
| 1326 | * field. |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1327 | * |
| 1328 | * FIX: This really needs to be unified with stdscan. |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1329 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1330 | static int ppscan(void *private_data, struct tokenval *tokval) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1331 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1332 | Token **tlineptr = private_data; |
| 1333 | Token *tline; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1334 | char ourcopy[MAX_KEYWORD+1], *p, *r, *s; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1335 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1336 | do { |
| 1337 | tline = *tlineptr; |
| 1338 | *tlineptr = tline ? tline->next : NULL; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 1339 | } while (tline && (tline->type == TOK_WHITESPACE || |
| 1340 | tline->type == TOK_COMMENT)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1341 | |
| 1342 | if (!tline) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1343 | return tokval->t_type = TOKEN_EOS; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1344 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1345 | tokval->t_charptr = tline->text; |
| 1346 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1347 | if (tline->text[0] == '$' && !tline->text[1]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1348 | return tokval->t_type = TOKEN_HERE; |
H. Peter Anvin | 7cf897e | 2002-05-30 21:30:33 +0000 | [diff] [blame] | 1349 | if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1350 | return tokval->t_type = TOKEN_BASE; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1351 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1352 | if (tline->type == TOK_ID) { |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1353 | p = tokval->t_charptr = tline->text; |
| 1354 | if (p[0] == '$') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1355 | tokval->t_charptr++; |
| 1356 | return tokval->t_type = TOKEN_ID; |
| 1357 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1358 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1359 | for (r = p, s = ourcopy; *r; r++) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1360 | if (r >= p+MAX_KEYWORD) |
| 1361 | return tokval->t_type = TOKEN_ID; /* Not a keyword */ |
H. Peter Anvin | ac8f8fc | 2008-06-11 15:49:41 -0700 | [diff] [blame] | 1362 | *s++ = nasm_tolower(*r); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1363 | } |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1364 | *s = '\0'; |
| 1365 | /* right, so we have an identifier sitting in temp storage. now, |
| 1366 | * is it actually a register or instruction name, or what? */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1367 | return nasm_token_hash(ourcopy, tokval); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1368 | } |
| 1369 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1370 | if (tline->type == TOK_NUMBER) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1371 | bool rn_error; |
| 1372 | tokval->t_integer = readnum(tline->text, &rn_error); |
| 1373 | tokval->t_charptr = tline->text; |
| 1374 | if (rn_error) |
| 1375 | return tokval->t_type = TOKEN_ERRNUM; |
| 1376 | else |
| 1377 | return tokval->t_type = TOKEN_NUM; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1378 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1379 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1380 | if (tline->type == TOK_FLOAT) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1381 | return tokval->t_type = TOKEN_FLOAT; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1382 | } |
| 1383 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1384 | if (tline->type == TOK_STRING) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1385 | char bq, *ep; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1386 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1387 | bq = tline->text[0]; |
H. Peter Anvin | 1162704 | 2008-06-09 20:45:19 -0700 | [diff] [blame] | 1388 | tokval->t_charptr = tline->text; |
| 1389 | tokval->t_inttwo = nasm_unquote(tline->text, &ep); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 1390 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1391 | if (ep[0] != bq || ep[1] != '\0') |
| 1392 | return tokval->t_type = TOKEN_ERRSTR; |
| 1393 | else |
| 1394 | return tokval->t_type = TOKEN_STR; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1395 | } |
| 1396 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1397 | if (tline->type == TOK_OTHER) { |
| 1398 | if (!strcmp(tline->text, "<<")) |
| 1399 | return tokval->t_type = TOKEN_SHL; |
| 1400 | if (!strcmp(tline->text, ">>")) |
| 1401 | return tokval->t_type = TOKEN_SHR; |
| 1402 | if (!strcmp(tline->text, "//")) |
| 1403 | return tokval->t_type = TOKEN_SDIV; |
| 1404 | if (!strcmp(tline->text, "%%")) |
| 1405 | return tokval->t_type = TOKEN_SMOD; |
| 1406 | if (!strcmp(tline->text, "==")) |
| 1407 | return tokval->t_type = TOKEN_EQ; |
| 1408 | if (!strcmp(tline->text, "<>")) |
| 1409 | return tokval->t_type = TOKEN_NE; |
| 1410 | if (!strcmp(tline->text, "!=")) |
| 1411 | return tokval->t_type = TOKEN_NE; |
| 1412 | if (!strcmp(tline->text, "<=")) |
| 1413 | return tokval->t_type = TOKEN_LE; |
| 1414 | if (!strcmp(tline->text, ">=")) |
| 1415 | return tokval->t_type = TOKEN_GE; |
| 1416 | if (!strcmp(tline->text, "&&")) |
| 1417 | return tokval->t_type = TOKEN_DBL_AND; |
| 1418 | if (!strcmp(tline->text, "^^")) |
| 1419 | return tokval->t_type = TOKEN_DBL_XOR; |
| 1420 | if (!strcmp(tline->text, "||")) |
| 1421 | return tokval->t_type = TOKEN_DBL_OR; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1422 | } |
| 1423 | |
| 1424 | /* |
| 1425 | * We have no other options: just return the first character of |
| 1426 | * the token text. |
| 1427 | */ |
| 1428 | return tokval->t_type = tline->text[0]; |
| 1429 | } |
| 1430 | |
| 1431 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1432 | * Compare a string to the name of an existing macro; this is a |
| 1433 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 1434 | * depending on the value of the `casesense' parameter. |
| 1435 | */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 1436 | static int mstrcmp(const char *p, const char *q, bool casesense) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1437 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1438 | return casesense ? strcmp(p, q) : nasm_stricmp(p, q); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | /* |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1442 | * Compare a string to the name of an existing macro; this is a |
| 1443 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 1444 | * depending on the value of the `casesense' parameter. |
| 1445 | */ |
| 1446 | static int mmemcmp(const char *p, const char *q, size_t l, bool casesense) |
| 1447 | { |
| 1448 | return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l); |
| 1449 | } |
| 1450 | |
| 1451 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1452 | * Return the Context structure associated with a %$ token. Return |
| 1453 | * NULL, having _already_ reported an error condition, if the |
| 1454 | * context stack isn't deep enough for the supplied number of $ |
| 1455 | * signs. |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1456 | * |
| 1457 | * If "namep" is non-NULL, set it to the pointer to the macro name |
| 1458 | * tail, i.e. the part beyond %$... |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1459 | */ |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1460 | static Context *get_ctx(const char *name, const char **namep) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1461 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1462 | Context *ctx; |
| 1463 | int i; |
| 1464 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1465 | if (namep) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1466 | *namep = name; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1467 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1468 | if (!name || name[0] != '%' || name[1] != '$') |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1469 | return NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1470 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1471 | if (!cstk) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1472 | nasm_error(ERR_NONFATAL, "`%s': context stack is empty", name); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1473 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1476 | name += 2; |
| 1477 | ctx = cstk; |
| 1478 | i = 0; |
| 1479 | while (ctx && *name == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1480 | name++; |
| 1481 | i++; |
| 1482 | ctx = ctx->next; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1483 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1484 | if (!ctx) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1485 | nasm_error(ERR_NONFATAL, "`%s': context stack is only" |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1486 | " %d level%s deep", name, i, (i == 1 ? "" : "s")); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1487 | return NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1488 | } |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1489 | |
| 1490 | if (namep) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1491 | *namep = name; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1492 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1493 | return ctx; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1494 | } |
| 1495 | |
| 1496 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1497 | * Open an include file. This routine must always return a valid |
| 1498 | * file pointer if it returns - it's responsible for throwing an |
| 1499 | * ERR_FATAL and bombing out completely if not. It should also try |
| 1500 | * the include path one by one until it finds the file or reaches |
| 1501 | * the end of the path. |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1502 | * |
| 1503 | * Note: for INC_PROBE the function returns NULL at all times; |
| 1504 | * instead look for the |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1505 | */ |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1506 | enum incopen_mode { |
| 1507 | INC_NEEDED, /* File must exist */ |
| 1508 | INC_OPTIONAL, /* Missing is OK */ |
| 1509 | INC_PROBE /* Only an existence probe */ |
| 1510 | }; |
| 1511 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1512 | /* This is conducts a full pathname search */ |
| 1513 | static FILE *inc_fopen_search(const char *file, StrList **slpath, |
| 1514 | enum incopen_mode omode, enum file_flags fmode) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1515 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1516 | FILE *fp; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1517 | char *prefix = ""; |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1518 | const IncPath *ip = ipath; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1519 | int len = strlen(file); |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1520 | size_t prefix_len = 0; |
| 1521 | StrList *sl; |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 1522 | size_t path_len; |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1523 | bool found; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1524 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1525 | while (1) { |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 1526 | path_len = prefix_len + len + 1; |
| 1527 | |
| 1528 | sl = nasm_malloc(path_len + sizeof sl->next); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1529 | memcpy(sl->str, prefix, prefix_len); |
| 1530 | memcpy(sl->str+prefix_len, file, len+1); |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1531 | sl->next = NULL; |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 1532 | |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1533 | if (omode == INC_PROBE) { |
| 1534 | fp = NULL; |
| 1535 | found = nasm_file_exists(sl->str); |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1536 | } else { |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1537 | fp = nasm_open_read(sl->str, fmode); |
| 1538 | found = (fp != NULL); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1539 | } |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1540 | if (found) { |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1541 | *slpath = sl; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1542 | return fp; |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1543 | } |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 1544 | |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 1545 | nasm_free(sl); |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 1546 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1547 | if (!ip) |
| 1548 | return NULL; |
| 1549 | |
| 1550 | prefix = ip->path; |
| 1551 | prefix_len = strlen(prefix); |
| 1552 | ip = ip->next; |
| 1553 | } |
| 1554 | } |
| 1555 | |
| 1556 | /* |
| 1557 | * Open a file, or test for the presence of one (depending on omode), |
| 1558 | * considering the include path. |
| 1559 | */ |
| 1560 | static FILE *inc_fopen(const char *file, |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 1561 | StrList **dhead, |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 1562 | const char **found_path, |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1563 | enum incopen_mode omode, |
| 1564 | enum file_flags fmode) |
| 1565 | { |
| 1566 | StrList *sl; |
| 1567 | struct hash_insert hi; |
| 1568 | void **hp; |
| 1569 | char *path; |
| 1570 | FILE *fp = NULL; |
| 1571 | |
| 1572 | hp = hash_find(&FileHash, file, &hi); |
| 1573 | if (hp) { |
| 1574 | path = *hp; |
| 1575 | } else { |
| 1576 | /* Need to do the actual path search */ |
| 1577 | size_t file_len; |
| 1578 | |
| 1579 | sl = NULL; |
| 1580 | fp = inc_fopen_search(file, &sl, omode, fmode); |
| 1581 | |
| 1582 | file_len = strlen(file); |
| 1583 | |
| 1584 | if (!sl) { |
| 1585 | /* Store negative result for this file */ |
| 1586 | sl = nasm_malloc(file_len + 1 + sizeof sl->next); |
| 1587 | memcpy(sl->str, file, file_len+1); |
| 1588 | sl->next = NULL; |
| 1589 | file = sl->str; |
| 1590 | path = NULL; |
| 1591 | } else { |
| 1592 | path = sl->str; |
| 1593 | file = strchr(path, '\0') - file_len; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1594 | } |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1595 | |
| 1596 | hash_add(&hi, file, path); /* Positive or negative result */ |
| 1597 | |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 1598 | /* |
| 1599 | * Add file to dependency path. The in_list() is needed |
| 1600 | * in case the file was already added with %depend. |
| 1601 | */ |
| 1602 | if (path || omode != INC_NEEDED) |
H. Peter Anvin | 436e367 | 2016-10-04 01:12:28 -0700 | [diff] [blame] | 1603 | nasm_add_to_strlist(dhead, sl); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1604 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1605 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1606 | if (!path) { |
| 1607 | if (omode == INC_NEEDED) |
| 1608 | nasm_fatal(0, "unable to open include file `%s'", file); |
| 1609 | |
| 1610 | if (found_path) |
| 1611 | *found_path = NULL; |
| 1612 | |
| 1613 | return NULL; |
| 1614 | } |
| 1615 | |
| 1616 | if (!fp && omode != INC_PROBE) |
| 1617 | fp = nasm_open_read(file, fmode); |
| 1618 | |
| 1619 | if (found_path) |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 1620 | *found_path = path; |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 1621 | |
| 1622 | return fp; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1623 | } |
| 1624 | |
| 1625 | /* |
Fabian Giesen | 0bbc38d | 2016-04-28 13:48:14 -0700 | [diff] [blame] | 1626 | * Opens an include or input file. Public version, for use by modules |
| 1627 | * that get a file:lineno pair and need to look at the file again |
| 1628 | * (e.g. the CodeView debug backend). Returns NULL on failure. |
| 1629 | */ |
H. Peter Anvin | 3e83cec | 2016-05-25 04:28:46 -0700 | [diff] [blame] | 1630 | FILE *pp_input_fopen(const char *filename, enum file_flags mode) |
Fabian Giesen | 0bbc38d | 2016-04-28 13:48:14 -0700 | [diff] [blame] | 1631 | { |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 1632 | return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode); |
Fabian Giesen | 0bbc38d | 2016-04-28 13:48:14 -0700 | [diff] [blame] | 1633 | } |
| 1634 | |
| 1635 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1636 | * 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] | 1637 | * 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] | 1638 | * return true if _any_ single-line macro of that name is defined. |
| 1639 | * Otherwise, will return true if a single-line macro with either |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1640 | * `nparam' or no parameters is defined. |
| 1641 | * |
| 1642 | * If a macro with precisely the right number of parameters is |
H. Peter Anvin | ef7468f | 2002-04-30 20:57:59 +0000 | [diff] [blame] | 1643 | * defined, or nparam is -1, the address of the definition structure |
| 1644 | * 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] | 1645 | * is NULL, no action will be taken regarding its contents, and no |
| 1646 | * error will occur. |
| 1647 | * |
| 1648 | * Note that this is also called with nparam zero to resolve |
| 1649 | * `ifdef'. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1650 | * |
| 1651 | * If you already know which context macro belongs to, you can pass |
| 1652 | * the context pointer as first parameter; if you won't but name begins |
| 1653 | * with %$ the context will be automatically computed. If all_contexts |
| 1654 | * is true, macro will be searched in outer contexts as well. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1655 | */ |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1656 | static bool |
H. Peter Anvin | b2a5fda | 2008-06-19 21:42:42 -0700 | [diff] [blame] | 1657 | smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn, |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1658 | bool nocase) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1659 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1660 | struct hash_table *smtbl; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1661 | SMacro *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1662 | |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1663 | if (ctx) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1664 | smtbl = &ctx->localmac; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1665 | } else if (name[0] == '%' && name[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1666 | if (cstk) |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1667 | ctx = get_ctx(name, &name); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1668 | if (!ctx) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1669 | return false; /* got to return _something_ */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1670 | smtbl = &ctx->localmac; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1671 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1672 | smtbl = &smacros; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1673 | } |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1674 | m = (SMacro *) hash_findix(smtbl, name); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1675 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1676 | while (m) { |
| 1677 | if (!mstrcmp(m->name, name, m->casesense && nocase) && |
Charles Crayne | 192d5b5 | 2007-10-18 19:02:42 -0700 | [diff] [blame] | 1678 | (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1679 | if (defn) { |
Charles Crayne | 192d5b5 | 2007-10-18 19:02:42 -0700 | [diff] [blame] | 1680 | if (nparam == (int) m->nparam || nparam == -1) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1681 | *defn = m; |
| 1682 | else |
| 1683 | *defn = NULL; |
| 1684 | } |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1685 | return true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1686 | } |
| 1687 | m = m->next; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1688 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1689 | |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1690 | return false; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1691 | } |
| 1692 | |
| 1693 | /* |
| 1694 | * Count and mark off the parameters in a multi-line macro call. |
| 1695 | * This is called both from within the multi-line macro expansion |
| 1696 | * code, and also to mark off the default parameters when provided |
| 1697 | * in a %macro definition line. |
| 1698 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1699 | static void count_mmac_params(Token * t, int *nparam, Token *** params) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1700 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1701 | int paramsize, brace; |
| 1702 | |
| 1703 | *nparam = paramsize = 0; |
| 1704 | *params = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1705 | while (t) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1706 | /* +1: we need space for the final NULL */ |
H. Peter Anvin | 91fb6f1 | 2008-09-01 10:56:33 -0700 | [diff] [blame] | 1707 | if (*nparam+1 >= paramsize) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1708 | paramsize += PARAM_DELTA; |
| 1709 | *params = nasm_realloc(*params, sizeof(**params) * paramsize); |
| 1710 | } |
| 1711 | skip_white_(t); |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1712 | brace = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1713 | if (tok_is_(t, "{")) |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1714 | brace++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1715 | (*params)[(*nparam)++] = t; |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1716 | if (brace) { |
| 1717 | while (brace && (t = t->next) != NULL) { |
| 1718 | if (tok_is_(t, "{")) |
| 1719 | brace++; |
| 1720 | else if (tok_is_(t, "}")) |
| 1721 | brace--; |
| 1722 | } |
| 1723 | |
| 1724 | if (t) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1725 | /* |
| 1726 | * Now we've found the closing brace, look further |
| 1727 | * for the comma. |
| 1728 | */ |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1729 | t = t->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1730 | skip_white_(t); |
| 1731 | if (tok_isnt_(t, ",")) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1732 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1733 | "braces do not enclose all of macro parameter"); |
| 1734 | while (tok_isnt_(t, ",")) |
| 1735 | t = t->next; |
| 1736 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1737 | } |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1738 | } else { |
| 1739 | while (tok_isnt_(t, ",")) |
| 1740 | t = t->next; |
| 1741 | } |
| 1742 | if (t) { /* got a comma/brace */ |
| 1743 | t = t->next; /* eat the comma */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1744 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1745 | } |
| 1746 | } |
| 1747 | |
| 1748 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1749 | * Determine whether one of the various `if' conditions is true or |
| 1750 | * not. |
| 1751 | * |
| 1752 | * We must free the tline we get passed. |
| 1753 | */ |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1754 | static bool if_condition(Token * tline, enum preproc_token ct) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1755 | { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1756 | enum pp_conditional i = PP_COND(ct); |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1757 | bool j; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1758 | Token *t, *tt, **tptr, *origline; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1759 | struct tokenval tokval; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1760 | expr *evalresult; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1761 | enum pp_token_type needtype; |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1762 | char *p; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1763 | |
| 1764 | origline = tline; |
| 1765 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1766 | switch (i) { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1767 | case PPC_IFCTX: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1768 | j = false; /* have we matched yet? */ |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1769 | while (true) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1770 | skip_white_(tline); |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1771 | if (!tline) |
| 1772 | break; |
| 1773 | if (tline->type != TOK_ID) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1774 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1775 | "`%s' expects context identifiers", pp_directives[ct]); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1776 | free_tlist(origline); |
| 1777 | return -1; |
| 1778 | } |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1779 | if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1780 | j = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1781 | tline = tline->next; |
| 1782 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1783 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1784 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1785 | case PPC_IFDEF: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1786 | j = false; /* have we matched yet? */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1787 | while (tline) { |
| 1788 | skip_white_(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1789 | if (!tline || (tline->type != TOK_ID && |
| 1790 | (tline->type != TOK_PREPROC_ID || |
| 1791 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1792 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1793 | "`%s' expects macro identifiers", pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1794 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1795 | } |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1796 | if (smacro_defined(NULL, tline->text, 0, NULL, true)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1797 | j = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1798 | tline = tline->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1799 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1800 | break; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1801 | |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1802 | case PPC_IFENV: |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1803 | tline = expand_smacro(tline); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1804 | j = false; /* have we matched yet? */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1805 | while (tline) { |
| 1806 | skip_white_(tline); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1807 | if (!tline || (tline->type != TOK_ID && |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1808 | tline->type != TOK_STRING && |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1809 | (tline->type != TOK_PREPROC_ID || |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1810 | tline->text[1] != '!'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1811 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1812 | "`%s' expects environment variable names", |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1813 | pp_directives[ct]); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1814 | goto fail; |
| 1815 | } |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1816 | p = tline->text; |
| 1817 | if (tline->type == TOK_PREPROC_ID) |
| 1818 | p += 2; /* Skip leading %! */ |
| 1819 | if (*p == '\'' || *p == '\"' || *p == '`') |
| 1820 | nasm_unquote_cstr(p, ct); |
| 1821 | if (getenv(p)) |
| 1822 | j = true; |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1823 | tline = tline->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1824 | } |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1825 | break; |
| 1826 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1827 | case PPC_IFIDN: |
| 1828 | case PPC_IFIDNI: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1829 | tline = expand_smacro(tline); |
| 1830 | t = tt = tline; |
| 1831 | while (tok_isnt_(tt, ",")) |
| 1832 | tt = tt->next; |
| 1833 | if (!tt) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1834 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1835 | "`%s' expects two comma-separated arguments", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1836 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1837 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1838 | } |
| 1839 | tt = tt->next; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1840 | j = true; /* assume equality unless proved not */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1841 | while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) { |
| 1842 | if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1843 | nasm_error(ERR_NONFATAL, "`%s': more than one comma on line", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1844 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1845 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1846 | } |
| 1847 | if (t->type == TOK_WHITESPACE) { |
| 1848 | t = t->next; |
| 1849 | continue; |
| 1850 | } |
| 1851 | if (tt->type == TOK_WHITESPACE) { |
| 1852 | tt = tt->next; |
| 1853 | continue; |
| 1854 | } |
| 1855 | if (tt->type != t->type) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1856 | j = false; /* found mismatching tokens */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1857 | break; |
| 1858 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1859 | /* When comparing strings, need to unquote them first */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1860 | if (t->type == TOK_STRING) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1861 | size_t l1 = nasm_unquote(t->text, NULL); |
| 1862 | size_t l2 = nasm_unquote(tt->text, NULL); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 1863 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1864 | if (l1 != l2) { |
| 1865 | j = false; |
| 1866 | break; |
| 1867 | } |
| 1868 | if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) { |
| 1869 | j = false; |
| 1870 | break; |
| 1871 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1872 | } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1873 | j = false; /* found mismatching tokens */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1874 | break; |
| 1875 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1876 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1877 | t = t->next; |
| 1878 | tt = tt->next; |
| 1879 | } |
| 1880 | if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1881 | j = false; /* trailing gunk on one end or other */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1882 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1883 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1884 | case PPC_IFMACRO: |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1885 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1886 | bool found = false; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1887 | MMacro searching, *mmac; |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 1888 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1889 | skip_white_(tline); |
| 1890 | tline = expand_id(tline); |
| 1891 | if (!tok_type_(tline, TOK_ID)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1892 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1893 | "`%s' expects a macro name", pp_directives[ct]); |
| 1894 | goto fail; |
| 1895 | } |
| 1896 | searching.name = nasm_strdup(tline->text); |
| 1897 | searching.casesense = true; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1898 | searching.plus = false; |
| 1899 | searching.nolist = false; |
| 1900 | searching.in_progress = 0; |
| 1901 | searching.max_depth = 0; |
| 1902 | searching.rep_nest = NULL; |
| 1903 | searching.nparam_min = 0; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1904 | searching.nparam_max = INT_MAX; |
| 1905 | tline = expand_smacro(tline->next); |
| 1906 | skip_white_(tline); |
| 1907 | if (!tline) { |
| 1908 | } else if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1909 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1910 | "`%s' expects a parameter count or nothing", |
| 1911 | pp_directives[ct]); |
| 1912 | } else { |
| 1913 | searching.nparam_min = searching.nparam_max = |
| 1914 | readnum(tline->text, &j); |
| 1915 | if (j) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1916 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1917 | "unable to parse parameter count `%s'", |
| 1918 | tline->text); |
| 1919 | } |
| 1920 | if (tline && tok_is_(tline->next, "-")) { |
| 1921 | tline = tline->next->next; |
| 1922 | if (tok_is_(tline, "*")) |
| 1923 | searching.nparam_max = INT_MAX; |
| 1924 | else if (!tok_type_(tline, TOK_NUMBER)) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1925 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1926 | "`%s' expects a parameter count after `-'", |
| 1927 | pp_directives[ct]); |
| 1928 | else { |
| 1929 | searching.nparam_max = readnum(tline->text, &j); |
| 1930 | if (j) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1931 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1932 | "unable to parse parameter count `%s'", |
| 1933 | tline->text); |
| 1934 | if (searching.nparam_min > searching.nparam_max) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1935 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1936 | "minimum parameter count exceeds maximum"); |
| 1937 | } |
| 1938 | } |
| 1939 | if (tline && tok_is_(tline->next, "+")) { |
| 1940 | tline = tline->next; |
| 1941 | searching.plus = true; |
| 1942 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1943 | mmac = (MMacro *) hash_findix(&mmacros, searching.name); |
| 1944 | while (mmac) { |
| 1945 | if (!strcmp(mmac->name, searching.name) && |
| 1946 | (mmac->nparam_min <= searching.nparam_max |
| 1947 | || searching.plus) |
| 1948 | && (searching.nparam_min <= mmac->nparam_max |
| 1949 | || mmac->plus)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1950 | found = true; |
| 1951 | break; |
| 1952 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1953 | mmac = mmac->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1954 | } |
| 1955 | if (tline && tline->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1956 | nasm_error(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1957 | "trailing garbage after %%ifmacro ignored"); |
| 1958 | nasm_free(searching.name); |
| 1959 | j = found; |
| 1960 | break; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1961 | } |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 1962 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1963 | case PPC_IFID: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1964 | needtype = TOK_ID; |
| 1965 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1966 | case PPC_IFNUM: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1967 | needtype = TOK_NUMBER; |
| 1968 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1969 | case PPC_IFSTR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1970 | needtype = TOK_STRING; |
| 1971 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1972 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1973 | iftype: |
| 1974 | t = tline = expand_smacro(tline); |
H. Peter Anvin | d85d250 | 2008-05-04 17:53:31 -0700 | [diff] [blame] | 1975 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1976 | while (tok_type_(t, TOK_WHITESPACE) || |
| 1977 | (needtype == TOK_NUMBER && |
| 1978 | tok_type_(t, TOK_OTHER) && |
| 1979 | (t->text[0] == '-' || t->text[0] == '+') && |
| 1980 | !t->text[1])) |
| 1981 | t = t->next; |
H. Peter Anvin | d85d250 | 2008-05-04 17:53:31 -0700 | [diff] [blame] | 1982 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1983 | j = tok_type_(t, needtype); |
| 1984 | break; |
H. Peter Anvin | cbf768d | 2008-02-16 16:41:25 -0800 | [diff] [blame] | 1985 | |
| 1986 | case PPC_IFTOKEN: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1987 | t = tline = expand_smacro(tline); |
| 1988 | while (tok_type_(t, TOK_WHITESPACE)) |
| 1989 | t = t->next; |
H. Peter Anvin | cbf768d | 2008-02-16 16:41:25 -0800 | [diff] [blame] | 1990 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1991 | j = false; |
| 1992 | if (t) { |
| 1993 | t = t->next; /* Skip the actual token */ |
| 1994 | while (tok_type_(t, TOK_WHITESPACE)) |
| 1995 | t = t->next; |
| 1996 | j = !t; /* Should be nothing left */ |
| 1997 | } |
| 1998 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1999 | |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 2000 | case PPC_IFEMPTY: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2001 | t = tline = expand_smacro(tline); |
| 2002 | while (tok_type_(t, TOK_WHITESPACE)) |
| 2003 | t = t->next; |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 2004 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2005 | j = !t; /* Should be empty */ |
| 2006 | break; |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 2007 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2008 | case PPC_IF: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2009 | t = tline = expand_smacro(tline); |
| 2010 | tptr = &t; |
| 2011 | tokval.t_type = TOKEN_INVALID; |
| 2012 | evalresult = evaluate(ppscan, tptr, &tokval, |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2013 | NULL, pass | CRITICAL, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2014 | if (!evalresult) |
| 2015 | return -1; |
| 2016 | if (tokval.t_type) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2017 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2018 | "trailing garbage after expression ignored"); |
| 2019 | if (!is_simple(evalresult)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2020 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2021 | "non-constant value given to `%s'", pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2022 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2023 | } |
Chuck Crayne | 60ae75d | 2007-05-02 01:59:16 +0000 | [diff] [blame] | 2024 | j = reloc_value(evalresult) != 0; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2025 | break; |
H. Peter Anvin | 95e2882 | 2007-09-12 04:20:08 +0000 | [diff] [blame] | 2026 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2027 | default: |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2028 | nasm_error(ERR_FATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2029 | "preprocessor directive `%s' not yet implemented", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2030 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2031 | goto fail; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2032 | } |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2033 | |
| 2034 | free_tlist(origline); |
| 2035 | return j ^ PP_NEGATIVE(ct); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2036 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2037 | fail: |
| 2038 | free_tlist(origline); |
| 2039 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2040 | } |
| 2041 | |
| 2042 | /* |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2043 | * Common code for defining an smacro |
| 2044 | */ |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 2045 | static bool define_smacro(Context *ctx, const char *mname, bool casesense, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2046 | int nparam, Token *expansion) |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2047 | { |
| 2048 | SMacro *smac, **smhead; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2049 | struct hash_table *smtbl; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2050 | |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2051 | if (smacro_defined(ctx, mname, nparam, &smac, casesense)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2052 | if (!smac) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2053 | nasm_error(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2054 | "single-line macro `%s' defined both with and" |
| 2055 | " without parameters", mname); |
| 2056 | /* |
| 2057 | * Some instances of the old code considered this a failure, |
| 2058 | * some others didn't. What is the right thing to do here? |
| 2059 | */ |
| 2060 | free_tlist(expansion); |
| 2061 | return false; /* Failure */ |
| 2062 | } else { |
| 2063 | /* |
| 2064 | * We're redefining, so we have to take over an |
| 2065 | * existing SMacro structure. This means freeing |
| 2066 | * what was already in it. |
| 2067 | */ |
| 2068 | nasm_free(smac->name); |
| 2069 | free_tlist(smac->expansion); |
| 2070 | } |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2071 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2072 | smtbl = ctx ? &ctx->localmac : &smacros; |
| 2073 | smhead = (SMacro **) hash_findi_add(smtbl, mname); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2074 | smac = nasm_malloc(sizeof(SMacro)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2075 | smac->next = *smhead; |
| 2076 | *smhead = smac; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2077 | } |
| 2078 | smac->name = nasm_strdup(mname); |
| 2079 | smac->casesense = casesense; |
| 2080 | smac->nparam = nparam; |
| 2081 | smac->expansion = expansion; |
| 2082 | smac->in_progress = false; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2083 | return true; /* Success */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2084 | } |
| 2085 | |
| 2086 | /* |
| 2087 | * Undefine an smacro |
| 2088 | */ |
| 2089 | static void undef_smacro(Context *ctx, const char *mname) |
| 2090 | { |
| 2091 | SMacro **smhead, *s, **sp; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2092 | struct hash_table *smtbl; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2093 | |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2094 | smtbl = ctx ? &ctx->localmac : &smacros; |
| 2095 | smhead = (SMacro **)hash_findi(smtbl, mname, NULL); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2096 | |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2097 | if (smhead) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2098 | /* |
| 2099 | * We now have a macro name... go hunt for it. |
| 2100 | */ |
| 2101 | sp = smhead; |
| 2102 | while ((s = *sp) != NULL) { |
| 2103 | if (!mstrcmp(s->name, mname, s->casesense)) { |
| 2104 | *sp = s->next; |
| 2105 | nasm_free(s->name); |
| 2106 | free_tlist(s->expansion); |
| 2107 | nasm_free(s); |
| 2108 | } else { |
| 2109 | sp = &s->next; |
| 2110 | } |
| 2111 | } |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2112 | } |
| 2113 | } |
| 2114 | |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2115 | /* |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2116 | * Parse a mmacro specification. |
| 2117 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2118 | 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] | 2119 | { |
| 2120 | bool err; |
| 2121 | |
| 2122 | tline = tline->next; |
| 2123 | skip_white_(tline); |
| 2124 | tline = expand_id(tline); |
| 2125 | if (!tok_type_(tline, TOK_ID)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2126 | nasm_error(ERR_NONFATAL, "`%s' expects a macro name", directive); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2127 | return false; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2128 | } |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2129 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2130 | def->prev = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2131 | def->name = nasm_strdup(tline->text); |
| 2132 | def->plus = false; |
| 2133 | def->nolist = false; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2134 | def->in_progress = 0; |
| 2135 | def->rep_nest = NULL; |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2136 | def->nparam_min = 0; |
| 2137 | def->nparam_max = 0; |
| 2138 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2139 | tline = expand_smacro(tline->next); |
| 2140 | skip_white_(tline); |
| 2141 | if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2142 | nasm_error(ERR_NONFATAL, "`%s' expects a parameter count", directive); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2143 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2144 | def->nparam_min = def->nparam_max = |
| 2145 | readnum(tline->text, &err); |
| 2146 | if (err) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2147 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2148 | "unable to parse parameter count `%s'", tline->text); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2149 | } |
| 2150 | if (tline && tok_is_(tline->next, "-")) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2151 | tline = tline->next->next; |
| 2152 | if (tok_is_(tline, "*")) { |
| 2153 | def->nparam_max = INT_MAX; |
| 2154 | } else if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2155 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2156 | "`%s' expects a parameter count after `-'", directive); |
| 2157 | } else { |
| 2158 | def->nparam_max = readnum(tline->text, &err); |
| 2159 | if (err) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2160 | nasm_error(ERR_NONFATAL, "unable to parse parameter count `%s'", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2161 | tline->text); |
| 2162 | } |
| 2163 | if (def->nparam_min > def->nparam_max) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2164 | nasm_error(ERR_NONFATAL, "minimum parameter count exceeds maximum"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2165 | } |
| 2166 | } |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2167 | } |
| 2168 | if (tline && tok_is_(tline->next, "+")) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2169 | tline = tline->next; |
| 2170 | def->plus = true; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2171 | } |
| 2172 | if (tline && tok_type_(tline->next, TOK_ID) && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2173 | !nasm_stricmp(tline->next->text, ".nolist")) { |
| 2174 | tline = tline->next; |
| 2175 | def->nolist = true; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2176 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2177 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2178 | /* |
| 2179 | * Handle default parameters. |
| 2180 | */ |
| 2181 | if (tline && tline->next) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2182 | def->dlist = tline->next; |
| 2183 | tline->next = NULL; |
| 2184 | count_mmac_params(def->dlist, &def->ndefs, &def->defaults); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2185 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2186 | def->dlist = NULL; |
| 2187 | def->defaults = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2188 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2189 | def->expansion = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2190 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2191 | if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2192 | !def->plus) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2193 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2194 | "too many default macro parameters"); |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2195 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2196 | return true; |
| 2197 | } |
| 2198 | |
| 2199 | |
| 2200 | /* |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2201 | * Decode a size directive |
| 2202 | */ |
| 2203 | static int parse_size(const char *str) { |
| 2204 | static const char *size_names[] = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2205 | { "byte", "dword", "oword", "qword", "tword", "word", "yword" }; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2206 | static const int sizes[] = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2207 | { 0, 1, 4, 16, 8, 10, 2, 32 }; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2208 | |
Cyrill Gorcunov | a731924 | 2010-06-03 22:04:36 +0400 | [diff] [blame] | 2209 | return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1]; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2210 | } |
| 2211 | |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2212 | /* |
| 2213 | * Process a preprocessor %pragma directive. Currently there are none. |
| 2214 | * Gets passed the token list starting with the "preproc" token from |
| 2215 | * "%pragma preproc". |
| 2216 | */ |
| 2217 | static void do_pragma_preproc(Token *tline) |
| 2218 | { |
| 2219 | /* Skip to the real stuff */ |
| 2220 | tline = tline->next; |
| 2221 | skip_white_(tline); |
| 2222 | if (!tline) |
| 2223 | return; |
| 2224 | |
| 2225 | (void)tline; /* Nothing else to do at present */ |
| 2226 | } |
| 2227 | |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2228 | /** |
| 2229 | * find and process preprocessor directive in passed line |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2230 | * Find out if a line contains a preprocessor directive, and deal |
| 2231 | * with it if so. |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2232 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2233 | * If a directive _is_ found, it is the responsibility of this routine |
| 2234 | * (and not the caller) to free_tlist() the line. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2235 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2236 | * @param tline a pointer to the current tokeninzed line linked list |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2237 | * @param output if this directive generated output |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2238 | * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2239 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2240 | */ |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2241 | static int do_directive(Token *tline, char **output) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2242 | { |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2243 | enum preproc_token i; |
| 2244 | int j; |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 2245 | bool err; |
| 2246 | int nparam; |
| 2247 | bool nolist; |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 2248 | bool casesense; |
H. Peter Anvin | 8cfdb9d | 2007-09-14 18:36:01 -0700 | [diff] [blame] | 2249 | int k, m; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2250 | int offset; |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 2251 | char *p, *pp; |
| 2252 | const char *found_path; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 2253 | const char *mname; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2254 | Include *inc; |
| 2255 | Context *ctx; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2256 | Cond *cond; |
| 2257 | MMacro *mmac, **mmhead; |
Jin Kyu Song | c9486b9 | 2013-10-28 17:07:57 -0700 | [diff] [blame] | 2258 | Token *t = NULL, *tt, *param_start, *macro_start, *last, **tptr, *origline; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2259 | Line *l; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2260 | struct tokenval tokval; |
| 2261 | expr *evalresult; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2262 | MMacro *tmp_defining; /* Used when manipulating rep_nest */ |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 2263 | int64_t count; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 2264 | size_t len; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2265 | int severity; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2266 | |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2267 | *output = NULL; /* No output generated */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2268 | origline = tline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2269 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2270 | skip_white_(tline); |
H. Peter Anvin | f2936d7 | 2008-06-04 15:11:23 -0700 | [diff] [blame] | 2271 | if (!tline || !tok_type_(tline, TOK_PREPROC_ID) || |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2272 | (tline->text[1] == '%' || tline->text[1] == '$' |
| 2273 | || tline->text[1] == '!')) |
| 2274 | return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2275 | |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2276 | i = pp_token_hash(tline->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2277 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2278 | /* |
| 2279 | * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO |
| 2280 | * since they are known to be buggy at moment, we need to fix them |
| 2281 | * in future release (2.09-2.10) |
| 2282 | */ |
Philipp Kloke | b432f57 | 2013-03-31 12:01:23 +0200 | [diff] [blame] | 2283 | if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2284 | nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'", |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2285 | tline->text); |
| 2286 | return NO_DIRECTIVE_FOUND; |
| 2287 | } |
| 2288 | |
| 2289 | /* |
| 2290 | * If we're in a non-emitting branch of a condition construct, |
| 2291 | * or walking to the end of an already terminated %rep block, |
| 2292 | * we should ignore all directives except for condition |
| 2293 | * directives. |
| 2294 | */ |
| 2295 | if (((istk->conds && !emitting(istk->conds->state)) || |
| 2296 | (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) { |
| 2297 | return NO_DIRECTIVE_FOUND; |
| 2298 | } |
| 2299 | |
| 2300 | /* |
| 2301 | * If we're defining a macro or reading a %rep block, we should |
| 2302 | * ignore all directives except for %macro/%imacro (which nest), |
| 2303 | * %endm/%endmacro, and (only if we're in a %rep block) %endrep. |
| 2304 | * If we're in a %rep block, another %rep nests, so should be let through. |
| 2305 | */ |
| 2306 | if (defining && i != PP_MACRO && i != PP_IMACRO && |
| 2307 | i != PP_RMACRO && i != PP_IRMACRO && |
| 2308 | i != PP_ENDMACRO && i != PP_ENDM && |
| 2309 | (defining->name || (i != PP_ENDREP && i != PP_REP))) { |
| 2310 | return NO_DIRECTIVE_FOUND; |
| 2311 | } |
| 2312 | |
| 2313 | if (defining) { |
| 2314 | if (i == PP_MACRO || i == PP_IMACRO || |
| 2315 | i == PP_RMACRO || i == PP_IRMACRO) { |
| 2316 | nested_mac_count++; |
| 2317 | return NO_DIRECTIVE_FOUND; |
| 2318 | } else if (nested_mac_count > 0) { |
| 2319 | if (i == PP_ENDMACRO) { |
| 2320 | nested_mac_count--; |
| 2321 | return NO_DIRECTIVE_FOUND; |
| 2322 | } |
| 2323 | } |
| 2324 | if (!defining->name) { |
| 2325 | if (i == PP_REP) { |
| 2326 | nested_rep_count++; |
| 2327 | return NO_DIRECTIVE_FOUND; |
| 2328 | } else if (nested_rep_count > 0) { |
| 2329 | if (i == PP_ENDREP) { |
| 2330 | nested_rep_count--; |
| 2331 | return NO_DIRECTIVE_FOUND; |
| 2332 | } |
| 2333 | } |
| 2334 | } |
| 2335 | } |
| 2336 | |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2337 | switch (i) { |
| 2338 | case PP_INVALID: |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2339 | nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'", |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2340 | tline->text); |
| 2341 | return NO_DIRECTIVE_FOUND; /* didn't get it */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2342 | |
H. Peter Anvin | 3f87a2a | 2016-10-04 14:07:19 -0700 | [diff] [blame] | 2343 | case PP_PRAGMA: |
| 2344 | /* |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2345 | * %pragma namespace options... |
| 2346 | * |
| 2347 | * The namespace "preproc" is reserved for the preprocessor; |
| 2348 | * all other namespaces generate a [pragma] assembly directive. |
| 2349 | * |
| 2350 | * Invalid %pragmas are ignored and may have different |
| 2351 | * meaning in future versions of NASM. |
H. Peter Anvin | 3f87a2a | 2016-10-04 14:07:19 -0700 | [diff] [blame] | 2352 | */ |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2353 | tline = tline->next; |
| 2354 | skip_white_(tline); |
| 2355 | tline = expand_smacro(tline); |
| 2356 | if (tok_type_(tline, TOK_ID)) { |
| 2357 | if (!nasm_stricmp(tline->text, "preproc")) { |
| 2358 | /* Preprocessor pragma */ |
| 2359 | do_pragma_preproc(tline); |
| 2360 | } else { |
| 2361 | /* Build the assembler directive */ |
| 2362 | t = new_Token(NULL, TOK_OTHER, "[", 1); |
| 2363 | t->next = new_Token(NULL, TOK_ID, "pragma", 6); |
| 2364 | t->next->next = new_Token(tline, TOK_WHITESPACE, NULL, 0); |
| 2365 | tline = t; |
| 2366 | for (t = tline; t->next; t = t->next) |
| 2367 | ; |
| 2368 | t->next = new_Token(NULL, TOK_OTHER, "]", 1); |
| 2369 | /* true here can be revisited in the future */ |
| 2370 | *output = detoken(tline, true); |
| 2371 | } |
| 2372 | } |
H. Peter Anvin | 3f87a2a | 2016-10-04 14:07:19 -0700 | [diff] [blame] | 2373 | free_tlist(origline); |
| 2374 | return DIRECTIVE_FOUND; |
| 2375 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2376 | case PP_STACKSIZE: |
| 2377 | /* Directive to tell NASM what the default stack size is. The |
| 2378 | * default is for a 16-bit stack, and this can be overriden with |
| 2379 | * %stacksize large. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2380 | */ |
| 2381 | tline = tline->next; |
| 2382 | if (tline && tline->type == TOK_WHITESPACE) |
| 2383 | tline = tline->next; |
| 2384 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2385 | nasm_error(ERR_NONFATAL, "`%%stacksize' missing size parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2386 | free_tlist(origline); |
| 2387 | return DIRECTIVE_FOUND; |
| 2388 | } |
| 2389 | if (nasm_stricmp(tline->text, "flat") == 0) { |
| 2390 | /* All subsequent ARG directives are for a 32-bit stack */ |
| 2391 | StackSize = 4; |
| 2392 | StackPointer = "ebp"; |
| 2393 | ArgOffset = 8; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2394 | LocalOffset = 0; |
Charles Crayne | 7eaf919 | 2007-11-08 22:11:14 -0800 | [diff] [blame] | 2395 | } else if (nasm_stricmp(tline->text, "flat64") == 0) { |
| 2396 | /* All subsequent ARG directives are for a 64-bit stack */ |
| 2397 | StackSize = 8; |
| 2398 | StackPointer = "rbp"; |
Per Jessen | 53252e0 | 2010-02-11 00:16:59 +0300 | [diff] [blame] | 2399 | ArgOffset = 16; |
Charles Crayne | 7eaf919 | 2007-11-08 22:11:14 -0800 | [diff] [blame] | 2400 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2401 | } else if (nasm_stricmp(tline->text, "large") == 0) { |
| 2402 | /* All subsequent ARG directives are for a 16-bit stack, |
| 2403 | * far function call. |
| 2404 | */ |
| 2405 | StackSize = 2; |
| 2406 | StackPointer = "bp"; |
| 2407 | ArgOffset = 4; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2408 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2409 | } else if (nasm_stricmp(tline->text, "small") == 0) { |
| 2410 | /* All subsequent ARG directives are for a 16-bit stack, |
| 2411 | * far function call. We don't support near functions. |
| 2412 | */ |
| 2413 | StackSize = 2; |
| 2414 | StackPointer = "bp"; |
| 2415 | ArgOffset = 6; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2416 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2417 | } else { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2418 | nasm_error(ERR_NONFATAL, "`%%stacksize' invalid size type"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2419 | free_tlist(origline); |
| 2420 | return DIRECTIVE_FOUND; |
| 2421 | } |
| 2422 | free_tlist(origline); |
| 2423 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2424 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2425 | case PP_ARG: |
| 2426 | /* TASM like ARG directive to define arguments to functions, in |
| 2427 | * the following form: |
| 2428 | * |
| 2429 | * ARG arg1:WORD, arg2:DWORD, arg4:QWORD |
| 2430 | */ |
| 2431 | offset = ArgOffset; |
| 2432 | do { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 2433 | char *arg, directive[256]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2434 | int size = StackSize; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2435 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2436 | /* Find the argument name */ |
| 2437 | tline = tline->next; |
| 2438 | if (tline && tline->type == TOK_WHITESPACE) |
| 2439 | tline = tline->next; |
| 2440 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2441 | nasm_error(ERR_NONFATAL, "`%%arg' missing argument parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2442 | free_tlist(origline); |
| 2443 | return DIRECTIVE_FOUND; |
| 2444 | } |
| 2445 | arg = tline->text; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2446 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2447 | /* Find the argument size type */ |
| 2448 | tline = tline->next; |
| 2449 | if (!tline || tline->type != TOK_OTHER |
| 2450 | || tline->text[0] != ':') { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2451 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2452 | "Syntax error processing `%%arg' directive"); |
| 2453 | free_tlist(origline); |
| 2454 | return DIRECTIVE_FOUND; |
| 2455 | } |
| 2456 | tline = tline->next; |
| 2457 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2458 | nasm_error(ERR_NONFATAL, "`%%arg' missing size type parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2459 | free_tlist(origline); |
| 2460 | return DIRECTIVE_FOUND; |
| 2461 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2462 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2463 | /* Allow macro expansion of type parameter */ |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2464 | tt = tokenize(tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2465 | tt = expand_smacro(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2466 | size = parse_size(tt->text); |
| 2467 | if (!size) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2468 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2469 | "Invalid size type for `%%arg' missing directive"); |
| 2470 | free_tlist(tt); |
| 2471 | free_tlist(origline); |
| 2472 | return DIRECTIVE_FOUND; |
| 2473 | } |
| 2474 | free_tlist(tt); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2475 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2476 | /* Round up to even stack slots */ |
| 2477 | size = ALIGN(size, StackSize); |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2478 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2479 | /* Now define the macro for the argument */ |
| 2480 | snprintf(directive, sizeof(directive), "%%define %s (%s+%d)", |
| 2481 | arg, StackPointer, offset); |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2482 | do_directive(tokenize(directive), output); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2483 | offset += size; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2484 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2485 | /* Move to the next argument in the list */ |
| 2486 | tline = tline->next; |
| 2487 | if (tline && tline->type == TOK_WHITESPACE) |
| 2488 | tline = tline->next; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2489 | } while (tline && tline->type == TOK_OTHER && tline->text[0] == ','); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2490 | ArgOffset = offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2491 | free_tlist(origline); |
| 2492 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2493 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2494 | case PP_LOCAL: |
| 2495 | /* TASM like LOCAL directive to define local variables for a |
| 2496 | * function, in the following form: |
| 2497 | * |
| 2498 | * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize |
| 2499 | * |
| 2500 | * The '= LocalSize' at the end is ignored by NASM, but is |
| 2501 | * required by TASM to define the local parameter size (and used |
| 2502 | * by the TASM macro package). |
| 2503 | */ |
| 2504 | offset = LocalOffset; |
| 2505 | do { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 2506 | char *local, directive[256]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2507 | int size = StackSize; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2508 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2509 | /* Find the argument name */ |
| 2510 | tline = tline->next; |
| 2511 | if (tline && tline->type == TOK_WHITESPACE) |
| 2512 | tline = tline->next; |
| 2513 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2514 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2515 | "`%%local' missing argument parameter"); |
| 2516 | free_tlist(origline); |
| 2517 | return DIRECTIVE_FOUND; |
| 2518 | } |
| 2519 | local = tline->text; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2520 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2521 | /* Find the argument size type */ |
| 2522 | tline = tline->next; |
| 2523 | if (!tline || tline->type != TOK_OTHER |
| 2524 | || tline->text[0] != ':') { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2525 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2526 | "Syntax error processing `%%local' directive"); |
| 2527 | free_tlist(origline); |
| 2528 | return DIRECTIVE_FOUND; |
| 2529 | } |
| 2530 | tline = tline->next; |
| 2531 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2532 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2533 | "`%%local' missing size type parameter"); |
| 2534 | free_tlist(origline); |
| 2535 | return DIRECTIVE_FOUND; |
| 2536 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2537 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2538 | /* Allow macro expansion of type parameter */ |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2539 | tt = tokenize(tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2540 | tt = expand_smacro(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2541 | size = parse_size(tt->text); |
| 2542 | if (!size) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2543 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2544 | "Invalid size type for `%%local' missing directive"); |
| 2545 | free_tlist(tt); |
| 2546 | free_tlist(origline); |
| 2547 | return DIRECTIVE_FOUND; |
| 2548 | } |
| 2549 | free_tlist(tt); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2550 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2551 | /* Round up to even stack slots */ |
| 2552 | size = ALIGN(size, StackSize); |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2553 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2554 | offset += size; /* Negative offset, increment before */ |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2555 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2556 | /* Now define the macro for the argument */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2557 | snprintf(directive, sizeof(directive), "%%define %s (%s-%d)", |
| 2558 | local, StackPointer, offset); |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 2559 | do_directive(tokenize(directive), output); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2560 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2561 | /* Now define the assign to setup the enter_c macro correctly */ |
| 2562 | snprintf(directive, sizeof(directive), |
| 2563 | "%%assign %%$localsize %%$localsize+%d", size); |
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 | /* Move to the next argument in the list */ |
| 2567 | tline = tline->next; |
| 2568 | if (tline && tline->type == TOK_WHITESPACE) |
| 2569 | tline = tline->next; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2570 | } while (tline && tline->type == TOK_OTHER && tline->text[0] == ','); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2571 | LocalOffset = offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2572 | free_tlist(origline); |
| 2573 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2574 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2575 | case PP_CLEAR: |
| 2576 | if (tline->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2577 | nasm_error(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2578 | "trailing garbage after `%%clear' ignored"); |
| 2579 | free_macros(); |
| 2580 | init_macros(); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2581 | free_tlist(origline); |
| 2582 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2583 | |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2584 | case PP_DEPEND: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2585 | t = tline->next = expand_smacro(tline->next); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2586 | skip_white_(t); |
| 2587 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2588 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2589 | nasm_error(ERR_NONFATAL, "`%%depend' expects a file name"); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2590 | free_tlist(origline); |
| 2591 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2592 | } |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2593 | if (t->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2594 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2595 | "trailing garbage after `%%depend' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2596 | p = t->text; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2597 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2598 | nasm_unquote_cstr(p, i); |
H. Peter Anvin | 436e367 | 2016-10-04 01:12:28 -0700 | [diff] [blame] | 2599 | nasm_add_string_to_strlist(dephead, p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2600 | free_tlist(origline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2601 | return DIRECTIVE_FOUND; |
| 2602 | |
| 2603 | case PP_INCLUDE: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2604 | t = tline->next = expand_smacro(tline->next); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2605 | skip_white_(t); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2606 | |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2607 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2608 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2609 | nasm_error(ERR_NONFATAL, "`%%include' expects a file name"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2610 | free_tlist(origline); |
| 2611 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2612 | } |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2613 | if (t->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2614 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2615 | "trailing garbage after `%%include' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2616 | p = t->text; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2617 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2618 | nasm_unquote_cstr(p, i); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2619 | inc = nasm_malloc(sizeof(Include)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2620 | inc->next = istk; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2621 | inc->conds = NULL; |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 2622 | found_path = NULL; |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 2623 | inc->fp = inc_fopen(p, dephead, &found_path, |
H. Peter Anvin | d81a235 | 2016-09-21 14:03:18 -0700 | [diff] [blame] | 2624 | pass == 0 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2625 | if (!inc->fp) { |
| 2626 | /* -MG given but file not found */ |
| 2627 | nasm_free(inc); |
| 2628 | } else { |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 2629 | inc->fname = src_set_fname(found_path ? found_path : p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2630 | inc->lineno = src_set_linnum(0); |
| 2631 | inc->lineinc = 1; |
| 2632 | inc->expansion = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2633 | inc->mstk = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2634 | istk = inc; |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 2635 | lfmt->uplevel(LIST_INCLUDE); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2636 | } |
| 2637 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2638 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2639 | |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2640 | case PP_USE: |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2641 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2642 | static macros_t *use_pkg; |
| 2643 | const char *pkg_macro = NULL; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2644 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2645 | tline = tline->next; |
| 2646 | skip_white_(tline); |
| 2647 | tline = expand_id(tline); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2648 | |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2649 | if (!tline || (tline->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2650 | tline->type != TOK_INTERNAL_STRING && |
| 2651 | tline->type != TOK_ID)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2652 | nasm_error(ERR_NONFATAL, "`%%use' expects a package name"); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2653 | free_tlist(origline); |
| 2654 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2655 | } |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2656 | if (tline->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2657 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2658 | "trailing garbage after `%%use' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2659 | if (tline->type == TOK_STRING) |
| 2660 | nasm_unquote_cstr(tline->text, i); |
| 2661 | use_pkg = nasm_stdmac_find_package(tline->text); |
| 2662 | if (!use_pkg) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2663 | nasm_error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2664 | else |
| 2665 | 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] | 2666 | if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2667 | /* Not already included, go ahead and include it */ |
| 2668 | stdmacpos = use_pkg; |
| 2669 | } |
| 2670 | free_tlist(origline); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2671 | return DIRECTIVE_FOUND; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2672 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2673 | case PP_PUSH: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2674 | case PP_REPL: |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2675 | case PP_POP: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2676 | tline = tline->next; |
| 2677 | skip_white_(tline); |
| 2678 | tline = expand_id(tline); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2679 | if (tline) { |
| 2680 | if (!tok_type_(tline, TOK_ID)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2681 | nasm_error(ERR_NONFATAL, "`%s' expects a context identifier", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2682 | pp_directives[i]); |
| 2683 | free_tlist(origline); |
| 2684 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2685 | } |
| 2686 | if (tline->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2687 | nasm_error(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2688 | "trailing garbage after `%s' ignored", |
| 2689 | pp_directives[i]); |
| 2690 | p = nasm_strdup(tline->text); |
| 2691 | } else { |
| 2692 | p = NULL; /* Anonymous */ |
| 2693 | } |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2694 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2695 | if (i == PP_PUSH) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2696 | ctx = nasm_malloc(sizeof(Context)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2697 | ctx->next = cstk; |
| 2698 | hash_init(&ctx->localmac, HASH_SMALL); |
| 2699 | ctx->name = p; |
| 2700 | ctx->number = unique++; |
| 2701 | cstk = ctx; |
| 2702 | } else { |
| 2703 | /* %pop or %repl */ |
| 2704 | if (!cstk) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2705 | nasm_error(ERR_NONFATAL, "`%s': context stack is empty", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2706 | pp_directives[i]); |
| 2707 | } else if (i == PP_POP) { |
| 2708 | if (p && (!cstk->name || nasm_stricmp(p, cstk->name))) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2709 | nasm_error(ERR_NONFATAL, "`%%pop' in wrong context: %s, " |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2710 | "expected %s", |
| 2711 | cstk->name ? cstk->name : "anonymous", p); |
| 2712 | else |
| 2713 | ctx_pop(); |
| 2714 | } else { |
| 2715 | /* i == PP_REPL */ |
| 2716 | nasm_free(cstk->name); |
| 2717 | cstk->name = p; |
| 2718 | p = NULL; |
| 2719 | } |
| 2720 | nasm_free(p); |
| 2721 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2722 | free_tlist(origline); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2723 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2724 | case PP_FATAL: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2725 | severity = ERR_FATAL; |
| 2726 | goto issue_error; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2727 | case PP_ERROR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2728 | severity = ERR_NONFATAL; |
| 2729 | goto issue_error; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2730 | case PP_WARNING: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2731 | severity = ERR_WARNING|ERR_WARN_USER; |
| 2732 | goto issue_error; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2733 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2734 | issue_error: |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2735 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2736 | /* Only error out if this is the final pass */ |
| 2737 | if (pass != 2 && i != PP_FATAL) |
| 2738 | return DIRECTIVE_FOUND; |
| 2739 | |
| 2740 | tline->next = expand_smacro(tline->next); |
| 2741 | tline = tline->next; |
| 2742 | skip_white_(tline); |
| 2743 | t = tline ? tline->next : NULL; |
| 2744 | skip_white_(t); |
| 2745 | if (tok_type_(tline, TOK_STRING) && !t) { |
| 2746 | /* The line contains only a quoted string */ |
| 2747 | p = tline->text; |
| 2748 | nasm_unquote(p, NULL); /* Ignore NUL character truncation */ |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2749 | nasm_error(severity, "%s", p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2750 | } else { |
| 2751 | /* Not a quoted string, or more than a quoted string */ |
| 2752 | p = detoken(tline, false); |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2753 | nasm_error(severity, "%s", p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2754 | nasm_free(p); |
| 2755 | } |
| 2756 | free_tlist(origline); |
| 2757 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2758 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2759 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2760 | CASE_PP_IF: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2761 | if (istk->conds && !emitting(istk->conds->state)) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2762 | j = COND_NEVER; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2763 | else { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2764 | j = if_condition(tline->next, i); |
| 2765 | tline->next = NULL; /* it got freed */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2766 | j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2767 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2768 | cond = nasm_malloc(sizeof(Cond)); |
| 2769 | cond->next = istk->conds; |
| 2770 | cond->state = j; |
| 2771 | istk->conds = cond; |
| 2772 | if(istk->mstk) |
| 2773 | istk->mstk->condcnt ++; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2774 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2775 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2776 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2777 | CASE_PP_ELIF: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2778 | if (!istk->conds) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2779 | nasm_error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2780 | switch(istk->conds->state) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2781 | case COND_IF_TRUE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2782 | istk->conds->state = COND_DONE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2783 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2784 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2785 | case COND_DONE: |
| 2786 | case COND_NEVER: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2787 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2788 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2789 | case COND_ELSE_TRUE: |
| 2790 | case COND_ELSE_FALSE: |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2791 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND, |
| 2792 | "`%%elif' after `%%else' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2793 | istk->conds->state = COND_NEVER; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2794 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2795 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2796 | case COND_IF_FALSE: |
| 2797 | /* |
| 2798 | * IMPORTANT: In the case of %if, we will already have |
| 2799 | * called expand_mmac_params(); however, if we're |
| 2800 | * processing an %elif we must have been in a |
| 2801 | * non-emitting mode, which would have inhibited |
| 2802 | * the normal invocation of expand_mmac_params(). |
| 2803 | * Therefore, we have to do it explicitly here. |
| 2804 | */ |
| 2805 | j = if_condition(expand_mmac_params(tline->next), i); |
| 2806 | tline->next = NULL; /* it got freed */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2807 | istk->conds->state = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2808 | j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; |
| 2809 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2810 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2811 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2812 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2813 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2814 | case PP_ELSE: |
| 2815 | if (tline->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2816 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND, |
| 2817 | "trailing garbage after `%%else' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2818 | if (!istk->conds) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2819 | nasm_fatal(0, "`%%else: no matching `%%if'"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2820 | switch(istk->conds->state) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2821 | case COND_IF_TRUE: |
| 2822 | case COND_DONE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2823 | istk->conds->state = COND_ELSE_FALSE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2824 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2825 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2826 | case COND_NEVER: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2827 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2828 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2829 | case COND_IF_FALSE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2830 | istk->conds->state = COND_ELSE_TRUE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2831 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2832 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2833 | case COND_ELSE_TRUE: |
| 2834 | case COND_ELSE_FALSE: |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2835 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2836 | "`%%else' after `%%else' ignored."); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2837 | istk->conds->state = COND_NEVER; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2838 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2839 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2840 | free_tlist(origline); |
| 2841 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2842 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2843 | case PP_ENDIF: |
| 2844 | if (tline->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2845 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND, |
| 2846 | "trailing garbage after `%%endif' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2847 | if (!istk->conds) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2848 | nasm_error(ERR_FATAL, "`%%endif': no matching `%%if'"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2849 | cond = istk->conds; |
| 2850 | istk->conds = cond->next; |
| 2851 | nasm_free(cond); |
| 2852 | if(istk->mstk) |
| 2853 | istk->mstk->condcnt --; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2854 | free_tlist(origline); |
| 2855 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2856 | |
H. Peter Anvin | db8f96e | 2009-07-15 09:07:29 -0400 | [diff] [blame] | 2857 | case PP_RMACRO: |
| 2858 | case PP_IRMACRO: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2859 | case PP_MACRO: |
| 2860 | case PP_IMACRO: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2861 | if (defining) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2862 | nasm_error(ERR_FATAL, "`%s': already defining a macro", |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2863 | pp_directives[i]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2864 | return DIRECTIVE_FOUND; |
| 2865 | } |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 2866 | defining = nasm_zalloc(sizeof(MMacro)); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2867 | defining->max_depth = |
| 2868 | (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0; |
| 2869 | defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO); |
| 2870 | if (!parse_mmacro_spec(tline, defining, pp_directives[i])) { |
| 2871 | nasm_free(defining); |
| 2872 | defining = NULL; |
| 2873 | return DIRECTIVE_FOUND; |
| 2874 | } |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2875 | |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 2876 | src_get(&defining->xline, &defining->fname); |
| 2877 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2878 | mmac = (MMacro *) hash_findix(&mmacros, defining->name); |
| 2879 | while (mmac) { |
| 2880 | if (!strcmp(mmac->name, defining->name) && |
| 2881 | (mmac->nparam_min <= defining->nparam_max |
| 2882 | || defining->plus) |
| 2883 | && (defining->nparam_min <= mmac->nparam_max |
| 2884 | || mmac->plus)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2885 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2886 | "redefining multi-line macro `%s'", defining->name); |
| 2887 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2888 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2889 | mmac = mmac->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2890 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2891 | free_tlist(origline); |
| 2892 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2893 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2894 | case PP_ENDM: |
| 2895 | case PP_ENDMACRO: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2896 | if (! (defining && defining->name)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2897 | nasm_error(ERR_NONFATAL, "`%s': not defining a macro", tline->text); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2898 | return DIRECTIVE_FOUND; |
| 2899 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2900 | mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name); |
| 2901 | defining->next = *mmhead; |
| 2902 | *mmhead = defining; |
| 2903 | defining = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2904 | free_tlist(origline); |
| 2905 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2906 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2907 | case PP_EXITMACRO: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2908 | /* |
| 2909 | * We must search along istk->expansion until we hit a |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2910 | * macro-end marker for a macro with a name. Then we |
| 2911 | * bypass all lines between exitmacro and endmacro. |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2912 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2913 | list_for_each(l, istk->expansion) |
| 2914 | if (l->finishes && l->finishes->name) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2915 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2916 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2917 | if (l) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2918 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2919 | * Remove all conditional entries relative to this |
| 2920 | * macro invocation. (safe to do in this context) |
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 | for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) { |
| 2923 | cond = istk->conds; |
| 2924 | istk->conds = cond->next; |
| 2925 | nasm_free(cond); |
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 | istk->expansion = l; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2928 | } else { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2929 | nasm_error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2930 | } |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 2931 | free_tlist(origline); |
| 2932 | return DIRECTIVE_FOUND; |
| 2933 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2934 | case PP_UNMACRO: |
| 2935 | case PP_UNIMACRO: |
| 2936 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2937 | MMacro **mmac_p; |
| 2938 | MMacro spec; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2939 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2940 | spec.casesense = (i == PP_UNMACRO); |
| 2941 | if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) { |
| 2942 | return DIRECTIVE_FOUND; |
| 2943 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2944 | mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL); |
| 2945 | while (mmac_p && *mmac_p) { |
| 2946 | mmac = *mmac_p; |
| 2947 | if (mmac->casesense == spec.casesense && |
| 2948 | !mstrcmp(mmac->name, spec.name, spec.casesense) && |
| 2949 | mmac->nparam_min == spec.nparam_min && |
| 2950 | mmac->nparam_max == spec.nparam_max && |
| 2951 | mmac->plus == spec.plus) { |
| 2952 | *mmac_p = mmac->next; |
| 2953 | free_mmacro(mmac); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2954 | } else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2955 | mmac_p = &mmac->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2956 | } |
| 2957 | } |
| 2958 | free_tlist(origline); |
| 2959 | free_tlist(spec.dlist); |
| 2960 | return DIRECTIVE_FOUND; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2961 | } |
| 2962 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2963 | case PP_ROTATE: |
| 2964 | if (tline->next && tline->next->type == TOK_WHITESPACE) |
| 2965 | tline = tline->next; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2966 | if (!tline->next) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2967 | free_tlist(origline); |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2968 | nasm_error(ERR_NONFATAL, "`%%rotate' missing rotate count"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2969 | return DIRECTIVE_FOUND; |
| 2970 | } |
| 2971 | t = expand_smacro(tline->next); |
| 2972 | tline->next = NULL; |
| 2973 | free_tlist(origline); |
| 2974 | tline = t; |
| 2975 | tptr = &t; |
| 2976 | tokval.t_type = TOKEN_INVALID; |
| 2977 | evalresult = |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2978 | evaluate(ppscan, tptr, &tokval, NULL, pass, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2979 | free_tlist(tline); |
| 2980 | if (!evalresult) |
| 2981 | return DIRECTIVE_FOUND; |
| 2982 | if (tokval.t_type) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2983 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2984 | "trailing garbage after expression ignored"); |
| 2985 | if (!is_simple(evalresult)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2986 | nasm_error(ERR_NONFATAL, "non-constant value given to `%%rotate'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2987 | return DIRECTIVE_FOUND; |
| 2988 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2989 | mmac = istk->mstk; |
| 2990 | while (mmac && !mmac->name) /* avoid mistaking %reps for macros */ |
| 2991 | mmac = mmac->next_active; |
| 2992 | if (!mmac) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2993 | nasm_error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2994 | } else if (mmac->nparam == 0) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2995 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2996 | "`%%rotate' invoked within macro without parameters"); |
| 2997 | } else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2998 | int rotate = mmac->rotate + reloc_value(evalresult); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2999 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3000 | rotate %= (int)mmac->nparam; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3001 | if (rotate < 0) |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3002 | rotate += mmac->nparam; |
| 3003 | |
| 3004 | mmac->rotate = rotate; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3005 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3006 | return DIRECTIVE_FOUND; |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 3007 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3008 | case PP_REP: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 3009 | nolist = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3010 | do { |
| 3011 | tline = tline->next; |
| 3012 | } while (tok_type_(tline, TOK_WHITESPACE)); |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 3013 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3014 | if (tok_type_(tline, TOK_ID) && |
| 3015 | nasm_stricmp(tline->text, ".nolist") == 0) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 3016 | nolist = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3017 | do { |
| 3018 | tline = tline->next; |
| 3019 | } while (tok_type_(tline, TOK_WHITESPACE)); |
| 3020 | } |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 3021 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3022 | if (tline) { |
| 3023 | t = expand_smacro(tline); |
| 3024 | tptr = &t; |
| 3025 | tokval.t_type = TOKEN_INVALID; |
| 3026 | evalresult = |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3027 | evaluate(ppscan, tptr, &tokval, NULL, pass, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3028 | if (!evalresult) { |
| 3029 | free_tlist(origline); |
| 3030 | return DIRECTIVE_FOUND; |
| 3031 | } |
| 3032 | if (tokval.t_type) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3033 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3034 | "trailing garbage after expression ignored"); |
| 3035 | if (!is_simple(evalresult)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3036 | nasm_error(ERR_NONFATAL, "non-constant value given to `%%rep'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3037 | return DIRECTIVE_FOUND; |
| 3038 | } |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 3039 | count = reloc_value(evalresult); |
| 3040 | if (count >= REP_LIMIT) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3041 | nasm_error(ERR_NONFATAL, "`%%rep' value exceeds limit"); |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 3042 | count = 0; |
| 3043 | } else |
| 3044 | count++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3045 | } else { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3046 | nasm_error(ERR_NONFATAL, "`%%rep' expects a repeat count"); |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 3047 | count = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3048 | } |
| 3049 | free_tlist(origline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3050 | |
| 3051 | tmp_defining = defining; |
| 3052 | defining = nasm_malloc(sizeof(MMacro)); |
| 3053 | defining->prev = NULL; |
| 3054 | defining->name = NULL; /* flags this macro as a %rep block */ |
| 3055 | defining->casesense = false; |
| 3056 | defining->plus = false; |
| 3057 | defining->nolist = nolist; |
| 3058 | defining->in_progress = count; |
| 3059 | defining->max_depth = 0; |
| 3060 | defining->nparam_min = defining->nparam_max = 0; |
| 3061 | defining->defaults = NULL; |
| 3062 | defining->dlist = NULL; |
| 3063 | defining->expansion = NULL; |
| 3064 | defining->next_active = istk->mstk; |
| 3065 | defining->rep_nest = tmp_defining; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3066 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3067 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3068 | case PP_ENDREP: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3069 | if (!defining || defining->name) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3070 | nasm_error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3071 | return DIRECTIVE_FOUND; |
| 3072 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3073 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3074 | /* |
| 3075 | * Now we have a "macro" defined - although it has no name |
| 3076 | * and we won't be entering it in the hash tables - we must |
| 3077 | * push a macro-end marker for it on to istk->expansion. |
| 3078 | * After that, it will take care of propagating itself (a |
| 3079 | * macro-end marker line for a macro which is really a %rep |
| 3080 | * block will cause the macro to be re-expanded, complete |
| 3081 | * with another macro-end marker to ensure the process |
| 3082 | * continues) until the whole expansion is forcibly removed |
| 3083 | * from istk->expansion by a %exitrep. |
| 3084 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3085 | l = nasm_malloc(sizeof(Line)); |
| 3086 | l->next = istk->expansion; |
| 3087 | l->finishes = defining; |
| 3088 | l->first = NULL; |
| 3089 | istk->expansion = l; |
| 3090 | |
| 3091 | istk->mstk = defining; |
| 3092 | |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 3093 | lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3094 | tmp_defining = defining; |
| 3095 | defining = defining->rep_nest; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3096 | free_tlist(origline); |
| 3097 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3098 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3099 | case PP_EXITREP: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3100 | /* |
| 3101 | * We must search along istk->expansion until we hit a |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3102 | * macro-end marker for a macro with no name. Then we set |
| 3103 | * its `in_progress' flag to 0. |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3104 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3105 | list_for_each(l, istk->expansion) |
| 3106 | if (l->finishes && !l->finishes->name) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3107 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3108 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3109 | if (l) |
| 3110 | l->finishes->in_progress = 1; |
| 3111 | else |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3112 | nasm_error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3113 | free_tlist(origline); |
| 3114 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3115 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3116 | case PP_XDEFINE: |
| 3117 | case PP_IXDEFINE: |
| 3118 | case PP_DEFINE: |
| 3119 | case PP_IDEFINE: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3120 | casesense = (i == PP_DEFINE || i == PP_XDEFINE); |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3121 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3122 | tline = tline->next; |
| 3123 | skip_white_(tline); |
| 3124 | tline = expand_id(tline); |
| 3125 | if (!tline || (tline->type != TOK_ID && |
| 3126 | (tline->type != TOK_PREPROC_ID || |
| 3127 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3128 | nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3129 | pp_directives[i]); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3130 | free_tlist(origline); |
| 3131 | return DIRECTIVE_FOUND; |
| 3132 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3133 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3134 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3135 | last = tline; |
| 3136 | param_start = tline = tline->next; |
| 3137 | nparam = 0; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3138 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3139 | /* Expand the macro definition now for %xdefine and %ixdefine */ |
| 3140 | if ((i == PP_XDEFINE) || (i == PP_IXDEFINE)) |
| 3141 | tline = expand_smacro(tline); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3142 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3143 | if (tok_is_(tline, "(")) { |
| 3144 | /* |
| 3145 | * This macro has parameters. |
| 3146 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3147 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3148 | tline = tline->next; |
| 3149 | while (1) { |
| 3150 | skip_white_(tline); |
| 3151 | if (!tline) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3152 | nasm_error(ERR_NONFATAL, "parameter identifier expected"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3153 | free_tlist(origline); |
| 3154 | return DIRECTIVE_FOUND; |
| 3155 | } |
| 3156 | if (tline->type != TOK_ID) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3157 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3158 | "`%s': parameter identifier expected", |
| 3159 | tline->text); |
| 3160 | free_tlist(origline); |
| 3161 | return DIRECTIVE_FOUND; |
| 3162 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3163 | tline->type = TOK_SMAC_PARAM + nparam++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3164 | tline = tline->next; |
| 3165 | skip_white_(tline); |
| 3166 | if (tok_is_(tline, ",")) { |
| 3167 | tline = tline->next; |
H. Peter Anvin | ca348b6 | 2008-07-23 10:49:26 -0400 | [diff] [blame] | 3168 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3169 | if (!tok_is_(tline, ")")) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3170 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3171 | "`)' expected to terminate macro template"); |
| 3172 | free_tlist(origline); |
| 3173 | return DIRECTIVE_FOUND; |
| 3174 | } |
| 3175 | break; |
| 3176 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3177 | } |
| 3178 | last = tline; |
| 3179 | tline = tline->next; |
| 3180 | } |
| 3181 | if (tok_type_(tline, TOK_WHITESPACE)) |
| 3182 | last = tline, tline = tline->next; |
| 3183 | macro_start = NULL; |
| 3184 | last->next = NULL; |
| 3185 | t = tline; |
| 3186 | while (t) { |
| 3187 | if (t->type == TOK_ID) { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3188 | list_for_each(tt, param_start) |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3189 | if (tt->type >= TOK_SMAC_PARAM && |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3190 | !strcmp(tt->text, t->text)) |
| 3191 | t->type = tt->type; |
| 3192 | } |
| 3193 | tt = t->next; |
| 3194 | t->next = macro_start; |
| 3195 | macro_start = t; |
| 3196 | t = tt; |
| 3197 | } |
| 3198 | /* |
| 3199 | * Good. We now have a macro name, a parameter count, and a |
| 3200 | * token list (in reverse order) for an expansion. We ought |
| 3201 | * to be OK just to create an SMacro, store it, and let |
| 3202 | * free_tlist have the rest of the line (which we have |
| 3203 | * carefully re-terminated after chopping off the expansion |
| 3204 | * from the end). |
| 3205 | */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 3206 | define_smacro(ctx, mname, casesense, nparam, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3207 | free_tlist(origline); |
| 3208 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3209 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3210 | case PP_UNDEF: |
| 3211 | tline = tline->next; |
| 3212 | skip_white_(tline); |
| 3213 | tline = expand_id(tline); |
| 3214 | if (!tline || (tline->type != TOK_ID && |
| 3215 | (tline->type != TOK_PREPROC_ID || |
| 3216 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3217 | nasm_error(ERR_NONFATAL, "`%%undef' expects a macro identifier"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3218 | free_tlist(origline); |
| 3219 | return DIRECTIVE_FOUND; |
| 3220 | } |
| 3221 | if (tline->next) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3222 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3223 | "trailing garbage after macro name ignored"); |
| 3224 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3225 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3226 | /* Find the context that symbol belongs to */ |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3227 | ctx = get_ctx(tline->text, &mname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3228 | undef_smacro(ctx, mname); |
| 3229 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3230 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3231 | |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3232 | case PP_DEFSTR: |
| 3233 | case PP_IDEFSTR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3234 | casesense = (i == PP_DEFSTR); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3235 | |
| 3236 | tline = tline->next; |
| 3237 | skip_white_(tline); |
| 3238 | tline = expand_id(tline); |
| 3239 | if (!tline || (tline->type != TOK_ID && |
| 3240 | (tline->type != TOK_PREPROC_ID || |
| 3241 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3242 | nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3243 | pp_directives[i]); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3244 | free_tlist(origline); |
| 3245 | return DIRECTIVE_FOUND; |
| 3246 | } |
| 3247 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3248 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3249 | last = tline; |
| 3250 | tline = expand_smacro(tline->next); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3251 | last->next = NULL; |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3252 | |
| 3253 | while (tok_type_(tline, TOK_WHITESPACE)) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3254 | tline = delete_Token(tline); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3255 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3256 | p = detoken(tline, false); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3257 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3258 | macro_start->next = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3259 | macro_start->text = nasm_quote(p, strlen(p)); |
| 3260 | macro_start->type = TOK_STRING; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3261 | macro_start->a.mac = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3262 | nasm_free(p); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3263 | |
| 3264 | /* |
| 3265 | * We now have a macro name, an implicit parameter count of |
| 3266 | * zero, and a string token to use as an expansion. Create |
| 3267 | * and store an SMacro. |
| 3268 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3269 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3270 | free_tlist(origline); |
| 3271 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3272 | |
H. Peter Anvin | 2f55bda | 2009-07-14 15:04:04 -0400 | [diff] [blame] | 3273 | case PP_DEFTOK: |
| 3274 | case PP_IDEFTOK: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3275 | casesense = (i == PP_DEFTOK); |
| 3276 | |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3277 | tline = tline->next; |
| 3278 | skip_white_(tline); |
| 3279 | tline = expand_id(tline); |
| 3280 | if (!tline || (tline->type != TOK_ID && |
| 3281 | (tline->type != TOK_PREPROC_ID || |
| 3282 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3283 | nasm_error(ERR_NONFATAL, |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3284 | "`%s' expects a macro identifier as first parameter", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3285 | pp_directives[i]); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3286 | free_tlist(origline); |
| 3287 | return DIRECTIVE_FOUND; |
| 3288 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3289 | ctx = get_ctx(tline->text, &mname); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3290 | last = tline; |
| 3291 | tline = expand_smacro(tline->next); |
| 3292 | last->next = NULL; |
| 3293 | |
| 3294 | t = tline; |
| 3295 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3296 | t = t->next; |
| 3297 | /* t should now point to the string */ |
Cyrill Gorcunov | 6908e58 | 2010-09-06 19:36:15 +0400 | [diff] [blame] | 3298 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3299 | nasm_error(ERR_NONFATAL, |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3300 | "`%s` requires string as second parameter", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3301 | pp_directives[i]); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3302 | free_tlist(tline); |
| 3303 | free_tlist(origline); |
| 3304 | return DIRECTIVE_FOUND; |
| 3305 | } |
| 3306 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 3307 | /* |
| 3308 | * Convert the string to a token stream. Note that smacros |
| 3309 | * are stored with the token stream reversed, so we have to |
| 3310 | * reverse the output of tokenize(). |
| 3311 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3312 | nasm_unquote_cstr(t->text, i); |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 3313 | macro_start = reverse_tokens(tokenize(t->text)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3314 | |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3315 | /* |
| 3316 | * We now have a macro name, an implicit parameter count of |
| 3317 | * zero, and a numeric token to use as an expansion. Create |
| 3318 | * and store an SMacro. |
| 3319 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3320 | define_smacro(ctx, mname, casesense, 0, macro_start); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3321 | free_tlist(tline); |
| 3322 | free_tlist(origline); |
| 3323 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3324 | |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3325 | case PP_PATHSEARCH: |
| 3326 | { |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 3327 | const char *found_path; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3328 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3329 | casesense = true; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3330 | |
| 3331 | tline = tline->next; |
| 3332 | skip_white_(tline); |
| 3333 | tline = expand_id(tline); |
| 3334 | if (!tline || (tline->type != TOK_ID && |
| 3335 | (tline->type != TOK_PREPROC_ID || |
| 3336 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3337 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3338 | "`%%pathsearch' expects a macro identifier as first parameter"); |
| 3339 | free_tlist(origline); |
| 3340 | return DIRECTIVE_FOUND; |
| 3341 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3342 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3343 | last = tline; |
| 3344 | tline = expand_smacro(tline->next); |
| 3345 | last->next = NULL; |
| 3346 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3347 | t = tline; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3348 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3349 | t = t->next; |
| 3350 | |
| 3351 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3352 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3353 | nasm_error(ERR_NONFATAL, "`%%pathsearch' expects a file name"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3354 | free_tlist(tline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3355 | free_tlist(origline); |
| 3356 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 3357 | } |
| 3358 | if (t->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3359 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3360 | "trailing garbage after `%%pathsearch' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3361 | p = t->text; |
H. Peter Anvin | 427cc91 | 2008-06-01 21:43:03 -0700 | [diff] [blame] | 3362 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3363 | nasm_unquote(p, NULL); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3364 | |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 3365 | inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY); |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 3366 | if (!found_path) |
| 3367 | found_path = p; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3368 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3369 | macro_start->next = NULL; |
H. Peter Anvin | ccad6f9 | 2016-10-04 00:34:35 -0700 | [diff] [blame] | 3370 | macro_start->text = nasm_quote(found_path, strlen(found_path)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3371 | macro_start->type = TOK_STRING; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3372 | macro_start->a.mac = NULL; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3373 | |
| 3374 | /* |
| 3375 | * We now have a macro name, an implicit parameter count of |
| 3376 | * zero, and a string token to use as an expansion. Create |
| 3377 | * and store an SMacro. |
| 3378 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3379 | define_smacro(ctx, mname, casesense, 0, macro_start); |
| 3380 | free_tlist(tline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3381 | free_tlist(origline); |
| 3382 | return DIRECTIVE_FOUND; |
| 3383 | } |
| 3384 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3385 | case PP_STRLEN: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3386 | casesense = true; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 3387 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3388 | tline = tline->next; |
| 3389 | skip_white_(tline); |
| 3390 | tline = expand_id(tline); |
| 3391 | if (!tline || (tline->type != TOK_ID && |
| 3392 | (tline->type != TOK_PREPROC_ID || |
| 3393 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3394 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3395 | "`%%strlen' expects a macro identifier as first parameter"); |
| 3396 | free_tlist(origline); |
| 3397 | return DIRECTIVE_FOUND; |
| 3398 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3399 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3400 | last = tline; |
| 3401 | tline = expand_smacro(tline->next); |
| 3402 | last->next = NULL; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3403 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3404 | t = tline; |
| 3405 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3406 | t = t->next; |
| 3407 | /* t should now point to the string */ |
Cyrill Gorcunov | 4e1d5ab | 2010-07-23 18:51:51 +0400 | [diff] [blame] | 3408 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3409 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3410 | "`%%strlen` requires string as second parameter"); |
| 3411 | free_tlist(tline); |
| 3412 | free_tlist(origline); |
| 3413 | return DIRECTIVE_FOUND; |
| 3414 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3415 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3416 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3417 | macro_start->next = NULL; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 3418 | make_tok_num(macro_start, nasm_unquote(t->text, NULL)); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3419 | macro_start->a.mac = NULL; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3420 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3421 | /* |
| 3422 | * We now have a macro name, an implicit parameter count of |
| 3423 | * zero, and a numeric token to use as an expansion. Create |
| 3424 | * and store an SMacro. |
| 3425 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3426 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3427 | free_tlist(tline); |
| 3428 | free_tlist(origline); |
| 3429 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3430 | |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3431 | case PP_STRCAT: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3432 | casesense = true; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3433 | |
| 3434 | tline = tline->next; |
| 3435 | skip_white_(tline); |
| 3436 | tline = expand_id(tline); |
| 3437 | if (!tline || (tline->type != TOK_ID && |
| 3438 | (tline->type != TOK_PREPROC_ID || |
| 3439 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3440 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3441 | "`%%strcat' expects a macro identifier as first parameter"); |
| 3442 | free_tlist(origline); |
| 3443 | return DIRECTIVE_FOUND; |
| 3444 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3445 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3446 | last = tline; |
| 3447 | tline = expand_smacro(tline->next); |
| 3448 | last->next = NULL; |
| 3449 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3450 | len = 0; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3451 | list_for_each(t, tline) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3452 | switch (t->type) { |
| 3453 | case TOK_WHITESPACE: |
| 3454 | break; |
| 3455 | case TOK_STRING: |
| 3456 | len += t->a.len = nasm_unquote(t->text, NULL); |
| 3457 | break; |
| 3458 | case TOK_OTHER: |
| 3459 | if (!strcmp(t->text, ",")) /* permit comma separators */ |
| 3460 | break; |
| 3461 | /* else fall through */ |
| 3462 | default: |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3463 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3464 | "non-string passed to `%%strcat' (%d)", t->type); |
| 3465 | free_tlist(tline); |
| 3466 | free_tlist(origline); |
| 3467 | return DIRECTIVE_FOUND; |
| 3468 | } |
| 3469 | } |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3470 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3471 | p = pp = nasm_malloc(len); |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3472 | list_for_each(t, tline) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3473 | if (t->type == TOK_STRING) { |
| 3474 | memcpy(p, t->text, t->a.len); |
| 3475 | p += t->a.len; |
| 3476 | } |
| 3477 | } |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3478 | |
| 3479 | /* |
| 3480 | * We now have a macro name, an implicit parameter count of |
| 3481 | * zero, and a numeric token to use as an expansion. Create |
| 3482 | * and store an SMacro. |
| 3483 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3484 | macro_start = new_Token(NULL, TOK_STRING, NULL, 0); |
| 3485 | macro_start->text = nasm_quote(pp, len); |
| 3486 | nasm_free(pp); |
| 3487 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3488 | free_tlist(tline); |
| 3489 | free_tlist(origline); |
| 3490 | return DIRECTIVE_FOUND; |
| 3491 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3492 | case PP_SUBSTR: |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3493 | { |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3494 | int64_t start, count; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3495 | size_t len; |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 3496 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3497 | casesense = true; |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3498 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3499 | tline = tline->next; |
| 3500 | skip_white_(tline); |
| 3501 | tline = expand_id(tline); |
| 3502 | if (!tline || (tline->type != TOK_ID && |
| 3503 | (tline->type != TOK_PREPROC_ID || |
| 3504 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3505 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3506 | "`%%substr' expects a macro identifier as first parameter"); |
| 3507 | free_tlist(origline); |
| 3508 | return DIRECTIVE_FOUND; |
| 3509 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3510 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3511 | last = tline; |
| 3512 | tline = expand_smacro(tline->next); |
| 3513 | last->next = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3514 | |
Cyrill Gorcunov | 35519d6 | 2010-09-06 23:49:52 +0400 | [diff] [blame] | 3515 | if (tline) /* skip expanded id */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3516 | t = tline->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3517 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3518 | t = t->next; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3519 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3520 | /* t should now point to the string */ |
Cyrill Gorcunov | 35519d6 | 2010-09-06 23:49:52 +0400 | [diff] [blame] | 3521 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3522 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3523 | "`%%substr` requires string as second parameter"); |
| 3524 | free_tlist(tline); |
| 3525 | free_tlist(origline); |
| 3526 | return DIRECTIVE_FOUND; |
| 3527 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3528 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3529 | tt = t->next; |
| 3530 | tptr = &tt; |
| 3531 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3532 | evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3533 | if (!evalresult) { |
| 3534 | free_tlist(tline); |
| 3535 | free_tlist(origline); |
| 3536 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3537 | } else if (!is_simple(evalresult)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3538 | nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3539 | free_tlist(tline); |
| 3540 | free_tlist(origline); |
| 3541 | return DIRECTIVE_FOUND; |
| 3542 | } |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3543 | start = evalresult->value - 1; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3544 | |
| 3545 | while (tok_type_(tt, TOK_WHITESPACE)) |
| 3546 | tt = tt->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3547 | if (!tt) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3548 | count = 1; /* Backwards compatibility: one character */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3549 | } else { |
| 3550 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3551 | evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3552 | if (!evalresult) { |
| 3553 | free_tlist(tline); |
| 3554 | free_tlist(origline); |
| 3555 | return DIRECTIVE_FOUND; |
| 3556 | } else if (!is_simple(evalresult)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3557 | nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3558 | free_tlist(tline); |
| 3559 | free_tlist(origline); |
| 3560 | return DIRECTIVE_FOUND; |
| 3561 | } |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3562 | count = evalresult->value; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3563 | } |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3564 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3565 | len = nasm_unquote(t->text, NULL); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3566 | |
Cyrill Gorcunov | cff031e | 2010-09-07 20:31:11 +0400 | [diff] [blame] | 3567 | /* make start and count being in range */ |
| 3568 | if (start < 0) |
| 3569 | start = 0; |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3570 | if (count < 0) |
| 3571 | count = len + count + 1 - start; |
| 3572 | if (start + count > (int64_t)len) |
Cyrill Gorcunov | cff031e | 2010-09-07 20:31:11 +0400 | [diff] [blame] | 3573 | count = len - start; |
| 3574 | if (!len || count < 0 || start >=(int64_t)len) |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3575 | start = -1, count = 0; /* empty string */ |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3576 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3577 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3578 | macro_start->next = NULL; |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3579 | macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3580 | macro_start->type = TOK_STRING; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3581 | macro_start->a.mac = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3582 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3583 | /* |
| 3584 | * We now have a macro name, an implicit parameter count of |
| 3585 | * zero, and a numeric token to use as an expansion. Create |
| 3586 | * and store an SMacro. |
| 3587 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3588 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3589 | free_tlist(tline); |
| 3590 | free_tlist(origline); |
| 3591 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3592 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3593 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3594 | case PP_ASSIGN: |
| 3595 | case PP_IASSIGN: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3596 | casesense = (i == PP_ASSIGN); |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3597 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3598 | tline = tline->next; |
| 3599 | skip_white_(tline); |
| 3600 | tline = expand_id(tline); |
| 3601 | if (!tline || (tline->type != TOK_ID && |
| 3602 | (tline->type != TOK_PREPROC_ID || |
| 3603 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3604 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3605 | "`%%%sassign' expects a macro identifier", |
| 3606 | (i == PP_IASSIGN ? "i" : "")); |
| 3607 | free_tlist(origline); |
| 3608 | return DIRECTIVE_FOUND; |
| 3609 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3610 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3611 | last = tline; |
| 3612 | tline = expand_smacro(tline->next); |
| 3613 | last->next = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3614 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3615 | t = tline; |
| 3616 | tptr = &t; |
| 3617 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3618 | evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3619 | free_tlist(tline); |
| 3620 | if (!evalresult) { |
| 3621 | free_tlist(origline); |
| 3622 | return DIRECTIVE_FOUND; |
| 3623 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3624 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3625 | if (tokval.t_type) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3626 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3627 | "trailing garbage after expression ignored"); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3628 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3629 | if (!is_simple(evalresult)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3630 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3631 | "non-constant value given to `%%%sassign'", |
| 3632 | (i == PP_IASSIGN ? "i" : "")); |
| 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 | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3637 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3638 | macro_start->next = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3639 | make_tok_num(macro_start, reloc_value(evalresult)); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3640 | macro_start->a.mac = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3641 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3642 | /* |
| 3643 | * We now have a macro name, an implicit parameter count of |
| 3644 | * zero, and a numeric token to use as an expansion. Create |
| 3645 | * and store an SMacro. |
| 3646 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3647 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3648 | free_tlist(origline); |
| 3649 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3650 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3651 | case PP_LINE: |
| 3652 | /* |
| 3653 | * Syntax is `%line nnn[+mmm] [filename]' |
| 3654 | */ |
| 3655 | tline = tline->next; |
| 3656 | skip_white_(tline); |
| 3657 | if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3658 | nasm_error(ERR_NONFATAL, "`%%line' expects line number"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3659 | free_tlist(origline); |
| 3660 | return DIRECTIVE_FOUND; |
| 3661 | } |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 3662 | k = readnum(tline->text, &err); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3663 | m = 1; |
| 3664 | tline = tline->next; |
| 3665 | if (tok_is_(tline, "+")) { |
| 3666 | tline = tline->next; |
| 3667 | if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3668 | nasm_error(ERR_NONFATAL, "`%%line' expects line increment"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3669 | free_tlist(origline); |
| 3670 | return DIRECTIVE_FOUND; |
| 3671 | } |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 3672 | m = readnum(tline->text, &err); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3673 | tline = tline->next; |
| 3674 | } |
| 3675 | skip_white_(tline); |
| 3676 | src_set_linnum(k); |
| 3677 | istk->lineinc = m; |
| 3678 | if (tline) { |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 3679 | char *fname = detoken(tline, false); |
| 3680 | src_set_fname(fname); |
| 3681 | nasm_free(fname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3682 | } |
| 3683 | free_tlist(origline); |
| 3684 | return DIRECTIVE_FOUND; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3685 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3686 | default: |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3687 | nasm_error(ERR_FATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3688 | "preprocessor directive `%s' not yet implemented", |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 3689 | pp_directives[i]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3690 | return DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3691 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3692 | } |
| 3693 | |
| 3694 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3695 | * Ensure that a macro parameter contains a condition code and |
| 3696 | * nothing else. Return the condition code index if so, or -1 |
| 3697 | * otherwise. |
| 3698 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3699 | static int find_cc(Token * t) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3700 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3701 | Token *tt; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3702 | |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3703 | if (!t) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3704 | return -1; /* Probably a %+ without a space */ |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3705 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3706 | skip_white_(t); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3707 | if (t->type != TOK_ID) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3708 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3709 | tt = t->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3710 | skip_white_(tt); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3711 | if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ","))) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3712 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3713 | |
Cyrill Gorcunov | 1945639 | 2012-05-02 00:18:56 +0400 | [diff] [blame] | 3714 | return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3715 | } |
| 3716 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3717 | /* |
| 3718 | * This routines walks over tokens strem and hadnles tokens |
| 3719 | * pasting, if @handle_explicit passed then explicit pasting |
| 3720 | * term is handled, otherwise -- implicit pastings only. |
| 3721 | */ |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 3722 | static bool paste_tokens(Token **head, const struct tokseq_match *m, |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3723 | size_t mnum, bool handle_explicit) |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3724 | { |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3725 | Token *tok, *next, **prev_next, **prev_nonspace; |
| 3726 | bool pasted = false; |
| 3727 | char *buf, *p; |
| 3728 | size_t len, i; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3729 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3730 | /* |
| 3731 | * The last token before pasting. We need it |
| 3732 | * to be able to connect new handled tokens. |
| 3733 | * In other words if there were a tokens stream |
| 3734 | * |
| 3735 | * A -> B -> C -> D |
| 3736 | * |
| 3737 | * and we've joined tokens B and C, the resulting |
| 3738 | * stream should be |
| 3739 | * |
| 3740 | * A -> BC -> D |
| 3741 | */ |
| 3742 | tok = *head; |
| 3743 | prev_next = NULL; |
| 3744 | |
| 3745 | if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE)) |
| 3746 | prev_nonspace = head; |
| 3747 | else |
| 3748 | prev_nonspace = NULL; |
| 3749 | |
| 3750 | while (tok && (next = tok->next)) { |
| 3751 | |
| 3752 | switch (tok->type) { |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3753 | case TOK_WHITESPACE: |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3754 | /* Zap redundant whitespaces */ |
| 3755 | while (tok_type_(next, TOK_WHITESPACE)) |
| 3756 | next = delete_Token(next); |
| 3757 | tok->next = next; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3758 | break; |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3759 | |
| 3760 | case TOK_PASTE: |
| 3761 | /* Explicit pasting */ |
| 3762 | if (!handle_explicit) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3763 | break; |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3764 | next = delete_Token(tok); |
| 3765 | |
| 3766 | while (tok_type_(next, TOK_WHITESPACE)) |
| 3767 | next = delete_Token(next); |
| 3768 | |
| 3769 | if (!pasted) |
| 3770 | pasted = true; |
| 3771 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3772 | /* Left pasting token is start of line */ |
| 3773 | if (!prev_nonspace) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3774 | nasm_error(ERR_FATAL, "No lvalue found on pasting"); |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3775 | |
Cyrill Gorcunov | 8b5c9fb | 2013-02-04 01:24:54 +0400 | [diff] [blame] | 3776 | /* |
| 3777 | * No ending token, this might happen in two |
| 3778 | * cases |
| 3779 | * |
| 3780 | * 1) There indeed no right token at all |
| 3781 | * 2) There is a bare "%define ID" statement, |
| 3782 | * and @ID does expand to whitespace. |
| 3783 | * |
| 3784 | * So technically we need to do a grammar analysis |
| 3785 | * in another stage of parsing, but for now lets don't |
| 3786 | * change the behaviour people used to. Simply allow |
| 3787 | * whitespace after paste token. |
| 3788 | */ |
| 3789 | if (!next) { |
| 3790 | /* |
| 3791 | * Zap ending space tokens and that's all. |
| 3792 | */ |
| 3793 | tok = (*prev_nonspace)->next; |
| 3794 | while (tok_type_(tok, TOK_WHITESPACE)) |
| 3795 | tok = delete_Token(tok); |
| 3796 | tok = *prev_nonspace; |
| 3797 | tok->next = NULL; |
| 3798 | break; |
| 3799 | } |
| 3800 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3801 | tok = *prev_nonspace; |
| 3802 | while (tok_type_(tok, TOK_WHITESPACE)) |
| 3803 | tok = delete_Token(tok); |
| 3804 | len = strlen(tok->text); |
| 3805 | len += strlen(next->text); |
| 3806 | |
| 3807 | p = buf = nasm_malloc(len + 1); |
| 3808 | strcpy(p, tok->text); |
| 3809 | p = strchr(p, '\0'); |
| 3810 | strcpy(p, next->text); |
| 3811 | |
| 3812 | delete_Token(tok); |
| 3813 | |
| 3814 | tok = tokenize(buf); |
| 3815 | nasm_free(buf); |
| 3816 | |
| 3817 | *prev_nonspace = tok; |
| 3818 | while (tok && tok->next) |
| 3819 | tok = tok->next; |
| 3820 | |
| 3821 | tok->next = delete_Token(next); |
| 3822 | |
| 3823 | /* Restart from pasted tokens head */ |
| 3824 | tok = *prev_nonspace; |
| 3825 | break; |
| 3826 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3827 | default: |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3828 | /* implicit pasting */ |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 3829 | for (i = 0; i < mnum; i++) { |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3830 | if (!(PP_CONCAT_MATCH(tok, m[i].mask_head))) |
| 3831 | continue; |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 3832 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3833 | len = 0; |
| 3834 | while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) { |
| 3835 | len += strlen(next->text); |
| 3836 | next = next->next; |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 3837 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3838 | |
| 3839 | /* No match */ |
| 3840 | if (tok == next) |
| 3841 | break; |
| 3842 | |
| 3843 | len += strlen(tok->text); |
| 3844 | p = buf = nasm_malloc(len + 1); |
| 3845 | |
| 3846 | while (tok != next) { |
| 3847 | strcpy(p, tok->text); |
| 3848 | p = strchr(p, '\0'); |
| 3849 | tok = delete_Token(tok); |
| 3850 | } |
| 3851 | |
| 3852 | tok = tokenize(buf); |
| 3853 | nasm_free(buf); |
| 3854 | |
| 3855 | if (prev_next) |
| 3856 | *prev_next = tok; |
| 3857 | else |
| 3858 | *head = tok; |
| 3859 | |
| 3860 | /* |
| 3861 | * Connect pasted into original stream, |
| 3862 | * ie A -> new-tokens -> B |
| 3863 | */ |
| 3864 | while (tok && tok->next) |
| 3865 | tok = tok->next; |
| 3866 | tok->next = next; |
| 3867 | |
| 3868 | if (!pasted) |
| 3869 | pasted = true; |
| 3870 | |
| 3871 | /* Restart from pasted tokens head */ |
| 3872 | tok = prev_next ? *prev_next : *head; |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 3873 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3874 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3875 | break; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3876 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3877 | |
| 3878 | prev_next = &tok->next; |
| 3879 | |
| 3880 | if (tok->next && |
| 3881 | !tok_type_(tok->next, TOK_WHITESPACE) && |
| 3882 | !tok_type_(tok->next, TOK_PASTE)) |
| 3883 | prev_nonspace = prev_next; |
| 3884 | |
| 3885 | tok = tok->next; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3886 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3887 | |
| 3888 | return pasted; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3889 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3890 | |
| 3891 | /* |
| 3892 | * expands to a list of tokens from %{x:y} |
| 3893 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3894 | static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3895 | { |
| 3896 | Token *t = tline, **tt, *tm, *head; |
| 3897 | char *pos; |
| 3898 | int fst, lst, j, i; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3899 | |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3900 | pos = strchr(tline->text, ':'); |
| 3901 | nasm_assert(pos); |
| 3902 | |
| 3903 | lst = atoi(pos + 1); |
| 3904 | fst = atoi(tline->text + 1); |
| 3905 | |
| 3906 | /* |
| 3907 | * only macros params are accounted so |
| 3908 | * if someone passes %0 -- we reject such |
| 3909 | * value(s) |
| 3910 | */ |
| 3911 | if (lst == 0 || fst == 0) |
| 3912 | goto err; |
| 3913 | |
| 3914 | /* the values should be sane */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3915 | if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) || |
| 3916 | (lst > (int)mac->nparam || lst < (-(int)mac->nparam))) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3917 | goto err; |
| 3918 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3919 | fst = fst < 0 ? fst + (int)mac->nparam + 1: fst; |
| 3920 | lst = lst < 0 ? lst + (int)mac->nparam + 1: lst; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3921 | |
| 3922 | /* counted from zero */ |
| 3923 | fst--, lst--; |
| 3924 | |
| 3925 | /* |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3926 | * It will be at least one token. Note we |
| 3927 | * need to scan params until separator, otherwise |
| 3928 | * only first token will be passed. |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3929 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3930 | tm = mac->params[(fst + mac->rotate) % mac->nparam]; |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3931 | head = new_Token(NULL, tm->type, tm->text, 0); |
| 3932 | tt = &head->next, tm = tm->next; |
| 3933 | while (tok_isnt_(tm, ",")) { |
| 3934 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 3935 | *tt = t, tt = &t->next, tm = tm->next; |
| 3936 | } |
| 3937 | |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3938 | if (fst < lst) { |
| 3939 | for (i = fst + 1; i <= lst; i++) { |
| 3940 | t = new_Token(NULL, TOK_OTHER, ",", 0); |
| 3941 | *tt = t, tt = &t->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3942 | j = (i + mac->rotate) % mac->nparam; |
| 3943 | tm = mac->params[j]; |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3944 | while (tok_isnt_(tm, ",")) { |
| 3945 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 3946 | *tt = t, tt = &t->next, tm = tm->next; |
| 3947 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3948 | } |
| 3949 | } else { |
| 3950 | for (i = fst - 1; i >= lst; i--) { |
| 3951 | t = new_Token(NULL, TOK_OTHER, ",", 0); |
| 3952 | *tt = t, tt = &t->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3953 | j = (i + mac->rotate) % mac->nparam; |
| 3954 | tm = mac->params[j]; |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3955 | while (tok_isnt_(tm, ",")) { |
| 3956 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 3957 | *tt = t, tt = &t->next, tm = tm->next; |
| 3958 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3959 | } |
| 3960 | } |
| 3961 | |
| 3962 | *last = tt; |
| 3963 | return head; |
| 3964 | |
| 3965 | err: |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3966 | nasm_error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range", |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3967 | &tline->text[1]); |
| 3968 | return tline; |
| 3969 | } |
| 3970 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3971 | /* |
| 3972 | * Expand MMacro-local things: parameter references (%0, %n, %+n, |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 3973 | * %-n) and MMacro-local identifiers (%%foo) as well as |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3974 | * macro indirection (%[...]) and range (%{..:..}). |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3975 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3976 | static Token *expand_mmac_params(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3977 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3978 | Token *t, *tt, **tail, *thead; |
H. Peter Anvin | 6125b62 | 2009-04-08 14:02:25 -0700 | [diff] [blame] | 3979 | bool changed = false; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3980 | char *pos; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3981 | |
| 3982 | tail = &thead; |
| 3983 | thead = NULL; |
| 3984 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3985 | while (tline) { |
| 3986 | if (tline->type == TOK_PREPROC_ID && |
Cyrill Gorcunov | ca61119 | 2010-06-04 09:22:12 +0400 | [diff] [blame] | 3987 | (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) || |
| 3988 | (tline->text[1] >= '0' && tline->text[1] <= '9') || |
| 3989 | tline->text[1] == '%')) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 3990 | char *text = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3991 | int type = 0, cc; /* type = 0 to placate optimisers */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 3992 | char tmpbuf[30]; |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3993 | unsigned int n; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3994 | int i; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3995 | MMacro *mac; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3996 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3997 | t = tline; |
| 3998 | tline = tline->next; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3999 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4000 | mac = istk->mstk; |
| 4001 | while (mac && !mac->name) /* avoid mistaking %reps for macros */ |
| 4002 | mac = mac->next_active; |
| 4003 | if (!mac) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4004 | nasm_error(ERR_NONFATAL, "`%s': not in a macro call", t->text); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4005 | } else { |
| 4006 | pos = strchr(t->text, ':'); |
| 4007 | if (!pos) { |
| 4008 | switch (t->text[1]) { |
| 4009 | /* |
| 4010 | * We have to make a substitution of one of the |
| 4011 | * forms %1, %-1, %+1, %%foo, %0. |
| 4012 | */ |
| 4013 | case '0': |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4014 | type = TOK_NUMBER; |
| 4015 | snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam); |
| 4016 | text = nasm_strdup(tmpbuf); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4017 | break; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4018 | case '%': |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4019 | type = TOK_ID; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4020 | snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".", |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4021 | mac->unique); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4022 | text = nasm_strcat(tmpbuf, t->text + 2); |
| 4023 | break; |
| 4024 | case '-': |
| 4025 | n = atoi(t->text + 2) - 1; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4026 | if (n >= mac->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4027 | tt = NULL; |
| 4028 | else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4029 | if (mac->nparam > 1) |
| 4030 | n = (n + mac->rotate) % mac->nparam; |
| 4031 | tt = mac->params[n]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4032 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4033 | cc = find_cc(tt); |
| 4034 | if (cc == -1) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4035 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4036 | "macro parameter %d is not a condition code", |
| 4037 | n + 1); |
| 4038 | text = NULL; |
| 4039 | } else { |
| 4040 | type = TOK_ID; |
| 4041 | if (inverse_ccs[cc] == -1) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4042 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4043 | "condition code `%s' is not invertible", |
| 4044 | conditions[cc]); |
| 4045 | text = NULL; |
| 4046 | } else |
| 4047 | text = nasm_strdup(conditions[inverse_ccs[cc]]); |
| 4048 | } |
| 4049 | break; |
| 4050 | case '+': |
| 4051 | n = atoi(t->text + 2) - 1; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4052 | if (n >= mac->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4053 | tt = NULL; |
| 4054 | else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4055 | if (mac->nparam > 1) |
| 4056 | n = (n + mac->rotate) % mac->nparam; |
| 4057 | tt = mac->params[n]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4058 | } |
| 4059 | cc = find_cc(tt); |
| 4060 | if (cc == -1) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4061 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4062 | "macro parameter %d is not a condition code", |
| 4063 | n + 1); |
| 4064 | text = NULL; |
| 4065 | } else { |
| 4066 | type = TOK_ID; |
| 4067 | text = nasm_strdup(conditions[cc]); |
| 4068 | } |
| 4069 | break; |
| 4070 | default: |
| 4071 | n = atoi(t->text + 1) - 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 | if (tt) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4080 | for (i = 0; i < mac->paramlen[n]; i++) { |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4081 | *tail = new_Token(NULL, tt->type, tt->text, 0); |
| 4082 | tail = &(*tail)->next; |
| 4083 | tt = tt->next; |
| 4084 | } |
| 4085 | } |
| 4086 | text = NULL; /* we've done it here */ |
| 4087 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4088 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4089 | } else { |
| 4090 | /* |
| 4091 | * seems we have a parameters range here |
| 4092 | */ |
| 4093 | Token *head, **last; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4094 | head = expand_mmac_params_range(mac, t, &last); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4095 | if (head != t) { |
| 4096 | *tail = head; |
| 4097 | *last = tline; |
| 4098 | tline = head; |
| 4099 | text = NULL; |
| 4100 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4101 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4102 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4103 | if (!text) { |
| 4104 | delete_Token(t); |
| 4105 | } else { |
| 4106 | *tail = t; |
| 4107 | tail = &t->next; |
| 4108 | t->type = type; |
| 4109 | nasm_free(t->text); |
| 4110 | t->text = text; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4111 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4112 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4113 | changed = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4114 | continue; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4115 | } else if (tline->type == TOK_INDIRECT) { |
| 4116 | t = tline; |
| 4117 | tline = tline->next; |
| 4118 | tt = tokenize(t->text); |
| 4119 | tt = expand_mmac_params(tt); |
| 4120 | tt = expand_smacro(tt); |
| 4121 | *tail = tt; |
| 4122 | while (tt) { |
| 4123 | tt->a.mac = NULL; /* Necessary? */ |
| 4124 | tail = &tt->next; |
| 4125 | tt = tt->next; |
| 4126 | } |
| 4127 | delete_Token(t); |
| 4128 | changed = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4129 | } else { |
| 4130 | t = *tail = tline; |
| 4131 | tline = tline->next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4132 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4133 | tail = &t->next; |
| 4134 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4135 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4136 | *tail = NULL; |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 4137 | |
Cyrill Gorcunov | c6a742c | 2011-06-27 01:23:09 +0400 | [diff] [blame] | 4138 | if (changed) { |
| 4139 | const struct tokseq_match t[] = { |
| 4140 | { |
| 4141 | PP_CONCAT_MASK(TOK_ID) | |
| 4142 | PP_CONCAT_MASK(TOK_FLOAT), /* head */ |
| 4143 | PP_CONCAT_MASK(TOK_ID) | |
| 4144 | PP_CONCAT_MASK(TOK_NUMBER) | |
| 4145 | PP_CONCAT_MASK(TOK_FLOAT) | |
| 4146 | PP_CONCAT_MASK(TOK_OTHER) /* tail */ |
| 4147 | }, |
| 4148 | { |
| 4149 | PP_CONCAT_MASK(TOK_NUMBER), /* head */ |
| 4150 | PP_CONCAT_MASK(TOK_NUMBER) /* tail */ |
| 4151 | } |
| 4152 | }; |
| 4153 | paste_tokens(&thead, t, ARRAY_SIZE(t), false); |
| 4154 | } |
H. Peter Anvin | 6125b62 | 2009-04-08 14:02:25 -0700 | [diff] [blame] | 4155 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4156 | return thead; |
| 4157 | } |
| 4158 | |
| 4159 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4160 | * Expand all single-line macro calls made in the given line. |
| 4161 | * Return the expanded version of the line. The original is deemed |
| 4162 | * to be destroyed in the process. (In reality we'll just move |
| 4163 | * Tokens from input to output a lot of the time, rather than |
| 4164 | * actually bothering to destroy and replicate.) |
| 4165 | */ |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4166 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4167 | static Token *expand_smacro(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4168 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4169 | Token *t, *tt, *mstart, **tail, *thead; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4170 | SMacro *head = NULL, *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4171 | Token **params; |
| 4172 | int *paramsize; |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 4173 | unsigned int nparam, sparam; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 4174 | int brackets; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4175 | Token *org_tline = tline; |
| 4176 | Context *ctx; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 4177 | const char *mname; |
H. Peter Anvin | 2a15e69 | 2007-11-19 13:14:59 -0800 | [diff] [blame] | 4178 | int deadman = DEADMAN_LIMIT; |
H. Peter Anvin | 8287daf | 2009-07-07 16:00:58 -0700 | [diff] [blame] | 4179 | bool expanded; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4180 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4181 | /* |
| 4182 | * Trick: we should avoid changing the start token pointer since it can |
| 4183 | * be contained in "next" field of other token. Because of this |
| 4184 | * we allocate a copy of first token and work with it; at the end of |
| 4185 | * routine we copy it back |
| 4186 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4187 | if (org_tline) { |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4188 | tline = new_Token(org_tline->next, org_tline->type, |
| 4189 | org_tline->text, 0); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4190 | tline->a.mac = org_tline->a.mac; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4191 | nasm_free(org_tline->text); |
| 4192 | org_tline->text = NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4193 | } |
| 4194 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4195 | expanded = true; /* Always expand %+ at least once */ |
H. Peter Anvin | 8287daf | 2009-07-07 16:00:58 -0700 | [diff] [blame] | 4196 | |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4197 | again: |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4198 | thead = NULL; |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4199 | tail = &thead; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4200 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4201 | while (tline) { /* main token loop */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4202 | if (!--deadman) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4203 | nasm_error(ERR_NONFATAL, "interminable macro recursion"); |
Cyrill Gorcunov | bd38c8f | 2009-11-21 11:11:23 +0300 | [diff] [blame] | 4204 | goto err; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4205 | } |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4206 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4207 | if ((mname = tline->text)) { |
| 4208 | /* if this token is a local macro, look in local context */ |
Cyrill Gorcunov | c56d9ad | 2010-02-11 15:12:19 +0300 | [diff] [blame] | 4209 | if (tline->type == TOK_ID) { |
| 4210 | head = (SMacro *)hash_findix(&smacros, mname); |
| 4211 | } else if (tline->type == TOK_PREPROC_ID) { |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 4212 | ctx = get_ctx(mname, &mname); |
Cyrill Gorcunov | c56d9ad | 2010-02-11 15:12:19 +0300 | [diff] [blame] | 4213 | head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL; |
| 4214 | } else |
| 4215 | head = NULL; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 4216 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4217 | /* |
| 4218 | * We've hit an identifier. As in is_mmacro below, we first |
| 4219 | * check whether the identifier is a single-line macro at |
| 4220 | * all, then think about checking for parameters if |
| 4221 | * necessary. |
| 4222 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4223 | list_for_each(m, head) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4224 | if (!mstrcmp(m->name, mname, m->casesense)) |
| 4225 | break; |
| 4226 | if (m) { |
| 4227 | mstart = tline; |
| 4228 | params = NULL; |
| 4229 | paramsize = NULL; |
| 4230 | if (m->nparam == 0) { |
| 4231 | /* |
| 4232 | * Simple case: the macro is parameterless. Discard the |
| 4233 | * one token that the macro call took, and push the |
| 4234 | * expansion back on the to-do stack. |
| 4235 | */ |
| 4236 | if (!m->expansion) { |
| 4237 | if (!strcmp("__FILE__", m->name)) { |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 4238 | const char *file = src_get_fname(); |
| 4239 | /* nasm_free(tline->text); here? */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4240 | tline->text = nasm_quote(file, strlen(file)); |
| 4241 | tline->type = TOK_STRING; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4242 | continue; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4243 | } |
| 4244 | if (!strcmp("__LINE__", m->name)) { |
| 4245 | nasm_free(tline->text); |
| 4246 | make_tok_num(tline, src_get_linnum()); |
| 4247 | continue; |
| 4248 | } |
| 4249 | if (!strcmp("__BITS__", m->name)) { |
| 4250 | nasm_free(tline->text); |
| 4251 | make_tok_num(tline, globalbits); |
| 4252 | continue; |
| 4253 | } |
| 4254 | tline = delete_Token(tline); |
| 4255 | continue; |
| 4256 | } |
| 4257 | } else { |
| 4258 | /* |
| 4259 | * Complicated case: at least one macro with this name |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4260 | * exists and takes parameters. We must find the |
| 4261 | * parameters in the call, count them, find the SMacro |
| 4262 | * that corresponds to that form of the macro call, and |
| 4263 | * substitute for the parameters when we expand. What a |
| 4264 | * pain. |
| 4265 | */ |
| 4266 | /*tline = tline->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4267 | skip_white_(tline); */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4268 | do { |
| 4269 | t = tline->next; |
| 4270 | while (tok_type_(t, TOK_SMAC_END)) { |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4271 | t->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4272 | t->text = NULL; |
| 4273 | t = tline->next = delete_Token(t); |
| 4274 | } |
| 4275 | tline = t; |
| 4276 | } while (tok_type_(tline, TOK_WHITESPACE)); |
| 4277 | if (!tok_is_(tline, "(")) { |
| 4278 | /* |
| 4279 | * This macro wasn't called with parameters: ignore |
| 4280 | * the call. (Behaviour borrowed from gnu cpp.) |
| 4281 | */ |
| 4282 | tline = mstart; |
| 4283 | m = NULL; |
| 4284 | } else { |
| 4285 | int paren = 0; |
| 4286 | int white = 0; |
| 4287 | brackets = 0; |
| 4288 | nparam = 0; |
| 4289 | sparam = PARAM_DELTA; |
| 4290 | params = nasm_malloc(sparam * sizeof(Token *)); |
| 4291 | params[0] = tline->next; |
| 4292 | paramsize = nasm_malloc(sparam * sizeof(int)); |
| 4293 | paramsize[0] = 0; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4294 | while (true) { /* parameter loop */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4295 | /* |
| 4296 | * For some unusual expansions |
| 4297 | * which concatenates function call |
| 4298 | */ |
| 4299 | t = tline->next; |
| 4300 | while (tok_type_(t, TOK_SMAC_END)) { |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4301 | t->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4302 | t->text = NULL; |
| 4303 | t = tline->next = delete_Token(t); |
| 4304 | } |
| 4305 | tline = t; |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 4306 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4307 | if (!tline) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4308 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4309 | "macro call expects terminating `)'"); |
| 4310 | break; |
| 4311 | } |
| 4312 | if (tline->type == TOK_WHITESPACE |
| 4313 | && brackets <= 0) { |
| 4314 | if (paramsize[nparam]) |
| 4315 | white++; |
| 4316 | else |
| 4317 | params[nparam] = tline->next; |
| 4318 | continue; /* parameter loop */ |
| 4319 | } |
| 4320 | if (tline->type == TOK_OTHER |
| 4321 | && tline->text[1] == 0) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4322 | char ch = tline->text[0]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4323 | if (ch == ',' && !paren && brackets <= 0) { |
| 4324 | if (++nparam >= sparam) { |
| 4325 | sparam += PARAM_DELTA; |
| 4326 | params = nasm_realloc(params, |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4327 | sparam * sizeof(Token *)); |
| 4328 | paramsize = nasm_realloc(paramsize, |
| 4329 | sparam * sizeof(int)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4330 | } |
| 4331 | params[nparam] = tline->next; |
| 4332 | paramsize[nparam] = 0; |
| 4333 | white = 0; |
| 4334 | continue; /* parameter loop */ |
| 4335 | } |
| 4336 | if (ch == '{' && |
| 4337 | (brackets > 0 || (brackets == 0 && |
| 4338 | !paramsize[nparam]))) |
| 4339 | { |
| 4340 | if (!(brackets++)) { |
| 4341 | params[nparam] = tline->next; |
| 4342 | continue; /* parameter loop */ |
| 4343 | } |
| 4344 | } |
| 4345 | if (ch == '}' && brackets > 0) |
| 4346 | if (--brackets == 0) { |
| 4347 | brackets = -1; |
| 4348 | continue; /* parameter loop */ |
| 4349 | } |
| 4350 | if (ch == '(' && !brackets) |
| 4351 | paren++; |
| 4352 | if (ch == ')' && brackets <= 0) |
| 4353 | if (--paren < 0) |
| 4354 | break; |
| 4355 | } |
| 4356 | if (brackets < 0) { |
| 4357 | brackets = 0; |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4358 | nasm_error(ERR_NONFATAL, "braces do not " |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4359 | "enclose all of macro parameter"); |
| 4360 | } |
| 4361 | paramsize[nparam] += white + 1; |
| 4362 | white = 0; |
| 4363 | } /* parameter loop */ |
| 4364 | nparam++; |
| 4365 | while (m && (m->nparam != nparam || |
| 4366 | mstrcmp(m->name, mname, |
| 4367 | m->casesense))) |
| 4368 | m = m->next; |
| 4369 | if (!m) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4370 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4371 | "macro `%s' exists, " |
| 4372 | "but not taking %d parameters", |
| 4373 | mstart->text, nparam); |
| 4374 | } |
| 4375 | } |
| 4376 | if (m && m->in_progress) |
| 4377 | m = NULL; |
| 4378 | if (!m) { /* in progess or didn't find '(' or wrong nparam */ |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 4379 | /* |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4380 | * Design question: should we handle !tline, which |
| 4381 | * indicates missing ')' here, or expand those |
| 4382 | * macros anyway, which requires the (t) test a few |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 4383 | * lines down? |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4384 | */ |
| 4385 | nasm_free(params); |
| 4386 | nasm_free(paramsize); |
| 4387 | tline = mstart; |
| 4388 | } else { |
| 4389 | /* |
| 4390 | * Expand the macro: we are placed on the last token of the |
| 4391 | * call, so that we can easily split the call from the |
| 4392 | * following tokens. We also start by pushing an SMAC_END |
| 4393 | * token for the cycle removal. |
| 4394 | */ |
| 4395 | t = tline; |
| 4396 | if (t) { |
| 4397 | tline = t->next; |
| 4398 | t->next = NULL; |
| 4399 | } |
| 4400 | tt = new_Token(tline, TOK_SMAC_END, NULL, 0); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4401 | tt->a.mac = m; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4402 | m->in_progress = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4403 | tline = tt; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 4404 | list_for_each(t, m->expansion) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4405 | if (t->type >= TOK_SMAC_PARAM) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4406 | Token *pcopy = tline, **ptail = &pcopy; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4407 | Token *ttt, *pt; |
| 4408 | int i; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4409 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4410 | ttt = params[t->type - TOK_SMAC_PARAM]; |
| 4411 | i = paramsize[t->type - TOK_SMAC_PARAM]; |
| 4412 | while (--i >= 0) { |
| 4413 | pt = *ptail = new_Token(tline, ttt->type, |
| 4414 | ttt->text, 0); |
| 4415 | ptail = &pt->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4416 | ttt = ttt->next; |
| 4417 | } |
| 4418 | tline = pcopy; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4419 | } else if (t->type == TOK_PREPROC_Q) { |
| 4420 | tt = new_Token(tline, TOK_ID, mname, 0); |
| 4421 | tline = tt; |
| 4422 | } else if (t->type == TOK_PREPROC_QQ) { |
| 4423 | tt = new_Token(tline, TOK_ID, m->name, 0); |
| 4424 | tline = tt; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4425 | } else { |
| 4426 | tt = new_Token(tline, t->type, t->text, 0); |
| 4427 | tline = tt; |
| 4428 | } |
| 4429 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4430 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4431 | /* |
| 4432 | * Having done that, get rid of the macro call, and clean |
| 4433 | * up the parameters. |
| 4434 | */ |
| 4435 | nasm_free(params); |
| 4436 | nasm_free(paramsize); |
| 4437 | free_tlist(mstart); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4438 | expanded = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4439 | continue; /* main token loop */ |
| 4440 | } |
| 4441 | } |
| 4442 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4443 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4444 | if (tline->type == TOK_SMAC_END) { |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4445 | tline->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4446 | tline = delete_Token(tline); |
| 4447 | } else { |
| 4448 | t = *tail = tline; |
| 4449 | tline = tline->next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4450 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4451 | t->next = NULL; |
| 4452 | tail = &t->next; |
| 4453 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4454 | } |
| 4455 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4456 | /* |
| 4457 | * 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] | 4458 | * after expansion (they can't be produced by tokenize()). The successive |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4459 | * TOK_IDs should be concatenated. |
| 4460 | * Also we look for %+ tokens and concatenate the tokens before and after |
| 4461 | * them (without white spaces in between). |
| 4462 | */ |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4463 | if (expanded) { |
Cyrill Gorcunov | c6a742c | 2011-06-27 01:23:09 +0400 | [diff] [blame] | 4464 | const struct tokseq_match t[] = { |
| 4465 | { |
| 4466 | PP_CONCAT_MASK(TOK_ID) | |
| 4467 | PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */ |
| 4468 | PP_CONCAT_MASK(TOK_ID) | |
| 4469 | PP_CONCAT_MASK(TOK_PREPROC_ID) | |
| 4470 | PP_CONCAT_MASK(TOK_NUMBER) /* tail */ |
| 4471 | } |
| 4472 | }; |
| 4473 | if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) { |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4474 | /* |
| 4475 | * If we concatenated something, *and* we had previously expanded |
| 4476 | * an actual macro, scan the lines again for macros... |
| 4477 | */ |
| 4478 | tline = thead; |
| 4479 | expanded = false; |
| 4480 | goto again; |
| 4481 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4482 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4483 | |
Cyrill Gorcunov | bd38c8f | 2009-11-21 11:11:23 +0300 | [diff] [blame] | 4484 | err: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4485 | if (org_tline) { |
| 4486 | if (thead) { |
| 4487 | *org_tline = *thead; |
| 4488 | /* since we just gave text to org_line, don't free it */ |
| 4489 | thead->text = NULL; |
| 4490 | delete_Token(thead); |
| 4491 | } else { |
| 4492 | /* the expression expanded to empty line; |
| 4493 | we can't return NULL for some reasons |
| 4494 | we just set the line to a single WHITESPACE token. */ |
| 4495 | memset(org_tline, 0, sizeof(*org_tline)); |
| 4496 | org_tline->text = NULL; |
| 4497 | org_tline->type = TOK_WHITESPACE; |
| 4498 | } |
| 4499 | thead = org_tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4500 | } |
| 4501 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4502 | return thead; |
| 4503 | } |
| 4504 | |
| 4505 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4506 | * Similar to expand_smacro but used exclusively with macro identifiers |
| 4507 | * right before they are fetched in. The reason is that there can be |
| 4508 | * identifiers consisting of several subparts. We consider that if there |
| 4509 | * are more than one element forming the name, user wants a expansion, |
| 4510 | * otherwise it will be left as-is. Example: |
| 4511 | * |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4512 | * %define %$abc cde |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4513 | * |
| 4514 | * the identifier %$abc will be left as-is so that the handler for %define |
| 4515 | * will suck it and define the corresponding value. Other case: |
| 4516 | * |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4517 | * %define _%$abc cde |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4518 | * |
| 4519 | * In this case user wants name to be expanded *before* %define starts |
| 4520 | * working, so we'll expand %$abc into something (if it has a value; |
| 4521 | * otherwise it will be left as-is) then concatenate all successive |
| 4522 | * PP_IDs into one. |
| 4523 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4524 | static Token *expand_id(Token * tline) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4525 | { |
| 4526 | Token *cur, *oldnext = NULL; |
| 4527 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4528 | if (!tline || !tline->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4529 | return tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4530 | |
| 4531 | cur = tline; |
| 4532 | while (cur->next && |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4533 | (cur->next->type == TOK_ID || |
| 4534 | cur->next->type == TOK_PREPROC_ID |
| 4535 | || cur->next->type == TOK_NUMBER)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4536 | cur = cur->next; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4537 | |
| 4538 | /* If identifier consists of just one token, don't expand */ |
| 4539 | if (cur == tline) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4540 | return tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4541 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4542 | if (cur) { |
| 4543 | oldnext = cur->next; /* Detach the tail past identifier */ |
| 4544 | cur->next = NULL; /* so that expand_smacro stops here */ |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4545 | } |
| 4546 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4547 | tline = expand_smacro(tline); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4548 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4549 | if (cur) { |
| 4550 | /* expand_smacro possibly changhed tline; re-scan for EOL */ |
| 4551 | cur = tline; |
| 4552 | while (cur && cur->next) |
| 4553 | cur = cur->next; |
| 4554 | if (cur) |
| 4555 | cur->next = oldnext; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4556 | } |
| 4557 | |
| 4558 | return tline; |
| 4559 | } |
| 4560 | |
| 4561 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4562 | * Determine whether the given line constitutes a multi-line macro |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4563 | * 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] | 4564 | * to check for an initial label - that's taken care of in |
| 4565 | * expand_mmacro - but must check numbers of parameters. Guaranteed |
| 4566 | * to be called with tline->type == TOK_ID, so the putative macro |
| 4567 | * name is easy to find. |
| 4568 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4569 | static MMacro *is_mmacro(Token * tline, Token *** params_array) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4570 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4571 | MMacro *head, *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4572 | Token **params; |
| 4573 | int nparam; |
| 4574 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4575 | head = (MMacro *) hash_findix(&mmacros, tline->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4576 | |
| 4577 | /* |
| 4578 | * Efficiency: first we see if any macro exists with the given |
| 4579 | * name. If not, we can return NULL immediately. _Then_ we |
| 4580 | * count the parameters, and then we look further along the |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4581 | * list if necessary to find the proper MMacro. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4582 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4583 | list_for_each(m, head) |
| 4584 | if (!mstrcmp(m->name, tline->text, m->casesense)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4585 | break; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4586 | if (!m) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4587 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4588 | |
| 4589 | /* |
| 4590 | * OK, we have a potential macro. Count and demarcate the |
| 4591 | * parameters. |
| 4592 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4593 | count_mmac_params(tline->next, &nparam, ¶ms); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4594 | |
| 4595 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4596 | * 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] | 4597 | * structure that handles this number. |
| 4598 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4599 | while (m) { |
| 4600 | if (m->nparam_min <= nparam |
| 4601 | && (m->plus || nparam <= m->nparam_max)) { |
| 4602 | /* |
| 4603 | * This one is right. Just check if cycle removal |
| 4604 | * prohibits us using it before we actually celebrate... |
| 4605 | */ |
| 4606 | if (m->in_progress > m->max_depth) { |
| 4607 | if (m->max_depth > 0) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4608 | nasm_error(ERR_WARNING, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4609 | "reached maximum recursion depth of %i", |
| 4610 | m->max_depth); |
| 4611 | } |
| 4612 | nasm_free(params); |
| 4613 | return NULL; |
| 4614 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4615 | /* |
| 4616 | * It's right, and we can use it. Add its default |
| 4617 | * parameters to the end of our list if necessary. |
| 4618 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4619 | if (m->defaults && nparam < m->nparam_min + m->ndefs) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4620 | params = |
| 4621 | nasm_realloc(params, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4622 | ((m->nparam_min + m->ndefs + |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4623 | 1) * sizeof(*params))); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4624 | while (nparam < m->nparam_min + m->ndefs) { |
| 4625 | params[nparam] = m->defaults[nparam - m->nparam_min]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4626 | nparam++; |
| 4627 | } |
| 4628 | } |
| 4629 | /* |
| 4630 | * If we've gone over the maximum parameter count (and |
| 4631 | * we're in Plus mode), ignore parameters beyond |
| 4632 | * nparam_max. |
| 4633 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4634 | if (m->plus && nparam > m->nparam_max) |
| 4635 | nparam = m->nparam_max; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4636 | /* |
| 4637 | * Then terminate the parameter list, and leave. |
| 4638 | */ |
| 4639 | if (!params) { /* need this special case */ |
| 4640 | params = nasm_malloc(sizeof(*params)); |
| 4641 | nparam = 0; |
| 4642 | } |
| 4643 | params[nparam] = NULL; |
| 4644 | *params_array = params; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4645 | return m; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4646 | } |
| 4647 | /* |
| 4648 | * This one wasn't right: look for the next one with the |
| 4649 | * same name. |
| 4650 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4651 | list_for_each(m, m->next) |
| 4652 | if (!mstrcmp(m->name, tline->text, m->casesense)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4653 | break; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4654 | } |
| 4655 | |
| 4656 | /* |
| 4657 | * After all that, we didn't find one with the right number of |
| 4658 | * parameters. Issue a warning, and fail to expand the macro. |
| 4659 | */ |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4660 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4661 | "macro `%s' exists, but not taking %d parameters", |
| 4662 | tline->text, nparam); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4663 | nasm_free(params); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4664 | return NULL; |
| 4665 | } |
| 4666 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4667 | |
| 4668 | /* |
| 4669 | * Save MMacro invocation specific fields in |
| 4670 | * preparation for a recursive macro expansion |
| 4671 | */ |
| 4672 | static void push_mmacro(MMacro *m) |
| 4673 | { |
| 4674 | MMacroInvocation *i; |
| 4675 | |
| 4676 | i = nasm_malloc(sizeof(MMacroInvocation)); |
| 4677 | i->prev = m->prev; |
| 4678 | i->params = m->params; |
| 4679 | i->iline = m->iline; |
| 4680 | i->nparam = m->nparam; |
| 4681 | i->rotate = m->rotate; |
| 4682 | i->paramlen = m->paramlen; |
| 4683 | i->unique = m->unique; |
| 4684 | i->condcnt = m->condcnt; |
| 4685 | m->prev = i; |
| 4686 | } |
| 4687 | |
| 4688 | |
| 4689 | /* |
| 4690 | * Restore MMacro invocation specific fields that were |
| 4691 | * saved during a previous recursive macro expansion |
| 4692 | */ |
| 4693 | static void pop_mmacro(MMacro *m) |
| 4694 | { |
| 4695 | MMacroInvocation *i; |
| 4696 | |
| 4697 | if (m->prev) { |
| 4698 | i = m->prev; |
| 4699 | m->prev = i->prev; |
| 4700 | m->params = i->params; |
| 4701 | m->iline = i->iline; |
| 4702 | m->nparam = i->nparam; |
| 4703 | m->rotate = i->rotate; |
| 4704 | m->paramlen = i->paramlen; |
| 4705 | m->unique = i->unique; |
| 4706 | m->condcnt = i->condcnt; |
| 4707 | nasm_free(i); |
| 4708 | } |
| 4709 | } |
| 4710 | |
| 4711 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4712 | /* |
| 4713 | * Expand the multi-line macro call made by the given line, if |
| 4714 | * 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] | 4715 | * istk->expansion and return 1. Otherwise return 0. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4716 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4717 | static int expand_mmacro(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4718 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4719 | Token *startline = tline; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4720 | Token *label = NULL; |
| 4721 | int dont_prepend = 0; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4722 | Token **params, *t, *tt; |
| 4723 | MMacro *m; |
| 4724 | Line *l, *ll; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4725 | int i, nparam, *paramlen; |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 4726 | const char *mname; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4727 | |
| 4728 | t = tline; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4729 | skip_white_(t); |
H. Peter Anvin | ce2233b | 2008-05-25 21:57:00 -0700 | [diff] [blame] | 4730 | /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */ |
H. Peter Anvin | dce1e2f | 2002-04-30 21:06:37 +0000 | [diff] [blame] | 4731 | 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] | 4732 | return 0; |
| 4733 | m = is_mmacro(t, ¶ms); |
| 4734 | if (m) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4735 | mname = t->text; |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 4736 | } else { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4737 | Token *last; |
| 4738 | /* |
| 4739 | * We have an id which isn't a macro call. We'll assume |
| 4740 | * it might be a label; we'll also check to see if a |
| 4741 | * colon follows it. Then, if there's another id after |
| 4742 | * that lot, we'll check it again for macro-hood. |
| 4743 | */ |
| 4744 | label = last = t; |
| 4745 | t = t->next; |
| 4746 | if (tok_type_(t, TOK_WHITESPACE)) |
| 4747 | last = t, t = t->next; |
| 4748 | if (tok_is_(t, ":")) { |
| 4749 | dont_prepend = 1; |
| 4750 | last = t, t = t->next; |
| 4751 | if (tok_type_(t, TOK_WHITESPACE)) |
| 4752 | last = t, t = t->next; |
| 4753 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4754 | if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, ¶ms))) |
| 4755 | return 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4756 | last->next = NULL; |
Keith Kanios | 891775e | 2009-07-11 06:08:54 -0500 | [diff] [blame] | 4757 | mname = t->text; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4758 | tline = t; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4759 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4760 | |
| 4761 | /* |
| 4762 | * Fix up the parameters: this involves stripping leading and |
| 4763 | * trailing whitespace, then stripping braces if they are |
| 4764 | * present. |
| 4765 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4766 | for (nparam = 0; params[nparam]; nparam++) ; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4767 | paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4768 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4769 | for (i = 0; params[i]; i++) { |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4770 | int brace = 0; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4771 | int comma = (!m->plus || i < nparam - 1); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4772 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4773 | t = params[i]; |
| 4774 | skip_white_(t); |
| 4775 | if (tok_is_(t, "{")) |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4776 | t = t->next, brace++, comma = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4777 | params[i] = t; |
| 4778 | paramlen[i] = 0; |
| 4779 | while (t) { |
| 4780 | if (comma && t->type == TOK_OTHER && !strcmp(t->text, ",")) |
| 4781 | break; /* ... because we have hit a comma */ |
| 4782 | if (comma && t->type == TOK_WHITESPACE |
| 4783 | && tok_is_(t->next, ",")) |
| 4784 | break; /* ... or a space then a comma */ |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4785 | if (brace && t->type == TOK_OTHER) { |
| 4786 | if (t->text[0] == '{') |
| 4787 | brace++; /* ... or a nested opening brace */ |
| 4788 | else if (t->text[0] == '}') |
| 4789 | if (!--brace) |
| 4790 | break; /* ... or a brace */ |
| 4791 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4792 | t = t->next; |
| 4793 | paramlen[i]++; |
| 4794 | } |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4795 | if (brace) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4796 | nasm_error(ERR_NONFATAL, "macro params should be enclosed in braces"); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4797 | } |
| 4798 | |
| 4799 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4800 | * OK, we have a MMacro structure together with a set of |
| 4801 | * parameters. We must now go through the expansion and push |
| 4802 | * copies of each Line on to istk->expansion. Substitution of |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4803 | * parameter tokens and macro-local tokens doesn't get done |
| 4804 | * until the single-line macro substitution process; this is |
| 4805 | * because delaying them allows us to change the semantics |
| 4806 | * later through %rotate. |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4807 | * |
| 4808 | * First, push an end marker on to istk->expansion, mark this |
| 4809 | * macro as in progress, and set up its invocation-specific |
| 4810 | * variables. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4811 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4812 | ll = nasm_malloc(sizeof(Line)); |
| 4813 | ll->next = istk->expansion; |
| 4814 | ll->finishes = m; |
| 4815 | ll->first = NULL; |
| 4816 | istk->expansion = ll; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4817 | |
| 4818 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4819 | * Save the previous MMacro expansion in the case of |
| 4820 | * macro recursion |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4821 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4822 | if (m->max_depth && m->in_progress) |
| 4823 | push_mmacro(m); |
| 4824 | |
| 4825 | m->in_progress ++; |
| 4826 | m->params = params; |
| 4827 | m->iline = tline; |
| 4828 | m->nparam = nparam; |
| 4829 | m->rotate = 0; |
| 4830 | m->paramlen = paramlen; |
| 4831 | m->unique = unique++; |
| 4832 | m->lineno = 0; |
| 4833 | m->condcnt = 0; |
| 4834 | |
| 4835 | m->next_active = istk->mstk; |
| 4836 | istk->mstk = m; |
| 4837 | |
| 4838 | list_for_each(l, m->expansion) { |
| 4839 | Token **tail; |
| 4840 | |
| 4841 | ll = nasm_malloc(sizeof(Line)); |
| 4842 | ll->finishes = NULL; |
| 4843 | ll->next = istk->expansion; |
| 4844 | istk->expansion = ll; |
| 4845 | tail = &ll->first; |
| 4846 | |
| 4847 | list_for_each(t, l->first) { |
| 4848 | Token *x = t; |
| 4849 | switch (t->type) { |
| 4850 | case TOK_PREPROC_Q: |
| 4851 | tt = *tail = new_Token(NULL, TOK_ID, mname, 0); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4852 | break; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4853 | case TOK_PREPROC_QQ: |
| 4854 | tt = *tail = new_Token(NULL, TOK_ID, m->name, 0); |
| 4855 | break; |
| 4856 | case TOK_PREPROC_ID: |
| 4857 | if (t->text[1] == '0' && t->text[2] == '0') { |
| 4858 | dont_prepend = -1; |
| 4859 | x = label; |
| 4860 | if (!x) |
| 4861 | continue; |
| 4862 | } |
| 4863 | /* fall through */ |
| 4864 | default: |
| 4865 | tt = *tail = new_Token(NULL, x->type, x->text, 0); |
| 4866 | break; |
| 4867 | } |
| 4868 | tail = &tt->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4869 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4870 | *tail = NULL; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4871 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4872 | |
| 4873 | /* |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4874 | * If we had a label, push it on as the first line of |
| 4875 | * the macro expansion. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4876 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4877 | if (label) { |
| 4878 | if (dont_prepend < 0) |
| 4879 | free_tlist(startline); |
| 4880 | else { |
| 4881 | ll = nasm_malloc(sizeof(Line)); |
| 4882 | ll->finishes = NULL; |
| 4883 | ll->next = istk->expansion; |
| 4884 | istk->expansion = ll; |
| 4885 | ll->first = startline; |
| 4886 | if (!dont_prepend) { |
| 4887 | while (label->next) |
| 4888 | label = label->next; |
| 4889 | label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4890 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4891 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4892 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4893 | |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 4894 | lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4895 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4896 | return 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4897 | } |
| 4898 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4899 | /* |
| 4900 | * This function adds macro names to error messages, and suppresses |
| 4901 | * them if necessary. |
| 4902 | */ |
| 4903 | 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] | 4904 | { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4905 | char buff[BUFSIZ]; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4906 | MMacro *mmac = NULL; |
| 4907 | int delta = 0; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4908 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4909 | /* |
| 4910 | * If we're in a dead branch of IF or something like it, ignore the error. |
| 4911 | * However, because %else etc are evaluated in the state context |
| 4912 | * of the previous branch, errors might get lost: |
| 4913 | * %if 0 ... %else trailing garbage ... %endif |
| 4914 | * So %else etc should set the ERR_PP_PRECOND flag. |
| 4915 | */ |
| 4916 | if ((severity & ERR_MASK) < ERR_FATAL && |
| 4917 | istk && istk->conds && |
| 4918 | ((severity & ERR_PP_PRECOND) ? |
| 4919 | istk->conds->state == COND_NEVER : |
H. Peter Anvin | eb6653f | 2016-04-05 13:03:10 -0700 | [diff] [blame] | 4920 | !emitting(istk->conds->state))) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4921 | return; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4922 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4923 | /* get %macro name */ |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4924 | if (!(severity & ERR_NOFILE) && istk && istk->mstk) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4925 | mmac = istk->mstk; |
| 4926 | /* but %rep blocks should be skipped */ |
| 4927 | while (mmac && !mmac->name) |
| 4928 | mmac = mmac->next_active, delta++; |
Cyrill Gorcunov | 9900c6b | 2011-10-09 18:58:46 +0400 | [diff] [blame] | 4929 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4930 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4931 | if (mmac) { |
| 4932 | vsnprintf(buff, sizeof(buff), fmt, arg); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4933 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4934 | nasm_set_verror(real_verror); |
| 4935 | nasm_error(severity, "(%s:%d) %s", |
| 4936 | mmac->name, mmac->lineno - delta, buff); |
| 4937 | nasm_set_verror(pp_verror); |
| 4938 | } else { |
| 4939 | real_verror(severity, fmt, arg); |
| 4940 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4941 | } |
| 4942 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4943 | static void |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4944 | pp_reset(char *file, int apass, StrList **deplist) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4945 | { |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 4946 | Token *t; |
| 4947 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4948 | cstk = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4949 | istk = nasm_malloc(sizeof(Include)); |
| 4950 | istk->next = NULL; |
| 4951 | istk->conds = NULL; |
| 4952 | istk->expansion = NULL; |
| 4953 | istk->mstk = NULL; |
H. Peter Anvin | 3e83cec | 2016-05-25 04:28:46 -0700 | [diff] [blame] | 4954 | istk->fp = nasm_open_read(file, NF_TEXT); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4955 | istk->fname = NULL; |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 4956 | src_set(0, file); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4957 | istk->lineinc = 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4958 | if (!istk->fp) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4959 | nasm_fatal(ERR_NOFILE, "unable to open input file `%s'", file); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4960 | defining = NULL; |
Charles Crayne | d4200be | 2008-07-12 16:42:33 -0700 | [diff] [blame] | 4961 | nested_mac_count = 0; |
| 4962 | nested_rep_count = 0; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 4963 | init_macros(); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4964 | unique = 0; |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 4965 | |
| 4966 | if (tasm_compatible_mode) |
| 4967 | pp_add_stdmac(nasm_stdmac_tasm); |
| 4968 | |
| 4969 | pp_add_stdmac(nasm_stdmac_nasm); |
| 4970 | pp_add_stdmac(nasm_stdmac_version); |
| 4971 | |
| 4972 | stdmacpos = stdmacros[0]; |
| 4973 | stdmacnext = &stdmacros[1]; |
| 4974 | |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 4975 | do_predef = true; |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 4976 | |
| 4977 | /* |
| 4978 | * 0 for dependencies, 1 for preparatory passes, 2 for final pass. |
| 4979 | * The caller, however, will also pass in 3 for preprocess-only so |
| 4980 | * we can set __PASS__ accordingly. |
| 4981 | */ |
| 4982 | pass = apass > 2 ? 2 : apass; |
| 4983 | |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 4984 | dephead = deplist; |
H. Peter Anvin | 436e367 | 2016-10-04 01:12:28 -0700 | [diff] [blame] | 4985 | nasm_add_string_to_strlist(dephead, file); |
H. Peter Anvin | 9924d1e | 2016-10-04 00:59:39 -0700 | [diff] [blame] | 4986 | |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 4987 | /* |
| 4988 | * Define the __PASS__ macro. This is defined here unlike |
| 4989 | * all the other builtins, because it is special -- it varies between |
| 4990 | * passes. |
| 4991 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4992 | t = nasm_malloc(sizeof(*t)); |
| 4993 | t->next = NULL; |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 4994 | make_tok_num(t, apass); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4995 | t->a.mac = NULL; |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 4996 | define_smacro(NULL, "__PASS__", true, 0, t); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4997 | } |
| 4998 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 4999 | static void pp_init(void) |
| 5000 | { |
| 5001 | hash_init(&FileHash, HASH_MEDIUM); |
| 5002 | } |
| 5003 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5004 | static char *pp_getline(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5005 | { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5006 | char *line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5007 | Token *tline; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5008 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5009 | real_verror = nasm_set_verror(pp_verror); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5010 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5011 | while (1) { |
| 5012 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5013 | * Fetch a tokenized line, either from the macro-expansion |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5014 | * buffer or from the input file. |
| 5015 | */ |
| 5016 | tline = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5017 | while (istk->expansion && istk->expansion->finishes) { |
| 5018 | Line *l = istk->expansion; |
| 5019 | if (!l->finishes->name && l->finishes->in_progress > 1) { |
| 5020 | Line *ll; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5021 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5022 | /* |
| 5023 | * This is a macro-end marker for a macro with no |
| 5024 | * name, which means it's not really a macro at all |
| 5025 | * but a %rep block, and the `in_progress' field is |
| 5026 | * more than 1, meaning that we still need to |
| 5027 | * repeat. (1 means the natural last repetition; 0 |
| 5028 | * means termination by %exitrep.) We have |
| 5029 | * therefore expanded up to the %endrep, and must |
| 5030 | * push the whole block on to the expansion buffer |
| 5031 | * again. We don't bother to remove the macro-end |
| 5032 | * marker: we'd only have to generate another one |
| 5033 | * if we did. |
| 5034 | */ |
| 5035 | l->finishes->in_progress--; |
| 5036 | list_for_each(l, l->finishes->expansion) { |
| 5037 | Token *t, *tt, **tail; |
| 5038 | |
| 5039 | ll = nasm_malloc(sizeof(Line)); |
| 5040 | ll->next = istk->expansion; |
| 5041 | ll->finishes = NULL; |
| 5042 | ll->first = NULL; |
| 5043 | tail = &ll->first; |
| 5044 | |
| 5045 | list_for_each(t, l->first) { |
| 5046 | if (t->text || t->type == TOK_WHITESPACE) { |
| 5047 | tt = *tail = new_Token(NULL, t->type, t->text, 0); |
| 5048 | tail = &tt->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5049 | } |
| 5050 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5051 | |
| 5052 | istk->expansion = ll; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5053 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5054 | } else { |
| 5055 | /* |
| 5056 | * Check whether a `%rep' was started and not ended |
| 5057 | * within this macro expansion. This can happen and |
| 5058 | * should be detected. It's a fatal error because |
| 5059 | * I'm too confused to work out how to recover |
| 5060 | * sensibly from it. |
| 5061 | */ |
| 5062 | if (defining) { |
| 5063 | if (defining->name) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5064 | nasm_panic(0, "defining with name in expansion"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5065 | else if (istk->mstk->name) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5066 | nasm_fatal(0, "`%%rep' without `%%endrep' within" |
| 5067 | " expansion of macro `%s'", |
| 5068 | istk->mstk->name); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5069 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5070 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5071 | /* |
| 5072 | * FIXME: investigate the relationship at this point between |
| 5073 | * istk->mstk and l->finishes |
| 5074 | */ |
| 5075 | { |
| 5076 | MMacro *m = istk->mstk; |
| 5077 | istk->mstk = m->next_active; |
| 5078 | if (m->name) { |
| 5079 | /* |
| 5080 | * This was a real macro call, not a %rep, and |
| 5081 | * therefore the parameter information needs to |
| 5082 | * be freed. |
| 5083 | */ |
| 5084 | if (m->prev) { |
| 5085 | pop_mmacro(m); |
| 5086 | l->finishes->in_progress --; |
| 5087 | } else { |
| 5088 | nasm_free(m->params); |
| 5089 | free_tlist(m->iline); |
| 5090 | nasm_free(m->paramlen); |
| 5091 | l->finishes->in_progress = 0; |
| 5092 | } |
| 5093 | } else |
| 5094 | free_mmacro(m); |
| 5095 | } |
| 5096 | istk->expansion = l->next; |
| 5097 | nasm_free(l); |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 5098 | lfmt->downlevel(LIST_MACRO); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5099 | } |
| 5100 | } |
| 5101 | while (1) { /* until we get a line we can use */ |
| 5102 | |
| 5103 | if (istk->expansion) { /* from a macro expansion */ |
| 5104 | char *p; |
| 5105 | Line *l = istk->expansion; |
| 5106 | if (istk->mstk) |
| 5107 | istk->mstk->lineno++; |
| 5108 | tline = l->first; |
| 5109 | istk->expansion = l->next; |
| 5110 | nasm_free(l); |
| 5111 | p = detoken(tline, false); |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 5112 | lfmt->line(LIST_MACRO, p); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5113 | nasm_free(p); |
| 5114 | break; |
| 5115 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5116 | line = read_line(); |
| 5117 | if (line) { /* from the current input file */ |
| 5118 | line = prepreproc(line); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5119 | tline = tokenize(line); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5120 | nasm_free(line); |
| 5121 | break; |
| 5122 | } |
| 5123 | /* |
| 5124 | * The current file has ended; work down the istk |
| 5125 | */ |
| 5126 | { |
| 5127 | Include *i = istk; |
| 5128 | fclose(i->fp); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5129 | if (i->conds) { |
| 5130 | /* nasm_error can't be conditionally suppressed */ |
H. Peter Anvin | 4108706 | 2016-03-03 14:27:34 -0800 | [diff] [blame] | 5131 | nasm_fatal(0, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5132 | "expected `%%endif' before end of file"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5133 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5134 | /* 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] | 5135 | if (i->next) |
| 5136 | src_set(i->lineno, i->fname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5137 | istk = i->next; |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 5138 | lfmt->downlevel(LIST_INCLUDE); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5139 | nasm_free(i); |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5140 | if (!istk) { |
| 5141 | line = NULL; |
| 5142 | goto done; |
| 5143 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5144 | if (istk->expansion && istk->expansion->finishes) |
| 5145 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5146 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5147 | } |
| 5148 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5149 | /* |
| 5150 | * We must expand MMacro parameters and MMacro-local labels |
| 5151 | * _before_ we plunge into directive processing, to cope |
| 5152 | * with things like `%define something %1' such as STRUC |
| 5153 | * uses. Unless we're _defining_ a MMacro, in which case |
| 5154 | * those tokens should be left alone to go into the |
| 5155 | * definition; and unless we're in a non-emitting |
| 5156 | * condition, in which case we don't want to meddle with |
| 5157 | * anything. |
| 5158 | */ |
| 5159 | if (!defining && !(istk->conds && !emitting(istk->conds->state)) |
| 5160 | && !(istk->mstk && !istk->mstk->in_progress)) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5161 | tline = expand_mmac_params(tline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5162 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5163 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5164 | /* |
| 5165 | * Check the line to see if it's a preprocessor directive. |
| 5166 | */ |
H. Peter Anvin | bc7f4fe | 2016-10-04 14:57:17 -0700 | [diff] [blame] | 5167 | if (do_directive(tline, &line) == DIRECTIVE_FOUND) { |
| 5168 | if (line) |
| 5169 | break; /* Directive generated output */ |
| 5170 | else |
| 5171 | continue; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5172 | } else if (defining) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5173 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5174 | * We're defining a multi-line macro. We emit nothing |
| 5175 | * at all, and just |
| 5176 | * shove the tokenized line on to the macro definition. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5177 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5178 | Line *l = nasm_malloc(sizeof(Line)); |
| 5179 | l->next = defining->expansion; |
| 5180 | l->first = tline; |
| 5181 | l->finishes = NULL; |
| 5182 | defining->expansion = l; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5183 | continue; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5184 | } else if (istk->conds && !emitting(istk->conds->state)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5185 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5186 | * We're in a non-emitting branch of a condition block. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5187 | * Emit nothing at all, not even a blank line: when we |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5188 | * emerge from the condition we'll give a line-number |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5189 | * directive so we keep our place correctly. |
| 5190 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5191 | free_tlist(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5192 | continue; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5193 | } else if (istk->mstk && !istk->mstk->in_progress) { |
| 5194 | /* |
| 5195 | * We're in a %rep block which has been terminated, so |
| 5196 | * we're walking through to the %endrep without |
| 5197 | * emitting anything. Emit nothing at all, not even a |
| 5198 | * blank line: when we emerge from the %rep block we'll |
| 5199 | * give a line-number directive so we keep our place |
| 5200 | * correctly. |
| 5201 | */ |
| 5202 | free_tlist(tline); |
| 5203 | continue; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5204 | } else { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5205 | tline = expand_smacro(tline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5206 | if (!expand_mmacro(tline)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5207 | /* |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5208 | * De-tokenize the line again, and emit it. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5209 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5210 | line = detoken(tline, true); |
| 5211 | free_tlist(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5212 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5213 | } else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5214 | continue; /* expand_mmacro calls free_tlist */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5215 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5216 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5217 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5218 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5219 | done: |
| 5220 | nasm_set_verror(real_verror); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5221 | return line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5222 | } |
| 5223 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5224 | static void pp_cleanup(int pass) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5225 | { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5226 | real_verror = nasm_set_verror(pp_verror); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5227 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5228 | if (defining) { |
| 5229 | if (defining->name) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5230 | nasm_error(ERR_NONFATAL, |
| 5231 | "end of file while still defining macro `%s'", |
| 5232 | defining->name); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5233 | } else { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5234 | nasm_error(ERR_NONFATAL, "end of file while still in %%rep"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5235 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5236 | |
| 5237 | free_mmacro(defining); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5238 | defining = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5239 | } |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5240 | |
| 5241 | nasm_set_verror(real_verror); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5242 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5243 | while (cstk) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5244 | ctx_pop(); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 5245 | free_macros(); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5246 | while (istk) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5247 | Include *i = istk; |
| 5248 | istk = istk->next; |
| 5249 | fclose(i->fp); |
Cyrill Gorcunov | 8dcfd88 | 2011-03-03 09:18:56 +0300 | [diff] [blame] | 5250 | nasm_free(i); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5251 | } |
| 5252 | while (cstk) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5253 | ctx_pop(); |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 5254 | src_set_fname(NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5255 | if (pass == 0) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5256 | IncPath *i; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5257 | free_llist(predef); |
Cyrill Gorcunov | dae24d7 | 2014-06-28 10:17:39 +0400 | [diff] [blame] | 5258 | predef = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5259 | delete_Blocks(); |
Cyrill Gorcunov | dae24d7 | 2014-06-28 10:17:39 +0400 | [diff] [blame] | 5260 | freeTokens = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5261 | while ((i = ipath)) { |
| 5262 | ipath = i->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5263 | if (i->path) |
| 5264 | nasm_free(i->path); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5265 | nasm_free(i); |
| 5266 | } |
H. Peter Anvin | 11dfa1a | 2008-07-02 18:11:04 -0700 | [diff] [blame] | 5267 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5268 | } |
| 5269 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5270 | static void pp_include_path(char *path) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5271 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5272 | IncPath *i; |
H. Peter Anvin | 37a321f | 2007-09-24 13:41:58 -0700 | [diff] [blame] | 5273 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5274 | i = nasm_malloc(sizeof(IncPath)); |
| 5275 | i->path = path ? nasm_strdup(path) : NULL; |
| 5276 | i->next = NULL; |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5277 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 5278 | if (ipath) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5279 | IncPath *j = ipath; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 5280 | while (j->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5281 | j = j->next; |
| 5282 | j->next = i; |
| 5283 | } else { |
| 5284 | ipath = i; |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5285 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5286 | } |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5287 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5288 | static void pp_pre_include(char *fname) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5289 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5290 | Token *inc, *space, *name; |
| 5291 | Line *l; |
| 5292 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5293 | name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0); |
| 5294 | space = new_Token(name, TOK_WHITESPACE, NULL, 0); |
| 5295 | inc = new_Token(space, TOK_PREPROC_ID, "%include", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5296 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5297 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5298 | l->next = predef; |
| 5299 | l->first = inc; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5300 | l->finishes = NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5301 | predef = l; |
| 5302 | } |
| 5303 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5304 | static void pp_pre_define(char *definition) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5305 | { |
| 5306 | Token *def, *space; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5307 | Line *l; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5308 | char *equals; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5309 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5310 | real_verror = nasm_set_verror(pp_verror); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5311 | |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5312 | equals = strchr(definition, '='); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5313 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 5314 | def = new_Token(space, TOK_PREPROC_ID, "%define", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5315 | if (equals) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5316 | *equals = ' '; |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5317 | space->next = tokenize(definition); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5318 | if (equals) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5319 | *equals = '='; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5320 | |
Cyrill Gorcunov | 6d42e9b | 2015-02-08 11:07:17 +0300 | [diff] [blame] | 5321 | if (space->next->type != TOK_PREPROC_ID && |
| 5322 | space->next->type != TOK_ID) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5323 | nasm_error(ERR_WARNING, "pre-defining non ID `%s\'\n", definition); |
Cyrill Gorcunov | 6d42e9b | 2015-02-08 11:07:17 +0300 | [diff] [blame] | 5324 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5325 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5326 | l->next = predef; |
| 5327 | l->first = def; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5328 | l->finishes = NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5329 | predef = l; |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5330 | |
| 5331 | nasm_set_verror(real_verror); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5332 | } |
| 5333 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5334 | static void pp_pre_undefine(char *definition) |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5335 | { |
| 5336 | Token *def, *space; |
| 5337 | Line *l; |
| 5338 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5339 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 5340 | def = new_Token(space, TOK_PREPROC_ID, "%undef", 0); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5341 | space->next = tokenize(definition); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5342 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5343 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5344 | l->next = predef; |
| 5345 | l->first = def; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5346 | l->finishes = NULL; |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5347 | predef = l; |
| 5348 | } |
| 5349 | |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 5350 | static void pp_add_stdmac(macros_t *macros) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5351 | { |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 5352 | macros_t **mp; |
| 5353 | |
| 5354 | /* Find the end of the list and avoid duplicates */ |
| 5355 | for (mp = stdmacros; *mp; mp++) { |
| 5356 | if (*mp == macros) |
| 5357 | return; /* Nothing to do */ |
| 5358 | } |
| 5359 | |
| 5360 | nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]); |
| 5361 | |
| 5362 | *mp = macros; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 5363 | } |
| 5364 | |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 5365 | static void make_tok_num(Token * tok, int64_t val) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5366 | { |
Cyrill Gorcunov | ce65274 | 2013-05-06 23:43:43 +0400 | [diff] [blame] | 5367 | char numbuf[32]; |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 5368 | snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5369 | tok->text = nasm_strdup(numbuf); |
| 5370 | tok->type = TOK_NUMBER; |
| 5371 | } |
| 5372 | |
H. Peter Anvin | 3736895 | 2016-05-09 14:10:32 -0700 | [diff] [blame] | 5373 | static void pp_list_one_macro(MMacro *m, int severity) |
| 5374 | { |
| 5375 | if (!m) |
| 5376 | return; |
| 5377 | |
| 5378 | /* We need to print the next_active list in reverse order */ |
| 5379 | pp_list_one_macro(m->next_active, severity); |
| 5380 | |
| 5381 | if (m->name && !m->nolist) { |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 5382 | src_set(m->xline + m->lineno, m->fname); |
H. Peter Anvin | 3736895 | 2016-05-09 14:10:32 -0700 | [diff] [blame] | 5383 | nasm_error(severity, "... from macro `%s' defined here", m->name); |
| 5384 | } |
| 5385 | } |
| 5386 | |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5387 | static void pp_error_list_macros(int severity) |
| 5388 | { |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5389 | int32_t saved_line; |
| 5390 | const char *saved_fname = NULL; |
| 5391 | |
| 5392 | severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY; |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 5393 | src_get(&saved_line, &saved_fname); |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5394 | |
Cyrill Gorcunov | 771d04e | 2016-05-10 23:27:03 +0300 | [diff] [blame] | 5395 | if (istk) |
| 5396 | pp_list_one_macro(istk->mstk, severity); |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5397 | |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 5398 | src_set(saved_line, saved_fname); |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5399 | } |
| 5400 | |
H. Peter Anvin | e746971 | 2016-02-18 02:20:59 -0800 | [diff] [blame] | 5401 | const struct preproc_ops nasmpp = { |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 5402 | pp_init, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5403 | pp_reset, |
| 5404 | pp_getline, |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5405 | pp_cleanup, |
H. Peter Anvin | f760661 | 2016-07-13 14:23:48 -0700 | [diff] [blame] | 5406 | pp_add_stdmac, |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5407 | pp_pre_define, |
| 5408 | pp_pre_undefine, |
| 5409 | pp_pre_include, |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5410 | pp_include_path, |
| 5411 | pp_error_list_macros, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5412 | }; |