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 | 215186f | 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> |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 72 | #include <inttypes.h> |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 73 | |
| 74 | #include "nasm.h" |
| 75 | #include "nasmlib.h" |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 76 | #include "preproc.h" |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 77 | #include "hashtbl.h" |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 78 | #include "quote.h" |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 79 | #include "stdscan.h" |
H. Peter Anvin | dbb640b | 2009-07-18 18:57:16 -0700 | [diff] [blame] | 80 | #include "eval.h" |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 81 | #include "tokens.h" |
H. Peter Anvin | a4835d4 | 2008-05-20 14:21:29 -0700 | [diff] [blame] | 82 | #include "tables.h" |
H. Peter Anvin | 172b840 | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 83 | #include "listing.h" |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 84 | |
| 85 | typedef struct SMacro SMacro; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 86 | typedef struct MMacro MMacro; |
| 87 | typedef struct MMacroInvocation MMacroInvocation; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 88 | typedef struct Context Context; |
| 89 | typedef struct Token Token; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 90 | typedef struct Blocks Blocks; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 91 | typedef struct Line Line; |
| 92 | typedef struct Include Include; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 93 | typedef struct Cond Cond; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 94 | typedef struct IncPath IncPath; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 95 | |
| 96 | /* |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 97 | * Note on the storage of both SMacro and MMacros: the hash table |
| 98 | * indexes them case-insensitively, and we then have to go through a |
| 99 | * linked list of potential case aliases (and, for MMacros, parameter |
| 100 | * ranges); this is to preserve the matching semantics of the earlier |
| 101 | * code. If the number of case aliases for a specific macro is a |
| 102 | * performance issue, you may want to reconsider your coding style. |
| 103 | */ |
| 104 | |
| 105 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 106 | * Store the definition of a single-line macro. |
| 107 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 108 | struct SMacro { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 109 | SMacro *next; |
| 110 | char *name; |
| 111 | bool casesense; |
| 112 | bool in_progress; |
| 113 | unsigned int nparam; |
| 114 | Token *expansion; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 118 | * Store the definition of a multi-line macro. This is also used to |
| 119 | * store the interiors of `%rep...%endrep' blocks, which are |
| 120 | * effectively self-re-invoking multi-line macros which simply |
| 121 | * don't have a name or bother to appear in the hash tables. %rep |
| 122 | * blocks are signified by having a NULL `name' field. |
| 123 | * |
| 124 | * In a MMacro describing a `%rep' block, the `in_progress' field |
| 125 | * isn't merely boolean, but gives the number of repeats left to |
| 126 | * run. |
| 127 | * |
| 128 | * The `next' field is used for storing MMacros in hash tables; the |
| 129 | * `next_active' field is for stacking them on istk entries. |
| 130 | * |
| 131 | * When a MMacro is being expanded, `params', `iline', `nparam', |
| 132 | * `paramlen', `rotate' and `unique' are local to the invocation. |
| 133 | */ |
| 134 | struct MMacro { |
| 135 | MMacro *next; |
| 136 | MMacroInvocation *prev; /* previous invocation */ |
| 137 | char *name; |
| 138 | int nparam_min, nparam_max; |
| 139 | bool casesense; |
| 140 | bool plus; /* is the last parameter greedy? */ |
| 141 | bool nolist; /* is this macro listing-inhibited? */ |
| 142 | int64_t in_progress; /* is this macro currently being expanded? */ |
| 143 | int32_t max_depth; /* maximum number of recursive expansions allowed */ |
| 144 | Token *dlist; /* All defaults as one list */ |
| 145 | Token **defaults; /* Parameter default pointers */ |
| 146 | int ndefs; /* number of default parameters */ |
| 147 | Line *expansion; |
| 148 | |
| 149 | MMacro *next_active; |
| 150 | MMacro *rep_nest; /* used for nesting %rep */ |
| 151 | Token **params; /* actual parameters */ |
| 152 | Token *iline; /* invocation line */ |
| 153 | unsigned int nparam, rotate; |
| 154 | int *paramlen; |
| 155 | uint64_t unique; |
| 156 | int lineno; /* Current line number on expansion */ |
| 157 | uint64_t condcnt; /* number of if blocks... */ |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 158 | |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 159 | const char *fname; /* File where defined */ |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 160 | int32_t xline; /* First line in macro */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | |
| 164 | /* Store the definition of a multi-line macro, as defined in a |
| 165 | * previous recursive macro expansion. |
| 166 | */ |
| 167 | struct MMacroInvocation { |
| 168 | MMacroInvocation *prev; /* previous invocation */ |
| 169 | Token **params; /* actual parameters */ |
| 170 | Token *iline; /* invocation line */ |
| 171 | unsigned int nparam, rotate; |
| 172 | int *paramlen; |
| 173 | uint64_t unique; |
| 174 | uint64_t condcnt; |
| 175 | }; |
| 176 | |
| 177 | |
| 178 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 179 | * The context stack is composed of a linked list of these. |
| 180 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 181 | struct Context { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 182 | Context *next; |
| 183 | char *name; |
| 184 | struct hash_table localmac; |
| 185 | uint32_t number; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | /* |
| 189 | * This is the internal form which we break input lines up into. |
| 190 | * Typically stored in linked lists. |
| 191 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 192 | * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not |
| 193 | * necessarily used as-is, but is intended to denote the number of |
| 194 | * the substituted parameter. So in the definition |
| 195 | * |
| 196 | * %define a(x,y) ( (x) & ~(y) ) |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 197 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 198 | * the token representing `x' will have its type changed to |
| 199 | * TOK_SMAC_PARAM, but the one representing `y' will be |
| 200 | * TOK_SMAC_PARAM+1. |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 201 | * |
| 202 | * TOK_INTERNAL_STRING is a dirty hack: it's a single string token |
| 203 | * which doesn't need quotes around it. Used in the pre-include |
| 204 | * mechanism as an alternative to trying to find a sensible type of |
| 205 | * quote to use on the filename we were passed. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 206 | */ |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 207 | enum pp_token_type { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 208 | TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID, |
| 209 | TOK_PREPROC_ID, TOK_STRING, |
| 210 | TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER, |
H. Peter Anvin | 6c81f0a | 2008-05-25 21:46:17 -0700 | [diff] [blame] | 211 | TOK_INTERNAL_STRING, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 212 | TOK_PREPROC_Q, TOK_PREPROC_QQ, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 213 | TOK_PASTE, /* %+ */ |
| 214 | TOK_INDIRECT, /* %[...] */ |
| 215 | TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */ |
| 216 | TOK_MAX = INT_MAX /* Keep compiler from reducing the range */ |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 217 | }; |
| 218 | |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 219 | #define PP_CONCAT_MASK(x) (1 << (x)) |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 220 | #define PP_CONCAT_MATCH(t, mask) (PP_CONCAT_MASK((t)->type) & mask) |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 221 | |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 222 | struct tokseq_match { |
| 223 | int mask_head; |
| 224 | int mask_tail; |
| 225 | }; |
| 226 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 227 | struct Token { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 228 | Token *next; |
| 229 | char *text; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 230 | union { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 231 | SMacro *mac; /* associated macro for TOK_SMAC_END */ |
| 232 | size_t len; /* scratch length field */ |
| 233 | } a; /* Auxiliary data */ |
| 234 | enum pp_token_type type; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 235 | }; |
| 236 | |
| 237 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 238 | * Multi-line macro definitions are stored as a linked list of |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 239 | * these, which is essentially a container to allow several linked |
| 240 | * lists of Tokens. |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 241 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 242 | * Note that in this module, linked lists are treated as stacks |
| 243 | * wherever possible. For this reason, Lines are _pushed_ on to the |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 244 | * `expansion' field in MMacro structures, so that the linked list, |
| 245 | * if walked, would give the macro lines in reverse order; this |
| 246 | * means that we can walk the list when expanding a macro, and thus |
| 247 | * push the lines on to the `expansion' field in _istk_ in reverse |
| 248 | * order (so that when popped back off they are in the right |
| 249 | * order). It may seem cockeyed, and it relies on my design having |
| 250 | * an even number of steps in, but it works... |
| 251 | * |
| 252 | * Some of these structures, rather than being actual lines, are |
| 253 | * markers delimiting the end of the expansion of a given macro. |
| 254 | * This is for use in the cycle-tracking and %rep-handling code. |
| 255 | * Such structures have `finishes' non-NULL, and `first' NULL. All |
| 256 | * others have `finishes' NULL, but `first' may still be NULL if |
| 257 | * the line is blank. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 258 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 259 | struct Line { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 260 | Line *next; |
| 261 | MMacro *finishes; |
| 262 | Token *first; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 263 | }; |
| 264 | |
| 265 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 266 | * To handle an arbitrary level of file inclusion, we maintain a |
| 267 | * stack (ie linked list) of these things. |
| 268 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 269 | struct Include { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 270 | Include *next; |
| 271 | FILE *fp; |
| 272 | Cond *conds; |
| 273 | Line *expansion; |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 274 | const char *fname; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 275 | int lineno, lineinc; |
| 276 | MMacro *mstk; /* stack of active macros/reps */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 277 | }; |
| 278 | |
| 279 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 280 | * Include search path. This is simply a list of strings which get |
| 281 | * prepended, in turn, to the name of an include file, in an |
| 282 | * attempt to find the file if it's not in the current directory. |
| 283 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 284 | struct IncPath { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 285 | IncPath *next; |
| 286 | char *path; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 287 | }; |
| 288 | |
| 289 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 290 | * Conditional assembly: we maintain a separate stack of these for |
| 291 | * each level of file inclusion. (The only reason we keep the |
| 292 | * stacks separate is to ensure that a stray `%endif' in a file |
| 293 | * included from within the true branch of a `%if' won't terminate |
| 294 | * it and cause confusion: instead, rightly, it'll cause an error.) |
| 295 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 296 | struct Cond { |
| 297 | Cond *next; |
| 298 | int state; |
| 299 | }; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 300 | enum { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 301 | /* |
| 302 | * These states are for use just after %if or %elif: IF_TRUE |
| 303 | * means the condition has evaluated to truth so we are |
| 304 | * currently emitting, whereas IF_FALSE means we are not |
| 305 | * currently emitting but will start doing so if a %else comes |
| 306 | * up. In these states, all directives are admissible: %elif, |
| 307 | * %else and %endif. (And of course %if.) |
| 308 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 309 | COND_IF_TRUE, COND_IF_FALSE, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 310 | /* |
| 311 | * These states come up after a %else: ELSE_TRUE means we're |
| 312 | * emitting, and ELSE_FALSE means we're not. In ELSE_* states, |
| 313 | * any %elif or %else will cause an error. |
| 314 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 315 | COND_ELSE_TRUE, COND_ELSE_FALSE, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 316 | /* |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 317 | * These states mean that we're not emitting now, and also that |
| 318 | * nothing until %endif will be emitted at all. COND_DONE is |
| 319 | * used when we've had our moment of emission |
| 320 | * and have now started seeing %elifs. COND_NEVER is used when |
| 321 | * the condition construct in question is contained within a |
| 322 | * non-emitting branch of a larger condition construct, |
| 323 | * or if there is an error. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 324 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 325 | COND_DONE, COND_NEVER |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 326 | }; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 327 | #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE ) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 328 | |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 329 | /* |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 330 | * These defines are used as the possible return values for do_directive |
| 331 | */ |
| 332 | #define NO_DIRECTIVE_FOUND 0 |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 333 | #define DIRECTIVE_FOUND 1 |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 334 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 335 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 336 | * This define sets the upper limit for smacro and recursive mmacro |
| 337 | * expansions |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 338 | */ |
| 339 | #define DEADMAN_LIMIT (1 << 20) |
| 340 | |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 341 | /* max reps */ |
| 342 | #define REP_LIMIT ((INT64_C(1) << 62)) |
| 343 | |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 344 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 345 | * Condition codes. Note that we use c_ prefix not C_ because C_ is |
| 346 | * used in nasm.h for the "real" condition codes. At _this_ level, |
| 347 | * we treat CXZ and ECXZ as condition codes, albeit non-invertible |
| 348 | * ones, so we need a different enum... |
| 349 | */ |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 350 | static const char * const conditions[] = { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 351 | "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le", |
| 352 | "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no", |
H. Peter Anvin | ce9be34 | 2007-09-12 00:22:29 +0000 | [diff] [blame] | 353 | "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z" |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 354 | }; |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 355 | enum pp_conds { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 356 | c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE, |
| 357 | 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] | 358 | c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z, |
| 359 | c_none = -1 |
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 | static const enum pp_conds inverse_ccs[] = { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 362 | c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE, |
| 363 | 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] | 364 | 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] | 365 | }; |
| 366 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 367 | /* |
| 368 | * Directive names. |
| 369 | */ |
| 370 | /* If this is a an IF, ELIF, ELSE or ENDIF keyword */ |
| 371 | static int is_condition(enum preproc_token arg) |
| 372 | { |
| 373 | return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF); |
| 374 | } |
| 375 | |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 376 | /* For TASM compatibility we need to be able to recognise TASM compatible |
| 377 | * conditional compilation directives. Using the NASM pre-processor does |
| 378 | * not work, so we look for them specifically from the following list and |
| 379 | * then jam in the equivalent NASM directive into the input stream. |
| 380 | */ |
| 381 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 382 | enum { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 383 | TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI, |
| 384 | TM_IFNDEF, TM_INCLUDE, TM_LOCAL |
| 385 | }; |
| 386 | |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 387 | static const char * const tasm_directives[] = { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 388 | "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi", |
| 389 | "ifndef", "include", "local" |
| 390 | }; |
| 391 | |
| 392 | static int StackSize = 4; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 393 | static char *StackPointer = "ebp"; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 394 | static int ArgOffset = 8; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 395 | static int LocalOffset = 0; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 396 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 397 | static Context *cstk; |
| 398 | static Include *istk; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 399 | static IncPath *ipath = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 400 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 401 | static int pass; /* HACK: pass 0 = generate dependencies only */ |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 402 | static StrList **dephead, **deptail; /* Dependency list */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 403 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 404 | static uint64_t unique; /* unique identifier numbers */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 405 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 406 | static Line *predef = NULL; |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 407 | static bool do_predef; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 408 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 409 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 410 | * The current set of multi-line macros we have defined. |
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 struct hash_table mmacros; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 413 | |
| 414 | /* |
| 415 | * The current set of single-line macros we have defined. |
| 416 | */ |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 417 | static struct hash_table smacros; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 418 | |
| 419 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 420 | * The multi-line macro we are currently defining, or the %rep |
| 421 | * block we are currently reading, if any. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 422 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 423 | static MMacro *defining; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 424 | |
Charles Crayne | d4200be | 2008-07-12 16:42:33 -0700 | [diff] [blame] | 425 | static uint64_t nested_mac_count; |
| 426 | static uint64_t nested_rep_count; |
| 427 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 428 | /* |
| 429 | * The number of macro parameters to allocate space for at a time. |
| 430 | */ |
| 431 | #define PARAM_DELTA 16 |
| 432 | |
| 433 | /* |
H. Peter Anvin | a4835d4 | 2008-05-20 14:21:29 -0700 | [diff] [blame] | 434 | * The standard macro set: defined in macros.c in the array nasm_stdmac. |
| 435 | * This gives our position in the macro set, when we're processing it. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 436 | */ |
H. Peter Anvin | a70547f | 2008-07-19 21:44:26 -0700 | [diff] [blame] | 437 | static macros_t *stdmacpos; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 438 | |
| 439 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 440 | * The extra standard macros that come from the object format, if |
| 441 | * any. |
| 442 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 443 | static macros_t *extrastdmac = NULL; |
H. Peter Anvin | cfb7176 | 2008-06-20 15:20:16 -0700 | [diff] [blame] | 444 | static bool any_extrastdmac; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 445 | |
| 446 | /* |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 447 | * Tokens are allocated in blocks to improve speed |
| 448 | */ |
| 449 | #define TOKEN_BLOCKSIZE 4096 |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 450 | static Token *freeTokens = NULL; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 451 | struct Blocks { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 452 | Blocks *next; |
| 453 | void *chunk; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 454 | }; |
| 455 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 456 | static Blocks blocks = { NULL, NULL }; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 457 | |
| 458 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 459 | * Forward declarations. |
| 460 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 461 | static Token *expand_mmac_params(Token * tline); |
| 462 | static Token *expand_smacro(Token * tline); |
| 463 | static Token *expand_id(Token * tline); |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 464 | static Context *get_ctx(const char *name, const char **namep); |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 465 | static void make_tok_num(Token * tok, int64_t val); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 466 | static void pp_verror(int severity, const char *fmt, va_list ap); |
| 467 | static vefunc real_verror; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 468 | static void *new_Block(size_t size); |
| 469 | static void delete_Blocks(void); |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 470 | static Token *new_Token(Token * next, enum pp_token_type type, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 471 | const char *text, int txtlen); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 472 | static Token *delete_Token(Token * t); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 473 | |
| 474 | /* |
| 475 | * Macros for safe checking of token pointers, avoid *(NULL) |
| 476 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 477 | #define tok_type_(x,t) ((x) && (x)->type == (t)) |
| 478 | #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next |
| 479 | #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v))) |
| 480 | #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] | 481 | |
Cyrill Gorcunov | 194ba89 | 2011-06-30 01:16:35 +0400 | [diff] [blame] | 482 | /* |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 483 | * nasm_unquote with error if the string contains NUL characters. |
| 484 | * If the string contains NUL characters, issue an error and return |
| 485 | * the C len, i.e. truncate at the NUL. |
| 486 | */ |
| 487 | static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive) |
| 488 | { |
| 489 | size_t len = nasm_unquote(qstr, NULL); |
| 490 | size_t clen = strlen(qstr); |
| 491 | |
| 492 | if (len != clen) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 493 | nasm_error(ERR_NONFATAL, "NUL character in `%s' directive", |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 494 | pp_directives[directive]); |
| 495 | |
| 496 | return clen; |
| 497 | } |
| 498 | |
| 499 | /* |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 500 | * In-place reverse a list of tokens. |
| 501 | */ |
| 502 | static Token *reverse_tokens(Token *t) |
| 503 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 504 | Token *prev = NULL; |
| 505 | Token *next; |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 506 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 507 | while (t) { |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 508 | next = t->next; |
| 509 | t->next = prev; |
| 510 | prev = t; |
| 511 | t = next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 512 | } |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 513 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 514 | return prev; |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | /* |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 518 | * Handle TASM specific directives, which do not contain a % in |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 519 | * front of them. We do it here because I could not find any other |
| 520 | * place to do it for the moment, and it is a hack (ideally it would |
| 521 | * be nice to be able to use the NASM pre-processor to do it). |
| 522 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 523 | static char *check_tasm_directive(char *line) |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 524 | { |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 525 | int32_t i, j, k, m, len; |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 526 | char *p, *q, *oldline, oldchar; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 527 | |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 528 | p = nasm_skip_spaces(line); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 529 | |
| 530 | /* Binary search for the directive name */ |
| 531 | i = -1; |
Cyrill Gorcunov | a731924 | 2010-06-03 22:04:36 +0400 | [diff] [blame] | 532 | j = ARRAY_SIZE(tasm_directives); |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 533 | q = nasm_skip_word(p); |
| 534 | len = q - p; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 535 | if (len) { |
| 536 | oldchar = p[len]; |
| 537 | p[len] = 0; |
| 538 | while (j - i > 1) { |
| 539 | k = (j + i) / 2; |
| 540 | m = nasm_stricmp(p, tasm_directives[k]); |
| 541 | if (m == 0) { |
| 542 | /* We have found a directive, so jam a % in front of it |
| 543 | * so that NASM will then recognise it as one if it's own. |
| 544 | */ |
| 545 | p[len] = oldchar; |
| 546 | len = strlen(p); |
| 547 | oldline = line; |
| 548 | line = nasm_malloc(len + 2); |
| 549 | line[0] = '%'; |
| 550 | if (k == TM_IFDIFI) { |
H. Peter Anvin | 18f4879 | 2009-06-27 15:56:27 -0700 | [diff] [blame] | 551 | /* |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 552 | * NASM does not recognise IFDIFI, so we convert |
| 553 | * it to %if 0. This is not used in NASM |
| 554 | * compatible code, but does need to parse for the |
| 555 | * TASM macro package. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 556 | */ |
H. Peter Anvin | 18f4879 | 2009-06-27 15:56:27 -0700 | [diff] [blame] | 557 | strcpy(line + 1, "if 0"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 558 | } else { |
| 559 | memcpy(line + 1, p, len + 1); |
| 560 | } |
| 561 | nasm_free(oldline); |
| 562 | return line; |
| 563 | } else if (m < 0) { |
| 564 | j = k; |
| 565 | } else |
| 566 | i = k; |
| 567 | } |
| 568 | p[len] = oldchar; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 569 | } |
| 570 | return line; |
| 571 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 572 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 573 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 574 | * The pre-preprocessing stage... This function translates line |
| 575 | * number indications as they emerge from GNU cpp (`# lineno "file" |
| 576 | * flags') into NASM preprocessor line number indications (`%line |
| 577 | * lineno file'). |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 578 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 579 | static char *prepreproc(char *line) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 580 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 581 | int lineno, fnlen; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 582 | char *fname, *oldline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 583 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 584 | if (line[0] == '#' && line[1] == ' ') { |
| 585 | oldline = line; |
| 586 | fname = oldline + 2; |
| 587 | lineno = atoi(fname); |
| 588 | fname += strspn(fname, "0123456789 "); |
| 589 | if (*fname == '"') |
| 590 | fname++; |
| 591 | fnlen = strcspn(fname, "\""); |
| 592 | line = nasm_malloc(20 + fnlen); |
| 593 | snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname); |
| 594 | nasm_free(oldline); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 595 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 596 | if (tasm_compatible_mode) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 597 | return check_tasm_directive(line); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 598 | return line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 602 | * Free a linked list of tokens. |
| 603 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 604 | static void free_tlist(Token * list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 605 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 606 | while (list) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 607 | list = delete_Token(list); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | /* |
| 611 | * Free a linked list of lines. |
| 612 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 613 | static void free_llist(Line * list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 614 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 615 | Line *l, *tmp; |
| 616 | list_for_each_safe(l, tmp, list) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 617 | free_tlist(l->first); |
| 618 | nasm_free(l); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 619 | } |
| 620 | } |
| 621 | |
| 622 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 623 | * Free an MMacro |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 624 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 625 | static void free_mmacro(MMacro * m) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 626 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 627 | nasm_free(m->name); |
| 628 | free_tlist(m->dlist); |
| 629 | nasm_free(m->defaults); |
| 630 | free_llist(m->expansion); |
| 631 | nasm_free(m); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | /* |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 635 | * Free all currently defined macros, and free the hash tables |
| 636 | */ |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 637 | static void free_smacro_table(struct hash_table *smt) |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 638 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 639 | SMacro *s, *tmp; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 640 | const char *key; |
| 641 | struct hash_tbl_node *it = NULL; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 642 | |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 643 | while ((s = hash_iterate(smt, &it, &key)) != NULL) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 644 | nasm_free((void *)key); |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 645 | list_for_each_safe(s, tmp, s) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 646 | nasm_free(s->name); |
| 647 | free_tlist(s->expansion); |
| 648 | nasm_free(s); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 649 | } |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 650 | } |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 651 | hash_free(smt); |
| 652 | } |
| 653 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 654 | static void free_mmacro_table(struct hash_table *mmt) |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 655 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 656 | MMacro *m, *tmp; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 657 | const char *key; |
| 658 | struct hash_tbl_node *it = NULL; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 659 | |
| 660 | it = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 661 | while ((m = hash_iterate(mmt, &it, &key)) != NULL) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 662 | nasm_free((void *)key); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 663 | list_for_each_safe(m ,tmp, m) |
| 664 | free_mmacro(m); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 665 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 666 | hash_free(mmt); |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | static void free_macros(void) |
| 670 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 671 | free_smacro_table(&smacros); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 672 | free_mmacro_table(&mmacros); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | /* |
| 676 | * Initialize the hash tables |
| 677 | */ |
| 678 | static void init_macros(void) |
| 679 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 680 | hash_init(&smacros, HASH_LARGE); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 681 | hash_init(&mmacros, HASH_LARGE); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 685 | * Pop the context stack. |
| 686 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 687 | static void ctx_pop(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 688 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 689 | Context *c = cstk; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 690 | |
| 691 | cstk = cstk->next; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 692 | free_smacro_table(&c->localmac); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 693 | nasm_free(c->name); |
| 694 | nasm_free(c); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 695 | } |
| 696 | |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 697 | /* |
| 698 | * Search for a key in the hash index; adding it if necessary |
| 699 | * (in which case we initialize the data pointer to NULL.) |
| 700 | */ |
| 701 | static void ** |
| 702 | hash_findi_add(struct hash_table *hash, const char *str) |
| 703 | { |
| 704 | struct hash_insert hi; |
| 705 | void **r; |
| 706 | char *strx; |
| 707 | |
| 708 | r = hash_findi(hash, str, &hi); |
| 709 | if (r) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 710 | return r; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 711 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 712 | strx = nasm_strdup(str); /* Use a more efficient allocator here? */ |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 713 | return hash_add(&hi, strx, NULL); |
| 714 | } |
| 715 | |
| 716 | /* |
| 717 | * Like hash_findi, but returns the data element rather than a pointer |
| 718 | * to it. Used only when not adding a new element, hence no third |
| 719 | * argument. |
| 720 | */ |
| 721 | static void * |
| 722 | hash_findix(struct hash_table *hash, const char *str) |
| 723 | { |
| 724 | void **p; |
| 725 | |
| 726 | p = hash_findi(hash, str, NULL); |
| 727 | return p ? *p : NULL; |
| 728 | } |
| 729 | |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 730 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 731 | * read line from standart macros set, |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 732 | * if there no more left -- return NULL |
| 733 | */ |
| 734 | static char *line_from_stdmac(void) |
| 735 | { |
| 736 | unsigned char c; |
| 737 | const unsigned char *p = stdmacpos; |
| 738 | char *line, *q; |
| 739 | size_t len = 0; |
| 740 | |
| 741 | if (!stdmacpos) |
| 742 | return NULL; |
| 743 | |
| 744 | while ((c = *p++)) { |
| 745 | if (c >= 0x80) |
| 746 | len += pp_directives_len[c - 0x80] + 1; |
| 747 | else |
| 748 | len++; |
| 749 | } |
| 750 | |
| 751 | line = nasm_malloc(len + 1); |
| 752 | q = line; |
| 753 | while ((c = *stdmacpos++)) { |
| 754 | if (c >= 0x80) { |
| 755 | memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]); |
| 756 | q += pp_directives_len[c - 0x80]; |
| 757 | *q++ = ' '; |
| 758 | } else { |
| 759 | *q++ = c; |
| 760 | } |
| 761 | } |
| 762 | stdmacpos = p; |
| 763 | *q = '\0'; |
| 764 | |
| 765 | if (!*stdmacpos) { |
| 766 | /* This was the last of the standard macro chain... */ |
| 767 | stdmacpos = NULL; |
| 768 | if (any_extrastdmac) { |
| 769 | stdmacpos = extrastdmac; |
| 770 | any_extrastdmac = false; |
| 771 | } else if (do_predef) { |
| 772 | Line *pd, *l; |
| 773 | Token *head, **tail, *t; |
| 774 | |
| 775 | /* |
| 776 | * Nasty hack: here we push the contents of |
| 777 | * `predef' on to the top-level expansion stack, |
| 778 | * since this is the most convenient way to |
| 779 | * implement the pre-include and pre-define |
| 780 | * features. |
| 781 | */ |
| 782 | list_for_each(pd, predef) { |
| 783 | head = NULL; |
| 784 | tail = &head; |
| 785 | list_for_each(t, pd->first) { |
| 786 | *tail = new_Token(NULL, t->type, t->text, 0); |
| 787 | tail = &(*tail)->next; |
| 788 | } |
| 789 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 790 | l = nasm_malloc(sizeof(Line)); |
| 791 | l->next = istk->expansion; |
| 792 | l->first = head; |
| 793 | l->finishes = NULL; |
| 794 | |
| 795 | istk->expansion = l; |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 796 | } |
| 797 | do_predef = false; |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | return line; |
| 802 | } |
| 803 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 804 | static char *read_line(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 805 | { |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 806 | unsigned int size, c, next; |
| 807 | const unsigned int delta = 512; |
| 808 | const unsigned int pad = 8; |
| 809 | unsigned int nr_cont = 0; |
| 810 | bool cont = false; |
| 811 | char *buffer, *p; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 812 | |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 813 | /* Standart macros set (predefined) goes first */ |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 814 | p = line_from_stdmac(); |
| 815 | if (p) |
| 816 | return p; |
H. Peter Anvin | 72edbb8 | 2008-06-19 16:00:04 -0700 | [diff] [blame] | 817 | |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 818 | size = delta; |
| 819 | p = buffer = nasm_malloc(size); |
| 820 | |
| 821 | for (;;) { |
| 822 | c = fgetc(istk->fp); |
| 823 | if ((int)(c) == EOF) { |
| 824 | p[0] = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 825 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 826 | } |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 827 | |
| 828 | switch (c) { |
| 829 | case '\r': |
| 830 | next = fgetc(istk->fp); |
| 831 | if (next != '\n') |
| 832 | ungetc(next, istk->fp); |
| 833 | if (cont) { |
| 834 | cont = false; |
| 835 | continue; |
| 836 | } |
| 837 | break; |
| 838 | |
| 839 | case '\n': |
| 840 | if (cont) { |
| 841 | cont = false; |
| 842 | continue; |
| 843 | } |
| 844 | break; |
| 845 | |
| 846 | case '\\': |
| 847 | next = fgetc(istk->fp); |
| 848 | ungetc(next, istk->fp); |
Cyrill Gorcunov | 490f85e | 2012-12-27 20:02:17 +0400 | [diff] [blame] | 849 | if (next == '\r' || next == '\n') { |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 850 | cont = true; |
| 851 | nr_cont++; |
| 852 | continue; |
| 853 | } |
| 854 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 855 | } |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 856 | |
| 857 | if (c == '\r' || c == '\n') { |
| 858 | *p++ = 0; |
| 859 | break; |
| 860 | } |
| 861 | |
| 862 | if (p >= (buffer + size - pad)) { |
| 863 | buffer = nasm_realloc(buffer, size + delta); |
| 864 | p = buffer + size - pad; |
| 865 | size += delta; |
| 866 | } |
| 867 | |
| 868 | *p++ = (unsigned char)c; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 869 | } |
| 870 | |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 871 | if (p == buffer) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 872 | nasm_free(buffer); |
| 873 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 874 | } |
| 875 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 876 | src_set_linnum(src_get_linnum() + istk->lineinc + |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 877 | (nr_cont * istk->lineinc)); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 878 | |
| 879 | /* |
| 880 | * Handle spurious ^Z, which may be inserted into source files |
| 881 | * by some file transfer utilities. |
| 882 | */ |
| 883 | buffer[strcspn(buffer, "\032")] = '\0'; |
| 884 | |
H. Peter Anvin | 172b840 | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 885 | lfmt->line(LIST_READ, buffer); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 886 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 887 | return buffer; |
| 888 | } |
| 889 | |
| 890 | /* |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 891 | * 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] | 892 | * don't need to parse the value out of e.g. numeric tokens: we |
| 893 | * simply split one string into many. |
| 894 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 895 | static Token *tokenize(char *line) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 896 | { |
H. Peter Anvin | ca544db | 2008-10-19 19:30:11 -0700 | [diff] [blame] | 897 | char c, *p = line; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 898 | enum pp_token_type type; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 899 | Token *list = NULL; |
| 900 | Token *t, **tail = &list; |
| 901 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 902 | while (*line) { |
| 903 | p = line; |
| 904 | if (*p == '%') { |
| 905 | p++; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 906 | if (*p == '+' && !nasm_isdigit(p[1])) { |
| 907 | p++; |
| 908 | type = TOK_PASTE; |
| 909 | } else if (nasm_isdigit(*p) || |
| 910 | ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 911 | do { |
| 912 | p++; |
| 913 | } |
H. Peter Anvin | bda7a6e | 2008-06-21 10:23:17 -0700 | [diff] [blame] | 914 | while (nasm_isdigit(*p)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 915 | type = TOK_PREPROC_ID; |
| 916 | } else if (*p == '{') { |
| 917 | p++; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 918 | while (*p) { |
| 919 | if (*p == '}') |
| 920 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 921 | p[-1] = *p; |
| 922 | p++; |
| 923 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 924 | if (*p != '}') |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 925 | nasm_error(ERR_WARNING | ERR_PASS1, |
| 926 | "unterminated %%{ construct"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 927 | p[-1] = '\0'; |
| 928 | if (*p) |
| 929 | p++; |
| 930 | type = TOK_PREPROC_ID; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 931 | } else if (*p == '[') { |
| 932 | int lvl = 1; |
| 933 | line += 2; /* Skip the leading %[ */ |
| 934 | p++; |
| 935 | while (lvl && (c = *p++)) { |
| 936 | switch (c) { |
| 937 | case ']': |
| 938 | lvl--; |
| 939 | break; |
| 940 | case '%': |
| 941 | if (*p == '[') |
| 942 | lvl++; |
| 943 | break; |
| 944 | case '\'': |
| 945 | case '\"': |
| 946 | case '`': |
Cyrill Gorcunov | c6360a7 | 2010-07-13 13:32:19 +0400 | [diff] [blame] | 947 | p = nasm_skip_string(p - 1) + 1; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 948 | break; |
| 949 | default: |
| 950 | break; |
| 951 | } |
| 952 | } |
| 953 | p--; |
| 954 | if (*p) |
| 955 | *p++ = '\0'; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 956 | if (lvl) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 957 | nasm_error(ERR_NONFATAL|ERR_PASS1, |
| 958 | "unterminated %%[ construct"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 959 | type = TOK_INDIRECT; |
| 960 | } else if (*p == '?') { |
| 961 | type = TOK_PREPROC_Q; /* %? */ |
| 962 | p++; |
| 963 | if (*p == '?') { |
| 964 | type = TOK_PREPROC_QQ; /* %?? */ |
| 965 | p++; |
| 966 | } |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 967 | } else if (*p == '!') { |
| 968 | type = TOK_PREPROC_ID; |
| 969 | p++; |
| 970 | if (isidchar(*p)) { |
| 971 | do { |
| 972 | p++; |
| 973 | } |
| 974 | while (isidchar(*p)); |
| 975 | } else if (*p == '\'' || *p == '\"' || *p == '`') { |
| 976 | p = nasm_skip_string(p); |
| 977 | if (*p) |
| 978 | p++; |
| 979 | else |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 980 | nasm_error(ERR_NONFATAL|ERR_PASS1, |
| 981 | "unterminated %%! string"); |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 982 | } else { |
| 983 | /* %! without string or identifier */ |
| 984 | type = TOK_OTHER; /* Legacy behavior... */ |
| 985 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 986 | } else if (isidchar(*p) || |
| 987 | ((*p == '!' || *p == '%' || *p == '$') && |
| 988 | isidchar(p[1]))) { |
| 989 | do { |
| 990 | p++; |
| 991 | } |
| 992 | while (isidchar(*p)); |
| 993 | type = TOK_PREPROC_ID; |
| 994 | } else { |
| 995 | type = TOK_OTHER; |
| 996 | if (*p == '%') |
| 997 | p++; |
| 998 | } |
| 999 | } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) { |
| 1000 | type = TOK_ID; |
| 1001 | p++; |
| 1002 | while (*p && isidchar(*p)) |
| 1003 | p++; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 1004 | } else if (*p == '\'' || *p == '"' || *p == '`') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1005 | /* |
| 1006 | * A string token. |
| 1007 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1008 | type = TOK_STRING; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1009 | p = nasm_skip_string(p); |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1010 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1011 | if (*p) { |
| 1012 | p++; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1013 | } else { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1014 | nasm_error(ERR_WARNING|ERR_PASS1, "unterminated string"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1015 | /* Handling unterminated strings by UNV */ |
| 1016 | /* type = -1; */ |
| 1017 | } |
Victor van den Elzen | fb5f251 | 2009-04-17 16:17:59 +0200 | [diff] [blame] | 1018 | } else if (p[0] == '$' && p[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1019 | type = TOK_OTHER; /* TOKEN_BASE */ |
Victor van den Elzen | fb5f251 | 2009-04-17 16:17:59 +0200 | [diff] [blame] | 1020 | p += 2; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1021 | } else if (isnumstart(*p)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1022 | bool is_hex = false; |
| 1023 | bool is_float = false; |
| 1024 | bool has_e = false; |
| 1025 | char c, *r; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1026 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1027 | /* |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1028 | * A numeric token. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1029 | */ |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1030 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1031 | if (*p == '$') { |
| 1032 | p++; |
| 1033 | is_hex = true; |
| 1034 | } |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1035 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1036 | for (;;) { |
| 1037 | c = *p++; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1038 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1039 | if (!is_hex && (c == 'e' || c == 'E')) { |
| 1040 | has_e = true; |
| 1041 | if (*p == '+' || *p == '-') { |
| 1042 | /* |
| 1043 | * e can only be followed by +/- if it is either a |
| 1044 | * prefixed hex number or a floating-point number |
| 1045 | */ |
| 1046 | p++; |
| 1047 | is_float = true; |
| 1048 | } |
| 1049 | } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') { |
| 1050 | is_hex = true; |
| 1051 | } else if (c == 'P' || c == 'p') { |
| 1052 | is_float = true; |
| 1053 | if (*p == '+' || *p == '-') |
| 1054 | p++; |
| 1055 | } else if (isnumchar(c) || c == '_') |
| 1056 | ; /* just advance */ |
| 1057 | else if (c == '.') { |
| 1058 | /* |
| 1059 | * we need to deal with consequences of the legacy |
| 1060 | * parser, like "1.nolist" being two tokens |
| 1061 | * (TOK_NUMBER, TOK_ID) here; at least give it |
| 1062 | * a shot for now. In the future, we probably need |
| 1063 | * a flex-based scanner with proper pattern matching |
| 1064 | * to do it as well as it can be done. Nothing in |
| 1065 | * the world is going to help the person who wants |
| 1066 | * 0x123.p16 interpreted as two tokens, though. |
| 1067 | */ |
| 1068 | r = p; |
| 1069 | while (*r == '_') |
| 1070 | r++; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1071 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1072 | if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) || |
| 1073 | (!is_hex && (*r == 'e' || *r == 'E')) || |
| 1074 | (*r == 'p' || *r == 'P')) { |
| 1075 | p = r; |
| 1076 | is_float = true; |
| 1077 | } else |
| 1078 | break; /* Terminate the token */ |
| 1079 | } else |
| 1080 | break; |
| 1081 | } |
| 1082 | p--; /* Point to first character beyond number */ |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1083 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1084 | if (p == line+1 && *line == '$') { |
| 1085 | type = TOK_OTHER; /* TOKEN_HERE */ |
| 1086 | } else { |
| 1087 | if (has_e && !is_hex) { |
| 1088 | /* 1e13 is floating-point, but 1e13h is not */ |
| 1089 | is_float = true; |
| 1090 | } |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 1091 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1092 | type = is_float ? TOK_FLOAT : TOK_NUMBER; |
| 1093 | } |
H. Peter Anvin | bda7a6e | 2008-06-21 10:23:17 -0700 | [diff] [blame] | 1094 | } else if (nasm_isspace(*p)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1095 | type = TOK_WHITESPACE; |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 1096 | p = nasm_skip_spaces(p); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1097 | /* |
| 1098 | * Whitespace just before end-of-line is discarded by |
| 1099 | * pretending it's a comment; whitespace just before a |
| 1100 | * comment gets lumped into the comment. |
| 1101 | */ |
| 1102 | if (!*p || *p == ';') { |
| 1103 | type = TOK_COMMENT; |
| 1104 | while (*p) |
| 1105 | p++; |
| 1106 | } |
| 1107 | } else if (*p == ';') { |
| 1108 | type = TOK_COMMENT; |
| 1109 | while (*p) |
| 1110 | p++; |
| 1111 | } else { |
| 1112 | /* |
| 1113 | * Anything else is an operator of some kind. We check |
| 1114 | * for all the double-character operators (>>, <<, //, |
| 1115 | * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1116 | * else is a single-character operator. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1117 | */ |
| 1118 | type = TOK_OTHER; |
| 1119 | if ((p[0] == '>' && p[1] == '>') || |
| 1120 | (p[0] == '<' && p[1] == '<') || |
| 1121 | (p[0] == '/' && p[1] == '/') || |
| 1122 | (p[0] == '<' && p[1] == '=') || |
| 1123 | (p[0] == '>' && p[1] == '=') || |
| 1124 | (p[0] == '=' && p[1] == '=') || |
| 1125 | (p[0] == '!' && p[1] == '=') || |
| 1126 | (p[0] == '<' && p[1] == '>') || |
| 1127 | (p[0] == '&' && p[1] == '&') || |
| 1128 | (p[0] == '|' && p[1] == '|') || |
| 1129 | (p[0] == '^' && p[1] == '^')) { |
| 1130 | p++; |
| 1131 | } |
| 1132 | p++; |
| 1133 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1134 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1135 | /* Handling unterminated string by UNV */ |
| 1136 | /*if (type == -1) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1137 | { |
| 1138 | *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1); |
| 1139 | t->text[p-line] = *line; |
| 1140 | tail = &t->next; |
| 1141 | } |
| 1142 | else */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1143 | if (type != TOK_COMMENT) { |
| 1144 | *tail = t = new_Token(NULL, type, line, p - line); |
| 1145 | tail = &t->next; |
| 1146 | } |
| 1147 | line = p; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1148 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1149 | return list; |
| 1150 | } |
| 1151 | |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1152 | /* |
| 1153 | * this function allocates a new managed block of memory and |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1154 | * returns a pointer to the block. The managed blocks are |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1155 | * deleted only all at once by the delete_Blocks function. |
| 1156 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1157 | static void *new_Block(size_t size) |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1158 | { |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1159 | Blocks *b = &blocks; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1160 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1161 | /* first, get to the end of the linked list */ |
| 1162 | while (b->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1163 | b = b->next; |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1164 | /* now allocate the requested chunk */ |
| 1165 | b->chunk = nasm_malloc(size); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1166 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1167 | /* now allocate a new block for the next request */ |
Cyrill Gorcunov | c31767c | 2014-06-28 02:22:17 +0400 | [diff] [blame] | 1168 | b->next = nasm_zalloc(sizeof(Blocks)); |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1169 | return b->chunk; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | /* |
| 1173 | * this function deletes all managed blocks of memory |
| 1174 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1175 | static void delete_Blocks(void) |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1176 | { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1177 | Blocks *a, *b = &blocks; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1178 | |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1179 | /* |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1180 | * keep in mind that the first block, pointed to by blocks |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1181 | * is a static and not dynamically allocated, so we don't |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1182 | * free it. |
| 1183 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1184 | while (b) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1185 | if (b->chunk) |
| 1186 | nasm_free(b->chunk); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1187 | a = b; |
| 1188 | b = b->next; |
| 1189 | if (a != &blocks) |
| 1190 | nasm_free(a); |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1191 | } |
Cyrill Gorcunov | dae24d7 | 2014-06-28 10:17:39 +0400 | [diff] [blame] | 1192 | memset(&blocks, 0, sizeof(blocks)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1193 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1194 | |
| 1195 | /* |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1196 | * this function creates a new Token and passes a pointer to it |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1197 | * back to the caller. It sets the type and text elements, and |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 1198 | * also the a.mac and next elements to NULL. |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1199 | */ |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1200 | static Token *new_Token(Token * next, enum pp_token_type type, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1201 | const char *text, int txtlen) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1202 | { |
| 1203 | Token *t; |
| 1204 | int i; |
| 1205 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1206 | if (!freeTokens) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1207 | freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token)); |
| 1208 | for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++) |
| 1209 | freeTokens[i].next = &freeTokens[i + 1]; |
| 1210 | freeTokens[i].next = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1211 | } |
| 1212 | t = freeTokens; |
| 1213 | freeTokens = t->next; |
| 1214 | t->next = next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 1215 | t->a.mac = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1216 | t->type = type; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1217 | if (type == TOK_WHITESPACE || !text) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1218 | t->text = NULL; |
| 1219 | } else { |
| 1220 | if (txtlen == 0) |
| 1221 | txtlen = strlen(text); |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1222 | t->text = nasm_malloc(txtlen+1); |
| 1223 | memcpy(t->text, text, txtlen); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1224 | t->text[txtlen] = '\0'; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1225 | } |
| 1226 | return t; |
| 1227 | } |
| 1228 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1229 | static Token *delete_Token(Token * t) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1230 | { |
| 1231 | Token *next = t->next; |
| 1232 | nasm_free(t->text); |
H. Peter Anvin | 788e6c1 | 2002-04-30 21:02:01 +0000 | [diff] [blame] | 1233 | t->next = freeTokens; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1234 | freeTokens = t; |
| 1235 | return next; |
| 1236 | } |
| 1237 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1238 | /* |
| 1239 | * Convert a line of tokens back into text. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1240 | * If expand_locals is not zero, identifiers of the form "%$*xxx" |
| 1241 | * will be transformed into ..@ctxnum.xxx |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1242 | */ |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 1243 | static char *detoken(Token * tlist, bool expand_locals) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1244 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1245 | Token *t; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1246 | char *line, *p; |
H. Peter Anvin | b4daadc | 2008-01-21 16:31:57 -0800 | [diff] [blame] | 1247 | const char *q; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1248 | int len = 0; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1249 | |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1250 | list_for_each(t, tlist) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1251 | if (t->type == TOK_PREPROC_ID && t->text[1] == '!') { |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1252 | char *v; |
| 1253 | char *q = t->text; |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1254 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1255 | v = t->text + 2; |
| 1256 | if (*v == '\'' || *v == '\"' || *v == '`') { |
| 1257 | size_t len = nasm_unquote(v, NULL); |
| 1258 | size_t clen = strlen(v); |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1259 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1260 | if (len != clen) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1261 | nasm_error(ERR_NONFATAL | ERR_PASS1, |
| 1262 | "NUL character in %%! string"); |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1263 | v = NULL; |
| 1264 | } |
| 1265 | } |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1266 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1267 | if (v) { |
| 1268 | char *p = getenv(v); |
| 1269 | if (!p) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1270 | nasm_error(ERR_NONFATAL | ERR_PASS1, |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1271 | "nonexistent environment variable `%s'", v); |
| 1272 | p = ""; |
| 1273 | } |
| 1274 | t->text = nasm_strdup(p); |
| 1275 | } |
| 1276 | nasm_free(q); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1277 | } |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1278 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1279 | /* Expand local macros here and not during preprocessing */ |
| 1280 | if (expand_locals && |
| 1281 | t->type == TOK_PREPROC_ID && t->text && |
| 1282 | t->text[0] == '%' && t->text[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1283 | const char *q; |
| 1284 | char *p; |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1285 | Context *ctx = get_ctx(t->text, &q); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1286 | if (ctx) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1287 | char buffer[40]; |
Keith Kanios | 93f2e9a | 2007-04-14 00:10:59 +0000 | [diff] [blame] | 1288 | snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1289 | p = nasm_strcat(buffer, q); |
| 1290 | nasm_free(t->text); |
| 1291 | t->text = p; |
| 1292 | } |
| 1293 | } |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1294 | if (t->type == TOK_WHITESPACE) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1295 | len++; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1296 | else if (t->text) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1297 | len += strlen(t->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1298 | } |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1299 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1300 | p = line = nasm_malloc(len + 1); |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1301 | |
| 1302 | list_for_each(t, tlist) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1303 | if (t->type == TOK_WHITESPACE) { |
H. Peter Anvin | b4daadc | 2008-01-21 16:31:57 -0800 | [diff] [blame] | 1304 | *p++ = ' '; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1305 | } else if (t->text) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1306 | q = t->text; |
| 1307 | while (*q) |
| 1308 | *p++ = *q++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1309 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1310 | } |
| 1311 | *p = '\0'; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1312 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1313 | return line; |
| 1314 | } |
| 1315 | |
| 1316 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1317 | * A scanner, suitable for use by the expression evaluator, which |
| 1318 | * operates on a line of Tokens. Expects a pointer to a pointer to |
| 1319 | * the first token in the line to be passed in as its private_data |
| 1320 | * field. |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1321 | * |
| 1322 | * FIX: This really needs to be unified with stdscan. |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1323 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1324 | static int ppscan(void *private_data, struct tokenval *tokval) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1325 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1326 | Token **tlineptr = private_data; |
| 1327 | Token *tline; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1328 | char ourcopy[MAX_KEYWORD+1], *p, *r, *s; |
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 | do { |
| 1331 | tline = *tlineptr; |
| 1332 | *tlineptr = tline ? tline->next : NULL; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 1333 | } while (tline && (tline->type == TOK_WHITESPACE || |
| 1334 | tline->type == TOK_COMMENT)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1335 | |
| 1336 | if (!tline) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1337 | return tokval->t_type = TOKEN_EOS; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1338 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1339 | tokval->t_charptr = tline->text; |
| 1340 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1341 | if (tline->text[0] == '$' && !tline->text[1]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1342 | return tokval->t_type = TOKEN_HERE; |
H. Peter Anvin | 7cf897e | 2002-05-30 21:30:33 +0000 | [diff] [blame] | 1343 | if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1344 | return tokval->t_type = TOKEN_BASE; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1345 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1346 | if (tline->type == TOK_ID) { |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1347 | p = tokval->t_charptr = tline->text; |
| 1348 | if (p[0] == '$') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1349 | tokval->t_charptr++; |
| 1350 | return tokval->t_type = TOKEN_ID; |
| 1351 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1352 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1353 | for (r = p, s = ourcopy; *r; r++) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1354 | if (r >= p+MAX_KEYWORD) |
| 1355 | return tokval->t_type = TOKEN_ID; /* Not a keyword */ |
H. Peter Anvin | ac8f8fc | 2008-06-11 15:49:41 -0700 | [diff] [blame] | 1356 | *s++ = nasm_tolower(*r); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1357 | } |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1358 | *s = '\0'; |
| 1359 | /* right, so we have an identifier sitting in temp storage. now, |
| 1360 | * is it actually a register or instruction name, or what? */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1361 | return nasm_token_hash(ourcopy, tokval); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1364 | if (tline->type == TOK_NUMBER) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1365 | bool rn_error; |
| 1366 | tokval->t_integer = readnum(tline->text, &rn_error); |
| 1367 | tokval->t_charptr = tline->text; |
| 1368 | if (rn_error) |
| 1369 | return tokval->t_type = TOKEN_ERRNUM; |
| 1370 | else |
| 1371 | return tokval->t_type = TOKEN_NUM; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1372 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1373 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1374 | if (tline->type == TOK_FLOAT) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1375 | return tokval->t_type = TOKEN_FLOAT; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1376 | } |
| 1377 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1378 | if (tline->type == TOK_STRING) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1379 | char bq, *ep; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1380 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1381 | bq = tline->text[0]; |
H. Peter Anvin | 1162704 | 2008-06-09 20:45:19 -0700 | [diff] [blame] | 1382 | tokval->t_charptr = tline->text; |
| 1383 | tokval->t_inttwo = nasm_unquote(tline->text, &ep); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 1384 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1385 | if (ep[0] != bq || ep[1] != '\0') |
| 1386 | return tokval->t_type = TOKEN_ERRSTR; |
| 1387 | else |
| 1388 | return tokval->t_type = TOKEN_STR; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1389 | } |
| 1390 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1391 | if (tline->type == TOK_OTHER) { |
| 1392 | if (!strcmp(tline->text, "<<")) |
| 1393 | return tokval->t_type = TOKEN_SHL; |
| 1394 | if (!strcmp(tline->text, ">>")) |
| 1395 | return tokval->t_type = TOKEN_SHR; |
| 1396 | if (!strcmp(tline->text, "//")) |
| 1397 | return tokval->t_type = TOKEN_SDIV; |
| 1398 | if (!strcmp(tline->text, "%%")) |
| 1399 | return tokval->t_type = TOKEN_SMOD; |
| 1400 | if (!strcmp(tline->text, "==")) |
| 1401 | return tokval->t_type = TOKEN_EQ; |
| 1402 | if (!strcmp(tline->text, "<>")) |
| 1403 | return tokval->t_type = TOKEN_NE; |
| 1404 | if (!strcmp(tline->text, "!=")) |
| 1405 | return tokval->t_type = TOKEN_NE; |
| 1406 | if (!strcmp(tline->text, "<=")) |
| 1407 | return tokval->t_type = TOKEN_LE; |
| 1408 | if (!strcmp(tline->text, ">=")) |
| 1409 | return tokval->t_type = TOKEN_GE; |
| 1410 | if (!strcmp(tline->text, "&&")) |
| 1411 | return tokval->t_type = TOKEN_DBL_AND; |
| 1412 | if (!strcmp(tline->text, "^^")) |
| 1413 | return tokval->t_type = TOKEN_DBL_XOR; |
| 1414 | if (!strcmp(tline->text, "||")) |
| 1415 | return tokval->t_type = TOKEN_DBL_OR; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1416 | } |
| 1417 | |
| 1418 | /* |
| 1419 | * We have no other options: just return the first character of |
| 1420 | * the token text. |
| 1421 | */ |
| 1422 | return tokval->t_type = tline->text[0]; |
| 1423 | } |
| 1424 | |
| 1425 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1426 | * Compare a string to the name of an existing macro; this is a |
| 1427 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 1428 | * depending on the value of the `casesense' parameter. |
| 1429 | */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 1430 | static int mstrcmp(const char *p, const char *q, bool casesense) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1431 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1432 | return casesense ? strcmp(p, q) : nasm_stricmp(p, q); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | /* |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1436 | * Compare a string to the name of an existing macro; this is a |
| 1437 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 1438 | * depending on the value of the `casesense' parameter. |
| 1439 | */ |
| 1440 | static int mmemcmp(const char *p, const char *q, size_t l, bool casesense) |
| 1441 | { |
| 1442 | return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l); |
| 1443 | } |
| 1444 | |
| 1445 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1446 | * Return the Context structure associated with a %$ token. Return |
| 1447 | * NULL, having _already_ reported an error condition, if the |
| 1448 | * context stack isn't deep enough for the supplied number of $ |
| 1449 | * signs. |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1450 | * |
| 1451 | * If "namep" is non-NULL, set it to the pointer to the macro name |
| 1452 | * tail, i.e. the part beyond %$... |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1453 | */ |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1454 | static Context *get_ctx(const char *name, const char **namep) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1455 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1456 | Context *ctx; |
| 1457 | int i; |
| 1458 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1459 | if (namep) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1460 | *namep = name; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1461 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1462 | if (!name || name[0] != '%' || name[1] != '$') |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1463 | return NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1464 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1465 | if (!cstk) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1466 | nasm_error(ERR_NONFATAL, "`%s': context stack is empty", name); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1467 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1468 | } |
| 1469 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1470 | name += 2; |
| 1471 | ctx = cstk; |
| 1472 | i = 0; |
| 1473 | while (ctx && *name == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1474 | name++; |
| 1475 | i++; |
| 1476 | ctx = ctx->next; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1477 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1478 | if (!ctx) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1479 | nasm_error(ERR_NONFATAL, "`%s': context stack is only" |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1480 | " %d level%s deep", name, i, (i == 1 ? "" : "s")); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1481 | return NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1482 | } |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1483 | |
| 1484 | if (namep) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1485 | *namep = name; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1486 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1487 | return ctx; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
| 1490 | /* |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1491 | * Check to see if a file is already in a string list |
| 1492 | */ |
| 1493 | static bool in_list(const StrList *list, const char *str) |
| 1494 | { |
| 1495 | while (list) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1496 | if (!strcmp(list->str, str)) |
| 1497 | return true; |
| 1498 | list = list->next; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1499 | } |
| 1500 | return false; |
| 1501 | } |
| 1502 | |
| 1503 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1504 | * Open an include file. This routine must always return a valid |
| 1505 | * file pointer if it returns - it's responsible for throwing an |
| 1506 | * ERR_FATAL and bombing out completely if not. It should also try |
| 1507 | * the include path one by one until it finds the file or reaches |
| 1508 | * the end of the path. |
| 1509 | */ |
H. Peter Anvin | 2b1c3b9 | 2008-06-06 10:38:46 -0700 | [diff] [blame] | 1510 | static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail, |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 1511 | char **found_path, bool missing_ok, const char *mode) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1512 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1513 | FILE *fp; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1514 | char *prefix = ""; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1515 | IncPath *ip = ipath; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1516 | int len = strlen(file); |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1517 | size_t prefix_len = 0; |
| 1518 | StrList *sl; |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 1519 | size_t path_len; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1520 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1521 | while (1) { |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 1522 | path_len = prefix_len + len + 1; |
| 1523 | |
| 1524 | sl = nasm_malloc(path_len + sizeof sl->next); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1525 | memcpy(sl->str, prefix, prefix_len); |
| 1526 | memcpy(sl->str+prefix_len, file, len+1); |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 1527 | |
| 1528 | if (found_path != NULL) { |
| 1529 | *found_path = nasm_malloc(path_len); |
| 1530 | memcpy(*found_path, sl->str, path_len); |
| 1531 | } |
| 1532 | |
Fabian Giesen | 142285d | 2016-04-28 13:48:15 -0700 | [diff] [blame] | 1533 | fp = fopen(sl->str, mode); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1534 | if (fp && dhead && !in_list(*dhead, sl->str)) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1535 | sl->next = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1536 | **dtail = sl; |
| 1537 | *dtail = &sl->next; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1538 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1539 | nasm_free(sl); |
| 1540 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1541 | if (fp) |
| 1542 | return fp; |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 1543 | |
| 1544 | if (found_path != NULL && *found_path != NULL) { |
| 1545 | nasm_free(*found_path); |
| 1546 | *found_path == NULL; |
| 1547 | } |
| 1548 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1549 | if (!ip) { |
| 1550 | if (!missing_ok) |
| 1551 | break; |
| 1552 | prefix = NULL; |
| 1553 | } else { |
| 1554 | prefix = ip->path; |
| 1555 | ip = ip->next; |
| 1556 | } |
| 1557 | if (prefix) { |
| 1558 | prefix_len = strlen(prefix); |
| 1559 | } else { |
| 1560 | /* -MG given and file not found */ |
| 1561 | if (dhead && !in_list(*dhead, file)) { |
| 1562 | sl = nasm_malloc(len+1+sizeof sl->next); |
| 1563 | sl->next = NULL; |
| 1564 | strcpy(sl->str, file); |
| 1565 | **dtail = sl; |
| 1566 | *dtail = &sl->next; |
| 1567 | } |
| 1568 | return NULL; |
| 1569 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1570 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1571 | |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1572 | nasm_error(ERR_FATAL, "unable to open include file `%s'", file); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1573 | return NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1574 | } |
| 1575 | |
| 1576 | /* |
Fabian Giesen | 86d8756 | 2016-04-28 13:48:14 -0700 | [diff] [blame] | 1577 | * Opens an include or input file. Public version, for use by modules |
| 1578 | * that get a file:lineno pair and need to look at the file again |
| 1579 | * (e.g. the CodeView debug backend). Returns NULL on failure. |
| 1580 | */ |
Fabian Giesen | 142285d | 2016-04-28 13:48:15 -0700 | [diff] [blame] | 1581 | FILE *pp_input_fopen(const char *filename, const char *mode) |
Fabian Giesen | 86d8756 | 2016-04-28 13:48:14 -0700 | [diff] [blame] | 1582 | { |
| 1583 | FILE *fp; |
| 1584 | StrList *xsl = NULL; |
| 1585 | StrList **xst = &xsl; |
| 1586 | |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 1587 | fp = inc_fopen(filename, &xsl, &xst, NULL, true, mode); |
Fabian Giesen | 86d8756 | 2016-04-28 13:48:14 -0700 | [diff] [blame] | 1588 | if (xsl) |
| 1589 | nasm_free(xsl); |
| 1590 | return fp; |
| 1591 | } |
| 1592 | |
| 1593 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1594 | * 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] | 1595 | * 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] | 1596 | * return true if _any_ single-line macro of that name is defined. |
| 1597 | * Otherwise, will return true if a single-line macro with either |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1598 | * `nparam' or no parameters is defined. |
| 1599 | * |
| 1600 | * If a macro with precisely the right number of parameters is |
H. Peter Anvin | ef7468f | 2002-04-30 20:57:59 +0000 | [diff] [blame] | 1601 | * defined, or nparam is -1, the address of the definition structure |
| 1602 | * 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] | 1603 | * is NULL, no action will be taken regarding its contents, and no |
| 1604 | * error will occur. |
| 1605 | * |
| 1606 | * Note that this is also called with nparam zero to resolve |
| 1607 | * `ifdef'. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1608 | * |
| 1609 | * If you already know which context macro belongs to, you can pass |
| 1610 | * the context pointer as first parameter; if you won't but name begins |
| 1611 | * with %$ the context will be automatically computed. If all_contexts |
| 1612 | * is true, macro will be searched in outer contexts as well. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1613 | */ |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1614 | static bool |
H. Peter Anvin | b2a5fda | 2008-06-19 21:42:42 -0700 | [diff] [blame] | 1615 | smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn, |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1616 | bool nocase) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1617 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1618 | struct hash_table *smtbl; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1619 | SMacro *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1620 | |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1621 | if (ctx) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1622 | smtbl = &ctx->localmac; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1623 | } else if (name[0] == '%' && name[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1624 | if (cstk) |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1625 | ctx = get_ctx(name, &name); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1626 | if (!ctx) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1627 | return false; /* got to return _something_ */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1628 | smtbl = &ctx->localmac; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1629 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1630 | smtbl = &smacros; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1631 | } |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1632 | m = (SMacro *) hash_findix(smtbl, name); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1633 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1634 | while (m) { |
| 1635 | if (!mstrcmp(m->name, name, m->casesense && nocase) && |
Charles Crayne | 192d5b5 | 2007-10-18 19:02:42 -0700 | [diff] [blame] | 1636 | (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1637 | if (defn) { |
Charles Crayne | 192d5b5 | 2007-10-18 19:02:42 -0700 | [diff] [blame] | 1638 | if (nparam == (int) m->nparam || nparam == -1) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1639 | *defn = m; |
| 1640 | else |
| 1641 | *defn = NULL; |
| 1642 | } |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1643 | return true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1644 | } |
| 1645 | m = m->next; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1646 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1647 | |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1648 | return false; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1649 | } |
| 1650 | |
| 1651 | /* |
| 1652 | * Count and mark off the parameters in a multi-line macro call. |
| 1653 | * This is called both from within the multi-line macro expansion |
| 1654 | * code, and also to mark off the default parameters when provided |
| 1655 | * in a %macro definition line. |
| 1656 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1657 | static void count_mmac_params(Token * t, int *nparam, Token *** params) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1658 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1659 | int paramsize, brace; |
| 1660 | |
| 1661 | *nparam = paramsize = 0; |
| 1662 | *params = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1663 | while (t) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1664 | /* +1: we need space for the final NULL */ |
H. Peter Anvin | 91fb6f1 | 2008-09-01 10:56:33 -0700 | [diff] [blame] | 1665 | if (*nparam+1 >= paramsize) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1666 | paramsize += PARAM_DELTA; |
| 1667 | *params = nasm_realloc(*params, sizeof(**params) * paramsize); |
| 1668 | } |
| 1669 | skip_white_(t); |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1670 | brace = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1671 | if (tok_is_(t, "{")) |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1672 | brace++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1673 | (*params)[(*nparam)++] = t; |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1674 | if (brace) { |
| 1675 | while (brace && (t = t->next) != NULL) { |
| 1676 | if (tok_is_(t, "{")) |
| 1677 | brace++; |
| 1678 | else if (tok_is_(t, "}")) |
| 1679 | brace--; |
| 1680 | } |
| 1681 | |
| 1682 | if (t) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1683 | /* |
| 1684 | * Now we've found the closing brace, look further |
| 1685 | * for the comma. |
| 1686 | */ |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1687 | t = t->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1688 | skip_white_(t); |
| 1689 | if (tok_isnt_(t, ",")) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1690 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1691 | "braces do not enclose all of macro parameter"); |
| 1692 | while (tok_isnt_(t, ",")) |
| 1693 | t = t->next; |
| 1694 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1695 | } |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1696 | } else { |
| 1697 | while (tok_isnt_(t, ",")) |
| 1698 | t = t->next; |
| 1699 | } |
| 1700 | if (t) { /* got a comma/brace */ |
| 1701 | t = t->next; /* eat the comma */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1702 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1703 | } |
| 1704 | } |
| 1705 | |
| 1706 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1707 | * Determine whether one of the various `if' conditions is true or |
| 1708 | * not. |
| 1709 | * |
| 1710 | * We must free the tline we get passed. |
| 1711 | */ |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1712 | static bool if_condition(Token * tline, enum preproc_token ct) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1713 | { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1714 | enum pp_conditional i = PP_COND(ct); |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1715 | bool j; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1716 | Token *t, *tt, **tptr, *origline; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1717 | struct tokenval tokval; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1718 | expr *evalresult; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1719 | enum pp_token_type needtype; |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1720 | char *p; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1721 | |
| 1722 | origline = tline; |
| 1723 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1724 | switch (i) { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1725 | case PPC_IFCTX: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1726 | j = false; /* have we matched yet? */ |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1727 | while (true) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1728 | skip_white_(tline); |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1729 | if (!tline) |
| 1730 | break; |
| 1731 | if (tline->type != TOK_ID) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1732 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1733 | "`%s' expects context identifiers", pp_directives[ct]); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1734 | free_tlist(origline); |
| 1735 | return -1; |
| 1736 | } |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1737 | if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1738 | j = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1739 | tline = tline->next; |
| 1740 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1741 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1742 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1743 | case PPC_IFDEF: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1744 | j = false; /* have we matched yet? */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1745 | while (tline) { |
| 1746 | skip_white_(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1747 | if (!tline || (tline->type != TOK_ID && |
| 1748 | (tline->type != TOK_PREPROC_ID || |
| 1749 | tline->text[1] != '$'))) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1750 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1751 | "`%s' expects macro identifiers", pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1752 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1753 | } |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1754 | if (smacro_defined(NULL, tline->text, 0, NULL, true)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1755 | j = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1756 | tline = tline->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1757 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1758 | break; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1759 | |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1760 | case PPC_IFENV: |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1761 | tline = expand_smacro(tline); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1762 | j = false; /* have we matched yet? */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1763 | while (tline) { |
| 1764 | skip_white_(tline); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1765 | if (!tline || (tline->type != TOK_ID && |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1766 | tline->type != TOK_STRING && |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1767 | (tline->type != TOK_PREPROC_ID || |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1768 | tline->text[1] != '!'))) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1769 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1770 | "`%s' expects environment variable names", |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1771 | pp_directives[ct]); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1772 | goto fail; |
| 1773 | } |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1774 | p = tline->text; |
| 1775 | if (tline->type == TOK_PREPROC_ID) |
| 1776 | p += 2; /* Skip leading %! */ |
| 1777 | if (*p == '\'' || *p == '\"' || *p == '`') |
| 1778 | nasm_unquote_cstr(p, ct); |
| 1779 | if (getenv(p)) |
| 1780 | j = true; |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1781 | tline = tline->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1782 | } |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1783 | break; |
| 1784 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1785 | case PPC_IFIDN: |
| 1786 | case PPC_IFIDNI: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1787 | tline = expand_smacro(tline); |
| 1788 | t = tt = tline; |
| 1789 | while (tok_isnt_(tt, ",")) |
| 1790 | tt = tt->next; |
| 1791 | if (!tt) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1792 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1793 | "`%s' expects two comma-separated arguments", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1794 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1795 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1796 | } |
| 1797 | tt = tt->next; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1798 | j = true; /* assume equality unless proved not */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1799 | while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) { |
| 1800 | if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1801 | nasm_error(ERR_NONFATAL, "`%s': more than one comma on line", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1802 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1803 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1804 | } |
| 1805 | if (t->type == TOK_WHITESPACE) { |
| 1806 | t = t->next; |
| 1807 | continue; |
| 1808 | } |
| 1809 | if (tt->type == TOK_WHITESPACE) { |
| 1810 | tt = tt->next; |
| 1811 | continue; |
| 1812 | } |
| 1813 | if (tt->type != t->type) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1814 | j = false; /* found mismatching tokens */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1815 | break; |
| 1816 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1817 | /* When comparing strings, need to unquote them first */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1818 | if (t->type == TOK_STRING) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1819 | size_t l1 = nasm_unquote(t->text, NULL); |
| 1820 | size_t l2 = nasm_unquote(tt->text, NULL); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 1821 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1822 | if (l1 != l2) { |
| 1823 | j = false; |
| 1824 | break; |
| 1825 | } |
| 1826 | if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) { |
| 1827 | j = false; |
| 1828 | break; |
| 1829 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1830 | } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1831 | j = false; /* found mismatching tokens */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1832 | break; |
| 1833 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1834 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1835 | t = t->next; |
| 1836 | tt = tt->next; |
| 1837 | } |
| 1838 | if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1839 | j = false; /* trailing gunk on one end or other */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1840 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1841 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1842 | case PPC_IFMACRO: |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1843 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1844 | bool found = false; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1845 | MMacro searching, *mmac; |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 1846 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1847 | skip_white_(tline); |
| 1848 | tline = expand_id(tline); |
| 1849 | if (!tok_type_(tline, TOK_ID)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1850 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1851 | "`%s' expects a macro name", pp_directives[ct]); |
| 1852 | goto fail; |
| 1853 | } |
| 1854 | searching.name = nasm_strdup(tline->text); |
| 1855 | searching.casesense = true; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1856 | searching.plus = false; |
| 1857 | searching.nolist = false; |
| 1858 | searching.in_progress = 0; |
| 1859 | searching.max_depth = 0; |
| 1860 | searching.rep_nest = NULL; |
| 1861 | searching.nparam_min = 0; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1862 | searching.nparam_max = INT_MAX; |
| 1863 | tline = expand_smacro(tline->next); |
| 1864 | skip_white_(tline); |
| 1865 | if (!tline) { |
| 1866 | } else if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1867 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1868 | "`%s' expects a parameter count or nothing", |
| 1869 | pp_directives[ct]); |
| 1870 | } else { |
| 1871 | searching.nparam_min = searching.nparam_max = |
| 1872 | readnum(tline->text, &j); |
| 1873 | if (j) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1874 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1875 | "unable to parse parameter count `%s'", |
| 1876 | tline->text); |
| 1877 | } |
| 1878 | if (tline && tok_is_(tline->next, "-")) { |
| 1879 | tline = tline->next->next; |
| 1880 | if (tok_is_(tline, "*")) |
| 1881 | searching.nparam_max = INT_MAX; |
| 1882 | else if (!tok_type_(tline, TOK_NUMBER)) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1883 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1884 | "`%s' expects a parameter count after `-'", |
| 1885 | pp_directives[ct]); |
| 1886 | else { |
| 1887 | searching.nparam_max = readnum(tline->text, &j); |
| 1888 | if (j) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1889 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1890 | "unable to parse parameter count `%s'", |
| 1891 | tline->text); |
| 1892 | if (searching.nparam_min > searching.nparam_max) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1893 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1894 | "minimum parameter count exceeds maximum"); |
| 1895 | } |
| 1896 | } |
| 1897 | if (tline && tok_is_(tline->next, "+")) { |
| 1898 | tline = tline->next; |
| 1899 | searching.plus = true; |
| 1900 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1901 | mmac = (MMacro *) hash_findix(&mmacros, searching.name); |
| 1902 | while (mmac) { |
| 1903 | if (!strcmp(mmac->name, searching.name) && |
| 1904 | (mmac->nparam_min <= searching.nparam_max |
| 1905 | || searching.plus) |
| 1906 | && (searching.nparam_min <= mmac->nparam_max |
| 1907 | || mmac->plus)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1908 | found = true; |
| 1909 | break; |
| 1910 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1911 | mmac = mmac->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1912 | } |
| 1913 | if (tline && tline->next) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1914 | nasm_error(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1915 | "trailing garbage after %%ifmacro ignored"); |
| 1916 | nasm_free(searching.name); |
| 1917 | j = found; |
| 1918 | break; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1919 | } |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 1920 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1921 | case PPC_IFID: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1922 | needtype = TOK_ID; |
| 1923 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1924 | case PPC_IFNUM: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1925 | needtype = TOK_NUMBER; |
| 1926 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1927 | case PPC_IFSTR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1928 | needtype = TOK_STRING; |
| 1929 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1930 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1931 | iftype: |
| 1932 | t = tline = expand_smacro(tline); |
H. Peter Anvin | d85d250 | 2008-05-04 17:53:31 -0700 | [diff] [blame] | 1933 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1934 | while (tok_type_(t, TOK_WHITESPACE) || |
| 1935 | (needtype == TOK_NUMBER && |
| 1936 | tok_type_(t, TOK_OTHER) && |
| 1937 | (t->text[0] == '-' || t->text[0] == '+') && |
| 1938 | !t->text[1])) |
| 1939 | t = t->next; |
H. Peter Anvin | d85d250 | 2008-05-04 17:53:31 -0700 | [diff] [blame] | 1940 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1941 | j = tok_type_(t, needtype); |
| 1942 | break; |
H. Peter Anvin | cbf768d | 2008-02-16 16:41:25 -0800 | [diff] [blame] | 1943 | |
| 1944 | case PPC_IFTOKEN: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1945 | t = tline = expand_smacro(tline); |
| 1946 | while (tok_type_(t, TOK_WHITESPACE)) |
| 1947 | t = t->next; |
H. Peter Anvin | cbf768d | 2008-02-16 16:41:25 -0800 | [diff] [blame] | 1948 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1949 | j = false; |
| 1950 | if (t) { |
| 1951 | t = t->next; /* Skip the actual token */ |
| 1952 | while (tok_type_(t, TOK_WHITESPACE)) |
| 1953 | t = t->next; |
| 1954 | j = !t; /* Should be nothing left */ |
| 1955 | } |
| 1956 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1957 | |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 1958 | case PPC_IFEMPTY: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1959 | t = tline = expand_smacro(tline); |
| 1960 | while (tok_type_(t, TOK_WHITESPACE)) |
| 1961 | t = t->next; |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 1962 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1963 | j = !t; /* Should be empty */ |
| 1964 | break; |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 1965 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1966 | case PPC_IF: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1967 | t = tline = expand_smacro(tline); |
| 1968 | tptr = &t; |
| 1969 | tokval.t_type = TOKEN_INVALID; |
| 1970 | evalresult = evaluate(ppscan, tptr, &tokval, |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1971 | NULL, pass | CRITICAL, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1972 | if (!evalresult) |
| 1973 | return -1; |
| 1974 | if (tokval.t_type) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1975 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1976 | "trailing garbage after expression ignored"); |
| 1977 | if (!is_simple(evalresult)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1978 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1979 | "non-constant value given to `%s'", pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1980 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1981 | } |
Chuck Crayne | 60ae75d | 2007-05-02 01:59:16 +0000 | [diff] [blame] | 1982 | j = reloc_value(evalresult) != 0; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1983 | break; |
H. Peter Anvin | 95e2882 | 2007-09-12 04:20:08 +0000 | [diff] [blame] | 1984 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1985 | default: |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1986 | nasm_error(ERR_FATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1987 | "preprocessor directive `%s' not yet implemented", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1988 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1989 | goto fail; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1990 | } |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1991 | |
| 1992 | free_tlist(origline); |
| 1993 | return j ^ PP_NEGATIVE(ct); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1994 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1995 | fail: |
| 1996 | free_tlist(origline); |
| 1997 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1998 | } |
| 1999 | |
| 2000 | /* |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2001 | * Common code for defining an smacro |
| 2002 | */ |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 2003 | static bool define_smacro(Context *ctx, const char *mname, bool casesense, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2004 | int nparam, Token *expansion) |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2005 | { |
| 2006 | SMacro *smac, **smhead; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2007 | struct hash_table *smtbl; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2008 | |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2009 | if (smacro_defined(ctx, mname, nparam, &smac, casesense)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2010 | if (!smac) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2011 | nasm_error(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2012 | "single-line macro `%s' defined both with and" |
| 2013 | " without parameters", mname); |
| 2014 | /* |
| 2015 | * Some instances of the old code considered this a failure, |
| 2016 | * some others didn't. What is the right thing to do here? |
| 2017 | */ |
| 2018 | free_tlist(expansion); |
| 2019 | return false; /* Failure */ |
| 2020 | } else { |
| 2021 | /* |
| 2022 | * We're redefining, so we have to take over an |
| 2023 | * existing SMacro structure. This means freeing |
| 2024 | * what was already in it. |
| 2025 | */ |
| 2026 | nasm_free(smac->name); |
| 2027 | free_tlist(smac->expansion); |
| 2028 | } |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2029 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2030 | smtbl = ctx ? &ctx->localmac : &smacros; |
| 2031 | smhead = (SMacro **) hash_findi_add(smtbl, mname); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2032 | smac = nasm_malloc(sizeof(SMacro)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2033 | smac->next = *smhead; |
| 2034 | *smhead = smac; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2035 | } |
| 2036 | smac->name = nasm_strdup(mname); |
| 2037 | smac->casesense = casesense; |
| 2038 | smac->nparam = nparam; |
| 2039 | smac->expansion = expansion; |
| 2040 | smac->in_progress = false; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2041 | return true; /* Success */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2042 | } |
| 2043 | |
| 2044 | /* |
| 2045 | * Undefine an smacro |
| 2046 | */ |
| 2047 | static void undef_smacro(Context *ctx, const char *mname) |
| 2048 | { |
| 2049 | SMacro **smhead, *s, **sp; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2050 | struct hash_table *smtbl; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2051 | |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2052 | smtbl = ctx ? &ctx->localmac : &smacros; |
| 2053 | smhead = (SMacro **)hash_findi(smtbl, mname, NULL); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2054 | |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2055 | if (smhead) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2056 | /* |
| 2057 | * We now have a macro name... go hunt for it. |
| 2058 | */ |
| 2059 | sp = smhead; |
| 2060 | while ((s = *sp) != NULL) { |
| 2061 | if (!mstrcmp(s->name, mname, s->casesense)) { |
| 2062 | *sp = s->next; |
| 2063 | nasm_free(s->name); |
| 2064 | free_tlist(s->expansion); |
| 2065 | nasm_free(s); |
| 2066 | } else { |
| 2067 | sp = &s->next; |
| 2068 | } |
| 2069 | } |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2070 | } |
| 2071 | } |
| 2072 | |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2073 | /* |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2074 | * Parse a mmacro specification. |
| 2075 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2076 | 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] | 2077 | { |
| 2078 | bool err; |
| 2079 | |
| 2080 | tline = tline->next; |
| 2081 | skip_white_(tline); |
| 2082 | tline = expand_id(tline); |
| 2083 | if (!tok_type_(tline, TOK_ID)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2084 | nasm_error(ERR_NONFATAL, "`%s' expects a macro name", directive); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2085 | return false; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2086 | } |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2087 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2088 | def->prev = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2089 | def->name = nasm_strdup(tline->text); |
| 2090 | def->plus = false; |
| 2091 | def->nolist = false; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2092 | def->in_progress = 0; |
| 2093 | def->rep_nest = NULL; |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2094 | def->nparam_min = 0; |
| 2095 | def->nparam_max = 0; |
| 2096 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2097 | tline = expand_smacro(tline->next); |
| 2098 | skip_white_(tline); |
| 2099 | if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2100 | nasm_error(ERR_NONFATAL, "`%s' expects a parameter count", directive); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2101 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2102 | def->nparam_min = def->nparam_max = |
| 2103 | readnum(tline->text, &err); |
| 2104 | if (err) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2105 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2106 | "unable to parse parameter count `%s'", tline->text); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2107 | } |
| 2108 | if (tline && tok_is_(tline->next, "-")) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2109 | tline = tline->next->next; |
| 2110 | if (tok_is_(tline, "*")) { |
| 2111 | def->nparam_max = INT_MAX; |
| 2112 | } else if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2113 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2114 | "`%s' expects a parameter count after `-'", directive); |
| 2115 | } else { |
| 2116 | def->nparam_max = readnum(tline->text, &err); |
| 2117 | if (err) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2118 | nasm_error(ERR_NONFATAL, "unable to parse parameter count `%s'", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2119 | tline->text); |
| 2120 | } |
| 2121 | if (def->nparam_min > def->nparam_max) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2122 | nasm_error(ERR_NONFATAL, "minimum parameter count exceeds maximum"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2123 | } |
| 2124 | } |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2125 | } |
| 2126 | if (tline && tok_is_(tline->next, "+")) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2127 | tline = tline->next; |
| 2128 | def->plus = true; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2129 | } |
| 2130 | if (tline && tok_type_(tline->next, TOK_ID) && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2131 | !nasm_stricmp(tline->next->text, ".nolist")) { |
| 2132 | tline = tline->next; |
| 2133 | def->nolist = true; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2134 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2135 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2136 | /* |
| 2137 | * Handle default parameters. |
| 2138 | */ |
| 2139 | if (tline && tline->next) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2140 | def->dlist = tline->next; |
| 2141 | tline->next = NULL; |
| 2142 | count_mmac_params(def->dlist, &def->ndefs, &def->defaults); |
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->dlist = NULL; |
| 2145 | def->defaults = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2146 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2147 | def->expansion = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2148 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2149 | if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2150 | !def->plus) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2151 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2152 | "too many default macro parameters"); |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2153 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2154 | return true; |
| 2155 | } |
| 2156 | |
| 2157 | |
| 2158 | /* |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2159 | * Decode a size directive |
| 2160 | */ |
| 2161 | static int parse_size(const char *str) { |
| 2162 | static const char *size_names[] = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2163 | { "byte", "dword", "oword", "qword", "tword", "word", "yword" }; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2164 | static const int sizes[] = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2165 | { 0, 1, 4, 16, 8, 10, 2, 32 }; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2166 | |
Cyrill Gorcunov | a731924 | 2010-06-03 22:04:36 +0400 | [diff] [blame] | 2167 | return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1]; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2168 | } |
| 2169 | |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2170 | /** |
| 2171 | * find and process preprocessor directive in passed line |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2172 | * Find out if a line contains a preprocessor directive, and deal |
| 2173 | * with it if so. |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2174 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2175 | * If a directive _is_ found, it is the responsibility of this routine |
| 2176 | * (and not the caller) to free_tlist() the line. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2177 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2178 | * @param tline a pointer to the current tokeninzed line linked list |
| 2179 | * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2180 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2181 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2182 | static int do_directive(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2183 | { |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2184 | enum preproc_token i; |
| 2185 | int j; |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 2186 | bool err; |
| 2187 | int nparam; |
| 2188 | bool nolist; |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 2189 | bool casesense; |
H. Peter Anvin | 8cfdb9d | 2007-09-14 18:36:01 -0700 | [diff] [blame] | 2190 | int k, m; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2191 | int offset; |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 2192 | char *p, *pp, *found_path; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 2193 | const char *mname; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2194 | Include *inc; |
| 2195 | Context *ctx; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2196 | Cond *cond; |
| 2197 | MMacro *mmac, **mmhead; |
Jin Kyu Song | c9486b9 | 2013-10-28 17:07:57 -0700 | [diff] [blame] | 2198 | Token *t = NULL, *tt, *param_start, *macro_start, *last, **tptr, *origline; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2199 | Line *l; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2200 | struct tokenval tokval; |
| 2201 | expr *evalresult; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2202 | MMacro *tmp_defining; /* Used when manipulating rep_nest */ |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 2203 | int64_t count; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 2204 | size_t len; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2205 | int severity; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2206 | |
| 2207 | origline = tline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2208 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2209 | skip_white_(tline); |
H. Peter Anvin | f2936d7 | 2008-06-04 15:11:23 -0700 | [diff] [blame] | 2210 | if (!tline || !tok_type_(tline, TOK_PREPROC_ID) || |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2211 | (tline->text[1] == '%' || tline->text[1] == '$' |
| 2212 | || tline->text[1] == '!')) |
| 2213 | return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2214 | |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2215 | i = pp_token_hash(tline->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2216 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2217 | /* |
| 2218 | * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO |
| 2219 | * since they are known to be buggy at moment, we need to fix them |
| 2220 | * in future release (2.09-2.10) |
| 2221 | */ |
Philipp Kloke | b432f57 | 2013-03-31 12:01:23 +0200 | [diff] [blame] | 2222 | if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2223 | nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'", |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2224 | tline->text); |
| 2225 | return NO_DIRECTIVE_FOUND; |
| 2226 | } |
| 2227 | |
| 2228 | /* |
| 2229 | * If we're in a non-emitting branch of a condition construct, |
| 2230 | * or walking to the end of an already terminated %rep block, |
| 2231 | * we should ignore all directives except for condition |
| 2232 | * directives. |
| 2233 | */ |
| 2234 | if (((istk->conds && !emitting(istk->conds->state)) || |
| 2235 | (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) { |
| 2236 | return NO_DIRECTIVE_FOUND; |
| 2237 | } |
| 2238 | |
| 2239 | /* |
| 2240 | * If we're defining a macro or reading a %rep block, we should |
| 2241 | * ignore all directives except for %macro/%imacro (which nest), |
| 2242 | * %endm/%endmacro, and (only if we're in a %rep block) %endrep. |
| 2243 | * If we're in a %rep block, another %rep nests, so should be let through. |
| 2244 | */ |
| 2245 | if (defining && i != PP_MACRO && i != PP_IMACRO && |
| 2246 | i != PP_RMACRO && i != PP_IRMACRO && |
| 2247 | i != PP_ENDMACRO && i != PP_ENDM && |
| 2248 | (defining->name || (i != PP_ENDREP && i != PP_REP))) { |
| 2249 | return NO_DIRECTIVE_FOUND; |
| 2250 | } |
| 2251 | |
| 2252 | if (defining) { |
| 2253 | if (i == PP_MACRO || i == PP_IMACRO || |
| 2254 | i == PP_RMACRO || i == PP_IRMACRO) { |
| 2255 | nested_mac_count++; |
| 2256 | return NO_DIRECTIVE_FOUND; |
| 2257 | } else if (nested_mac_count > 0) { |
| 2258 | if (i == PP_ENDMACRO) { |
| 2259 | nested_mac_count--; |
| 2260 | return NO_DIRECTIVE_FOUND; |
| 2261 | } |
| 2262 | } |
| 2263 | if (!defining->name) { |
| 2264 | if (i == PP_REP) { |
| 2265 | nested_rep_count++; |
| 2266 | return NO_DIRECTIVE_FOUND; |
| 2267 | } else if (nested_rep_count > 0) { |
| 2268 | if (i == PP_ENDREP) { |
| 2269 | nested_rep_count--; |
| 2270 | return NO_DIRECTIVE_FOUND; |
| 2271 | } |
| 2272 | } |
| 2273 | } |
| 2274 | } |
| 2275 | |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2276 | switch (i) { |
| 2277 | case PP_INVALID: |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2278 | nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'", |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2279 | tline->text); |
| 2280 | return NO_DIRECTIVE_FOUND; /* didn't get it */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2281 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2282 | case PP_STACKSIZE: |
| 2283 | /* Directive to tell NASM what the default stack size is. The |
| 2284 | * default is for a 16-bit stack, and this can be overriden with |
| 2285 | * %stacksize large. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2286 | */ |
| 2287 | tline = tline->next; |
| 2288 | if (tline && tline->type == TOK_WHITESPACE) |
| 2289 | tline = tline->next; |
| 2290 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2291 | nasm_error(ERR_NONFATAL, "`%%stacksize' missing size parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2292 | free_tlist(origline); |
| 2293 | return DIRECTIVE_FOUND; |
| 2294 | } |
| 2295 | if (nasm_stricmp(tline->text, "flat") == 0) { |
| 2296 | /* All subsequent ARG directives are for a 32-bit stack */ |
| 2297 | StackSize = 4; |
| 2298 | StackPointer = "ebp"; |
| 2299 | ArgOffset = 8; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2300 | LocalOffset = 0; |
Charles Crayne | 7eaf919 | 2007-11-08 22:11:14 -0800 | [diff] [blame] | 2301 | } else if (nasm_stricmp(tline->text, "flat64") == 0) { |
| 2302 | /* All subsequent ARG directives are for a 64-bit stack */ |
| 2303 | StackSize = 8; |
| 2304 | StackPointer = "rbp"; |
Per Jessen | 53252e0 | 2010-02-11 00:16:59 +0300 | [diff] [blame] | 2305 | ArgOffset = 16; |
Charles Crayne | 7eaf919 | 2007-11-08 22:11:14 -0800 | [diff] [blame] | 2306 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2307 | } else if (nasm_stricmp(tline->text, "large") == 0) { |
| 2308 | /* All subsequent ARG directives are for a 16-bit stack, |
| 2309 | * far function call. |
| 2310 | */ |
| 2311 | StackSize = 2; |
| 2312 | StackPointer = "bp"; |
| 2313 | ArgOffset = 4; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2314 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2315 | } else if (nasm_stricmp(tline->text, "small") == 0) { |
| 2316 | /* All subsequent ARG directives are for a 16-bit stack, |
| 2317 | * far function call. We don't support near functions. |
| 2318 | */ |
| 2319 | StackSize = 2; |
| 2320 | StackPointer = "bp"; |
| 2321 | ArgOffset = 6; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2322 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2323 | } else { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2324 | nasm_error(ERR_NONFATAL, "`%%stacksize' invalid size type"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2325 | free_tlist(origline); |
| 2326 | return DIRECTIVE_FOUND; |
| 2327 | } |
| 2328 | free_tlist(origline); |
| 2329 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2330 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2331 | case PP_ARG: |
| 2332 | /* TASM like ARG directive to define arguments to functions, in |
| 2333 | * the following form: |
| 2334 | * |
| 2335 | * ARG arg1:WORD, arg2:DWORD, arg4:QWORD |
| 2336 | */ |
| 2337 | offset = ArgOffset; |
| 2338 | do { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 2339 | char *arg, directive[256]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2340 | int size = StackSize; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2341 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2342 | /* Find the argument name */ |
| 2343 | tline = tline->next; |
| 2344 | if (tline && tline->type == TOK_WHITESPACE) |
| 2345 | tline = tline->next; |
| 2346 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2347 | nasm_error(ERR_NONFATAL, "`%%arg' missing argument parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2348 | free_tlist(origline); |
| 2349 | return DIRECTIVE_FOUND; |
| 2350 | } |
| 2351 | arg = tline->text; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2352 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2353 | /* Find the argument size type */ |
| 2354 | tline = tline->next; |
| 2355 | if (!tline || tline->type != TOK_OTHER |
| 2356 | || tline->text[0] != ':') { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2357 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2358 | "Syntax error processing `%%arg' directive"); |
| 2359 | free_tlist(origline); |
| 2360 | return DIRECTIVE_FOUND; |
| 2361 | } |
| 2362 | tline = tline->next; |
| 2363 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2364 | nasm_error(ERR_NONFATAL, "`%%arg' missing size type parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2365 | free_tlist(origline); |
| 2366 | return DIRECTIVE_FOUND; |
| 2367 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2368 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2369 | /* Allow macro expansion of type parameter */ |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2370 | tt = tokenize(tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2371 | tt = expand_smacro(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2372 | size = parse_size(tt->text); |
| 2373 | if (!size) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2374 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2375 | "Invalid size type for `%%arg' missing directive"); |
| 2376 | free_tlist(tt); |
| 2377 | free_tlist(origline); |
| 2378 | return DIRECTIVE_FOUND; |
| 2379 | } |
| 2380 | free_tlist(tt); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2381 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2382 | /* Round up to even stack slots */ |
| 2383 | size = ALIGN(size, StackSize); |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2384 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2385 | /* Now define the macro for the argument */ |
| 2386 | snprintf(directive, sizeof(directive), "%%define %s (%s+%d)", |
| 2387 | arg, StackPointer, offset); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2388 | do_directive(tokenize(directive)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2389 | offset += size; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2390 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2391 | /* Move to the next argument in the list */ |
| 2392 | tline = tline->next; |
| 2393 | if (tline && tline->type == TOK_WHITESPACE) |
| 2394 | tline = tline->next; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2395 | } while (tline && tline->type == TOK_OTHER && tline->text[0] == ','); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2396 | ArgOffset = offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2397 | free_tlist(origline); |
| 2398 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2399 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2400 | case PP_LOCAL: |
| 2401 | /* TASM like LOCAL directive to define local variables for a |
| 2402 | * function, in the following form: |
| 2403 | * |
| 2404 | * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize |
| 2405 | * |
| 2406 | * The '= LocalSize' at the end is ignored by NASM, but is |
| 2407 | * required by TASM to define the local parameter size (and used |
| 2408 | * by the TASM macro package). |
| 2409 | */ |
| 2410 | offset = LocalOffset; |
| 2411 | do { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 2412 | char *local, directive[256]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2413 | int size = StackSize; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2414 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2415 | /* Find the argument name */ |
| 2416 | tline = tline->next; |
| 2417 | if (tline && tline->type == TOK_WHITESPACE) |
| 2418 | tline = tline->next; |
| 2419 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2420 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2421 | "`%%local' missing argument parameter"); |
| 2422 | free_tlist(origline); |
| 2423 | return DIRECTIVE_FOUND; |
| 2424 | } |
| 2425 | local = tline->text; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2426 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2427 | /* Find the argument size type */ |
| 2428 | tline = tline->next; |
| 2429 | if (!tline || tline->type != TOK_OTHER |
| 2430 | || tline->text[0] != ':') { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2431 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2432 | "Syntax error processing `%%local' directive"); |
| 2433 | free_tlist(origline); |
| 2434 | return DIRECTIVE_FOUND; |
| 2435 | } |
| 2436 | tline = tline->next; |
| 2437 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2438 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2439 | "`%%local' missing size type parameter"); |
| 2440 | free_tlist(origline); |
| 2441 | return DIRECTIVE_FOUND; |
| 2442 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2443 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2444 | /* Allow macro expansion of type parameter */ |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2445 | tt = tokenize(tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2446 | tt = expand_smacro(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2447 | size = parse_size(tt->text); |
| 2448 | if (!size) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2449 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2450 | "Invalid size type for `%%local' missing directive"); |
| 2451 | free_tlist(tt); |
| 2452 | free_tlist(origline); |
| 2453 | return DIRECTIVE_FOUND; |
| 2454 | } |
| 2455 | free_tlist(tt); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2456 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2457 | /* Round up to even stack slots */ |
| 2458 | size = ALIGN(size, StackSize); |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2459 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2460 | offset += size; /* Negative offset, increment before */ |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2461 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2462 | /* Now define the macro for the argument */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2463 | snprintf(directive, sizeof(directive), "%%define %s (%s-%d)", |
| 2464 | local, StackPointer, offset); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2465 | do_directive(tokenize(directive)); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2466 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2467 | /* Now define the assign to setup the enter_c macro correctly */ |
| 2468 | snprintf(directive, sizeof(directive), |
| 2469 | "%%assign %%$localsize %%$localsize+%d", size); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2470 | do_directive(tokenize(directive)); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2471 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2472 | /* Move to the next argument in the list */ |
| 2473 | tline = tline->next; |
| 2474 | if (tline && tline->type == TOK_WHITESPACE) |
| 2475 | tline = tline->next; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2476 | } while (tline && tline->type == TOK_OTHER && tline->text[0] == ','); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2477 | LocalOffset = offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2478 | free_tlist(origline); |
| 2479 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2480 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2481 | case PP_CLEAR: |
| 2482 | if (tline->next) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2483 | nasm_error(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2484 | "trailing garbage after `%%clear' ignored"); |
| 2485 | free_macros(); |
| 2486 | init_macros(); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2487 | free_tlist(origline); |
| 2488 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2489 | |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2490 | case PP_DEPEND: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2491 | t = tline->next = expand_smacro(tline->next); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2492 | skip_white_(t); |
| 2493 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2494 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2495 | nasm_error(ERR_NONFATAL, "`%%depend' expects a file name"); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2496 | free_tlist(origline); |
| 2497 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2498 | } |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2499 | if (t->next) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2500 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2501 | "trailing garbage after `%%depend' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2502 | p = t->text; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2503 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2504 | nasm_unquote_cstr(p, i); |
| 2505 | if (dephead && !in_list(*dephead, p)) { |
| 2506 | StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next); |
| 2507 | sl->next = NULL; |
| 2508 | strcpy(sl->str, p); |
| 2509 | *deptail = sl; |
| 2510 | deptail = &sl->next; |
| 2511 | } |
| 2512 | free_tlist(origline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2513 | return DIRECTIVE_FOUND; |
| 2514 | |
| 2515 | case PP_INCLUDE: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2516 | t = tline->next = expand_smacro(tline->next); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2517 | skip_white_(t); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2518 | |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2519 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2520 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2521 | nasm_error(ERR_NONFATAL, "`%%include' expects a file name"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2522 | free_tlist(origline); |
| 2523 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2524 | } |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2525 | if (t->next) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2526 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2527 | "trailing garbage after `%%include' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2528 | p = t->text; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2529 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2530 | nasm_unquote_cstr(p, i); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2531 | inc = nasm_malloc(sizeof(Include)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2532 | inc->next = istk; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2533 | inc->conds = NULL; |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 2534 | found_path = NULL; |
| 2535 | inc->fp = inc_fopen(p, dephead, &deptail, &found_path, pass == 0, "r"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2536 | if (!inc->fp) { |
| 2537 | /* -MG given but file not found */ |
| 2538 | nasm_free(inc); |
| 2539 | } else { |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 2540 | inc->fname = src_set_fname(found_path ? found_path : p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2541 | inc->lineno = src_set_linnum(0); |
| 2542 | inc->lineinc = 1; |
| 2543 | inc->expansion = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2544 | inc->mstk = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2545 | istk = inc; |
H. Peter Anvin | 172b840 | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 2546 | lfmt->uplevel(LIST_INCLUDE); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2547 | } |
| 2548 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2549 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2550 | |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2551 | case PP_USE: |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2552 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2553 | static macros_t *use_pkg; |
| 2554 | const char *pkg_macro = NULL; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2555 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2556 | tline = tline->next; |
| 2557 | skip_white_(tline); |
| 2558 | tline = expand_id(tline); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2559 | |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2560 | if (!tline || (tline->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2561 | tline->type != TOK_INTERNAL_STRING && |
| 2562 | tline->type != TOK_ID)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2563 | nasm_error(ERR_NONFATAL, "`%%use' expects a package name"); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2564 | free_tlist(origline); |
| 2565 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2566 | } |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2567 | if (tline->next) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2568 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2569 | "trailing garbage after `%%use' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2570 | if (tline->type == TOK_STRING) |
| 2571 | nasm_unquote_cstr(tline->text, i); |
| 2572 | use_pkg = nasm_stdmac_find_package(tline->text); |
| 2573 | if (!use_pkg) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2574 | nasm_error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2575 | else |
| 2576 | 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] | 2577 | if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2578 | /* Not already included, go ahead and include it */ |
| 2579 | stdmacpos = use_pkg; |
| 2580 | } |
| 2581 | free_tlist(origline); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2582 | return DIRECTIVE_FOUND; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2583 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2584 | case PP_PUSH: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2585 | case PP_REPL: |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2586 | case PP_POP: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2587 | tline = tline->next; |
| 2588 | skip_white_(tline); |
| 2589 | tline = expand_id(tline); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2590 | if (tline) { |
| 2591 | if (!tok_type_(tline, TOK_ID)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2592 | nasm_error(ERR_NONFATAL, "`%s' expects a context identifier", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2593 | pp_directives[i]); |
| 2594 | free_tlist(origline); |
| 2595 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2596 | } |
| 2597 | if (tline->next) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2598 | nasm_error(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2599 | "trailing garbage after `%s' ignored", |
| 2600 | pp_directives[i]); |
| 2601 | p = nasm_strdup(tline->text); |
| 2602 | } else { |
| 2603 | p = NULL; /* Anonymous */ |
| 2604 | } |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2605 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2606 | if (i == PP_PUSH) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2607 | ctx = nasm_malloc(sizeof(Context)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2608 | ctx->next = cstk; |
| 2609 | hash_init(&ctx->localmac, HASH_SMALL); |
| 2610 | ctx->name = p; |
| 2611 | ctx->number = unique++; |
| 2612 | cstk = ctx; |
| 2613 | } else { |
| 2614 | /* %pop or %repl */ |
| 2615 | if (!cstk) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2616 | nasm_error(ERR_NONFATAL, "`%s': context stack is empty", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2617 | pp_directives[i]); |
| 2618 | } else if (i == PP_POP) { |
| 2619 | if (p && (!cstk->name || nasm_stricmp(p, cstk->name))) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2620 | nasm_error(ERR_NONFATAL, "`%%pop' in wrong context: %s, " |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2621 | "expected %s", |
| 2622 | cstk->name ? cstk->name : "anonymous", p); |
| 2623 | else |
| 2624 | ctx_pop(); |
| 2625 | } else { |
| 2626 | /* i == PP_REPL */ |
| 2627 | nasm_free(cstk->name); |
| 2628 | cstk->name = p; |
| 2629 | p = NULL; |
| 2630 | } |
| 2631 | nasm_free(p); |
| 2632 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2633 | free_tlist(origline); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2634 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2635 | case PP_FATAL: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2636 | severity = ERR_FATAL; |
| 2637 | goto issue_error; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2638 | case PP_ERROR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2639 | severity = ERR_NONFATAL; |
| 2640 | goto issue_error; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2641 | case PP_WARNING: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2642 | severity = ERR_WARNING|ERR_WARN_USER; |
| 2643 | goto issue_error; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2644 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2645 | issue_error: |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2646 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2647 | /* Only error out if this is the final pass */ |
| 2648 | if (pass != 2 && i != PP_FATAL) |
| 2649 | return DIRECTIVE_FOUND; |
| 2650 | |
| 2651 | tline->next = expand_smacro(tline->next); |
| 2652 | tline = tline->next; |
| 2653 | skip_white_(tline); |
| 2654 | t = tline ? tline->next : NULL; |
| 2655 | skip_white_(t); |
| 2656 | if (tok_type_(tline, TOK_STRING) && !t) { |
| 2657 | /* The line contains only a quoted string */ |
| 2658 | p = tline->text; |
| 2659 | nasm_unquote(p, NULL); /* Ignore NUL character truncation */ |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2660 | nasm_error(severity, "%s", p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2661 | } else { |
| 2662 | /* Not a quoted string, or more than a quoted string */ |
| 2663 | p = detoken(tline, false); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2664 | nasm_error(severity, "%s", p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2665 | nasm_free(p); |
| 2666 | } |
| 2667 | free_tlist(origline); |
| 2668 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2669 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2670 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2671 | CASE_PP_IF: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2672 | if (istk->conds && !emitting(istk->conds->state)) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2673 | j = COND_NEVER; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2674 | else { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2675 | j = if_condition(tline->next, i); |
| 2676 | tline->next = NULL; /* it got freed */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2677 | j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2678 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2679 | cond = nasm_malloc(sizeof(Cond)); |
| 2680 | cond->next = istk->conds; |
| 2681 | cond->state = j; |
| 2682 | istk->conds = cond; |
| 2683 | if(istk->mstk) |
| 2684 | istk->mstk->condcnt ++; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2685 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2686 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2687 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2688 | CASE_PP_ELIF: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2689 | if (!istk->conds) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2690 | nasm_error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2691 | switch(istk->conds->state) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2692 | case COND_IF_TRUE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2693 | istk->conds->state = COND_DONE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2694 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2695 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2696 | case COND_DONE: |
| 2697 | case COND_NEVER: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2698 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2699 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2700 | case COND_ELSE_TRUE: |
| 2701 | case COND_ELSE_FALSE: |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2702 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND, |
| 2703 | "`%%elif' after `%%else' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2704 | istk->conds->state = COND_NEVER; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2705 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2706 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2707 | case COND_IF_FALSE: |
| 2708 | /* |
| 2709 | * IMPORTANT: In the case of %if, we will already have |
| 2710 | * called expand_mmac_params(); however, if we're |
| 2711 | * processing an %elif we must have been in a |
| 2712 | * non-emitting mode, which would have inhibited |
| 2713 | * the normal invocation of expand_mmac_params(). |
| 2714 | * Therefore, we have to do it explicitly here. |
| 2715 | */ |
| 2716 | j = if_condition(expand_mmac_params(tline->next), i); |
| 2717 | tline->next = NULL; /* it got freed */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2718 | istk->conds->state = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2719 | j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; |
| 2720 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2721 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2722 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2723 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2724 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2725 | case PP_ELSE: |
| 2726 | if (tline->next) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2727 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND, |
| 2728 | "trailing garbage after `%%else' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2729 | if (!istk->conds) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2730 | nasm_fatal(0, "`%%else: no matching `%%if'"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2731 | switch(istk->conds->state) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2732 | case COND_IF_TRUE: |
| 2733 | case COND_DONE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2734 | istk->conds->state = COND_ELSE_FALSE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2735 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2736 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2737 | case COND_NEVER: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2738 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2739 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2740 | case COND_IF_FALSE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2741 | istk->conds->state = COND_ELSE_TRUE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2742 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2743 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2744 | case COND_ELSE_TRUE: |
| 2745 | case COND_ELSE_FALSE: |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2746 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2747 | "`%%else' after `%%else' ignored."); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2748 | istk->conds->state = COND_NEVER; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2749 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2750 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2751 | free_tlist(origline); |
| 2752 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2753 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2754 | case PP_ENDIF: |
| 2755 | if (tline->next) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2756 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND, |
| 2757 | "trailing garbage after `%%endif' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2758 | if (!istk->conds) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2759 | nasm_error(ERR_FATAL, "`%%endif': no matching `%%if'"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2760 | cond = istk->conds; |
| 2761 | istk->conds = cond->next; |
| 2762 | nasm_free(cond); |
| 2763 | if(istk->mstk) |
| 2764 | istk->mstk->condcnt --; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2765 | free_tlist(origline); |
| 2766 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2767 | |
H. Peter Anvin | db8f96e | 2009-07-15 09:07:29 -0400 | [diff] [blame] | 2768 | case PP_RMACRO: |
| 2769 | case PP_IRMACRO: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2770 | case PP_MACRO: |
| 2771 | case PP_IMACRO: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2772 | if (defining) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2773 | nasm_error(ERR_FATAL, "`%s': already defining a macro", |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2774 | pp_directives[i]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2775 | return DIRECTIVE_FOUND; |
| 2776 | } |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 2777 | defining = nasm_zalloc(sizeof(MMacro)); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2778 | defining->max_depth = |
| 2779 | (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0; |
| 2780 | defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO); |
| 2781 | if (!parse_mmacro_spec(tline, defining, pp_directives[i])) { |
| 2782 | nasm_free(defining); |
| 2783 | defining = NULL; |
| 2784 | return DIRECTIVE_FOUND; |
| 2785 | } |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2786 | |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 2787 | src_get(&defining->xline, &defining->fname); |
| 2788 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2789 | mmac = (MMacro *) hash_findix(&mmacros, defining->name); |
| 2790 | while (mmac) { |
| 2791 | if (!strcmp(mmac->name, defining->name) && |
| 2792 | (mmac->nparam_min <= defining->nparam_max |
| 2793 | || defining->plus) |
| 2794 | && (defining->nparam_min <= mmac->nparam_max |
| 2795 | || mmac->plus)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2796 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2797 | "redefining multi-line macro `%s'", defining->name); |
| 2798 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2799 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2800 | mmac = mmac->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2801 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2802 | free_tlist(origline); |
| 2803 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2804 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2805 | case PP_ENDM: |
| 2806 | case PP_ENDMACRO: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2807 | if (! (defining && defining->name)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2808 | nasm_error(ERR_NONFATAL, "`%s': not defining a macro", tline->text); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2809 | return DIRECTIVE_FOUND; |
| 2810 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2811 | mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name); |
| 2812 | defining->next = *mmhead; |
| 2813 | *mmhead = defining; |
| 2814 | defining = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2815 | free_tlist(origline); |
| 2816 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2817 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2818 | case PP_EXITMACRO: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2819 | /* |
| 2820 | * We must search along istk->expansion until we hit a |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2821 | * macro-end marker for a macro with a name. Then we |
| 2822 | * bypass all lines between exitmacro and endmacro. |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2823 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2824 | list_for_each(l, istk->expansion) |
| 2825 | if (l->finishes && l->finishes->name) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2826 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2827 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2828 | if (l) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2829 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2830 | * Remove all conditional entries relative to this |
| 2831 | * macro invocation. (safe to do in this context) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2832 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2833 | for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) { |
| 2834 | cond = istk->conds; |
| 2835 | istk->conds = cond->next; |
| 2836 | nasm_free(cond); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2837 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2838 | istk->expansion = l; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2839 | } else { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2840 | nasm_error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2841 | } |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 2842 | free_tlist(origline); |
| 2843 | return DIRECTIVE_FOUND; |
| 2844 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2845 | case PP_UNMACRO: |
| 2846 | case PP_UNIMACRO: |
| 2847 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2848 | MMacro **mmac_p; |
| 2849 | MMacro spec; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2850 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2851 | spec.casesense = (i == PP_UNMACRO); |
| 2852 | if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) { |
| 2853 | return DIRECTIVE_FOUND; |
| 2854 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2855 | mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL); |
| 2856 | while (mmac_p && *mmac_p) { |
| 2857 | mmac = *mmac_p; |
| 2858 | if (mmac->casesense == spec.casesense && |
| 2859 | !mstrcmp(mmac->name, spec.name, spec.casesense) && |
| 2860 | mmac->nparam_min == spec.nparam_min && |
| 2861 | mmac->nparam_max == spec.nparam_max && |
| 2862 | mmac->plus == spec.plus) { |
| 2863 | *mmac_p = mmac->next; |
| 2864 | free_mmacro(mmac); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2865 | } else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2866 | mmac_p = &mmac->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2867 | } |
| 2868 | } |
| 2869 | free_tlist(origline); |
| 2870 | free_tlist(spec.dlist); |
| 2871 | return DIRECTIVE_FOUND; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2872 | } |
| 2873 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2874 | case PP_ROTATE: |
| 2875 | if (tline->next && tline->next->type == TOK_WHITESPACE) |
| 2876 | tline = tline->next; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2877 | if (!tline->next) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2878 | free_tlist(origline); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2879 | nasm_error(ERR_NONFATAL, "`%%rotate' missing rotate count"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2880 | return DIRECTIVE_FOUND; |
| 2881 | } |
| 2882 | t = expand_smacro(tline->next); |
| 2883 | tline->next = NULL; |
| 2884 | free_tlist(origline); |
| 2885 | tline = t; |
| 2886 | tptr = &t; |
| 2887 | tokval.t_type = TOKEN_INVALID; |
| 2888 | evalresult = |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2889 | evaluate(ppscan, tptr, &tokval, NULL, pass, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2890 | free_tlist(tline); |
| 2891 | if (!evalresult) |
| 2892 | return DIRECTIVE_FOUND; |
| 2893 | if (tokval.t_type) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2894 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2895 | "trailing garbage after expression ignored"); |
| 2896 | if (!is_simple(evalresult)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2897 | nasm_error(ERR_NONFATAL, "non-constant value given to `%%rotate'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2898 | return DIRECTIVE_FOUND; |
| 2899 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2900 | mmac = istk->mstk; |
| 2901 | while (mmac && !mmac->name) /* avoid mistaking %reps for macros */ |
| 2902 | mmac = mmac->next_active; |
| 2903 | if (!mmac) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2904 | nasm_error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2905 | } else if (mmac->nparam == 0) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2906 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2907 | "`%%rotate' invoked within macro without parameters"); |
| 2908 | } else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2909 | int rotate = mmac->rotate + reloc_value(evalresult); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2910 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2911 | rotate %= (int)mmac->nparam; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2912 | if (rotate < 0) |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2913 | rotate += mmac->nparam; |
| 2914 | |
| 2915 | mmac->rotate = rotate; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2916 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2917 | return DIRECTIVE_FOUND; |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 2918 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2919 | case PP_REP: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 2920 | nolist = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2921 | do { |
| 2922 | tline = tline->next; |
| 2923 | } while (tok_type_(tline, TOK_WHITESPACE)); |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 2924 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2925 | if (tok_type_(tline, TOK_ID) && |
| 2926 | nasm_stricmp(tline->text, ".nolist") == 0) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 2927 | nolist = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2928 | do { |
| 2929 | tline = tline->next; |
| 2930 | } while (tok_type_(tline, TOK_WHITESPACE)); |
| 2931 | } |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 2932 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2933 | if (tline) { |
| 2934 | t = expand_smacro(tline); |
| 2935 | tptr = &t; |
| 2936 | tokval.t_type = TOKEN_INVALID; |
| 2937 | evalresult = |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2938 | evaluate(ppscan, tptr, &tokval, NULL, pass, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2939 | if (!evalresult) { |
| 2940 | free_tlist(origline); |
| 2941 | return DIRECTIVE_FOUND; |
| 2942 | } |
| 2943 | if (tokval.t_type) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2944 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2945 | "trailing garbage after expression ignored"); |
| 2946 | if (!is_simple(evalresult)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2947 | nasm_error(ERR_NONFATAL, "non-constant value given to `%%rep'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2948 | return DIRECTIVE_FOUND; |
| 2949 | } |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 2950 | count = reloc_value(evalresult); |
| 2951 | if (count >= REP_LIMIT) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2952 | nasm_error(ERR_NONFATAL, "`%%rep' value exceeds limit"); |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 2953 | count = 0; |
| 2954 | } else |
| 2955 | count++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2956 | } else { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2957 | nasm_error(ERR_NONFATAL, "`%%rep' expects a repeat count"); |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 2958 | count = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2959 | } |
| 2960 | free_tlist(origline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2961 | |
| 2962 | tmp_defining = defining; |
| 2963 | defining = nasm_malloc(sizeof(MMacro)); |
| 2964 | defining->prev = NULL; |
| 2965 | defining->name = NULL; /* flags this macro as a %rep block */ |
| 2966 | defining->casesense = false; |
| 2967 | defining->plus = false; |
| 2968 | defining->nolist = nolist; |
| 2969 | defining->in_progress = count; |
| 2970 | defining->max_depth = 0; |
| 2971 | defining->nparam_min = defining->nparam_max = 0; |
| 2972 | defining->defaults = NULL; |
| 2973 | defining->dlist = NULL; |
| 2974 | defining->expansion = NULL; |
| 2975 | defining->next_active = istk->mstk; |
| 2976 | defining->rep_nest = tmp_defining; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2977 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2978 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2979 | case PP_ENDREP: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2980 | if (!defining || defining->name) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2981 | nasm_error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2982 | return DIRECTIVE_FOUND; |
| 2983 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2984 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2985 | /* |
| 2986 | * Now we have a "macro" defined - although it has no name |
| 2987 | * and we won't be entering it in the hash tables - we must |
| 2988 | * push a macro-end marker for it on to istk->expansion. |
| 2989 | * After that, it will take care of propagating itself (a |
| 2990 | * macro-end marker line for a macro which is really a %rep |
| 2991 | * block will cause the macro to be re-expanded, complete |
| 2992 | * with another macro-end marker to ensure the process |
| 2993 | * continues) until the whole expansion is forcibly removed |
| 2994 | * from istk->expansion by a %exitrep. |
| 2995 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2996 | l = nasm_malloc(sizeof(Line)); |
| 2997 | l->next = istk->expansion; |
| 2998 | l->finishes = defining; |
| 2999 | l->first = NULL; |
| 3000 | istk->expansion = l; |
| 3001 | |
| 3002 | istk->mstk = defining; |
| 3003 | |
H. Peter Anvin | 172b840 | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 3004 | lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3005 | tmp_defining = defining; |
| 3006 | defining = defining->rep_nest; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3007 | free_tlist(origline); |
| 3008 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3009 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3010 | case PP_EXITREP: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3011 | /* |
| 3012 | * We must search along istk->expansion until we hit a |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3013 | * macro-end marker for a macro with no name. Then we set |
| 3014 | * its `in_progress' flag to 0. |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3015 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3016 | list_for_each(l, istk->expansion) |
| 3017 | if (l->finishes && !l->finishes->name) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3018 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3019 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3020 | if (l) |
| 3021 | l->finishes->in_progress = 1; |
| 3022 | else |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3023 | nasm_error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3024 | free_tlist(origline); |
| 3025 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3026 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3027 | case PP_XDEFINE: |
| 3028 | case PP_IXDEFINE: |
| 3029 | case PP_DEFINE: |
| 3030 | case PP_IDEFINE: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3031 | casesense = (i == PP_DEFINE || i == PP_XDEFINE); |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3032 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3033 | tline = tline->next; |
| 3034 | skip_white_(tline); |
| 3035 | tline = expand_id(tline); |
| 3036 | if (!tline || (tline->type != TOK_ID && |
| 3037 | (tline->type != TOK_PREPROC_ID || |
| 3038 | tline->text[1] != '$'))) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3039 | nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3040 | pp_directives[i]); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3041 | free_tlist(origline); |
| 3042 | return DIRECTIVE_FOUND; |
| 3043 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3044 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3045 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3046 | last = tline; |
| 3047 | param_start = tline = tline->next; |
| 3048 | nparam = 0; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3049 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3050 | /* Expand the macro definition now for %xdefine and %ixdefine */ |
| 3051 | if ((i == PP_XDEFINE) || (i == PP_IXDEFINE)) |
| 3052 | tline = expand_smacro(tline); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3053 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3054 | if (tok_is_(tline, "(")) { |
| 3055 | /* |
| 3056 | * This macro has parameters. |
| 3057 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3058 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3059 | tline = tline->next; |
| 3060 | while (1) { |
| 3061 | skip_white_(tline); |
| 3062 | if (!tline) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3063 | nasm_error(ERR_NONFATAL, "parameter identifier expected"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3064 | free_tlist(origline); |
| 3065 | return DIRECTIVE_FOUND; |
| 3066 | } |
| 3067 | if (tline->type != TOK_ID) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3068 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3069 | "`%s': parameter identifier expected", |
| 3070 | tline->text); |
| 3071 | free_tlist(origline); |
| 3072 | return DIRECTIVE_FOUND; |
| 3073 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3074 | tline->type = TOK_SMAC_PARAM + nparam++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3075 | tline = tline->next; |
| 3076 | skip_white_(tline); |
| 3077 | if (tok_is_(tline, ",")) { |
| 3078 | tline = tline->next; |
H. Peter Anvin | ca348b6 | 2008-07-23 10:49:26 -0400 | [diff] [blame] | 3079 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3080 | if (!tok_is_(tline, ")")) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3081 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3082 | "`)' expected to terminate macro template"); |
| 3083 | free_tlist(origline); |
| 3084 | return DIRECTIVE_FOUND; |
| 3085 | } |
| 3086 | break; |
| 3087 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3088 | } |
| 3089 | last = tline; |
| 3090 | tline = tline->next; |
| 3091 | } |
| 3092 | if (tok_type_(tline, TOK_WHITESPACE)) |
| 3093 | last = tline, tline = tline->next; |
| 3094 | macro_start = NULL; |
| 3095 | last->next = NULL; |
| 3096 | t = tline; |
| 3097 | while (t) { |
| 3098 | if (t->type == TOK_ID) { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3099 | list_for_each(tt, param_start) |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3100 | if (tt->type >= TOK_SMAC_PARAM && |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3101 | !strcmp(tt->text, t->text)) |
| 3102 | t->type = tt->type; |
| 3103 | } |
| 3104 | tt = t->next; |
| 3105 | t->next = macro_start; |
| 3106 | macro_start = t; |
| 3107 | t = tt; |
| 3108 | } |
| 3109 | /* |
| 3110 | * Good. We now have a macro name, a parameter count, and a |
| 3111 | * token list (in reverse order) for an expansion. We ought |
| 3112 | * to be OK just to create an SMacro, store it, and let |
| 3113 | * free_tlist have the rest of the line (which we have |
| 3114 | * carefully re-terminated after chopping off the expansion |
| 3115 | * from the end). |
| 3116 | */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 3117 | define_smacro(ctx, mname, casesense, nparam, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3118 | free_tlist(origline); |
| 3119 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3120 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3121 | case PP_UNDEF: |
| 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 | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3128 | nasm_error(ERR_NONFATAL, "`%%undef' expects a macro identifier"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3129 | free_tlist(origline); |
| 3130 | return DIRECTIVE_FOUND; |
| 3131 | } |
| 3132 | if (tline->next) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3133 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3134 | "trailing garbage after macro name ignored"); |
| 3135 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3136 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3137 | /* Find the context that symbol belongs to */ |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3138 | ctx = get_ctx(tline->text, &mname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3139 | undef_smacro(ctx, mname); |
| 3140 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3141 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3142 | |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3143 | case PP_DEFSTR: |
| 3144 | case PP_IDEFSTR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3145 | casesense = (i == PP_DEFSTR); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3146 | |
| 3147 | tline = tline->next; |
| 3148 | skip_white_(tline); |
| 3149 | tline = expand_id(tline); |
| 3150 | if (!tline || (tline->type != TOK_ID && |
| 3151 | (tline->type != TOK_PREPROC_ID || |
| 3152 | tline->text[1] != '$'))) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3153 | nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3154 | pp_directives[i]); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3155 | free_tlist(origline); |
| 3156 | return DIRECTIVE_FOUND; |
| 3157 | } |
| 3158 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3159 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3160 | last = tline; |
| 3161 | tline = expand_smacro(tline->next); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3162 | last->next = NULL; |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3163 | |
| 3164 | while (tok_type_(tline, TOK_WHITESPACE)) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3165 | tline = delete_Token(tline); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3166 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3167 | p = detoken(tline, false); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3168 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3169 | macro_start->next = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3170 | macro_start->text = nasm_quote(p, strlen(p)); |
| 3171 | macro_start->type = TOK_STRING; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3172 | macro_start->a.mac = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3173 | nasm_free(p); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3174 | |
| 3175 | /* |
| 3176 | * We now have a macro name, an implicit parameter count of |
| 3177 | * zero, and a string token to use as an expansion. Create |
| 3178 | * and store an SMacro. |
| 3179 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3180 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3181 | free_tlist(origline); |
| 3182 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3183 | |
H. Peter Anvin | 2f55bda | 2009-07-14 15:04:04 -0400 | [diff] [blame] | 3184 | case PP_DEFTOK: |
| 3185 | case PP_IDEFTOK: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3186 | casesense = (i == PP_DEFTOK); |
| 3187 | |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3188 | tline = tline->next; |
| 3189 | skip_white_(tline); |
| 3190 | tline = expand_id(tline); |
| 3191 | if (!tline || (tline->type != TOK_ID && |
| 3192 | (tline->type != TOK_PREPROC_ID || |
| 3193 | tline->text[1] != '$'))) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3194 | nasm_error(ERR_NONFATAL, |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3195 | "`%s' expects a macro identifier as first parameter", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3196 | pp_directives[i]); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3197 | free_tlist(origline); |
| 3198 | return DIRECTIVE_FOUND; |
| 3199 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3200 | ctx = get_ctx(tline->text, &mname); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3201 | last = tline; |
| 3202 | tline = expand_smacro(tline->next); |
| 3203 | last->next = NULL; |
| 3204 | |
| 3205 | t = tline; |
| 3206 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3207 | t = t->next; |
| 3208 | /* t should now point to the string */ |
Cyrill Gorcunov | 6908e58 | 2010-09-06 19:36:15 +0400 | [diff] [blame] | 3209 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3210 | nasm_error(ERR_NONFATAL, |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3211 | "`%s` requires string as second parameter", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3212 | pp_directives[i]); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3213 | free_tlist(tline); |
| 3214 | free_tlist(origline); |
| 3215 | return DIRECTIVE_FOUND; |
| 3216 | } |
| 3217 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 3218 | /* |
| 3219 | * Convert the string to a token stream. Note that smacros |
| 3220 | * are stored with the token stream reversed, so we have to |
| 3221 | * reverse the output of tokenize(). |
| 3222 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3223 | nasm_unquote_cstr(t->text, i); |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 3224 | macro_start = reverse_tokens(tokenize(t->text)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3225 | |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3226 | /* |
| 3227 | * We now have a macro name, an implicit parameter count of |
| 3228 | * zero, and a numeric token to use as an expansion. Create |
| 3229 | * and store an SMacro. |
| 3230 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3231 | define_smacro(ctx, mname, casesense, 0, macro_start); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3232 | free_tlist(tline); |
| 3233 | free_tlist(origline); |
| 3234 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3235 | |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3236 | case PP_PATHSEARCH: |
| 3237 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3238 | FILE *fp; |
| 3239 | StrList *xsl = NULL; |
| 3240 | StrList **xst = &xsl; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3241 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3242 | casesense = true; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3243 | |
| 3244 | tline = tline->next; |
| 3245 | skip_white_(tline); |
| 3246 | tline = expand_id(tline); |
| 3247 | if (!tline || (tline->type != TOK_ID && |
| 3248 | (tline->type != TOK_PREPROC_ID || |
| 3249 | tline->text[1] != '$'))) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3250 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3251 | "`%%pathsearch' expects a macro identifier as first parameter"); |
| 3252 | free_tlist(origline); |
| 3253 | return DIRECTIVE_FOUND; |
| 3254 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3255 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3256 | last = tline; |
| 3257 | tline = expand_smacro(tline->next); |
| 3258 | last->next = NULL; |
| 3259 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3260 | t = tline; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3261 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3262 | t = t->next; |
| 3263 | |
| 3264 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3265 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3266 | nasm_error(ERR_NONFATAL, "`%%pathsearch' expects a file name"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3267 | free_tlist(tline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3268 | free_tlist(origline); |
| 3269 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 3270 | } |
| 3271 | if (t->next) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3272 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3273 | "trailing garbage after `%%pathsearch' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3274 | p = t->text; |
H. Peter Anvin | 427cc91 | 2008-06-01 21:43:03 -0700 | [diff] [blame] | 3275 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3276 | nasm_unquote(p, NULL); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3277 | |
Jim Kukunas | 65a8afc | 2016-06-13 16:00:42 -0400 | [diff] [blame] | 3278 | fp = inc_fopen(p, &xsl, &xst, NULL, true, "r"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3279 | if (fp) { |
| 3280 | p = xsl->str; |
| 3281 | fclose(fp); /* Don't actually care about the file */ |
| 3282 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3283 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3284 | macro_start->next = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3285 | macro_start->text = nasm_quote(p, strlen(p)); |
| 3286 | macro_start->type = TOK_STRING; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3287 | macro_start->a.mac = NULL; |
| 3288 | if (xsl) |
| 3289 | nasm_free(xsl); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3290 | |
| 3291 | /* |
| 3292 | * We now have a macro name, an implicit parameter count of |
| 3293 | * zero, and a string token to use as an expansion. Create |
| 3294 | * and store an SMacro. |
| 3295 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3296 | define_smacro(ctx, mname, casesense, 0, macro_start); |
| 3297 | free_tlist(tline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3298 | free_tlist(origline); |
| 3299 | return DIRECTIVE_FOUND; |
| 3300 | } |
| 3301 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3302 | case PP_STRLEN: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3303 | casesense = true; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 3304 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3305 | tline = tline->next; |
| 3306 | skip_white_(tline); |
| 3307 | tline = expand_id(tline); |
| 3308 | if (!tline || (tline->type != TOK_ID && |
| 3309 | (tline->type != TOK_PREPROC_ID || |
| 3310 | tline->text[1] != '$'))) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3311 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3312 | "`%%strlen' expects a macro identifier as first parameter"); |
| 3313 | free_tlist(origline); |
| 3314 | return DIRECTIVE_FOUND; |
| 3315 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3316 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3317 | last = tline; |
| 3318 | tline = expand_smacro(tline->next); |
| 3319 | last->next = NULL; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3320 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3321 | t = tline; |
| 3322 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3323 | t = t->next; |
| 3324 | /* t should now point to the string */ |
Cyrill Gorcunov | 4e1d5ab | 2010-07-23 18:51:51 +0400 | [diff] [blame] | 3325 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3326 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3327 | "`%%strlen` requires string as second parameter"); |
| 3328 | free_tlist(tline); |
| 3329 | free_tlist(origline); |
| 3330 | return DIRECTIVE_FOUND; |
| 3331 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3332 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3333 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3334 | macro_start->next = NULL; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 3335 | make_tok_num(macro_start, nasm_unquote(t->text, NULL)); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3336 | macro_start->a.mac = NULL; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3337 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3338 | /* |
| 3339 | * We now have a macro name, an implicit parameter count of |
| 3340 | * zero, and a numeric token to use as an expansion. Create |
| 3341 | * and store an SMacro. |
| 3342 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3343 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3344 | free_tlist(tline); |
| 3345 | free_tlist(origline); |
| 3346 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3347 | |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3348 | case PP_STRCAT: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3349 | casesense = true; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3350 | |
| 3351 | tline = tline->next; |
| 3352 | skip_white_(tline); |
| 3353 | tline = expand_id(tline); |
| 3354 | if (!tline || (tline->type != TOK_ID && |
| 3355 | (tline->type != TOK_PREPROC_ID || |
| 3356 | tline->text[1] != '$'))) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3357 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3358 | "`%%strcat' expects a macro identifier as first parameter"); |
| 3359 | free_tlist(origline); |
| 3360 | return DIRECTIVE_FOUND; |
| 3361 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3362 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3363 | last = tline; |
| 3364 | tline = expand_smacro(tline->next); |
| 3365 | last->next = NULL; |
| 3366 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3367 | len = 0; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3368 | list_for_each(t, tline) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3369 | switch (t->type) { |
| 3370 | case TOK_WHITESPACE: |
| 3371 | break; |
| 3372 | case TOK_STRING: |
| 3373 | len += t->a.len = nasm_unquote(t->text, NULL); |
| 3374 | break; |
| 3375 | case TOK_OTHER: |
| 3376 | if (!strcmp(t->text, ",")) /* permit comma separators */ |
| 3377 | break; |
| 3378 | /* else fall through */ |
| 3379 | default: |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3380 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3381 | "non-string passed to `%%strcat' (%d)", t->type); |
| 3382 | free_tlist(tline); |
| 3383 | free_tlist(origline); |
| 3384 | return DIRECTIVE_FOUND; |
| 3385 | } |
| 3386 | } |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3387 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3388 | p = pp = nasm_malloc(len); |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3389 | list_for_each(t, tline) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3390 | if (t->type == TOK_STRING) { |
| 3391 | memcpy(p, t->text, t->a.len); |
| 3392 | p += t->a.len; |
| 3393 | } |
| 3394 | } |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3395 | |
| 3396 | /* |
| 3397 | * We now have a macro name, an implicit parameter count of |
| 3398 | * zero, and a numeric token to use as an expansion. Create |
| 3399 | * and store an SMacro. |
| 3400 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3401 | macro_start = new_Token(NULL, TOK_STRING, NULL, 0); |
| 3402 | macro_start->text = nasm_quote(pp, len); |
| 3403 | nasm_free(pp); |
| 3404 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3405 | free_tlist(tline); |
| 3406 | free_tlist(origline); |
| 3407 | return DIRECTIVE_FOUND; |
| 3408 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3409 | case PP_SUBSTR: |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3410 | { |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3411 | int64_t start, count; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3412 | size_t len; |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 3413 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3414 | casesense = true; |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3415 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3416 | tline = tline->next; |
| 3417 | skip_white_(tline); |
| 3418 | tline = expand_id(tline); |
| 3419 | if (!tline || (tline->type != TOK_ID && |
| 3420 | (tline->type != TOK_PREPROC_ID || |
| 3421 | tline->text[1] != '$'))) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3422 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3423 | "`%%substr' expects a macro identifier as first parameter"); |
| 3424 | free_tlist(origline); |
| 3425 | return DIRECTIVE_FOUND; |
| 3426 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3427 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3428 | last = tline; |
| 3429 | tline = expand_smacro(tline->next); |
| 3430 | last->next = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3431 | |
Cyrill Gorcunov | 35519d6 | 2010-09-06 23:49:52 +0400 | [diff] [blame] | 3432 | if (tline) /* skip expanded id */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3433 | t = tline->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3434 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3435 | t = t->next; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3436 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3437 | /* t should now point to the string */ |
Cyrill Gorcunov | 35519d6 | 2010-09-06 23:49:52 +0400 | [diff] [blame] | 3438 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3439 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3440 | "`%%substr` requires string as second parameter"); |
| 3441 | free_tlist(tline); |
| 3442 | free_tlist(origline); |
| 3443 | return DIRECTIVE_FOUND; |
| 3444 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3445 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3446 | tt = t->next; |
| 3447 | tptr = &tt; |
| 3448 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3449 | evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3450 | if (!evalresult) { |
| 3451 | free_tlist(tline); |
| 3452 | free_tlist(origline); |
| 3453 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3454 | } else if (!is_simple(evalresult)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3455 | nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3456 | free_tlist(tline); |
| 3457 | free_tlist(origline); |
| 3458 | return DIRECTIVE_FOUND; |
| 3459 | } |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3460 | start = evalresult->value - 1; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3461 | |
| 3462 | while (tok_type_(tt, TOK_WHITESPACE)) |
| 3463 | tt = tt->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3464 | if (!tt) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3465 | count = 1; /* Backwards compatibility: one character */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3466 | } else { |
| 3467 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3468 | evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3469 | if (!evalresult) { |
| 3470 | free_tlist(tline); |
| 3471 | free_tlist(origline); |
| 3472 | return DIRECTIVE_FOUND; |
| 3473 | } else if (!is_simple(evalresult)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3474 | nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3475 | free_tlist(tline); |
| 3476 | free_tlist(origline); |
| 3477 | return DIRECTIVE_FOUND; |
| 3478 | } |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3479 | count = evalresult->value; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3480 | } |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3481 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3482 | len = nasm_unquote(t->text, NULL); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3483 | |
Cyrill Gorcunov | cff031e | 2010-09-07 20:31:11 +0400 | [diff] [blame] | 3484 | /* make start and count being in range */ |
| 3485 | if (start < 0) |
| 3486 | start = 0; |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3487 | if (count < 0) |
| 3488 | count = len + count + 1 - start; |
| 3489 | if (start + count > (int64_t)len) |
Cyrill Gorcunov | cff031e | 2010-09-07 20:31:11 +0400 | [diff] [blame] | 3490 | count = len - start; |
| 3491 | if (!len || count < 0 || start >=(int64_t)len) |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3492 | start = -1, count = 0; /* empty string */ |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3493 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3494 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3495 | macro_start->next = NULL; |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3496 | macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3497 | macro_start->type = TOK_STRING; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3498 | macro_start->a.mac = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3499 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3500 | /* |
| 3501 | * We now have a macro name, an implicit parameter count of |
| 3502 | * zero, and a numeric token to use as an expansion. Create |
| 3503 | * and store an SMacro. |
| 3504 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3505 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3506 | free_tlist(tline); |
| 3507 | free_tlist(origline); |
| 3508 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3509 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3510 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3511 | case PP_ASSIGN: |
| 3512 | case PP_IASSIGN: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3513 | casesense = (i == PP_ASSIGN); |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3514 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3515 | tline = tline->next; |
| 3516 | skip_white_(tline); |
| 3517 | tline = expand_id(tline); |
| 3518 | if (!tline || (tline->type != TOK_ID && |
| 3519 | (tline->type != TOK_PREPROC_ID || |
| 3520 | tline->text[1] != '$'))) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3521 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3522 | "`%%%sassign' expects a macro identifier", |
| 3523 | (i == PP_IASSIGN ? "i" : "")); |
| 3524 | free_tlist(origline); |
| 3525 | return DIRECTIVE_FOUND; |
| 3526 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3527 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3528 | last = tline; |
| 3529 | tline = expand_smacro(tline->next); |
| 3530 | last->next = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3531 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3532 | t = tline; |
| 3533 | tptr = &t; |
| 3534 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3535 | evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3536 | free_tlist(tline); |
| 3537 | if (!evalresult) { |
| 3538 | free_tlist(origline); |
| 3539 | return DIRECTIVE_FOUND; |
| 3540 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3541 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3542 | if (tokval.t_type) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3543 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3544 | "trailing garbage after expression ignored"); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3545 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3546 | if (!is_simple(evalresult)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3547 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3548 | "non-constant value given to `%%%sassign'", |
| 3549 | (i == PP_IASSIGN ? "i" : "")); |
| 3550 | free_tlist(origline); |
| 3551 | return DIRECTIVE_FOUND; |
| 3552 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3553 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3554 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3555 | macro_start->next = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3556 | make_tok_num(macro_start, reloc_value(evalresult)); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3557 | macro_start->a.mac = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3558 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3559 | /* |
| 3560 | * We now have a macro name, an implicit parameter count of |
| 3561 | * zero, and a numeric token to use as an expansion. Create |
| 3562 | * and store an SMacro. |
| 3563 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3564 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3565 | free_tlist(origline); |
| 3566 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3567 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3568 | case PP_LINE: |
| 3569 | /* |
| 3570 | * Syntax is `%line nnn[+mmm] [filename]' |
| 3571 | */ |
| 3572 | tline = tline->next; |
| 3573 | skip_white_(tline); |
| 3574 | if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3575 | nasm_error(ERR_NONFATAL, "`%%line' expects line number"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3576 | free_tlist(origline); |
| 3577 | return DIRECTIVE_FOUND; |
| 3578 | } |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 3579 | k = readnum(tline->text, &err); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3580 | m = 1; |
| 3581 | tline = tline->next; |
| 3582 | if (tok_is_(tline, "+")) { |
| 3583 | tline = tline->next; |
| 3584 | if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3585 | nasm_error(ERR_NONFATAL, "`%%line' expects line increment"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3586 | free_tlist(origline); |
| 3587 | return DIRECTIVE_FOUND; |
| 3588 | } |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 3589 | m = readnum(tline->text, &err); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3590 | tline = tline->next; |
| 3591 | } |
| 3592 | skip_white_(tline); |
| 3593 | src_set_linnum(k); |
| 3594 | istk->lineinc = m; |
| 3595 | if (tline) { |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 3596 | char *fname = detoken(tline, false); |
| 3597 | src_set_fname(fname); |
| 3598 | nasm_free(fname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3599 | } |
| 3600 | free_tlist(origline); |
| 3601 | return DIRECTIVE_FOUND; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3602 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3603 | default: |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3604 | nasm_error(ERR_FATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3605 | "preprocessor directive `%s' not yet implemented", |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 3606 | pp_directives[i]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3607 | return DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3608 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3609 | } |
| 3610 | |
| 3611 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3612 | * Ensure that a macro parameter contains a condition code and |
| 3613 | * nothing else. Return the condition code index if so, or -1 |
| 3614 | * otherwise. |
| 3615 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3616 | static int find_cc(Token * t) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3617 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3618 | Token *tt; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3619 | |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3620 | if (!t) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3621 | return -1; /* Probably a %+ without a space */ |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3622 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3623 | skip_white_(t); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3624 | if (t->type != TOK_ID) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3625 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3626 | tt = t->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3627 | skip_white_(tt); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3628 | if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ","))) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3629 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3630 | |
Cyrill Gorcunov | 1945639 | 2012-05-02 00:18:56 +0400 | [diff] [blame] | 3631 | return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3632 | } |
| 3633 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3634 | /* |
| 3635 | * This routines walks over tokens strem and hadnles tokens |
| 3636 | * pasting, if @handle_explicit passed then explicit pasting |
| 3637 | * term is handled, otherwise -- implicit pastings only. |
| 3638 | */ |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 3639 | static bool paste_tokens(Token **head, const struct tokseq_match *m, |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3640 | size_t mnum, bool handle_explicit) |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3641 | { |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3642 | Token *tok, *next, **prev_next, **prev_nonspace; |
| 3643 | bool pasted = false; |
| 3644 | char *buf, *p; |
| 3645 | size_t len, i; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3646 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3647 | /* |
| 3648 | * The last token before pasting. We need it |
| 3649 | * to be able to connect new handled tokens. |
| 3650 | * In other words if there were a tokens stream |
| 3651 | * |
| 3652 | * A -> B -> C -> D |
| 3653 | * |
| 3654 | * and we've joined tokens B and C, the resulting |
| 3655 | * stream should be |
| 3656 | * |
| 3657 | * A -> BC -> D |
| 3658 | */ |
| 3659 | tok = *head; |
| 3660 | prev_next = NULL; |
| 3661 | |
| 3662 | if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE)) |
| 3663 | prev_nonspace = head; |
| 3664 | else |
| 3665 | prev_nonspace = NULL; |
| 3666 | |
| 3667 | while (tok && (next = tok->next)) { |
| 3668 | |
| 3669 | switch (tok->type) { |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3670 | case TOK_WHITESPACE: |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3671 | /* Zap redundant whitespaces */ |
| 3672 | while (tok_type_(next, TOK_WHITESPACE)) |
| 3673 | next = delete_Token(next); |
| 3674 | tok->next = next; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3675 | break; |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3676 | |
| 3677 | case TOK_PASTE: |
| 3678 | /* Explicit pasting */ |
| 3679 | if (!handle_explicit) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3680 | break; |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3681 | next = delete_Token(tok); |
| 3682 | |
| 3683 | while (tok_type_(next, TOK_WHITESPACE)) |
| 3684 | next = delete_Token(next); |
| 3685 | |
| 3686 | if (!pasted) |
| 3687 | pasted = true; |
| 3688 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3689 | /* Left pasting token is start of line */ |
| 3690 | if (!prev_nonspace) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3691 | nasm_error(ERR_FATAL, "No lvalue found on pasting"); |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3692 | |
Cyrill Gorcunov | 8b5c9fb | 2013-02-04 01:24:54 +0400 | [diff] [blame] | 3693 | /* |
| 3694 | * No ending token, this might happen in two |
| 3695 | * cases |
| 3696 | * |
| 3697 | * 1) There indeed no right token at all |
| 3698 | * 2) There is a bare "%define ID" statement, |
| 3699 | * and @ID does expand to whitespace. |
| 3700 | * |
| 3701 | * So technically we need to do a grammar analysis |
| 3702 | * in another stage of parsing, but for now lets don't |
| 3703 | * change the behaviour people used to. Simply allow |
| 3704 | * whitespace after paste token. |
| 3705 | */ |
| 3706 | if (!next) { |
| 3707 | /* |
| 3708 | * Zap ending space tokens and that's all. |
| 3709 | */ |
| 3710 | tok = (*prev_nonspace)->next; |
| 3711 | while (tok_type_(tok, TOK_WHITESPACE)) |
| 3712 | tok = delete_Token(tok); |
| 3713 | tok = *prev_nonspace; |
| 3714 | tok->next = NULL; |
| 3715 | break; |
| 3716 | } |
| 3717 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3718 | tok = *prev_nonspace; |
| 3719 | while (tok_type_(tok, TOK_WHITESPACE)) |
| 3720 | tok = delete_Token(tok); |
| 3721 | len = strlen(tok->text); |
| 3722 | len += strlen(next->text); |
| 3723 | |
| 3724 | p = buf = nasm_malloc(len + 1); |
| 3725 | strcpy(p, tok->text); |
| 3726 | p = strchr(p, '\0'); |
| 3727 | strcpy(p, next->text); |
| 3728 | |
| 3729 | delete_Token(tok); |
| 3730 | |
| 3731 | tok = tokenize(buf); |
| 3732 | nasm_free(buf); |
| 3733 | |
| 3734 | *prev_nonspace = tok; |
| 3735 | while (tok && tok->next) |
| 3736 | tok = tok->next; |
| 3737 | |
| 3738 | tok->next = delete_Token(next); |
| 3739 | |
| 3740 | /* Restart from pasted tokens head */ |
| 3741 | tok = *prev_nonspace; |
| 3742 | break; |
| 3743 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3744 | default: |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3745 | /* implicit pasting */ |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 3746 | for (i = 0; i < mnum; i++) { |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3747 | if (!(PP_CONCAT_MATCH(tok, m[i].mask_head))) |
| 3748 | continue; |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 3749 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3750 | len = 0; |
| 3751 | while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) { |
| 3752 | len += strlen(next->text); |
| 3753 | next = next->next; |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 3754 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3755 | |
| 3756 | /* No match */ |
| 3757 | if (tok == next) |
| 3758 | break; |
| 3759 | |
| 3760 | len += strlen(tok->text); |
| 3761 | p = buf = nasm_malloc(len + 1); |
| 3762 | |
| 3763 | while (tok != next) { |
| 3764 | strcpy(p, tok->text); |
| 3765 | p = strchr(p, '\0'); |
| 3766 | tok = delete_Token(tok); |
| 3767 | } |
| 3768 | |
| 3769 | tok = tokenize(buf); |
| 3770 | nasm_free(buf); |
| 3771 | |
| 3772 | if (prev_next) |
| 3773 | *prev_next = tok; |
| 3774 | else |
| 3775 | *head = tok; |
| 3776 | |
| 3777 | /* |
| 3778 | * Connect pasted into original stream, |
| 3779 | * ie A -> new-tokens -> B |
| 3780 | */ |
| 3781 | while (tok && tok->next) |
| 3782 | tok = tok->next; |
| 3783 | tok->next = next; |
| 3784 | |
| 3785 | if (!pasted) |
| 3786 | pasted = true; |
| 3787 | |
| 3788 | /* Restart from pasted tokens head */ |
| 3789 | tok = prev_next ? *prev_next : *head; |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 3790 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3791 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3792 | break; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3793 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3794 | |
| 3795 | prev_next = &tok->next; |
| 3796 | |
| 3797 | if (tok->next && |
| 3798 | !tok_type_(tok->next, TOK_WHITESPACE) && |
| 3799 | !tok_type_(tok->next, TOK_PASTE)) |
| 3800 | prev_nonspace = prev_next; |
| 3801 | |
| 3802 | tok = tok->next; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3803 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3804 | |
| 3805 | return pasted; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3806 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3807 | |
| 3808 | /* |
| 3809 | * expands to a list of tokens from %{x:y} |
| 3810 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3811 | static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3812 | { |
| 3813 | Token *t = tline, **tt, *tm, *head; |
| 3814 | char *pos; |
| 3815 | int fst, lst, j, i; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3816 | |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3817 | pos = strchr(tline->text, ':'); |
| 3818 | nasm_assert(pos); |
| 3819 | |
| 3820 | lst = atoi(pos + 1); |
| 3821 | fst = atoi(tline->text + 1); |
| 3822 | |
| 3823 | /* |
| 3824 | * only macros params are accounted so |
| 3825 | * if someone passes %0 -- we reject such |
| 3826 | * value(s) |
| 3827 | */ |
| 3828 | if (lst == 0 || fst == 0) |
| 3829 | goto err; |
| 3830 | |
| 3831 | /* the values should be sane */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3832 | if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) || |
| 3833 | (lst > (int)mac->nparam || lst < (-(int)mac->nparam))) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3834 | goto err; |
| 3835 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3836 | fst = fst < 0 ? fst + (int)mac->nparam + 1: fst; |
| 3837 | lst = lst < 0 ? lst + (int)mac->nparam + 1: lst; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3838 | |
| 3839 | /* counted from zero */ |
| 3840 | fst--, lst--; |
| 3841 | |
| 3842 | /* |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3843 | * It will be at least one token. Note we |
| 3844 | * need to scan params until separator, otherwise |
| 3845 | * only first token will be passed. |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3846 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3847 | tm = mac->params[(fst + mac->rotate) % mac->nparam]; |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3848 | head = new_Token(NULL, tm->type, tm->text, 0); |
| 3849 | tt = &head->next, tm = tm->next; |
| 3850 | while (tok_isnt_(tm, ",")) { |
| 3851 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 3852 | *tt = t, tt = &t->next, tm = tm->next; |
| 3853 | } |
| 3854 | |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3855 | if (fst < lst) { |
| 3856 | for (i = fst + 1; i <= lst; i++) { |
| 3857 | t = new_Token(NULL, TOK_OTHER, ",", 0); |
| 3858 | *tt = t, tt = &t->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3859 | j = (i + mac->rotate) % mac->nparam; |
| 3860 | tm = mac->params[j]; |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3861 | while (tok_isnt_(tm, ",")) { |
| 3862 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 3863 | *tt = t, tt = &t->next, tm = tm->next; |
| 3864 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3865 | } |
| 3866 | } else { |
| 3867 | for (i = fst - 1; i >= lst; i--) { |
| 3868 | t = new_Token(NULL, TOK_OTHER, ",", 0); |
| 3869 | *tt = t, tt = &t->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3870 | j = (i + mac->rotate) % mac->nparam; |
| 3871 | tm = mac->params[j]; |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3872 | while (tok_isnt_(tm, ",")) { |
| 3873 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 3874 | *tt = t, tt = &t->next, tm = tm->next; |
| 3875 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3876 | } |
| 3877 | } |
| 3878 | |
| 3879 | *last = tt; |
| 3880 | return head; |
| 3881 | |
| 3882 | err: |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3883 | nasm_error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range", |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3884 | &tline->text[1]); |
| 3885 | return tline; |
| 3886 | } |
| 3887 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3888 | /* |
| 3889 | * Expand MMacro-local things: parameter references (%0, %n, %+n, |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 3890 | * %-n) and MMacro-local identifiers (%%foo) as well as |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3891 | * macro indirection (%[...]) and range (%{..:..}). |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3892 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3893 | static Token *expand_mmac_params(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3894 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3895 | Token *t, *tt, **tail, *thead; |
H. Peter Anvin | 6125b62 | 2009-04-08 14:02:25 -0700 | [diff] [blame] | 3896 | bool changed = false; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3897 | char *pos; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3898 | |
| 3899 | tail = &thead; |
| 3900 | thead = NULL; |
| 3901 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3902 | while (tline) { |
| 3903 | if (tline->type == TOK_PREPROC_ID && |
Cyrill Gorcunov | ca61119 | 2010-06-04 09:22:12 +0400 | [diff] [blame] | 3904 | (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) || |
| 3905 | (tline->text[1] >= '0' && tline->text[1] <= '9') || |
| 3906 | tline->text[1] == '%')) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 3907 | char *text = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3908 | int type = 0, cc; /* type = 0 to placate optimisers */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 3909 | char tmpbuf[30]; |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3910 | unsigned int n; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3911 | int i; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3912 | MMacro *mac; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3913 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3914 | t = tline; |
| 3915 | tline = tline->next; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3916 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3917 | mac = istk->mstk; |
| 3918 | while (mac && !mac->name) /* avoid mistaking %reps for macros */ |
| 3919 | mac = mac->next_active; |
| 3920 | if (!mac) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3921 | nasm_error(ERR_NONFATAL, "`%s': not in a macro call", t->text); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3922 | } else { |
| 3923 | pos = strchr(t->text, ':'); |
| 3924 | if (!pos) { |
| 3925 | switch (t->text[1]) { |
| 3926 | /* |
| 3927 | * We have to make a substitution of one of the |
| 3928 | * forms %1, %-1, %+1, %%foo, %0. |
| 3929 | */ |
| 3930 | case '0': |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3931 | type = TOK_NUMBER; |
| 3932 | snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam); |
| 3933 | text = nasm_strdup(tmpbuf); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3934 | break; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3935 | case '%': |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3936 | type = TOK_ID; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3937 | snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".", |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3938 | mac->unique); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3939 | text = nasm_strcat(tmpbuf, t->text + 2); |
| 3940 | break; |
| 3941 | case '-': |
| 3942 | n = atoi(t->text + 2) - 1; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3943 | if (n >= mac->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3944 | tt = NULL; |
| 3945 | else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3946 | if (mac->nparam > 1) |
| 3947 | n = (n + mac->rotate) % mac->nparam; |
| 3948 | tt = mac->params[n]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3949 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3950 | cc = find_cc(tt); |
| 3951 | if (cc == -1) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3952 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3953 | "macro parameter %d is not a condition code", |
| 3954 | n + 1); |
| 3955 | text = NULL; |
| 3956 | } else { |
| 3957 | type = TOK_ID; |
| 3958 | if (inverse_ccs[cc] == -1) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3959 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3960 | "condition code `%s' is not invertible", |
| 3961 | conditions[cc]); |
| 3962 | text = NULL; |
| 3963 | } else |
| 3964 | text = nasm_strdup(conditions[inverse_ccs[cc]]); |
| 3965 | } |
| 3966 | break; |
| 3967 | case '+': |
| 3968 | n = atoi(t->text + 2) - 1; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3969 | if (n >= mac->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3970 | tt = NULL; |
| 3971 | else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3972 | if (mac->nparam > 1) |
| 3973 | n = (n + mac->rotate) % mac->nparam; |
| 3974 | tt = mac->params[n]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3975 | } |
| 3976 | cc = find_cc(tt); |
| 3977 | if (cc == -1) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3978 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3979 | "macro parameter %d is not a condition code", |
| 3980 | n + 1); |
| 3981 | text = NULL; |
| 3982 | } else { |
| 3983 | type = TOK_ID; |
| 3984 | text = nasm_strdup(conditions[cc]); |
| 3985 | } |
| 3986 | break; |
| 3987 | default: |
| 3988 | n = atoi(t->text + 1) - 1; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3989 | if (n >= mac->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3990 | tt = NULL; |
| 3991 | else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3992 | if (mac->nparam > 1) |
| 3993 | n = (n + mac->rotate) % mac->nparam; |
| 3994 | tt = mac->params[n]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3995 | } |
| 3996 | if (tt) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3997 | for (i = 0; i < mac->paramlen[n]; i++) { |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3998 | *tail = new_Token(NULL, tt->type, tt->text, 0); |
| 3999 | tail = &(*tail)->next; |
| 4000 | tt = tt->next; |
| 4001 | } |
| 4002 | } |
| 4003 | text = NULL; /* we've done it here */ |
| 4004 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4005 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4006 | } else { |
| 4007 | /* |
| 4008 | * seems we have a parameters range here |
| 4009 | */ |
| 4010 | Token *head, **last; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4011 | head = expand_mmac_params_range(mac, t, &last); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4012 | if (head != t) { |
| 4013 | *tail = head; |
| 4014 | *last = tline; |
| 4015 | tline = head; |
| 4016 | text = NULL; |
| 4017 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4018 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4019 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4020 | if (!text) { |
| 4021 | delete_Token(t); |
| 4022 | } else { |
| 4023 | *tail = t; |
| 4024 | tail = &t->next; |
| 4025 | t->type = type; |
| 4026 | nasm_free(t->text); |
| 4027 | t->text = text; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4028 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4029 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4030 | changed = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4031 | continue; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4032 | } else if (tline->type == TOK_INDIRECT) { |
| 4033 | t = tline; |
| 4034 | tline = tline->next; |
| 4035 | tt = tokenize(t->text); |
| 4036 | tt = expand_mmac_params(tt); |
| 4037 | tt = expand_smacro(tt); |
| 4038 | *tail = tt; |
| 4039 | while (tt) { |
| 4040 | tt->a.mac = NULL; /* Necessary? */ |
| 4041 | tail = &tt->next; |
| 4042 | tt = tt->next; |
| 4043 | } |
| 4044 | delete_Token(t); |
| 4045 | changed = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4046 | } else { |
| 4047 | t = *tail = tline; |
| 4048 | tline = tline->next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4049 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4050 | tail = &t->next; |
| 4051 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4052 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4053 | *tail = NULL; |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 4054 | |
Cyrill Gorcunov | c6a742c | 2011-06-27 01:23:09 +0400 | [diff] [blame] | 4055 | if (changed) { |
| 4056 | const struct tokseq_match t[] = { |
| 4057 | { |
| 4058 | PP_CONCAT_MASK(TOK_ID) | |
| 4059 | PP_CONCAT_MASK(TOK_FLOAT), /* head */ |
| 4060 | PP_CONCAT_MASK(TOK_ID) | |
| 4061 | PP_CONCAT_MASK(TOK_NUMBER) | |
| 4062 | PP_CONCAT_MASK(TOK_FLOAT) | |
| 4063 | PP_CONCAT_MASK(TOK_OTHER) /* tail */ |
| 4064 | }, |
| 4065 | { |
| 4066 | PP_CONCAT_MASK(TOK_NUMBER), /* head */ |
| 4067 | PP_CONCAT_MASK(TOK_NUMBER) /* tail */ |
| 4068 | } |
| 4069 | }; |
| 4070 | paste_tokens(&thead, t, ARRAY_SIZE(t), false); |
| 4071 | } |
H. Peter Anvin | 6125b62 | 2009-04-08 14:02:25 -0700 | [diff] [blame] | 4072 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4073 | return thead; |
| 4074 | } |
| 4075 | |
| 4076 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4077 | * Expand all single-line macro calls made in the given line. |
| 4078 | * Return the expanded version of the line. The original is deemed |
| 4079 | * to be destroyed in the process. (In reality we'll just move |
| 4080 | * Tokens from input to output a lot of the time, rather than |
| 4081 | * actually bothering to destroy and replicate.) |
| 4082 | */ |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4083 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4084 | static Token *expand_smacro(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4085 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4086 | Token *t, *tt, *mstart, **tail, *thead; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4087 | SMacro *head = NULL, *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4088 | Token **params; |
| 4089 | int *paramsize; |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 4090 | unsigned int nparam, sparam; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 4091 | int brackets; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4092 | Token *org_tline = tline; |
| 4093 | Context *ctx; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 4094 | const char *mname; |
H. Peter Anvin | 2a15e69 | 2007-11-19 13:14:59 -0800 | [diff] [blame] | 4095 | int deadman = DEADMAN_LIMIT; |
H. Peter Anvin | 8287daf | 2009-07-07 16:00:58 -0700 | [diff] [blame] | 4096 | bool expanded; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4097 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4098 | /* |
| 4099 | * Trick: we should avoid changing the start token pointer since it can |
| 4100 | * be contained in "next" field of other token. Because of this |
| 4101 | * we allocate a copy of first token and work with it; at the end of |
| 4102 | * routine we copy it back |
| 4103 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4104 | if (org_tline) { |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4105 | tline = new_Token(org_tline->next, org_tline->type, |
| 4106 | org_tline->text, 0); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4107 | tline->a.mac = org_tline->a.mac; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4108 | nasm_free(org_tline->text); |
| 4109 | org_tline->text = NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4110 | } |
| 4111 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4112 | expanded = true; /* Always expand %+ at least once */ |
H. Peter Anvin | 8287daf | 2009-07-07 16:00:58 -0700 | [diff] [blame] | 4113 | |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4114 | again: |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4115 | thead = NULL; |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4116 | tail = &thead; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4117 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4118 | while (tline) { /* main token loop */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4119 | if (!--deadman) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4120 | nasm_error(ERR_NONFATAL, "interminable macro recursion"); |
Cyrill Gorcunov | bd38c8f | 2009-11-21 11:11:23 +0300 | [diff] [blame] | 4121 | goto err; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4122 | } |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4123 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4124 | if ((mname = tline->text)) { |
| 4125 | /* if this token is a local macro, look in local context */ |
Cyrill Gorcunov | c56d9ad | 2010-02-11 15:12:19 +0300 | [diff] [blame] | 4126 | if (tline->type == TOK_ID) { |
| 4127 | head = (SMacro *)hash_findix(&smacros, mname); |
| 4128 | } else if (tline->type == TOK_PREPROC_ID) { |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 4129 | ctx = get_ctx(mname, &mname); |
Cyrill Gorcunov | c56d9ad | 2010-02-11 15:12:19 +0300 | [diff] [blame] | 4130 | head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL; |
| 4131 | } else |
| 4132 | head = NULL; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 4133 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4134 | /* |
| 4135 | * We've hit an identifier. As in is_mmacro below, we first |
| 4136 | * check whether the identifier is a single-line macro at |
| 4137 | * all, then think about checking for parameters if |
| 4138 | * necessary. |
| 4139 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4140 | list_for_each(m, head) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4141 | if (!mstrcmp(m->name, mname, m->casesense)) |
| 4142 | break; |
| 4143 | if (m) { |
| 4144 | mstart = tline; |
| 4145 | params = NULL; |
| 4146 | paramsize = NULL; |
| 4147 | if (m->nparam == 0) { |
| 4148 | /* |
| 4149 | * Simple case: the macro is parameterless. Discard the |
| 4150 | * one token that the macro call took, and push the |
| 4151 | * expansion back on the to-do stack. |
| 4152 | */ |
| 4153 | if (!m->expansion) { |
| 4154 | if (!strcmp("__FILE__", m->name)) { |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 4155 | const char *file = src_get_fname(); |
| 4156 | /* nasm_free(tline->text); here? */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4157 | tline->text = nasm_quote(file, strlen(file)); |
| 4158 | tline->type = TOK_STRING; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4159 | continue; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4160 | } |
| 4161 | if (!strcmp("__LINE__", m->name)) { |
| 4162 | nasm_free(tline->text); |
| 4163 | make_tok_num(tline, src_get_linnum()); |
| 4164 | continue; |
| 4165 | } |
| 4166 | if (!strcmp("__BITS__", m->name)) { |
| 4167 | nasm_free(tline->text); |
| 4168 | make_tok_num(tline, globalbits); |
| 4169 | continue; |
| 4170 | } |
| 4171 | tline = delete_Token(tline); |
| 4172 | continue; |
| 4173 | } |
| 4174 | } else { |
| 4175 | /* |
| 4176 | * Complicated case: at least one macro with this name |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4177 | * exists and takes parameters. We must find the |
| 4178 | * parameters in the call, count them, find the SMacro |
| 4179 | * that corresponds to that form of the macro call, and |
| 4180 | * substitute for the parameters when we expand. What a |
| 4181 | * pain. |
| 4182 | */ |
| 4183 | /*tline = tline->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4184 | skip_white_(tline); */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4185 | do { |
| 4186 | t = tline->next; |
| 4187 | while (tok_type_(t, TOK_SMAC_END)) { |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4188 | t->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4189 | t->text = NULL; |
| 4190 | t = tline->next = delete_Token(t); |
| 4191 | } |
| 4192 | tline = t; |
| 4193 | } while (tok_type_(tline, TOK_WHITESPACE)); |
| 4194 | if (!tok_is_(tline, "(")) { |
| 4195 | /* |
| 4196 | * This macro wasn't called with parameters: ignore |
| 4197 | * the call. (Behaviour borrowed from gnu cpp.) |
| 4198 | */ |
| 4199 | tline = mstart; |
| 4200 | m = NULL; |
| 4201 | } else { |
| 4202 | int paren = 0; |
| 4203 | int white = 0; |
| 4204 | brackets = 0; |
| 4205 | nparam = 0; |
| 4206 | sparam = PARAM_DELTA; |
| 4207 | params = nasm_malloc(sparam * sizeof(Token *)); |
| 4208 | params[0] = tline->next; |
| 4209 | paramsize = nasm_malloc(sparam * sizeof(int)); |
| 4210 | paramsize[0] = 0; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4211 | while (true) { /* parameter loop */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4212 | /* |
| 4213 | * For some unusual expansions |
| 4214 | * which concatenates function call |
| 4215 | */ |
| 4216 | t = tline->next; |
| 4217 | while (tok_type_(t, TOK_SMAC_END)) { |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4218 | t->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4219 | t->text = NULL; |
| 4220 | t = tline->next = delete_Token(t); |
| 4221 | } |
| 4222 | tline = t; |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 4223 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4224 | if (!tline) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4225 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4226 | "macro call expects terminating `)'"); |
| 4227 | break; |
| 4228 | } |
| 4229 | if (tline->type == TOK_WHITESPACE |
| 4230 | && brackets <= 0) { |
| 4231 | if (paramsize[nparam]) |
| 4232 | white++; |
| 4233 | else |
| 4234 | params[nparam] = tline->next; |
| 4235 | continue; /* parameter loop */ |
| 4236 | } |
| 4237 | if (tline->type == TOK_OTHER |
| 4238 | && tline->text[1] == 0) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4239 | char ch = tline->text[0]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4240 | if (ch == ',' && !paren && brackets <= 0) { |
| 4241 | if (++nparam >= sparam) { |
| 4242 | sparam += PARAM_DELTA; |
| 4243 | params = nasm_realloc(params, |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4244 | sparam * sizeof(Token *)); |
| 4245 | paramsize = nasm_realloc(paramsize, |
| 4246 | sparam * sizeof(int)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4247 | } |
| 4248 | params[nparam] = tline->next; |
| 4249 | paramsize[nparam] = 0; |
| 4250 | white = 0; |
| 4251 | continue; /* parameter loop */ |
| 4252 | } |
| 4253 | if (ch == '{' && |
| 4254 | (brackets > 0 || (brackets == 0 && |
| 4255 | !paramsize[nparam]))) |
| 4256 | { |
| 4257 | if (!(brackets++)) { |
| 4258 | params[nparam] = tline->next; |
| 4259 | continue; /* parameter loop */ |
| 4260 | } |
| 4261 | } |
| 4262 | if (ch == '}' && brackets > 0) |
| 4263 | if (--brackets == 0) { |
| 4264 | brackets = -1; |
| 4265 | continue; /* parameter loop */ |
| 4266 | } |
| 4267 | if (ch == '(' && !brackets) |
| 4268 | paren++; |
| 4269 | if (ch == ')' && brackets <= 0) |
| 4270 | if (--paren < 0) |
| 4271 | break; |
| 4272 | } |
| 4273 | if (brackets < 0) { |
| 4274 | brackets = 0; |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4275 | nasm_error(ERR_NONFATAL, "braces do not " |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4276 | "enclose all of macro parameter"); |
| 4277 | } |
| 4278 | paramsize[nparam] += white + 1; |
| 4279 | white = 0; |
| 4280 | } /* parameter loop */ |
| 4281 | nparam++; |
| 4282 | while (m && (m->nparam != nparam || |
| 4283 | mstrcmp(m->name, mname, |
| 4284 | m->casesense))) |
| 4285 | m = m->next; |
| 4286 | if (!m) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4287 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4288 | "macro `%s' exists, " |
| 4289 | "but not taking %d parameters", |
| 4290 | mstart->text, nparam); |
| 4291 | } |
| 4292 | } |
| 4293 | if (m && m->in_progress) |
| 4294 | m = NULL; |
| 4295 | if (!m) { /* in progess or didn't find '(' or wrong nparam */ |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 4296 | /* |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4297 | * Design question: should we handle !tline, which |
| 4298 | * indicates missing ')' here, or expand those |
| 4299 | * macros anyway, which requires the (t) test a few |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 4300 | * lines down? |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4301 | */ |
| 4302 | nasm_free(params); |
| 4303 | nasm_free(paramsize); |
| 4304 | tline = mstart; |
| 4305 | } else { |
| 4306 | /* |
| 4307 | * Expand the macro: we are placed on the last token of the |
| 4308 | * call, so that we can easily split the call from the |
| 4309 | * following tokens. We also start by pushing an SMAC_END |
| 4310 | * token for the cycle removal. |
| 4311 | */ |
| 4312 | t = tline; |
| 4313 | if (t) { |
| 4314 | tline = t->next; |
| 4315 | t->next = NULL; |
| 4316 | } |
| 4317 | tt = new_Token(tline, TOK_SMAC_END, NULL, 0); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4318 | tt->a.mac = m; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4319 | m->in_progress = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4320 | tline = tt; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 4321 | list_for_each(t, m->expansion) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4322 | if (t->type >= TOK_SMAC_PARAM) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4323 | Token *pcopy = tline, **ptail = &pcopy; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4324 | Token *ttt, *pt; |
| 4325 | int i; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4326 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4327 | ttt = params[t->type - TOK_SMAC_PARAM]; |
| 4328 | i = paramsize[t->type - TOK_SMAC_PARAM]; |
| 4329 | while (--i >= 0) { |
| 4330 | pt = *ptail = new_Token(tline, ttt->type, |
| 4331 | ttt->text, 0); |
| 4332 | ptail = &pt->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4333 | ttt = ttt->next; |
| 4334 | } |
| 4335 | tline = pcopy; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4336 | } else if (t->type == TOK_PREPROC_Q) { |
| 4337 | tt = new_Token(tline, TOK_ID, mname, 0); |
| 4338 | tline = tt; |
| 4339 | } else if (t->type == TOK_PREPROC_QQ) { |
| 4340 | tt = new_Token(tline, TOK_ID, m->name, 0); |
| 4341 | tline = tt; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4342 | } else { |
| 4343 | tt = new_Token(tline, t->type, t->text, 0); |
| 4344 | tline = tt; |
| 4345 | } |
| 4346 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4347 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4348 | /* |
| 4349 | * Having done that, get rid of the macro call, and clean |
| 4350 | * up the parameters. |
| 4351 | */ |
| 4352 | nasm_free(params); |
| 4353 | nasm_free(paramsize); |
| 4354 | free_tlist(mstart); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4355 | expanded = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4356 | continue; /* main token loop */ |
| 4357 | } |
| 4358 | } |
| 4359 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4360 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4361 | if (tline->type == TOK_SMAC_END) { |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4362 | tline->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4363 | tline = delete_Token(tline); |
| 4364 | } else { |
| 4365 | t = *tail = tline; |
| 4366 | tline = tline->next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4367 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4368 | t->next = NULL; |
| 4369 | tail = &t->next; |
| 4370 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4371 | } |
| 4372 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4373 | /* |
| 4374 | * 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] | 4375 | * after expansion (they can't be produced by tokenize()). The successive |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4376 | * TOK_IDs should be concatenated. |
| 4377 | * Also we look for %+ tokens and concatenate the tokens before and after |
| 4378 | * them (without white spaces in between). |
| 4379 | */ |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4380 | if (expanded) { |
Cyrill Gorcunov | c6a742c | 2011-06-27 01:23:09 +0400 | [diff] [blame] | 4381 | const struct tokseq_match t[] = { |
| 4382 | { |
| 4383 | PP_CONCAT_MASK(TOK_ID) | |
| 4384 | PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */ |
| 4385 | PP_CONCAT_MASK(TOK_ID) | |
| 4386 | PP_CONCAT_MASK(TOK_PREPROC_ID) | |
| 4387 | PP_CONCAT_MASK(TOK_NUMBER) /* tail */ |
| 4388 | } |
| 4389 | }; |
| 4390 | if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) { |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4391 | /* |
| 4392 | * If we concatenated something, *and* we had previously expanded |
| 4393 | * an actual macro, scan the lines again for macros... |
| 4394 | */ |
| 4395 | tline = thead; |
| 4396 | expanded = false; |
| 4397 | goto again; |
| 4398 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4399 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4400 | |
Cyrill Gorcunov | bd38c8f | 2009-11-21 11:11:23 +0300 | [diff] [blame] | 4401 | err: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4402 | if (org_tline) { |
| 4403 | if (thead) { |
| 4404 | *org_tline = *thead; |
| 4405 | /* since we just gave text to org_line, don't free it */ |
| 4406 | thead->text = NULL; |
| 4407 | delete_Token(thead); |
| 4408 | } else { |
| 4409 | /* the expression expanded to empty line; |
| 4410 | we can't return NULL for some reasons |
| 4411 | we just set the line to a single WHITESPACE token. */ |
| 4412 | memset(org_tline, 0, sizeof(*org_tline)); |
| 4413 | org_tline->text = NULL; |
| 4414 | org_tline->type = TOK_WHITESPACE; |
| 4415 | } |
| 4416 | thead = org_tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4417 | } |
| 4418 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4419 | return thead; |
| 4420 | } |
| 4421 | |
| 4422 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4423 | * Similar to expand_smacro but used exclusively with macro identifiers |
| 4424 | * right before they are fetched in. The reason is that there can be |
| 4425 | * identifiers consisting of several subparts. We consider that if there |
| 4426 | * are more than one element forming the name, user wants a expansion, |
| 4427 | * otherwise it will be left as-is. Example: |
| 4428 | * |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4429 | * %define %$abc cde |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4430 | * |
| 4431 | * the identifier %$abc will be left as-is so that the handler for %define |
| 4432 | * will suck it and define the corresponding value. Other case: |
| 4433 | * |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4434 | * %define _%$abc cde |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4435 | * |
| 4436 | * In this case user wants name to be expanded *before* %define starts |
| 4437 | * working, so we'll expand %$abc into something (if it has a value; |
| 4438 | * otherwise it will be left as-is) then concatenate all successive |
| 4439 | * PP_IDs into one. |
| 4440 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4441 | static Token *expand_id(Token * tline) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4442 | { |
| 4443 | Token *cur, *oldnext = NULL; |
| 4444 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4445 | if (!tline || !tline->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4446 | return tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4447 | |
| 4448 | cur = tline; |
| 4449 | while (cur->next && |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4450 | (cur->next->type == TOK_ID || |
| 4451 | cur->next->type == TOK_PREPROC_ID |
| 4452 | || cur->next->type == TOK_NUMBER)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4453 | cur = cur->next; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4454 | |
| 4455 | /* If identifier consists of just one token, don't expand */ |
| 4456 | if (cur == tline) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4457 | return tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4458 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4459 | if (cur) { |
| 4460 | oldnext = cur->next; /* Detach the tail past identifier */ |
| 4461 | cur->next = NULL; /* so that expand_smacro stops here */ |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4462 | } |
| 4463 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4464 | tline = expand_smacro(tline); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4465 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4466 | if (cur) { |
| 4467 | /* expand_smacro possibly changhed tline; re-scan for EOL */ |
| 4468 | cur = tline; |
| 4469 | while (cur && cur->next) |
| 4470 | cur = cur->next; |
| 4471 | if (cur) |
| 4472 | cur->next = oldnext; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4473 | } |
| 4474 | |
| 4475 | return tline; |
| 4476 | } |
| 4477 | |
| 4478 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4479 | * Determine whether the given line constitutes a multi-line macro |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4480 | * 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] | 4481 | * to check for an initial label - that's taken care of in |
| 4482 | * expand_mmacro - but must check numbers of parameters. Guaranteed |
| 4483 | * to be called with tline->type == TOK_ID, so the putative macro |
| 4484 | * name is easy to find. |
| 4485 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4486 | static MMacro *is_mmacro(Token * tline, Token *** params_array) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4487 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4488 | MMacro *head, *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4489 | Token **params; |
| 4490 | int nparam; |
| 4491 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4492 | head = (MMacro *) hash_findix(&mmacros, tline->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4493 | |
| 4494 | /* |
| 4495 | * Efficiency: first we see if any macro exists with the given |
| 4496 | * name. If not, we can return NULL immediately. _Then_ we |
| 4497 | * count the parameters, and then we look further along the |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4498 | * list if necessary to find the proper MMacro. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4499 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4500 | list_for_each(m, head) |
| 4501 | if (!mstrcmp(m->name, tline->text, m->casesense)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4502 | break; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4503 | if (!m) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4504 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4505 | |
| 4506 | /* |
| 4507 | * OK, we have a potential macro. Count and demarcate the |
| 4508 | * parameters. |
| 4509 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4510 | count_mmac_params(tline->next, &nparam, ¶ms); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4511 | |
| 4512 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4513 | * 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] | 4514 | * structure that handles this number. |
| 4515 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4516 | while (m) { |
| 4517 | if (m->nparam_min <= nparam |
| 4518 | && (m->plus || nparam <= m->nparam_max)) { |
| 4519 | /* |
| 4520 | * This one is right. Just check if cycle removal |
| 4521 | * prohibits us using it before we actually celebrate... |
| 4522 | */ |
| 4523 | if (m->in_progress > m->max_depth) { |
| 4524 | if (m->max_depth > 0) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4525 | nasm_error(ERR_WARNING, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4526 | "reached maximum recursion depth of %i", |
| 4527 | m->max_depth); |
| 4528 | } |
| 4529 | nasm_free(params); |
| 4530 | return NULL; |
| 4531 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4532 | /* |
| 4533 | * It's right, and we can use it. Add its default |
| 4534 | * parameters to the end of our list if necessary. |
| 4535 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4536 | if (m->defaults && nparam < m->nparam_min + m->ndefs) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4537 | params = |
| 4538 | nasm_realloc(params, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4539 | ((m->nparam_min + m->ndefs + |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4540 | 1) * sizeof(*params))); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4541 | while (nparam < m->nparam_min + m->ndefs) { |
| 4542 | params[nparam] = m->defaults[nparam - m->nparam_min]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4543 | nparam++; |
| 4544 | } |
| 4545 | } |
| 4546 | /* |
| 4547 | * If we've gone over the maximum parameter count (and |
| 4548 | * we're in Plus mode), ignore parameters beyond |
| 4549 | * nparam_max. |
| 4550 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4551 | if (m->plus && nparam > m->nparam_max) |
| 4552 | nparam = m->nparam_max; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4553 | /* |
| 4554 | * Then terminate the parameter list, and leave. |
| 4555 | */ |
| 4556 | if (!params) { /* need this special case */ |
| 4557 | params = nasm_malloc(sizeof(*params)); |
| 4558 | nparam = 0; |
| 4559 | } |
| 4560 | params[nparam] = NULL; |
| 4561 | *params_array = params; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4562 | return m; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4563 | } |
| 4564 | /* |
| 4565 | * This one wasn't right: look for the next one with the |
| 4566 | * same name. |
| 4567 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4568 | list_for_each(m, m->next) |
| 4569 | if (!mstrcmp(m->name, tline->text, m->casesense)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4570 | break; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4571 | } |
| 4572 | |
| 4573 | /* |
| 4574 | * After all that, we didn't find one with the right number of |
| 4575 | * parameters. Issue a warning, and fail to expand the macro. |
| 4576 | */ |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4577 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4578 | "macro `%s' exists, but not taking %d parameters", |
| 4579 | tline->text, nparam); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4580 | nasm_free(params); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4581 | return NULL; |
| 4582 | } |
| 4583 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4584 | |
| 4585 | /* |
| 4586 | * Save MMacro invocation specific fields in |
| 4587 | * preparation for a recursive macro expansion |
| 4588 | */ |
| 4589 | static void push_mmacro(MMacro *m) |
| 4590 | { |
| 4591 | MMacroInvocation *i; |
| 4592 | |
| 4593 | i = nasm_malloc(sizeof(MMacroInvocation)); |
| 4594 | i->prev = m->prev; |
| 4595 | i->params = m->params; |
| 4596 | i->iline = m->iline; |
| 4597 | i->nparam = m->nparam; |
| 4598 | i->rotate = m->rotate; |
| 4599 | i->paramlen = m->paramlen; |
| 4600 | i->unique = m->unique; |
| 4601 | i->condcnt = m->condcnt; |
| 4602 | m->prev = i; |
| 4603 | } |
| 4604 | |
| 4605 | |
| 4606 | /* |
| 4607 | * Restore MMacro invocation specific fields that were |
| 4608 | * saved during a previous recursive macro expansion |
| 4609 | */ |
| 4610 | static void pop_mmacro(MMacro *m) |
| 4611 | { |
| 4612 | MMacroInvocation *i; |
| 4613 | |
| 4614 | if (m->prev) { |
| 4615 | i = m->prev; |
| 4616 | m->prev = i->prev; |
| 4617 | m->params = i->params; |
| 4618 | m->iline = i->iline; |
| 4619 | m->nparam = i->nparam; |
| 4620 | m->rotate = i->rotate; |
| 4621 | m->paramlen = i->paramlen; |
| 4622 | m->unique = i->unique; |
| 4623 | m->condcnt = i->condcnt; |
| 4624 | nasm_free(i); |
| 4625 | } |
| 4626 | } |
| 4627 | |
| 4628 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4629 | /* |
| 4630 | * Expand the multi-line macro call made by the given line, if |
| 4631 | * 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] | 4632 | * istk->expansion and return 1. Otherwise return 0. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4633 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4634 | static int expand_mmacro(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4635 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4636 | Token *startline = tline; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4637 | Token *label = NULL; |
| 4638 | int dont_prepend = 0; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4639 | Token **params, *t, *tt; |
| 4640 | MMacro *m; |
| 4641 | Line *l, *ll; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4642 | int i, nparam, *paramlen; |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 4643 | const char *mname; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4644 | |
| 4645 | t = tline; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4646 | skip_white_(t); |
H. Peter Anvin | ce2233b | 2008-05-25 21:57:00 -0700 | [diff] [blame] | 4647 | /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */ |
H. Peter Anvin | dce1e2f | 2002-04-30 21:06:37 +0000 | [diff] [blame] | 4648 | 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] | 4649 | return 0; |
| 4650 | m = is_mmacro(t, ¶ms); |
| 4651 | if (m) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4652 | mname = t->text; |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 4653 | } else { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4654 | Token *last; |
| 4655 | /* |
| 4656 | * We have an id which isn't a macro call. We'll assume |
| 4657 | * it might be a label; we'll also check to see if a |
| 4658 | * colon follows it. Then, if there's another id after |
| 4659 | * that lot, we'll check it again for macro-hood. |
| 4660 | */ |
| 4661 | label = last = t; |
| 4662 | t = t->next; |
| 4663 | if (tok_type_(t, TOK_WHITESPACE)) |
| 4664 | last = t, t = t->next; |
| 4665 | if (tok_is_(t, ":")) { |
| 4666 | dont_prepend = 1; |
| 4667 | last = t, t = t->next; |
| 4668 | if (tok_type_(t, TOK_WHITESPACE)) |
| 4669 | last = t, t = t->next; |
| 4670 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4671 | if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, ¶ms))) |
| 4672 | return 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4673 | last->next = NULL; |
Keith Kanios | 891775e | 2009-07-11 06:08:54 -0500 | [diff] [blame] | 4674 | mname = t->text; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4675 | tline = t; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4676 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4677 | |
| 4678 | /* |
| 4679 | * Fix up the parameters: this involves stripping leading and |
| 4680 | * trailing whitespace, then stripping braces if they are |
| 4681 | * present. |
| 4682 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4683 | for (nparam = 0; params[nparam]; nparam++) ; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4684 | paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4685 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4686 | for (i = 0; params[i]; i++) { |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4687 | int brace = 0; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4688 | int comma = (!m->plus || i < nparam - 1); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4689 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4690 | t = params[i]; |
| 4691 | skip_white_(t); |
| 4692 | if (tok_is_(t, "{")) |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4693 | t = t->next, brace++, comma = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4694 | params[i] = t; |
| 4695 | paramlen[i] = 0; |
| 4696 | while (t) { |
| 4697 | if (comma && t->type == TOK_OTHER && !strcmp(t->text, ",")) |
| 4698 | break; /* ... because we have hit a comma */ |
| 4699 | if (comma && t->type == TOK_WHITESPACE |
| 4700 | && tok_is_(t->next, ",")) |
| 4701 | break; /* ... or a space then a comma */ |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4702 | if (brace && t->type == TOK_OTHER) { |
| 4703 | if (t->text[0] == '{') |
| 4704 | brace++; /* ... or a nested opening brace */ |
| 4705 | else if (t->text[0] == '}') |
| 4706 | if (!--brace) |
| 4707 | break; /* ... or a brace */ |
| 4708 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4709 | t = t->next; |
| 4710 | paramlen[i]++; |
| 4711 | } |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4712 | if (brace) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4713 | nasm_error(ERR_NONFATAL, "macro params should be enclosed in braces"); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4714 | } |
| 4715 | |
| 4716 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4717 | * OK, we have a MMacro structure together with a set of |
| 4718 | * parameters. We must now go through the expansion and push |
| 4719 | * copies of each Line on to istk->expansion. Substitution of |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4720 | * parameter tokens and macro-local tokens doesn't get done |
| 4721 | * until the single-line macro substitution process; this is |
| 4722 | * because delaying them allows us to change the semantics |
| 4723 | * later through %rotate. |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4724 | * |
| 4725 | * First, push an end marker on to istk->expansion, mark this |
| 4726 | * macro as in progress, and set up its invocation-specific |
| 4727 | * variables. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4728 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4729 | ll = nasm_malloc(sizeof(Line)); |
| 4730 | ll->next = istk->expansion; |
| 4731 | ll->finishes = m; |
| 4732 | ll->first = NULL; |
| 4733 | istk->expansion = ll; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4734 | |
| 4735 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4736 | * Save the previous MMacro expansion in the case of |
| 4737 | * macro recursion |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4738 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4739 | if (m->max_depth && m->in_progress) |
| 4740 | push_mmacro(m); |
| 4741 | |
| 4742 | m->in_progress ++; |
| 4743 | m->params = params; |
| 4744 | m->iline = tline; |
| 4745 | m->nparam = nparam; |
| 4746 | m->rotate = 0; |
| 4747 | m->paramlen = paramlen; |
| 4748 | m->unique = unique++; |
| 4749 | m->lineno = 0; |
| 4750 | m->condcnt = 0; |
| 4751 | |
| 4752 | m->next_active = istk->mstk; |
| 4753 | istk->mstk = m; |
| 4754 | |
| 4755 | list_for_each(l, m->expansion) { |
| 4756 | Token **tail; |
| 4757 | |
| 4758 | ll = nasm_malloc(sizeof(Line)); |
| 4759 | ll->finishes = NULL; |
| 4760 | ll->next = istk->expansion; |
| 4761 | istk->expansion = ll; |
| 4762 | tail = &ll->first; |
| 4763 | |
| 4764 | list_for_each(t, l->first) { |
| 4765 | Token *x = t; |
| 4766 | switch (t->type) { |
| 4767 | case TOK_PREPROC_Q: |
| 4768 | tt = *tail = new_Token(NULL, TOK_ID, mname, 0); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4769 | break; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4770 | case TOK_PREPROC_QQ: |
| 4771 | tt = *tail = new_Token(NULL, TOK_ID, m->name, 0); |
| 4772 | break; |
| 4773 | case TOK_PREPROC_ID: |
| 4774 | if (t->text[1] == '0' && t->text[2] == '0') { |
| 4775 | dont_prepend = -1; |
| 4776 | x = label; |
| 4777 | if (!x) |
| 4778 | continue; |
| 4779 | } |
| 4780 | /* fall through */ |
| 4781 | default: |
| 4782 | tt = *tail = new_Token(NULL, x->type, x->text, 0); |
| 4783 | break; |
| 4784 | } |
| 4785 | tail = &tt->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4786 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4787 | *tail = NULL; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4788 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4789 | |
| 4790 | /* |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4791 | * If we had a label, push it on as the first line of |
| 4792 | * the macro expansion. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4793 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4794 | if (label) { |
| 4795 | if (dont_prepend < 0) |
| 4796 | free_tlist(startline); |
| 4797 | else { |
| 4798 | ll = nasm_malloc(sizeof(Line)); |
| 4799 | ll->finishes = NULL; |
| 4800 | ll->next = istk->expansion; |
| 4801 | istk->expansion = ll; |
| 4802 | ll->first = startline; |
| 4803 | if (!dont_prepend) { |
| 4804 | while (label->next) |
| 4805 | label = label->next; |
| 4806 | label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4807 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4808 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4809 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4810 | |
H. Peter Anvin | 172b840 | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 4811 | lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4812 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4813 | return 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4814 | } |
| 4815 | |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4816 | /* |
| 4817 | * This function adds macro names to error messages, and suppresses |
| 4818 | * them if necessary. |
| 4819 | */ |
| 4820 | 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] | 4821 | { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4822 | char buff[BUFSIZ]; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4823 | MMacro *mmac = NULL; |
| 4824 | int delta = 0; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4825 | |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4826 | /* |
| 4827 | * If we're in a dead branch of IF or something like it, ignore the error. |
| 4828 | * However, because %else etc are evaluated in the state context |
| 4829 | * of the previous branch, errors might get lost: |
| 4830 | * %if 0 ... %else trailing garbage ... %endif |
| 4831 | * So %else etc should set the ERR_PP_PRECOND flag. |
| 4832 | */ |
| 4833 | if ((severity & ERR_MASK) < ERR_FATAL && |
| 4834 | istk && istk->conds && |
| 4835 | ((severity & ERR_PP_PRECOND) ? |
| 4836 | istk->conds->state == COND_NEVER : |
H. Peter Anvin | eb6653f | 2016-04-05 13:03:10 -0700 | [diff] [blame] | 4837 | !emitting(istk->conds->state))) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4838 | return; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4839 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4840 | /* get %macro name */ |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4841 | if (!(severity & ERR_NOFILE) && istk && istk->mstk) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4842 | mmac = istk->mstk; |
| 4843 | /* but %rep blocks should be skipped */ |
| 4844 | while (mmac && !mmac->name) |
| 4845 | mmac = mmac->next_active, delta++; |
Cyrill Gorcunov | 9900c6b | 2011-10-09 18:58:46 +0400 | [diff] [blame] | 4846 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4847 | |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4848 | if (mmac) { |
| 4849 | vsnprintf(buff, sizeof(buff), fmt, arg); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4850 | |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4851 | nasm_set_verror(real_verror); |
| 4852 | nasm_error(severity, "(%s:%d) %s", |
| 4853 | mmac->name, mmac->lineno - delta, buff); |
| 4854 | nasm_set_verror(pp_verror); |
| 4855 | } else { |
| 4856 | real_verror(severity, fmt, arg); |
| 4857 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4858 | } |
| 4859 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4860 | static void |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4861 | pp_reset(char *file, int apass, StrList **deplist) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4862 | { |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 4863 | Token *t; |
| 4864 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4865 | cstk = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4866 | istk = nasm_malloc(sizeof(Include)); |
| 4867 | istk->next = NULL; |
| 4868 | istk->conds = NULL; |
| 4869 | istk->expansion = NULL; |
| 4870 | istk->mstk = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4871 | istk->fp = fopen(file, "r"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4872 | istk->fname = NULL; |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 4873 | src_set(0, file); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4874 | istk->lineinc = 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4875 | if (!istk->fp) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4876 | nasm_fatal(ERR_NOFILE, "unable to open input file `%s'", file); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4877 | defining = NULL; |
Charles Crayne | d4200be | 2008-07-12 16:42:33 -0700 | [diff] [blame] | 4878 | nested_mac_count = 0; |
| 4879 | nested_rep_count = 0; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 4880 | init_macros(); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4881 | unique = 0; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4882 | if (tasm_compatible_mode) { |
H. Peter Anvin | a4835d4 | 2008-05-20 14:21:29 -0700 | [diff] [blame] | 4883 | stdmacpos = nasm_stdmac; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4884 | } else { |
H. Peter Anvin | a4835d4 | 2008-05-20 14:21:29 -0700 | [diff] [blame] | 4885 | stdmacpos = nasm_stdmac_after_tasm; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4886 | } |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 4887 | any_extrastdmac = extrastdmac && *extrastdmac; |
| 4888 | do_predef = true; |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 4889 | |
| 4890 | /* |
| 4891 | * 0 for dependencies, 1 for preparatory passes, 2 for final pass. |
| 4892 | * The caller, however, will also pass in 3 for preprocess-only so |
| 4893 | * we can set __PASS__ accordingly. |
| 4894 | */ |
| 4895 | pass = apass > 2 ? 2 : apass; |
| 4896 | |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 4897 | dephead = deptail = deplist; |
| 4898 | if (deplist) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4899 | StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next); |
| 4900 | sl->next = NULL; |
| 4901 | strcpy(sl->str, file); |
| 4902 | *deptail = sl; |
| 4903 | deptail = &sl->next; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 4904 | } |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 4905 | |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 4906 | /* |
| 4907 | * Define the __PASS__ macro. This is defined here unlike |
| 4908 | * all the other builtins, because it is special -- it varies between |
| 4909 | * passes. |
| 4910 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4911 | t = nasm_malloc(sizeof(*t)); |
| 4912 | t->next = NULL; |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 4913 | make_tok_num(t, apass); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4914 | t->a.mac = NULL; |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 4915 | define_smacro(NULL, "__PASS__", true, 0, t); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4916 | } |
| 4917 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4918 | static char *pp_getline(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4919 | { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4920 | char *line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4921 | Token *tline; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4922 | |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4923 | real_verror = nasm_set_verror(pp_verror); |
| 4924 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4925 | while (1) { |
| 4926 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4927 | * Fetch a tokenized line, either from the macro-expansion |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4928 | * buffer or from the input file. |
| 4929 | */ |
| 4930 | tline = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4931 | while (istk->expansion && istk->expansion->finishes) { |
| 4932 | Line *l = istk->expansion; |
| 4933 | if (!l->finishes->name && l->finishes->in_progress > 1) { |
| 4934 | Line *ll; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4935 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4936 | /* |
| 4937 | * This is a macro-end marker for a macro with no |
| 4938 | * name, which means it's not really a macro at all |
| 4939 | * but a %rep block, and the `in_progress' field is |
| 4940 | * more than 1, meaning that we still need to |
| 4941 | * repeat. (1 means the natural last repetition; 0 |
| 4942 | * means termination by %exitrep.) We have |
| 4943 | * therefore expanded up to the %endrep, and must |
| 4944 | * push the whole block on to the expansion buffer |
| 4945 | * again. We don't bother to remove the macro-end |
| 4946 | * marker: we'd only have to generate another one |
| 4947 | * if we did. |
| 4948 | */ |
| 4949 | l->finishes->in_progress--; |
| 4950 | list_for_each(l, l->finishes->expansion) { |
| 4951 | Token *t, *tt, **tail; |
| 4952 | |
| 4953 | ll = nasm_malloc(sizeof(Line)); |
| 4954 | ll->next = istk->expansion; |
| 4955 | ll->finishes = NULL; |
| 4956 | ll->first = NULL; |
| 4957 | tail = &ll->first; |
| 4958 | |
| 4959 | list_for_each(t, l->first) { |
| 4960 | if (t->text || t->type == TOK_WHITESPACE) { |
| 4961 | tt = *tail = new_Token(NULL, t->type, t->text, 0); |
| 4962 | tail = &tt->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4963 | } |
| 4964 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4965 | |
| 4966 | istk->expansion = ll; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4967 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4968 | } else { |
| 4969 | /* |
| 4970 | * Check whether a `%rep' was started and not ended |
| 4971 | * within this macro expansion. This can happen and |
| 4972 | * should be detected. It's a fatal error because |
| 4973 | * I'm too confused to work out how to recover |
| 4974 | * sensibly from it. |
| 4975 | */ |
| 4976 | if (defining) { |
| 4977 | if (defining->name) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4978 | nasm_panic(0, "defining with name in expansion"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4979 | else if (istk->mstk->name) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4980 | nasm_fatal(0, "`%%rep' without `%%endrep' within" |
| 4981 | " expansion of macro `%s'", |
| 4982 | istk->mstk->name); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4983 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4984 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4985 | /* |
| 4986 | * FIXME: investigate the relationship at this point between |
| 4987 | * istk->mstk and l->finishes |
| 4988 | */ |
| 4989 | { |
| 4990 | MMacro *m = istk->mstk; |
| 4991 | istk->mstk = m->next_active; |
| 4992 | if (m->name) { |
| 4993 | /* |
| 4994 | * This was a real macro call, not a %rep, and |
| 4995 | * therefore the parameter information needs to |
| 4996 | * be freed. |
| 4997 | */ |
| 4998 | if (m->prev) { |
| 4999 | pop_mmacro(m); |
| 5000 | l->finishes->in_progress --; |
| 5001 | } else { |
| 5002 | nasm_free(m->params); |
| 5003 | free_tlist(m->iline); |
| 5004 | nasm_free(m->paramlen); |
| 5005 | l->finishes->in_progress = 0; |
| 5006 | } |
| 5007 | } else |
| 5008 | free_mmacro(m); |
| 5009 | } |
| 5010 | istk->expansion = l->next; |
| 5011 | nasm_free(l); |
H. Peter Anvin | 172b840 | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 5012 | lfmt->downlevel(LIST_MACRO); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5013 | } |
| 5014 | } |
| 5015 | while (1) { /* until we get a line we can use */ |
| 5016 | |
| 5017 | if (istk->expansion) { /* from a macro expansion */ |
| 5018 | char *p; |
| 5019 | Line *l = istk->expansion; |
| 5020 | if (istk->mstk) |
| 5021 | istk->mstk->lineno++; |
| 5022 | tline = l->first; |
| 5023 | istk->expansion = l->next; |
| 5024 | nasm_free(l); |
| 5025 | p = detoken(tline, false); |
H. Peter Anvin | 172b840 | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 5026 | lfmt->line(LIST_MACRO, p); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5027 | nasm_free(p); |
| 5028 | break; |
| 5029 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5030 | line = read_line(); |
| 5031 | if (line) { /* from the current input file */ |
| 5032 | line = prepreproc(line); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5033 | tline = tokenize(line); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5034 | nasm_free(line); |
| 5035 | break; |
| 5036 | } |
| 5037 | /* |
| 5038 | * The current file has ended; work down the istk |
| 5039 | */ |
| 5040 | { |
| 5041 | Include *i = istk; |
| 5042 | fclose(i->fp); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5043 | if (i->conds) { |
| 5044 | /* nasm_error can't be conditionally suppressed */ |
H. Peter Anvin | 4108706 | 2016-03-03 14:27:34 -0800 | [diff] [blame] | 5045 | nasm_fatal(0, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5046 | "expected `%%endif' before end of file"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5047 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5048 | /* 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] | 5049 | if (i->next) |
| 5050 | src_set(i->lineno, i->fname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5051 | istk = i->next; |
H. Peter Anvin | 172b840 | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 5052 | lfmt->downlevel(LIST_INCLUDE); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5053 | nasm_free(i); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5054 | if (!istk) { |
| 5055 | line = NULL; |
| 5056 | goto done; |
| 5057 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5058 | if (istk->expansion && istk->expansion->finishes) |
| 5059 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5060 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5061 | } |
| 5062 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5063 | /* |
| 5064 | * We must expand MMacro parameters and MMacro-local labels |
| 5065 | * _before_ we plunge into directive processing, to cope |
| 5066 | * with things like `%define something %1' such as STRUC |
| 5067 | * uses. Unless we're _defining_ a MMacro, in which case |
| 5068 | * those tokens should be left alone to go into the |
| 5069 | * definition; and unless we're in a non-emitting |
| 5070 | * condition, in which case we don't want to meddle with |
| 5071 | * anything. |
| 5072 | */ |
| 5073 | if (!defining && !(istk->conds && !emitting(istk->conds->state)) |
| 5074 | && !(istk->mstk && !istk->mstk->in_progress)) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5075 | tline = expand_mmac_params(tline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5076 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5077 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5078 | /* |
| 5079 | * Check the line to see if it's a preprocessor directive. |
| 5080 | */ |
| 5081 | if (do_directive(tline) == DIRECTIVE_FOUND) { |
| 5082 | continue; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5083 | } else if (defining) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5084 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5085 | * We're defining a multi-line macro. We emit nothing |
| 5086 | * at all, and just |
| 5087 | * shove the tokenized line on to the macro definition. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5088 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5089 | Line *l = nasm_malloc(sizeof(Line)); |
| 5090 | l->next = defining->expansion; |
| 5091 | l->first = tline; |
| 5092 | l->finishes = NULL; |
| 5093 | defining->expansion = l; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5094 | continue; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5095 | } else if (istk->conds && !emitting(istk->conds->state)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5096 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5097 | * We're in a non-emitting branch of a condition block. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5098 | * Emit nothing at all, not even a blank line: when we |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5099 | * emerge from the condition we'll give a line-number |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5100 | * directive so we keep our place correctly. |
| 5101 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5102 | free_tlist(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5103 | continue; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5104 | } else if (istk->mstk && !istk->mstk->in_progress) { |
| 5105 | /* |
| 5106 | * We're in a %rep block which has been terminated, so |
| 5107 | * we're walking through to the %endrep without |
| 5108 | * emitting anything. Emit nothing at all, not even a |
| 5109 | * blank line: when we emerge from the %rep block we'll |
| 5110 | * give a line-number directive so we keep our place |
| 5111 | * correctly. |
| 5112 | */ |
| 5113 | free_tlist(tline); |
| 5114 | continue; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5115 | } else { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5116 | tline = expand_smacro(tline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5117 | if (!expand_mmacro(tline)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5118 | /* |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5119 | * De-tokenize the line again, and emit it. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5120 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5121 | line = detoken(tline, true); |
| 5122 | free_tlist(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5123 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5124 | } else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5125 | continue; /* expand_mmacro calls free_tlist */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5126 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5127 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5128 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5129 | |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5130 | done: |
| 5131 | nasm_set_verror(real_verror); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5132 | return line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5133 | } |
| 5134 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5135 | static void pp_cleanup(int pass) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5136 | { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5137 | real_verror = nasm_set_verror(pp_verror); |
| 5138 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5139 | if (defining) { |
| 5140 | if (defining->name) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5141 | nasm_error(ERR_NONFATAL, |
| 5142 | "end of file while still defining macro `%s'", |
| 5143 | defining->name); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5144 | } else { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5145 | nasm_error(ERR_NONFATAL, "end of file while still in %%rep"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5146 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5147 | |
| 5148 | free_mmacro(defining); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5149 | defining = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5150 | } |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5151 | |
| 5152 | nasm_set_verror(real_verror); |
| 5153 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5154 | while (cstk) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5155 | ctx_pop(); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 5156 | free_macros(); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5157 | while (istk) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5158 | Include *i = istk; |
| 5159 | istk = istk->next; |
| 5160 | fclose(i->fp); |
Cyrill Gorcunov | 8dcfd88 | 2011-03-03 09:18:56 +0300 | [diff] [blame] | 5161 | nasm_free(i); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5162 | } |
| 5163 | while (cstk) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5164 | ctx_pop(); |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 5165 | src_set_fname(NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5166 | if (pass == 0) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5167 | IncPath *i; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5168 | free_llist(predef); |
Cyrill Gorcunov | dae24d7 | 2014-06-28 10:17:39 +0400 | [diff] [blame] | 5169 | predef = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5170 | delete_Blocks(); |
Cyrill Gorcunov | dae24d7 | 2014-06-28 10:17:39 +0400 | [diff] [blame] | 5171 | freeTokens = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5172 | while ((i = ipath)) { |
| 5173 | ipath = i->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5174 | if (i->path) |
| 5175 | nasm_free(i->path); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5176 | nasm_free(i); |
| 5177 | } |
H. Peter Anvin | 11dfa1a | 2008-07-02 18:11:04 -0700 | [diff] [blame] | 5178 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5179 | } |
| 5180 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5181 | static void pp_include_path(char *path) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5182 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5183 | IncPath *i; |
H. Peter Anvin | 37a321f | 2007-09-24 13:41:58 -0700 | [diff] [blame] | 5184 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5185 | i = nasm_malloc(sizeof(IncPath)); |
| 5186 | i->path = path ? nasm_strdup(path) : NULL; |
| 5187 | i->next = NULL; |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5188 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 5189 | if (ipath) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5190 | IncPath *j = ipath; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 5191 | while (j->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5192 | j = j->next; |
| 5193 | j->next = i; |
| 5194 | } else { |
| 5195 | ipath = i; |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5196 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5197 | } |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5198 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5199 | static void pp_pre_include(char *fname) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5200 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5201 | Token *inc, *space, *name; |
| 5202 | Line *l; |
| 5203 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5204 | name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0); |
| 5205 | space = new_Token(name, TOK_WHITESPACE, NULL, 0); |
| 5206 | inc = new_Token(space, TOK_PREPROC_ID, "%include", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5207 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5208 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5209 | l->next = predef; |
| 5210 | l->first = inc; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5211 | l->finishes = NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5212 | predef = l; |
| 5213 | } |
| 5214 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5215 | static void pp_pre_define(char *definition) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5216 | { |
| 5217 | Token *def, *space; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5218 | Line *l; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5219 | char *equals; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5220 | |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5221 | real_verror = nasm_set_verror(pp_verror); |
| 5222 | |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5223 | equals = strchr(definition, '='); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5224 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 5225 | def = new_Token(space, TOK_PREPROC_ID, "%define", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5226 | if (equals) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5227 | *equals = ' '; |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5228 | space->next = tokenize(definition); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5229 | if (equals) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5230 | *equals = '='; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5231 | |
Cyrill Gorcunov | 6d42e9b | 2015-02-08 11:07:17 +0300 | [diff] [blame] | 5232 | if (space->next->type != TOK_PREPROC_ID && |
| 5233 | space->next->type != TOK_ID) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5234 | nasm_error(ERR_WARNING, "pre-defining non ID `%s\'\n", definition); |
Cyrill Gorcunov | 6d42e9b | 2015-02-08 11:07:17 +0300 | [diff] [blame] | 5235 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5236 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5237 | l->next = predef; |
| 5238 | l->first = def; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5239 | l->finishes = NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5240 | predef = l; |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5241 | |
| 5242 | nasm_set_verror(real_verror); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5243 | } |
| 5244 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5245 | static void pp_pre_undefine(char *definition) |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5246 | { |
| 5247 | Token *def, *space; |
| 5248 | Line *l; |
| 5249 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5250 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 5251 | def = new_Token(space, TOK_PREPROC_ID, "%undef", 0); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5252 | space->next = tokenize(definition); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5253 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5254 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5255 | l->next = predef; |
| 5256 | l->first = def; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5257 | l->finishes = NULL; |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5258 | predef = l; |
| 5259 | } |
| 5260 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5261 | static void pp_extra_stdmac(macros_t *macros) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5262 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 5263 | extrastdmac = macros; |
| 5264 | } |
| 5265 | |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 5266 | static void make_tok_num(Token * tok, int64_t val) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5267 | { |
Cyrill Gorcunov | ce65274 | 2013-05-06 23:43:43 +0400 | [diff] [blame] | 5268 | char numbuf[32]; |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 5269 | snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5270 | tok->text = nasm_strdup(numbuf); |
| 5271 | tok->type = TOK_NUMBER; |
| 5272 | } |
| 5273 | |
H. Peter Anvin | 3736895 | 2016-05-09 14:10:32 -0700 | [diff] [blame] | 5274 | static void pp_list_one_macro(MMacro *m, int severity) |
| 5275 | { |
| 5276 | if (!m) |
| 5277 | return; |
| 5278 | |
| 5279 | /* We need to print the next_active list in reverse order */ |
| 5280 | pp_list_one_macro(m->next_active, severity); |
| 5281 | |
| 5282 | if (m->name && !m->nolist) { |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 5283 | src_set(m->xline + m->lineno, m->fname); |
H. Peter Anvin | 3736895 | 2016-05-09 14:10:32 -0700 | [diff] [blame] | 5284 | nasm_error(severity, "... from macro `%s' defined here", m->name); |
| 5285 | } |
| 5286 | } |
| 5287 | |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5288 | static void pp_error_list_macros(int severity) |
| 5289 | { |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5290 | int32_t saved_line; |
| 5291 | const char *saved_fname = NULL; |
| 5292 | |
| 5293 | severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY; |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 5294 | src_get(&saved_line, &saved_fname); |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5295 | |
Cyrill Gorcunov | 771d04e | 2016-05-10 23:27:03 +0300 | [diff] [blame] | 5296 | if (istk) |
| 5297 | pp_list_one_macro(istk->mstk, severity); |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5298 | |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 5299 | src_set(saved_line, saved_fname); |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5300 | } |
| 5301 | |
| 5302 | const struct preproc_ops nasmpp = { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5303 | pp_reset, |
| 5304 | pp_getline, |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5305 | pp_cleanup, |
| 5306 | pp_extra_stdmac, |
| 5307 | pp_pre_define, |
| 5308 | pp_pre_undefine, |
| 5309 | pp_pre_include, |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5310 | pp_include_path, |
| 5311 | pp_error_list_macros, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5312 | }; |