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 | |
| 159 | char *fname; /* File where defined */ |
| 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; |
| 274 | char *fname; |
| 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); |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 631 | nasm_free(m->fname); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 632 | nasm_free(m); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | /* |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 636 | * Free all currently defined macros, and free the hash tables |
| 637 | */ |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 638 | static void free_smacro_table(struct hash_table *smt) |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 639 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 640 | SMacro *s, *tmp; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 641 | const char *key; |
| 642 | struct hash_tbl_node *it = NULL; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 643 | |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 644 | while ((s = hash_iterate(smt, &it, &key)) != NULL) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 645 | nasm_free((void *)key); |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 646 | list_for_each_safe(s, tmp, s) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 647 | nasm_free(s->name); |
| 648 | free_tlist(s->expansion); |
| 649 | nasm_free(s); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 650 | } |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 651 | } |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 652 | hash_free(smt); |
| 653 | } |
| 654 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 655 | static void free_mmacro_table(struct hash_table *mmt) |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 656 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 657 | MMacro *m, *tmp; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 658 | const char *key; |
| 659 | struct hash_tbl_node *it = NULL; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 660 | |
| 661 | it = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 662 | while ((m = hash_iterate(mmt, &it, &key)) != NULL) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 663 | nasm_free((void *)key); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 664 | list_for_each_safe(m ,tmp, m) |
| 665 | free_mmacro(m); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 666 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 667 | hash_free(mmt); |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | static void free_macros(void) |
| 671 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 672 | free_smacro_table(&smacros); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 673 | free_mmacro_table(&mmacros); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | /* |
| 677 | * Initialize the hash tables |
| 678 | */ |
| 679 | static void init_macros(void) |
| 680 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 681 | hash_init(&smacros, HASH_LARGE); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 682 | hash_init(&mmacros, HASH_LARGE); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 686 | * Pop the context stack. |
| 687 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 688 | static void ctx_pop(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 689 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 690 | Context *c = cstk; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 691 | |
| 692 | cstk = cstk->next; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 693 | free_smacro_table(&c->localmac); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 694 | nasm_free(c->name); |
| 695 | nasm_free(c); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 696 | } |
| 697 | |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 698 | /* |
| 699 | * Search for a key in the hash index; adding it if necessary |
| 700 | * (in which case we initialize the data pointer to NULL.) |
| 701 | */ |
| 702 | static void ** |
| 703 | hash_findi_add(struct hash_table *hash, const char *str) |
| 704 | { |
| 705 | struct hash_insert hi; |
| 706 | void **r; |
| 707 | char *strx; |
| 708 | |
| 709 | r = hash_findi(hash, str, &hi); |
| 710 | if (r) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 711 | return r; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 712 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 713 | strx = nasm_strdup(str); /* Use a more efficient allocator here? */ |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 714 | return hash_add(&hi, strx, NULL); |
| 715 | } |
| 716 | |
| 717 | /* |
| 718 | * Like hash_findi, but returns the data element rather than a pointer |
| 719 | * to it. Used only when not adding a new element, hence no third |
| 720 | * argument. |
| 721 | */ |
| 722 | static void * |
| 723 | hash_findix(struct hash_table *hash, const char *str) |
| 724 | { |
| 725 | void **p; |
| 726 | |
| 727 | p = hash_findi(hash, str, NULL); |
| 728 | return p ? *p : NULL; |
| 729 | } |
| 730 | |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 731 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 732 | * read line from standart macros set, |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 733 | * if there no more left -- return NULL |
| 734 | */ |
| 735 | static char *line_from_stdmac(void) |
| 736 | { |
| 737 | unsigned char c; |
| 738 | const unsigned char *p = stdmacpos; |
| 739 | char *line, *q; |
| 740 | size_t len = 0; |
| 741 | |
| 742 | if (!stdmacpos) |
| 743 | return NULL; |
| 744 | |
| 745 | while ((c = *p++)) { |
| 746 | if (c >= 0x80) |
| 747 | len += pp_directives_len[c - 0x80] + 1; |
| 748 | else |
| 749 | len++; |
| 750 | } |
| 751 | |
| 752 | line = nasm_malloc(len + 1); |
| 753 | q = line; |
| 754 | while ((c = *stdmacpos++)) { |
| 755 | if (c >= 0x80) { |
| 756 | memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]); |
| 757 | q += pp_directives_len[c - 0x80]; |
| 758 | *q++ = ' '; |
| 759 | } else { |
| 760 | *q++ = c; |
| 761 | } |
| 762 | } |
| 763 | stdmacpos = p; |
| 764 | *q = '\0'; |
| 765 | |
| 766 | if (!*stdmacpos) { |
| 767 | /* This was the last of the standard macro chain... */ |
| 768 | stdmacpos = NULL; |
| 769 | if (any_extrastdmac) { |
| 770 | stdmacpos = extrastdmac; |
| 771 | any_extrastdmac = false; |
| 772 | } else if (do_predef) { |
| 773 | Line *pd, *l; |
| 774 | Token *head, **tail, *t; |
| 775 | |
| 776 | /* |
| 777 | * Nasty hack: here we push the contents of |
| 778 | * `predef' on to the top-level expansion stack, |
| 779 | * since this is the most convenient way to |
| 780 | * implement the pre-include and pre-define |
| 781 | * features. |
| 782 | */ |
| 783 | list_for_each(pd, predef) { |
| 784 | head = NULL; |
| 785 | tail = &head; |
| 786 | list_for_each(t, pd->first) { |
| 787 | *tail = new_Token(NULL, t->type, t->text, 0); |
| 788 | tail = &(*tail)->next; |
| 789 | } |
| 790 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 791 | l = nasm_malloc(sizeof(Line)); |
| 792 | l->next = istk->expansion; |
| 793 | l->first = head; |
| 794 | l->finishes = NULL; |
| 795 | |
| 796 | istk->expansion = l; |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 797 | } |
| 798 | do_predef = false; |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | return line; |
| 803 | } |
| 804 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 805 | static char *read_line(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 806 | { |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 807 | unsigned int size, c, next; |
| 808 | const unsigned int delta = 512; |
| 809 | const unsigned int pad = 8; |
| 810 | unsigned int nr_cont = 0; |
| 811 | bool cont = false; |
| 812 | char *buffer, *p; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 813 | |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 814 | /* Standart macros set (predefined) goes first */ |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 815 | p = line_from_stdmac(); |
| 816 | if (p) |
| 817 | return p; |
H. Peter Anvin | 72edbb8 | 2008-06-19 16:00:04 -0700 | [diff] [blame] | 818 | |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 819 | size = delta; |
| 820 | p = buffer = nasm_malloc(size); |
| 821 | |
| 822 | for (;;) { |
| 823 | c = fgetc(istk->fp); |
| 824 | if ((int)(c) == EOF) { |
| 825 | p[0] = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 826 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 827 | } |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 828 | |
| 829 | switch (c) { |
| 830 | case '\r': |
| 831 | next = fgetc(istk->fp); |
| 832 | if (next != '\n') |
| 833 | ungetc(next, istk->fp); |
| 834 | if (cont) { |
| 835 | cont = false; |
| 836 | continue; |
| 837 | } |
| 838 | break; |
| 839 | |
| 840 | case '\n': |
| 841 | if (cont) { |
| 842 | cont = false; |
| 843 | continue; |
| 844 | } |
| 845 | break; |
| 846 | |
| 847 | case '\\': |
| 848 | next = fgetc(istk->fp); |
| 849 | ungetc(next, istk->fp); |
Cyrill Gorcunov | 490f85e | 2012-12-27 20:02:17 +0400 | [diff] [blame] | 850 | if (next == '\r' || next == '\n') { |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 851 | cont = true; |
| 852 | nr_cont++; |
| 853 | continue; |
| 854 | } |
| 855 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 856 | } |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 857 | |
| 858 | if (c == '\r' || c == '\n') { |
| 859 | *p++ = 0; |
| 860 | break; |
| 861 | } |
| 862 | |
| 863 | if (p >= (buffer + size - pad)) { |
| 864 | buffer = nasm_realloc(buffer, size + delta); |
| 865 | p = buffer + size - pad; |
| 866 | size += delta; |
| 867 | } |
| 868 | |
| 869 | *p++ = (unsigned char)c; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 870 | } |
| 871 | |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 872 | if (p == buffer) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 873 | nasm_free(buffer); |
| 874 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 875 | } |
| 876 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 877 | src_set_linnum(src_get_linnum() + istk->lineinc + |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 878 | (nr_cont * istk->lineinc)); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 879 | |
| 880 | /* |
| 881 | * Handle spurious ^Z, which may be inserted into source files |
| 882 | * by some file transfer utilities. |
| 883 | */ |
| 884 | buffer[strcspn(buffer, "\032")] = '\0'; |
| 885 | |
H. Peter Anvin | 172b840 | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 886 | lfmt->line(LIST_READ, buffer); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 887 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 888 | return buffer; |
| 889 | } |
| 890 | |
| 891 | /* |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 892 | * 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] | 893 | * don't need to parse the value out of e.g. numeric tokens: we |
| 894 | * simply split one string into many. |
| 895 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 896 | static Token *tokenize(char *line) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 897 | { |
H. Peter Anvin | ca544db | 2008-10-19 19:30:11 -0700 | [diff] [blame] | 898 | char c, *p = line; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 899 | enum pp_token_type type; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 900 | Token *list = NULL; |
| 901 | Token *t, **tail = &list; |
| 902 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 903 | while (*line) { |
| 904 | p = line; |
| 905 | if (*p == '%') { |
| 906 | p++; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 907 | if (*p == '+' && !nasm_isdigit(p[1])) { |
| 908 | p++; |
| 909 | type = TOK_PASTE; |
| 910 | } else if (nasm_isdigit(*p) || |
| 911 | ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 912 | do { |
| 913 | p++; |
| 914 | } |
H. Peter Anvin | bda7a6e | 2008-06-21 10:23:17 -0700 | [diff] [blame] | 915 | while (nasm_isdigit(*p)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 916 | type = TOK_PREPROC_ID; |
| 917 | } else if (*p == '{') { |
| 918 | p++; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 919 | while (*p) { |
| 920 | if (*p == '}') |
| 921 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 922 | p[-1] = *p; |
| 923 | p++; |
| 924 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 925 | if (*p != '}') |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 926 | nasm_error(ERR_WARNING | ERR_PASS1, |
| 927 | "unterminated %%{ construct"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 928 | p[-1] = '\0'; |
| 929 | if (*p) |
| 930 | p++; |
| 931 | type = TOK_PREPROC_ID; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 932 | } else if (*p == '[') { |
| 933 | int lvl = 1; |
| 934 | line += 2; /* Skip the leading %[ */ |
| 935 | p++; |
| 936 | while (lvl && (c = *p++)) { |
| 937 | switch (c) { |
| 938 | case ']': |
| 939 | lvl--; |
| 940 | break; |
| 941 | case '%': |
| 942 | if (*p == '[') |
| 943 | lvl++; |
| 944 | break; |
| 945 | case '\'': |
| 946 | case '\"': |
| 947 | case '`': |
Cyrill Gorcunov | c6360a7 | 2010-07-13 13:32:19 +0400 | [diff] [blame] | 948 | p = nasm_skip_string(p - 1) + 1; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 949 | break; |
| 950 | default: |
| 951 | break; |
| 952 | } |
| 953 | } |
| 954 | p--; |
| 955 | if (*p) |
| 956 | *p++ = '\0'; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 957 | if (lvl) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 958 | nasm_error(ERR_NONFATAL|ERR_PASS1, |
| 959 | "unterminated %%[ construct"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 960 | type = TOK_INDIRECT; |
| 961 | } else if (*p == '?') { |
| 962 | type = TOK_PREPROC_Q; /* %? */ |
| 963 | p++; |
| 964 | if (*p == '?') { |
| 965 | type = TOK_PREPROC_QQ; /* %?? */ |
| 966 | p++; |
| 967 | } |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 968 | } else if (*p == '!') { |
| 969 | type = TOK_PREPROC_ID; |
| 970 | p++; |
| 971 | if (isidchar(*p)) { |
| 972 | do { |
| 973 | p++; |
| 974 | } |
| 975 | while (isidchar(*p)); |
| 976 | } else if (*p == '\'' || *p == '\"' || *p == '`') { |
| 977 | p = nasm_skip_string(p); |
| 978 | if (*p) |
| 979 | p++; |
| 980 | else |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 981 | nasm_error(ERR_NONFATAL|ERR_PASS1, |
| 982 | "unterminated %%! string"); |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 983 | } else { |
| 984 | /* %! without string or identifier */ |
| 985 | type = TOK_OTHER; /* Legacy behavior... */ |
| 986 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 987 | } else if (isidchar(*p) || |
| 988 | ((*p == '!' || *p == '%' || *p == '$') && |
| 989 | isidchar(p[1]))) { |
| 990 | do { |
| 991 | p++; |
| 992 | } |
| 993 | while (isidchar(*p)); |
| 994 | type = TOK_PREPROC_ID; |
| 995 | } else { |
| 996 | type = TOK_OTHER; |
| 997 | if (*p == '%') |
| 998 | p++; |
| 999 | } |
| 1000 | } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) { |
| 1001 | type = TOK_ID; |
| 1002 | p++; |
| 1003 | while (*p && isidchar(*p)) |
| 1004 | p++; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 1005 | } else if (*p == '\'' || *p == '"' || *p == '`') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1006 | /* |
| 1007 | * A string token. |
| 1008 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1009 | type = TOK_STRING; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1010 | p = nasm_skip_string(p); |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1011 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1012 | if (*p) { |
| 1013 | p++; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1014 | } else { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1015 | nasm_error(ERR_WARNING|ERR_PASS1, "unterminated string"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1016 | /* Handling unterminated strings by UNV */ |
| 1017 | /* type = -1; */ |
| 1018 | } |
Victor van den Elzen | fb5f251 | 2009-04-17 16:17:59 +0200 | [diff] [blame] | 1019 | } else if (p[0] == '$' && p[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1020 | type = TOK_OTHER; /* TOKEN_BASE */ |
Victor van den Elzen | fb5f251 | 2009-04-17 16:17:59 +0200 | [diff] [blame] | 1021 | p += 2; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1022 | } else if (isnumstart(*p)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1023 | bool is_hex = false; |
| 1024 | bool is_float = false; |
| 1025 | bool has_e = false; |
| 1026 | char c, *r; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1027 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1028 | /* |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1029 | * A numeric token. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1030 | */ |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1031 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1032 | if (*p == '$') { |
| 1033 | p++; |
| 1034 | is_hex = true; |
| 1035 | } |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1036 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1037 | for (;;) { |
| 1038 | c = *p++; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1039 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1040 | if (!is_hex && (c == 'e' || c == 'E')) { |
| 1041 | has_e = true; |
| 1042 | if (*p == '+' || *p == '-') { |
| 1043 | /* |
| 1044 | * e can only be followed by +/- if it is either a |
| 1045 | * prefixed hex number or a floating-point number |
| 1046 | */ |
| 1047 | p++; |
| 1048 | is_float = true; |
| 1049 | } |
| 1050 | } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') { |
| 1051 | is_hex = true; |
| 1052 | } else if (c == 'P' || c == 'p') { |
| 1053 | is_float = true; |
| 1054 | if (*p == '+' || *p == '-') |
| 1055 | p++; |
| 1056 | } else if (isnumchar(c) || c == '_') |
| 1057 | ; /* just advance */ |
| 1058 | else if (c == '.') { |
| 1059 | /* |
| 1060 | * we need to deal with consequences of the legacy |
| 1061 | * parser, like "1.nolist" being two tokens |
| 1062 | * (TOK_NUMBER, TOK_ID) here; at least give it |
| 1063 | * a shot for now. In the future, we probably need |
| 1064 | * a flex-based scanner with proper pattern matching |
| 1065 | * to do it as well as it can be done. Nothing in |
| 1066 | * the world is going to help the person who wants |
| 1067 | * 0x123.p16 interpreted as two tokens, though. |
| 1068 | */ |
| 1069 | r = p; |
| 1070 | while (*r == '_') |
| 1071 | r++; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1072 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1073 | if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) || |
| 1074 | (!is_hex && (*r == 'e' || *r == 'E')) || |
| 1075 | (*r == 'p' || *r == 'P')) { |
| 1076 | p = r; |
| 1077 | is_float = true; |
| 1078 | } else |
| 1079 | break; /* Terminate the token */ |
| 1080 | } else |
| 1081 | break; |
| 1082 | } |
| 1083 | p--; /* Point to first character beyond number */ |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1084 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1085 | if (p == line+1 && *line == '$') { |
| 1086 | type = TOK_OTHER; /* TOKEN_HERE */ |
| 1087 | } else { |
| 1088 | if (has_e && !is_hex) { |
| 1089 | /* 1e13 is floating-point, but 1e13h is not */ |
| 1090 | is_float = true; |
| 1091 | } |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 1092 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1093 | type = is_float ? TOK_FLOAT : TOK_NUMBER; |
| 1094 | } |
H. Peter Anvin | bda7a6e | 2008-06-21 10:23:17 -0700 | [diff] [blame] | 1095 | } else if (nasm_isspace(*p)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1096 | type = TOK_WHITESPACE; |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 1097 | p = nasm_skip_spaces(p); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1098 | /* |
| 1099 | * Whitespace just before end-of-line is discarded by |
| 1100 | * pretending it's a comment; whitespace just before a |
| 1101 | * comment gets lumped into the comment. |
| 1102 | */ |
| 1103 | if (!*p || *p == ';') { |
| 1104 | type = TOK_COMMENT; |
| 1105 | while (*p) |
| 1106 | p++; |
| 1107 | } |
| 1108 | } else if (*p == ';') { |
| 1109 | type = TOK_COMMENT; |
| 1110 | while (*p) |
| 1111 | p++; |
| 1112 | } else { |
| 1113 | /* |
| 1114 | * Anything else is an operator of some kind. We check |
| 1115 | * for all the double-character operators (>>, <<, //, |
| 1116 | * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1117 | * else is a single-character operator. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1118 | */ |
| 1119 | type = TOK_OTHER; |
| 1120 | if ((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[0] == '^' && p[1] == '^')) { |
| 1131 | p++; |
| 1132 | } |
| 1133 | p++; |
| 1134 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1135 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1136 | /* Handling unterminated string by UNV */ |
| 1137 | /*if (type == -1) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1138 | { |
| 1139 | *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1); |
| 1140 | t->text[p-line] = *line; |
| 1141 | tail = &t->next; |
| 1142 | } |
| 1143 | else */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1144 | if (type != TOK_COMMENT) { |
| 1145 | *tail = t = new_Token(NULL, type, line, p - line); |
| 1146 | tail = &t->next; |
| 1147 | } |
| 1148 | line = p; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1149 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1150 | return list; |
| 1151 | } |
| 1152 | |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1153 | /* |
| 1154 | * this function allocates a new managed block of memory and |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1155 | * returns a pointer to the block. The managed blocks are |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1156 | * deleted only all at once by the delete_Blocks function. |
| 1157 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1158 | static void *new_Block(size_t size) |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1159 | { |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1160 | Blocks *b = &blocks; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1161 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1162 | /* first, get to the end of the linked list */ |
| 1163 | while (b->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1164 | b = b->next; |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1165 | /* now allocate the requested chunk */ |
| 1166 | b->chunk = nasm_malloc(size); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1167 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1168 | /* now allocate a new block for the next request */ |
Cyrill Gorcunov | c31767c | 2014-06-28 02:22:17 +0400 | [diff] [blame] | 1169 | b->next = nasm_zalloc(sizeof(Blocks)); |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1170 | return b->chunk; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | /* |
| 1174 | * this function deletes all managed blocks of memory |
| 1175 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1176 | static void delete_Blocks(void) |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1177 | { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1178 | Blocks *a, *b = &blocks; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1179 | |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1180 | /* |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1181 | * keep in mind that the first block, pointed to by blocks |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1182 | * is a static and not dynamically allocated, so we don't |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1183 | * free it. |
| 1184 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1185 | while (b) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1186 | if (b->chunk) |
| 1187 | nasm_free(b->chunk); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1188 | a = b; |
| 1189 | b = b->next; |
| 1190 | if (a != &blocks) |
| 1191 | nasm_free(a); |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1192 | } |
Cyrill Gorcunov | dae24d7 | 2014-06-28 10:17:39 +0400 | [diff] [blame] | 1193 | memset(&blocks, 0, sizeof(blocks)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1194 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1195 | |
| 1196 | /* |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1197 | * 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] | 1198 | * 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] | 1199 | * also the a.mac and next elements to NULL. |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1200 | */ |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1201 | static Token *new_Token(Token * next, enum pp_token_type type, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1202 | const char *text, int txtlen) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1203 | { |
| 1204 | Token *t; |
| 1205 | int i; |
| 1206 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1207 | if (!freeTokens) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1208 | freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token)); |
| 1209 | for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++) |
| 1210 | freeTokens[i].next = &freeTokens[i + 1]; |
| 1211 | freeTokens[i].next = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1212 | } |
| 1213 | t = freeTokens; |
| 1214 | freeTokens = t->next; |
| 1215 | t->next = next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 1216 | t->a.mac = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1217 | t->type = type; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1218 | if (type == TOK_WHITESPACE || !text) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1219 | t->text = NULL; |
| 1220 | } else { |
| 1221 | if (txtlen == 0) |
| 1222 | txtlen = strlen(text); |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1223 | t->text = nasm_malloc(txtlen+1); |
| 1224 | memcpy(t->text, text, txtlen); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1225 | t->text[txtlen] = '\0'; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1226 | } |
| 1227 | return t; |
| 1228 | } |
| 1229 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1230 | static Token *delete_Token(Token * t) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1231 | { |
| 1232 | Token *next = t->next; |
| 1233 | nasm_free(t->text); |
H. Peter Anvin | 788e6c1 | 2002-04-30 21:02:01 +0000 | [diff] [blame] | 1234 | t->next = freeTokens; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1235 | freeTokens = t; |
| 1236 | return next; |
| 1237 | } |
| 1238 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1239 | /* |
| 1240 | * Convert a line of tokens back into text. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1241 | * If expand_locals is not zero, identifiers of the form "%$*xxx" |
| 1242 | * will be transformed into ..@ctxnum.xxx |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1243 | */ |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 1244 | static char *detoken(Token * tlist, bool expand_locals) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1245 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1246 | Token *t; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1247 | char *line, *p; |
H. Peter Anvin | b4daadc | 2008-01-21 16:31:57 -0800 | [diff] [blame] | 1248 | const char *q; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1249 | int len = 0; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1250 | |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1251 | list_for_each(t, tlist) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1252 | if (t->type == TOK_PREPROC_ID && t->text[1] == '!') { |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1253 | char *v; |
| 1254 | char *q = t->text; |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1255 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1256 | v = t->text + 2; |
| 1257 | if (*v == '\'' || *v == '\"' || *v == '`') { |
| 1258 | size_t len = nasm_unquote(v, NULL); |
| 1259 | size_t clen = strlen(v); |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1260 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1261 | if (len != clen) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1262 | nasm_error(ERR_NONFATAL | ERR_PASS1, |
| 1263 | "NUL character in %%! string"); |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1264 | v = NULL; |
| 1265 | } |
| 1266 | } |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1267 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1268 | if (v) { |
| 1269 | char *p = getenv(v); |
| 1270 | if (!p) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1271 | nasm_error(ERR_NONFATAL | ERR_PASS1, |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1272 | "nonexistent environment variable `%s'", v); |
| 1273 | p = ""; |
| 1274 | } |
| 1275 | t->text = nasm_strdup(p); |
| 1276 | } |
| 1277 | nasm_free(q); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1278 | } |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1279 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1280 | /* Expand local macros here and not during preprocessing */ |
| 1281 | if (expand_locals && |
| 1282 | t->type == TOK_PREPROC_ID && t->text && |
| 1283 | t->text[0] == '%' && t->text[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1284 | const char *q; |
| 1285 | char *p; |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1286 | Context *ctx = get_ctx(t->text, &q); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1287 | if (ctx) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1288 | char buffer[40]; |
Keith Kanios | 93f2e9a | 2007-04-14 00:10:59 +0000 | [diff] [blame] | 1289 | snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1290 | p = nasm_strcat(buffer, q); |
| 1291 | nasm_free(t->text); |
| 1292 | t->text = p; |
| 1293 | } |
| 1294 | } |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1295 | if (t->type == TOK_WHITESPACE) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1296 | len++; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1297 | else if (t->text) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1298 | len += strlen(t->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1299 | } |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1300 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1301 | p = line = nasm_malloc(len + 1); |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1302 | |
| 1303 | list_for_each(t, tlist) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1304 | if (t->type == TOK_WHITESPACE) { |
H. Peter Anvin | b4daadc | 2008-01-21 16:31:57 -0800 | [diff] [blame] | 1305 | *p++ = ' '; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1306 | } else if (t->text) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1307 | q = t->text; |
| 1308 | while (*q) |
| 1309 | *p++ = *q++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1310 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1311 | } |
| 1312 | *p = '\0'; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1313 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1314 | return line; |
| 1315 | } |
| 1316 | |
| 1317 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1318 | * A scanner, suitable for use by the expression evaluator, which |
| 1319 | * operates on a line of Tokens. Expects a pointer to a pointer to |
| 1320 | * the first token in the line to be passed in as its private_data |
| 1321 | * field. |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1322 | * |
| 1323 | * FIX: This really needs to be unified with stdscan. |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1324 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1325 | static int ppscan(void *private_data, struct tokenval *tokval) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1326 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1327 | Token **tlineptr = private_data; |
| 1328 | Token *tline; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1329 | char ourcopy[MAX_KEYWORD+1], *p, *r, *s; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1330 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1331 | do { |
| 1332 | tline = *tlineptr; |
| 1333 | *tlineptr = tline ? tline->next : NULL; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 1334 | } while (tline && (tline->type == TOK_WHITESPACE || |
| 1335 | tline->type == TOK_COMMENT)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1336 | |
| 1337 | if (!tline) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1338 | return tokval->t_type = TOKEN_EOS; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1339 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1340 | tokval->t_charptr = tline->text; |
| 1341 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1342 | if (tline->text[0] == '$' && !tline->text[1]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1343 | return tokval->t_type = TOKEN_HERE; |
H. Peter Anvin | 7cf897e | 2002-05-30 21:30:33 +0000 | [diff] [blame] | 1344 | if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1345 | return tokval->t_type = TOKEN_BASE; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1346 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1347 | if (tline->type == TOK_ID) { |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1348 | p = tokval->t_charptr = tline->text; |
| 1349 | if (p[0] == '$') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1350 | tokval->t_charptr++; |
| 1351 | return tokval->t_type = TOKEN_ID; |
| 1352 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1353 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1354 | for (r = p, s = ourcopy; *r; r++) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1355 | if (r >= p+MAX_KEYWORD) |
| 1356 | return tokval->t_type = TOKEN_ID; /* Not a keyword */ |
H. Peter Anvin | ac8f8fc | 2008-06-11 15:49:41 -0700 | [diff] [blame] | 1357 | *s++ = nasm_tolower(*r); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1358 | } |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1359 | *s = '\0'; |
| 1360 | /* right, so we have an identifier sitting in temp storage. now, |
| 1361 | * is it actually a register or instruction name, or what? */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1362 | return nasm_token_hash(ourcopy, tokval); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1365 | if (tline->type == TOK_NUMBER) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1366 | bool rn_error; |
| 1367 | tokval->t_integer = readnum(tline->text, &rn_error); |
| 1368 | tokval->t_charptr = tline->text; |
| 1369 | if (rn_error) |
| 1370 | return tokval->t_type = TOKEN_ERRNUM; |
| 1371 | else |
| 1372 | return tokval->t_type = TOKEN_NUM; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1373 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1374 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1375 | if (tline->type == TOK_FLOAT) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1376 | return tokval->t_type = TOKEN_FLOAT; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1379 | if (tline->type == TOK_STRING) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1380 | char bq, *ep; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1381 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1382 | bq = tline->text[0]; |
H. Peter Anvin | 1162704 | 2008-06-09 20:45:19 -0700 | [diff] [blame] | 1383 | tokval->t_charptr = tline->text; |
| 1384 | tokval->t_inttwo = nasm_unquote(tline->text, &ep); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 1385 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1386 | if (ep[0] != bq || ep[1] != '\0') |
| 1387 | return tokval->t_type = TOKEN_ERRSTR; |
| 1388 | else |
| 1389 | return tokval->t_type = TOKEN_STR; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1390 | } |
| 1391 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1392 | if (tline->type == TOK_OTHER) { |
| 1393 | if (!strcmp(tline->text, "<<")) |
| 1394 | return tokval->t_type = TOKEN_SHL; |
| 1395 | if (!strcmp(tline->text, ">>")) |
| 1396 | return tokval->t_type = TOKEN_SHR; |
| 1397 | if (!strcmp(tline->text, "//")) |
| 1398 | return tokval->t_type = TOKEN_SDIV; |
| 1399 | if (!strcmp(tline->text, "%%")) |
| 1400 | return tokval->t_type = TOKEN_SMOD; |
| 1401 | if (!strcmp(tline->text, "==")) |
| 1402 | return tokval->t_type = TOKEN_EQ; |
| 1403 | if (!strcmp(tline->text, "<>")) |
| 1404 | return tokval->t_type = TOKEN_NE; |
| 1405 | if (!strcmp(tline->text, "!=")) |
| 1406 | return tokval->t_type = TOKEN_NE; |
| 1407 | if (!strcmp(tline->text, "<=")) |
| 1408 | return tokval->t_type = TOKEN_LE; |
| 1409 | if (!strcmp(tline->text, ">=")) |
| 1410 | return tokval->t_type = TOKEN_GE; |
| 1411 | if (!strcmp(tline->text, "&&")) |
| 1412 | return tokval->t_type = TOKEN_DBL_AND; |
| 1413 | if (!strcmp(tline->text, "^^")) |
| 1414 | return tokval->t_type = TOKEN_DBL_XOR; |
| 1415 | if (!strcmp(tline->text, "||")) |
| 1416 | return tokval->t_type = TOKEN_DBL_OR; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
| 1419 | /* |
| 1420 | * We have no other options: just return the first character of |
| 1421 | * the token text. |
| 1422 | */ |
| 1423 | return tokval->t_type = tline->text[0]; |
| 1424 | } |
| 1425 | |
| 1426 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1427 | * Compare a string to the name of an existing macro; this is a |
| 1428 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 1429 | * depending on the value of the `casesense' parameter. |
| 1430 | */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 1431 | static int mstrcmp(const char *p, const char *q, bool casesense) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1432 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1433 | return casesense ? strcmp(p, q) : nasm_stricmp(p, q); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | /* |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1437 | * Compare a string to the name of an existing macro; this is a |
| 1438 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 1439 | * depending on the value of the `casesense' parameter. |
| 1440 | */ |
| 1441 | static int mmemcmp(const char *p, const char *q, size_t l, bool casesense) |
| 1442 | { |
| 1443 | return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l); |
| 1444 | } |
| 1445 | |
| 1446 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1447 | * Return the Context structure associated with a %$ token. Return |
| 1448 | * NULL, having _already_ reported an error condition, if the |
| 1449 | * context stack isn't deep enough for the supplied number of $ |
| 1450 | * signs. |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1451 | * |
| 1452 | * If "namep" is non-NULL, set it to the pointer to the macro name |
| 1453 | * tail, i.e. the part beyond %$... |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1454 | */ |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1455 | static Context *get_ctx(const char *name, const char **namep) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1456 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1457 | Context *ctx; |
| 1458 | int i; |
| 1459 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1460 | if (namep) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1461 | *namep = name; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1462 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1463 | if (!name || name[0] != '%' || name[1] != '$') |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1464 | return NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1465 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1466 | if (!cstk) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1467 | nasm_error(ERR_NONFATAL, "`%s': context stack is empty", name); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1468 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1469 | } |
| 1470 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1471 | name += 2; |
| 1472 | ctx = cstk; |
| 1473 | i = 0; |
| 1474 | while (ctx && *name == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1475 | name++; |
| 1476 | i++; |
| 1477 | ctx = ctx->next; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1478 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1479 | if (!ctx) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1480 | nasm_error(ERR_NONFATAL, "`%s': context stack is only" |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1481 | " %d level%s deep", name, i, (i == 1 ? "" : "s")); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1482 | return NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1483 | } |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1484 | |
| 1485 | if (namep) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1486 | *namep = name; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1487 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1488 | return ctx; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1489 | } |
| 1490 | |
| 1491 | /* |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1492 | * Check to see if a file is already in a string list |
| 1493 | */ |
| 1494 | static bool in_list(const StrList *list, const char *str) |
| 1495 | { |
| 1496 | while (list) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1497 | if (!strcmp(list->str, str)) |
| 1498 | return true; |
| 1499 | list = list->next; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1500 | } |
| 1501 | return false; |
| 1502 | } |
| 1503 | |
| 1504 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1505 | * Open an include file. This routine must always return a valid |
| 1506 | * file pointer if it returns - it's responsible for throwing an |
| 1507 | * ERR_FATAL and bombing out completely if not. It should also try |
| 1508 | * the include path one by one until it finds the file or reaches |
| 1509 | * the end of the path. |
| 1510 | */ |
H. Peter Anvin | 2b1c3b9 | 2008-06-06 10:38:46 -0700 | [diff] [blame] | 1511 | static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1512 | bool missing_ok) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1513 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1514 | FILE *fp; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1515 | char *prefix = ""; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1516 | IncPath *ip = ipath; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1517 | int len = strlen(file); |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1518 | size_t prefix_len = 0; |
| 1519 | StrList *sl; |
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) { |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1522 | sl = nasm_malloc(prefix_len+len+1+sizeof sl->next); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1523 | memcpy(sl->str, prefix, prefix_len); |
| 1524 | memcpy(sl->str+prefix_len, file, len+1); |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1525 | fp = fopen(sl->str, "r"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1526 | if (fp && dhead && !in_list(*dhead, sl->str)) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1527 | sl->next = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1528 | **dtail = sl; |
| 1529 | *dtail = &sl->next; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1530 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1531 | nasm_free(sl); |
| 1532 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1533 | if (fp) |
| 1534 | return fp; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1535 | if (!ip) { |
| 1536 | if (!missing_ok) |
| 1537 | break; |
| 1538 | prefix = NULL; |
| 1539 | } else { |
| 1540 | prefix = ip->path; |
| 1541 | ip = ip->next; |
| 1542 | } |
| 1543 | if (prefix) { |
| 1544 | prefix_len = strlen(prefix); |
| 1545 | } else { |
| 1546 | /* -MG given and file not found */ |
| 1547 | if (dhead && !in_list(*dhead, file)) { |
| 1548 | sl = nasm_malloc(len+1+sizeof sl->next); |
| 1549 | sl->next = NULL; |
| 1550 | strcpy(sl->str, file); |
| 1551 | **dtail = sl; |
| 1552 | *dtail = &sl->next; |
| 1553 | } |
| 1554 | return NULL; |
| 1555 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1556 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1557 | |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1558 | nasm_error(ERR_FATAL, "unable to open include file `%s'", file); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1559 | return NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1560 | } |
| 1561 | |
| 1562 | /* |
Fabian Giesen | 86d8756 | 2016-04-28 13:48:14 -0700 | [diff] [blame^] | 1563 | * Opens an include or input file. Public version, for use by modules |
| 1564 | * that get a file:lineno pair and need to look at the file again |
| 1565 | * (e.g. the CodeView debug backend). Returns NULL on failure. |
| 1566 | */ |
| 1567 | FILE *pp_input_fopen(const char *filename) |
| 1568 | { |
| 1569 | FILE *fp; |
| 1570 | StrList *xsl = NULL; |
| 1571 | StrList **xst = &xsl; |
| 1572 | |
| 1573 | fp = inc_fopen(filename, &xsl, &xst, true); |
| 1574 | if (xsl) |
| 1575 | nasm_free(xsl); |
| 1576 | return fp; |
| 1577 | } |
| 1578 | |
| 1579 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1580 | * 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] | 1581 | * 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] | 1582 | * return true if _any_ single-line macro of that name is defined. |
| 1583 | * Otherwise, will return true if a single-line macro with either |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1584 | * `nparam' or no parameters is defined. |
| 1585 | * |
| 1586 | * If a macro with precisely the right number of parameters is |
H. Peter Anvin | ef7468f | 2002-04-30 20:57:59 +0000 | [diff] [blame] | 1587 | * defined, or nparam is -1, the address of the definition structure |
| 1588 | * 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] | 1589 | * is NULL, no action will be taken regarding its contents, and no |
| 1590 | * error will occur. |
| 1591 | * |
| 1592 | * Note that this is also called with nparam zero to resolve |
| 1593 | * `ifdef'. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1594 | * |
| 1595 | * If you already know which context macro belongs to, you can pass |
| 1596 | * the context pointer as first parameter; if you won't but name begins |
| 1597 | * with %$ the context will be automatically computed. If all_contexts |
| 1598 | * is true, macro will be searched in outer contexts as well. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1599 | */ |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1600 | static bool |
H. Peter Anvin | b2a5fda | 2008-06-19 21:42:42 -0700 | [diff] [blame] | 1601 | smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn, |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1602 | bool nocase) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1603 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1604 | struct hash_table *smtbl; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1605 | SMacro *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1606 | |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1607 | if (ctx) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1608 | smtbl = &ctx->localmac; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1609 | } else if (name[0] == '%' && name[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1610 | if (cstk) |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1611 | ctx = get_ctx(name, &name); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1612 | if (!ctx) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1613 | return false; /* got to return _something_ */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1614 | smtbl = &ctx->localmac; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1615 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1616 | smtbl = &smacros; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1617 | } |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1618 | m = (SMacro *) hash_findix(smtbl, name); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1619 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1620 | while (m) { |
| 1621 | if (!mstrcmp(m->name, name, m->casesense && nocase) && |
Charles Crayne | 192d5b5 | 2007-10-18 19:02:42 -0700 | [diff] [blame] | 1622 | (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1623 | if (defn) { |
Charles Crayne | 192d5b5 | 2007-10-18 19:02:42 -0700 | [diff] [blame] | 1624 | if (nparam == (int) m->nparam || nparam == -1) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1625 | *defn = m; |
| 1626 | else |
| 1627 | *defn = NULL; |
| 1628 | } |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1629 | return true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1630 | } |
| 1631 | m = m->next; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1632 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1633 | |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1634 | return false; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
| 1637 | /* |
| 1638 | * Count and mark off the parameters in a multi-line macro call. |
| 1639 | * This is called both from within the multi-line macro expansion |
| 1640 | * code, and also to mark off the default parameters when provided |
| 1641 | * in a %macro definition line. |
| 1642 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1643 | static void count_mmac_params(Token * t, int *nparam, Token *** params) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1644 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1645 | int paramsize, brace; |
| 1646 | |
| 1647 | *nparam = paramsize = 0; |
| 1648 | *params = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1649 | while (t) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1650 | /* +1: we need space for the final NULL */ |
H. Peter Anvin | 91fb6f1 | 2008-09-01 10:56:33 -0700 | [diff] [blame] | 1651 | if (*nparam+1 >= paramsize) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1652 | paramsize += PARAM_DELTA; |
| 1653 | *params = nasm_realloc(*params, sizeof(**params) * paramsize); |
| 1654 | } |
| 1655 | skip_white_(t); |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1656 | brace = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1657 | if (tok_is_(t, "{")) |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1658 | brace++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1659 | (*params)[(*nparam)++] = t; |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1660 | if (brace) { |
| 1661 | while (brace && (t = t->next) != NULL) { |
| 1662 | if (tok_is_(t, "{")) |
| 1663 | brace++; |
| 1664 | else if (tok_is_(t, "}")) |
| 1665 | brace--; |
| 1666 | } |
| 1667 | |
| 1668 | if (t) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1669 | /* |
| 1670 | * Now we've found the closing brace, look further |
| 1671 | * for the comma. |
| 1672 | */ |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1673 | t = t->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1674 | skip_white_(t); |
| 1675 | if (tok_isnt_(t, ",")) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1676 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1677 | "braces do not enclose all of macro parameter"); |
| 1678 | while (tok_isnt_(t, ",")) |
| 1679 | t = t->next; |
| 1680 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1681 | } |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1682 | } else { |
| 1683 | while (tok_isnt_(t, ",")) |
| 1684 | t = t->next; |
| 1685 | } |
| 1686 | if (t) { /* got a comma/brace */ |
| 1687 | t = t->next; /* eat the comma */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1688 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1689 | } |
| 1690 | } |
| 1691 | |
| 1692 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1693 | * Determine whether one of the various `if' conditions is true or |
| 1694 | * not. |
| 1695 | * |
| 1696 | * We must free the tline we get passed. |
| 1697 | */ |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1698 | static bool if_condition(Token * tline, enum preproc_token ct) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1699 | { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1700 | enum pp_conditional i = PP_COND(ct); |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1701 | bool j; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1702 | Token *t, *tt, **tptr, *origline; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1703 | struct tokenval tokval; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1704 | expr *evalresult; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1705 | enum pp_token_type needtype; |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1706 | char *p; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1707 | |
| 1708 | origline = tline; |
| 1709 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1710 | switch (i) { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1711 | case PPC_IFCTX: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1712 | j = false; /* have we matched yet? */ |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1713 | while (true) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1714 | skip_white_(tline); |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1715 | if (!tline) |
| 1716 | break; |
| 1717 | if (tline->type != TOK_ID) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1718 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1719 | "`%s' expects context identifiers", pp_directives[ct]); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1720 | free_tlist(origline); |
| 1721 | return -1; |
| 1722 | } |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1723 | if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1724 | j = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1725 | tline = tline->next; |
| 1726 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1727 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1728 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1729 | case PPC_IFDEF: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1730 | j = false; /* have we matched yet? */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1731 | while (tline) { |
| 1732 | skip_white_(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1733 | if (!tline || (tline->type != TOK_ID && |
| 1734 | (tline->type != TOK_PREPROC_ID || |
| 1735 | tline->text[1] != '$'))) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1736 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1737 | "`%s' expects macro identifiers", pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1738 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1739 | } |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1740 | if (smacro_defined(NULL, tline->text, 0, NULL, true)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1741 | j = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1742 | tline = tline->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1743 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1744 | break; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1745 | |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1746 | case PPC_IFENV: |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1747 | tline = expand_smacro(tline); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1748 | j = false; /* have we matched yet? */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1749 | while (tline) { |
| 1750 | skip_white_(tline); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1751 | if (!tline || (tline->type != TOK_ID && |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1752 | tline->type != TOK_STRING && |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1753 | (tline->type != TOK_PREPROC_ID || |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1754 | tline->text[1] != '!'))) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1755 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1756 | "`%s' expects environment variable names", |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1757 | pp_directives[ct]); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1758 | goto fail; |
| 1759 | } |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1760 | p = tline->text; |
| 1761 | if (tline->type == TOK_PREPROC_ID) |
| 1762 | p += 2; /* Skip leading %! */ |
| 1763 | if (*p == '\'' || *p == '\"' || *p == '`') |
| 1764 | nasm_unquote_cstr(p, ct); |
| 1765 | if (getenv(p)) |
| 1766 | j = true; |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1767 | tline = tline->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1768 | } |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1769 | break; |
| 1770 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1771 | case PPC_IFIDN: |
| 1772 | case PPC_IFIDNI: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1773 | tline = expand_smacro(tline); |
| 1774 | t = tt = tline; |
| 1775 | while (tok_isnt_(tt, ",")) |
| 1776 | tt = tt->next; |
| 1777 | if (!tt) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1778 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1779 | "`%s' expects two comma-separated arguments", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1780 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1781 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1782 | } |
| 1783 | tt = tt->next; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1784 | j = true; /* assume equality unless proved not */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1785 | while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) { |
| 1786 | if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1787 | nasm_error(ERR_NONFATAL, "`%s': more than one comma on line", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1788 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1789 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1790 | } |
| 1791 | if (t->type == TOK_WHITESPACE) { |
| 1792 | t = t->next; |
| 1793 | continue; |
| 1794 | } |
| 1795 | if (tt->type == TOK_WHITESPACE) { |
| 1796 | tt = tt->next; |
| 1797 | continue; |
| 1798 | } |
| 1799 | if (tt->type != t->type) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1800 | j = false; /* found mismatching tokens */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1801 | break; |
| 1802 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1803 | /* When comparing strings, need to unquote them first */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1804 | if (t->type == TOK_STRING) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1805 | size_t l1 = nasm_unquote(t->text, NULL); |
| 1806 | size_t l2 = nasm_unquote(tt->text, NULL); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 1807 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1808 | if (l1 != l2) { |
| 1809 | j = false; |
| 1810 | break; |
| 1811 | } |
| 1812 | if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) { |
| 1813 | j = false; |
| 1814 | break; |
| 1815 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1816 | } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1817 | j = false; /* found mismatching tokens */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1818 | break; |
| 1819 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1820 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1821 | t = t->next; |
| 1822 | tt = tt->next; |
| 1823 | } |
| 1824 | if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1825 | j = false; /* trailing gunk on one end or other */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1826 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1827 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1828 | case PPC_IFMACRO: |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1829 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1830 | bool found = false; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1831 | MMacro searching, *mmac; |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 1832 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1833 | skip_white_(tline); |
| 1834 | tline = expand_id(tline); |
| 1835 | if (!tok_type_(tline, TOK_ID)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1836 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1837 | "`%s' expects a macro name", pp_directives[ct]); |
| 1838 | goto fail; |
| 1839 | } |
| 1840 | searching.name = nasm_strdup(tline->text); |
| 1841 | searching.casesense = true; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1842 | searching.plus = false; |
| 1843 | searching.nolist = false; |
| 1844 | searching.in_progress = 0; |
| 1845 | searching.max_depth = 0; |
| 1846 | searching.rep_nest = NULL; |
| 1847 | searching.nparam_min = 0; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1848 | searching.nparam_max = INT_MAX; |
| 1849 | tline = expand_smacro(tline->next); |
| 1850 | skip_white_(tline); |
| 1851 | if (!tline) { |
| 1852 | } else if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1853 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1854 | "`%s' expects a parameter count or nothing", |
| 1855 | pp_directives[ct]); |
| 1856 | } else { |
| 1857 | searching.nparam_min = searching.nparam_max = |
| 1858 | readnum(tline->text, &j); |
| 1859 | if (j) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1860 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1861 | "unable to parse parameter count `%s'", |
| 1862 | tline->text); |
| 1863 | } |
| 1864 | if (tline && tok_is_(tline->next, "-")) { |
| 1865 | tline = tline->next->next; |
| 1866 | if (tok_is_(tline, "*")) |
| 1867 | searching.nparam_max = INT_MAX; |
| 1868 | else if (!tok_type_(tline, TOK_NUMBER)) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1869 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1870 | "`%s' expects a parameter count after `-'", |
| 1871 | pp_directives[ct]); |
| 1872 | else { |
| 1873 | searching.nparam_max = readnum(tline->text, &j); |
| 1874 | if (j) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1875 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1876 | "unable to parse parameter count `%s'", |
| 1877 | tline->text); |
| 1878 | if (searching.nparam_min > searching.nparam_max) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1879 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1880 | "minimum parameter count exceeds maximum"); |
| 1881 | } |
| 1882 | } |
| 1883 | if (tline && tok_is_(tline->next, "+")) { |
| 1884 | tline = tline->next; |
| 1885 | searching.plus = true; |
| 1886 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1887 | mmac = (MMacro *) hash_findix(&mmacros, searching.name); |
| 1888 | while (mmac) { |
| 1889 | if (!strcmp(mmac->name, searching.name) && |
| 1890 | (mmac->nparam_min <= searching.nparam_max |
| 1891 | || searching.plus) |
| 1892 | && (searching.nparam_min <= mmac->nparam_max |
| 1893 | || mmac->plus)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1894 | found = true; |
| 1895 | break; |
| 1896 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1897 | mmac = mmac->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1898 | } |
| 1899 | if (tline && tline->next) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1900 | nasm_error(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1901 | "trailing garbage after %%ifmacro ignored"); |
| 1902 | nasm_free(searching.name); |
| 1903 | j = found; |
| 1904 | break; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1905 | } |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 1906 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1907 | case PPC_IFID: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1908 | needtype = TOK_ID; |
| 1909 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1910 | case PPC_IFNUM: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1911 | needtype = TOK_NUMBER; |
| 1912 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1913 | case PPC_IFSTR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1914 | needtype = TOK_STRING; |
| 1915 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1916 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1917 | iftype: |
| 1918 | t = tline = expand_smacro(tline); |
H. Peter Anvin | d85d250 | 2008-05-04 17:53:31 -0700 | [diff] [blame] | 1919 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1920 | while (tok_type_(t, TOK_WHITESPACE) || |
| 1921 | (needtype == TOK_NUMBER && |
| 1922 | tok_type_(t, TOK_OTHER) && |
| 1923 | (t->text[0] == '-' || t->text[0] == '+') && |
| 1924 | !t->text[1])) |
| 1925 | t = t->next; |
H. Peter Anvin | d85d250 | 2008-05-04 17:53:31 -0700 | [diff] [blame] | 1926 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1927 | j = tok_type_(t, needtype); |
| 1928 | break; |
H. Peter Anvin | cbf768d | 2008-02-16 16:41:25 -0800 | [diff] [blame] | 1929 | |
| 1930 | case PPC_IFTOKEN: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1931 | t = tline = expand_smacro(tline); |
| 1932 | while (tok_type_(t, TOK_WHITESPACE)) |
| 1933 | t = t->next; |
H. Peter Anvin | cbf768d | 2008-02-16 16:41:25 -0800 | [diff] [blame] | 1934 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1935 | j = false; |
| 1936 | if (t) { |
| 1937 | t = t->next; /* Skip the actual token */ |
| 1938 | while (tok_type_(t, TOK_WHITESPACE)) |
| 1939 | t = t->next; |
| 1940 | j = !t; /* Should be nothing left */ |
| 1941 | } |
| 1942 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1943 | |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 1944 | case PPC_IFEMPTY: |
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 | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 1948 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1949 | j = !t; /* Should be empty */ |
| 1950 | break; |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 1951 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1952 | case PPC_IF: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1953 | t = tline = expand_smacro(tline); |
| 1954 | tptr = &t; |
| 1955 | tokval.t_type = TOKEN_INVALID; |
| 1956 | evalresult = evaluate(ppscan, tptr, &tokval, |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1957 | NULL, pass | CRITICAL, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1958 | if (!evalresult) |
| 1959 | return -1; |
| 1960 | if (tokval.t_type) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1961 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1962 | "trailing garbage after expression ignored"); |
| 1963 | if (!is_simple(evalresult)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1964 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1965 | "non-constant value given to `%s'", pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1966 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1967 | } |
Chuck Crayne | 60ae75d | 2007-05-02 01:59:16 +0000 | [diff] [blame] | 1968 | j = reloc_value(evalresult) != 0; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1969 | break; |
H. Peter Anvin | 95e2882 | 2007-09-12 04:20:08 +0000 | [diff] [blame] | 1970 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1971 | default: |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1972 | nasm_error(ERR_FATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1973 | "preprocessor directive `%s' not yet implemented", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1974 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1975 | goto fail; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1976 | } |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1977 | |
| 1978 | free_tlist(origline); |
| 1979 | return j ^ PP_NEGATIVE(ct); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1980 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1981 | fail: |
| 1982 | free_tlist(origline); |
| 1983 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1984 | } |
| 1985 | |
| 1986 | /* |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 1987 | * Common code for defining an smacro |
| 1988 | */ |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1989 | static bool define_smacro(Context *ctx, const char *mname, bool casesense, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1990 | int nparam, Token *expansion) |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 1991 | { |
| 1992 | SMacro *smac, **smhead; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1993 | struct hash_table *smtbl; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1994 | |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 1995 | if (smacro_defined(ctx, mname, nparam, &smac, casesense)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1996 | if (!smac) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1997 | nasm_error(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1998 | "single-line macro `%s' defined both with and" |
| 1999 | " without parameters", mname); |
| 2000 | /* |
| 2001 | * Some instances of the old code considered this a failure, |
| 2002 | * some others didn't. What is the right thing to do here? |
| 2003 | */ |
| 2004 | free_tlist(expansion); |
| 2005 | return false; /* Failure */ |
| 2006 | } else { |
| 2007 | /* |
| 2008 | * We're redefining, so we have to take over an |
| 2009 | * existing SMacro structure. This means freeing |
| 2010 | * what was already in it. |
| 2011 | */ |
| 2012 | nasm_free(smac->name); |
| 2013 | free_tlist(smac->expansion); |
| 2014 | } |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2015 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2016 | smtbl = ctx ? &ctx->localmac : &smacros; |
| 2017 | smhead = (SMacro **) hash_findi_add(smtbl, mname); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2018 | smac = nasm_malloc(sizeof(SMacro)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2019 | smac->next = *smhead; |
| 2020 | *smhead = smac; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2021 | } |
| 2022 | smac->name = nasm_strdup(mname); |
| 2023 | smac->casesense = casesense; |
| 2024 | smac->nparam = nparam; |
| 2025 | smac->expansion = expansion; |
| 2026 | smac->in_progress = false; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2027 | return true; /* Success */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2028 | } |
| 2029 | |
| 2030 | /* |
| 2031 | * Undefine an smacro |
| 2032 | */ |
| 2033 | static void undef_smacro(Context *ctx, const char *mname) |
| 2034 | { |
| 2035 | SMacro **smhead, *s, **sp; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2036 | struct hash_table *smtbl; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2037 | |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2038 | smtbl = ctx ? &ctx->localmac : &smacros; |
| 2039 | smhead = (SMacro **)hash_findi(smtbl, mname, NULL); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2040 | |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2041 | if (smhead) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2042 | /* |
| 2043 | * We now have a macro name... go hunt for it. |
| 2044 | */ |
| 2045 | sp = smhead; |
| 2046 | while ((s = *sp) != NULL) { |
| 2047 | if (!mstrcmp(s->name, mname, s->casesense)) { |
| 2048 | *sp = s->next; |
| 2049 | nasm_free(s->name); |
| 2050 | free_tlist(s->expansion); |
| 2051 | nasm_free(s); |
| 2052 | } else { |
| 2053 | sp = &s->next; |
| 2054 | } |
| 2055 | } |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2056 | } |
| 2057 | } |
| 2058 | |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2059 | /* |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2060 | * Parse a mmacro specification. |
| 2061 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2062 | 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] | 2063 | { |
| 2064 | bool err; |
| 2065 | |
| 2066 | tline = tline->next; |
| 2067 | skip_white_(tline); |
| 2068 | tline = expand_id(tline); |
| 2069 | if (!tok_type_(tline, TOK_ID)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2070 | nasm_error(ERR_NONFATAL, "`%s' expects a macro name", directive); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2071 | return false; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2072 | } |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2073 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2074 | def->prev = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2075 | def->name = nasm_strdup(tline->text); |
| 2076 | def->plus = false; |
| 2077 | def->nolist = false; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2078 | def->in_progress = 0; |
| 2079 | def->rep_nest = NULL; |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2080 | def->nparam_min = 0; |
| 2081 | def->nparam_max = 0; |
| 2082 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2083 | tline = expand_smacro(tline->next); |
| 2084 | skip_white_(tline); |
| 2085 | if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2086 | nasm_error(ERR_NONFATAL, "`%s' expects a parameter count", directive); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2087 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2088 | def->nparam_min = def->nparam_max = |
| 2089 | readnum(tline->text, &err); |
| 2090 | if (err) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2091 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2092 | "unable to parse parameter count `%s'", tline->text); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2093 | } |
| 2094 | if (tline && tok_is_(tline->next, "-")) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2095 | tline = tline->next->next; |
| 2096 | if (tok_is_(tline, "*")) { |
| 2097 | def->nparam_max = INT_MAX; |
| 2098 | } else if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2099 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2100 | "`%s' expects a parameter count after `-'", directive); |
| 2101 | } else { |
| 2102 | def->nparam_max = readnum(tline->text, &err); |
| 2103 | if (err) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2104 | nasm_error(ERR_NONFATAL, "unable to parse parameter count `%s'", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2105 | tline->text); |
| 2106 | } |
| 2107 | if (def->nparam_min > def->nparam_max) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2108 | nasm_error(ERR_NONFATAL, "minimum parameter count exceeds maximum"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2109 | } |
| 2110 | } |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2111 | } |
| 2112 | if (tline && tok_is_(tline->next, "+")) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2113 | tline = tline->next; |
| 2114 | def->plus = true; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2115 | } |
| 2116 | if (tline && tok_type_(tline->next, TOK_ID) && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2117 | !nasm_stricmp(tline->next->text, ".nolist")) { |
| 2118 | tline = tline->next; |
| 2119 | def->nolist = true; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2120 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2121 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2122 | /* |
| 2123 | * Handle default parameters. |
| 2124 | */ |
| 2125 | if (tline && tline->next) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2126 | def->dlist = tline->next; |
| 2127 | tline->next = NULL; |
| 2128 | count_mmac_params(def->dlist, &def->ndefs, &def->defaults); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2129 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2130 | def->dlist = NULL; |
| 2131 | def->defaults = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2132 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2133 | def->expansion = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2134 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2135 | if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2136 | !def->plus) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2137 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2138 | "too many default macro parameters"); |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2139 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2140 | return true; |
| 2141 | } |
| 2142 | |
| 2143 | |
| 2144 | /* |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2145 | * Decode a size directive |
| 2146 | */ |
| 2147 | static int parse_size(const char *str) { |
| 2148 | static const char *size_names[] = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2149 | { "byte", "dword", "oword", "qword", "tword", "word", "yword" }; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2150 | static const int sizes[] = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2151 | { 0, 1, 4, 16, 8, 10, 2, 32 }; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2152 | |
Cyrill Gorcunov | a731924 | 2010-06-03 22:04:36 +0400 | [diff] [blame] | 2153 | return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1]; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2154 | } |
| 2155 | |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2156 | /** |
| 2157 | * find and process preprocessor directive in passed line |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2158 | * Find out if a line contains a preprocessor directive, and deal |
| 2159 | * with it if so. |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2160 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2161 | * If a directive _is_ found, it is the responsibility of this routine |
| 2162 | * (and not the caller) to free_tlist() the line. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2163 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2164 | * @param tline a pointer to the current tokeninzed line linked list |
| 2165 | * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2166 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2167 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2168 | static int do_directive(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2169 | { |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2170 | enum preproc_token i; |
| 2171 | int j; |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 2172 | bool err; |
| 2173 | int nparam; |
| 2174 | bool nolist; |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 2175 | bool casesense; |
H. Peter Anvin | 8cfdb9d | 2007-09-14 18:36:01 -0700 | [diff] [blame] | 2176 | int k, m; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2177 | int offset; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 2178 | char *p, *pp; |
| 2179 | const char *mname; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2180 | Include *inc; |
| 2181 | Context *ctx; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2182 | Cond *cond; |
| 2183 | MMacro *mmac, **mmhead; |
Jin Kyu Song | c9486b9 | 2013-10-28 17:07:57 -0700 | [diff] [blame] | 2184 | Token *t = NULL, *tt, *param_start, *macro_start, *last, **tptr, *origline; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2185 | Line *l; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2186 | struct tokenval tokval; |
| 2187 | expr *evalresult; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2188 | MMacro *tmp_defining; /* Used when manipulating rep_nest */ |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 2189 | int64_t count; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 2190 | size_t len; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2191 | int severity; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2192 | |
| 2193 | origline = tline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2194 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2195 | skip_white_(tline); |
H. Peter Anvin | f2936d7 | 2008-06-04 15:11:23 -0700 | [diff] [blame] | 2196 | if (!tline || !tok_type_(tline, TOK_PREPROC_ID) || |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2197 | (tline->text[1] == '%' || tline->text[1] == '$' |
| 2198 | || tline->text[1] == '!')) |
| 2199 | return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2200 | |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2201 | i = pp_token_hash(tline->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2202 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2203 | /* |
| 2204 | * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO |
| 2205 | * since they are known to be buggy at moment, we need to fix them |
| 2206 | * in future release (2.09-2.10) |
| 2207 | */ |
Philipp Kloke | b432f57 | 2013-03-31 12:01:23 +0200 | [diff] [blame] | 2208 | if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2209 | nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'", |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2210 | tline->text); |
| 2211 | return NO_DIRECTIVE_FOUND; |
| 2212 | } |
| 2213 | |
| 2214 | /* |
| 2215 | * If we're in a non-emitting branch of a condition construct, |
| 2216 | * or walking to the end of an already terminated %rep block, |
| 2217 | * we should ignore all directives except for condition |
| 2218 | * directives. |
| 2219 | */ |
| 2220 | if (((istk->conds && !emitting(istk->conds->state)) || |
| 2221 | (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) { |
| 2222 | return NO_DIRECTIVE_FOUND; |
| 2223 | } |
| 2224 | |
| 2225 | /* |
| 2226 | * If we're defining a macro or reading a %rep block, we should |
| 2227 | * ignore all directives except for %macro/%imacro (which nest), |
| 2228 | * %endm/%endmacro, and (only if we're in a %rep block) %endrep. |
| 2229 | * If we're in a %rep block, another %rep nests, so should be let through. |
| 2230 | */ |
| 2231 | if (defining && i != PP_MACRO && i != PP_IMACRO && |
| 2232 | i != PP_RMACRO && i != PP_IRMACRO && |
| 2233 | i != PP_ENDMACRO && i != PP_ENDM && |
| 2234 | (defining->name || (i != PP_ENDREP && i != PP_REP))) { |
| 2235 | return NO_DIRECTIVE_FOUND; |
| 2236 | } |
| 2237 | |
| 2238 | if (defining) { |
| 2239 | if (i == PP_MACRO || i == PP_IMACRO || |
| 2240 | i == PP_RMACRO || i == PP_IRMACRO) { |
| 2241 | nested_mac_count++; |
| 2242 | return NO_DIRECTIVE_FOUND; |
| 2243 | } else if (nested_mac_count > 0) { |
| 2244 | if (i == PP_ENDMACRO) { |
| 2245 | nested_mac_count--; |
| 2246 | return NO_DIRECTIVE_FOUND; |
| 2247 | } |
| 2248 | } |
| 2249 | if (!defining->name) { |
| 2250 | if (i == PP_REP) { |
| 2251 | nested_rep_count++; |
| 2252 | return NO_DIRECTIVE_FOUND; |
| 2253 | } else if (nested_rep_count > 0) { |
| 2254 | if (i == PP_ENDREP) { |
| 2255 | nested_rep_count--; |
| 2256 | return NO_DIRECTIVE_FOUND; |
| 2257 | } |
| 2258 | } |
| 2259 | } |
| 2260 | } |
| 2261 | |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2262 | switch (i) { |
| 2263 | case PP_INVALID: |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2264 | nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'", |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2265 | tline->text); |
| 2266 | return NO_DIRECTIVE_FOUND; /* didn't get it */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2267 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2268 | case PP_STACKSIZE: |
| 2269 | /* Directive to tell NASM what the default stack size is. The |
| 2270 | * default is for a 16-bit stack, and this can be overriden with |
| 2271 | * %stacksize large. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2272 | */ |
| 2273 | tline = tline->next; |
| 2274 | if (tline && tline->type == TOK_WHITESPACE) |
| 2275 | tline = tline->next; |
| 2276 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2277 | nasm_error(ERR_NONFATAL, "`%%stacksize' missing size parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2278 | free_tlist(origline); |
| 2279 | return DIRECTIVE_FOUND; |
| 2280 | } |
| 2281 | if (nasm_stricmp(tline->text, "flat") == 0) { |
| 2282 | /* All subsequent ARG directives are for a 32-bit stack */ |
| 2283 | StackSize = 4; |
| 2284 | StackPointer = "ebp"; |
| 2285 | ArgOffset = 8; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2286 | LocalOffset = 0; |
Charles Crayne | 7eaf919 | 2007-11-08 22:11:14 -0800 | [diff] [blame] | 2287 | } else if (nasm_stricmp(tline->text, "flat64") == 0) { |
| 2288 | /* All subsequent ARG directives are for a 64-bit stack */ |
| 2289 | StackSize = 8; |
| 2290 | StackPointer = "rbp"; |
Per Jessen | 53252e0 | 2010-02-11 00:16:59 +0300 | [diff] [blame] | 2291 | ArgOffset = 16; |
Charles Crayne | 7eaf919 | 2007-11-08 22:11:14 -0800 | [diff] [blame] | 2292 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2293 | } else if (nasm_stricmp(tline->text, "large") == 0) { |
| 2294 | /* All subsequent ARG directives are for a 16-bit stack, |
| 2295 | * far function call. |
| 2296 | */ |
| 2297 | StackSize = 2; |
| 2298 | StackPointer = "bp"; |
| 2299 | ArgOffset = 4; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2300 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2301 | } else if (nasm_stricmp(tline->text, "small") == 0) { |
| 2302 | /* All subsequent ARG directives are for a 16-bit stack, |
| 2303 | * far function call. We don't support near functions. |
| 2304 | */ |
| 2305 | StackSize = 2; |
| 2306 | StackPointer = "bp"; |
| 2307 | ArgOffset = 6; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2308 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2309 | } else { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2310 | nasm_error(ERR_NONFATAL, "`%%stacksize' invalid size type"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2311 | free_tlist(origline); |
| 2312 | return DIRECTIVE_FOUND; |
| 2313 | } |
| 2314 | free_tlist(origline); |
| 2315 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2316 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2317 | case PP_ARG: |
| 2318 | /* TASM like ARG directive to define arguments to functions, in |
| 2319 | * the following form: |
| 2320 | * |
| 2321 | * ARG arg1:WORD, arg2:DWORD, arg4:QWORD |
| 2322 | */ |
| 2323 | offset = ArgOffset; |
| 2324 | do { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 2325 | char *arg, directive[256]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2326 | int size = StackSize; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2327 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2328 | /* Find the argument name */ |
| 2329 | tline = tline->next; |
| 2330 | if (tline && tline->type == TOK_WHITESPACE) |
| 2331 | tline = tline->next; |
| 2332 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2333 | nasm_error(ERR_NONFATAL, "`%%arg' missing argument parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2334 | free_tlist(origline); |
| 2335 | return DIRECTIVE_FOUND; |
| 2336 | } |
| 2337 | arg = tline->text; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2338 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2339 | /* Find the argument size type */ |
| 2340 | tline = tline->next; |
| 2341 | if (!tline || tline->type != TOK_OTHER |
| 2342 | || tline->text[0] != ':') { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2343 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2344 | "Syntax error processing `%%arg' directive"); |
| 2345 | free_tlist(origline); |
| 2346 | return DIRECTIVE_FOUND; |
| 2347 | } |
| 2348 | tline = tline->next; |
| 2349 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2350 | nasm_error(ERR_NONFATAL, "`%%arg' missing size type parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2351 | free_tlist(origline); |
| 2352 | return DIRECTIVE_FOUND; |
| 2353 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2354 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2355 | /* Allow macro expansion of type parameter */ |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2356 | tt = tokenize(tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2357 | tt = expand_smacro(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2358 | size = parse_size(tt->text); |
| 2359 | if (!size) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2360 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2361 | "Invalid size type for `%%arg' missing directive"); |
| 2362 | free_tlist(tt); |
| 2363 | free_tlist(origline); |
| 2364 | return DIRECTIVE_FOUND; |
| 2365 | } |
| 2366 | free_tlist(tt); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2367 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2368 | /* Round up to even stack slots */ |
| 2369 | size = ALIGN(size, StackSize); |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2370 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2371 | /* Now define the macro for the argument */ |
| 2372 | snprintf(directive, sizeof(directive), "%%define %s (%s+%d)", |
| 2373 | arg, StackPointer, offset); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2374 | do_directive(tokenize(directive)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2375 | offset += size; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2376 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2377 | /* Move to the next argument in the list */ |
| 2378 | tline = tline->next; |
| 2379 | if (tline && tline->type == TOK_WHITESPACE) |
| 2380 | tline = tline->next; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2381 | } while (tline && tline->type == TOK_OTHER && tline->text[0] == ','); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2382 | ArgOffset = offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2383 | free_tlist(origline); |
| 2384 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2385 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2386 | case PP_LOCAL: |
| 2387 | /* TASM like LOCAL directive to define local variables for a |
| 2388 | * function, in the following form: |
| 2389 | * |
| 2390 | * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize |
| 2391 | * |
| 2392 | * The '= LocalSize' at the end is ignored by NASM, but is |
| 2393 | * required by TASM to define the local parameter size (and used |
| 2394 | * by the TASM macro package). |
| 2395 | */ |
| 2396 | offset = LocalOffset; |
| 2397 | do { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 2398 | char *local, directive[256]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2399 | int size = StackSize; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2400 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2401 | /* Find the argument name */ |
| 2402 | tline = tline->next; |
| 2403 | if (tline && tline->type == TOK_WHITESPACE) |
| 2404 | tline = tline->next; |
| 2405 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2406 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2407 | "`%%local' missing argument parameter"); |
| 2408 | free_tlist(origline); |
| 2409 | return DIRECTIVE_FOUND; |
| 2410 | } |
| 2411 | local = tline->text; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2412 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2413 | /* Find the argument size type */ |
| 2414 | tline = tline->next; |
| 2415 | if (!tline || tline->type != TOK_OTHER |
| 2416 | || tline->text[0] != ':') { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2417 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2418 | "Syntax error processing `%%local' directive"); |
| 2419 | free_tlist(origline); |
| 2420 | return DIRECTIVE_FOUND; |
| 2421 | } |
| 2422 | tline = tline->next; |
| 2423 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2424 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2425 | "`%%local' missing size type parameter"); |
| 2426 | free_tlist(origline); |
| 2427 | return DIRECTIVE_FOUND; |
| 2428 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2429 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2430 | /* Allow macro expansion of type parameter */ |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2431 | tt = tokenize(tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2432 | tt = expand_smacro(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2433 | size = parse_size(tt->text); |
| 2434 | if (!size) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2435 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2436 | "Invalid size type for `%%local' missing directive"); |
| 2437 | free_tlist(tt); |
| 2438 | free_tlist(origline); |
| 2439 | return DIRECTIVE_FOUND; |
| 2440 | } |
| 2441 | free_tlist(tt); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2442 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2443 | /* Round up to even stack slots */ |
| 2444 | size = ALIGN(size, StackSize); |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2445 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2446 | offset += size; /* Negative offset, increment before */ |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2447 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2448 | /* Now define the macro for the argument */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2449 | snprintf(directive, sizeof(directive), "%%define %s (%s-%d)", |
| 2450 | local, StackPointer, offset); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2451 | do_directive(tokenize(directive)); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2452 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2453 | /* Now define the assign to setup the enter_c macro correctly */ |
| 2454 | snprintf(directive, sizeof(directive), |
| 2455 | "%%assign %%$localsize %%$localsize+%d", size); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2456 | do_directive(tokenize(directive)); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2457 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2458 | /* Move to the next argument in the list */ |
| 2459 | tline = tline->next; |
| 2460 | if (tline && tline->type == TOK_WHITESPACE) |
| 2461 | tline = tline->next; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2462 | } while (tline && tline->type == TOK_OTHER && tline->text[0] == ','); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2463 | LocalOffset = offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2464 | free_tlist(origline); |
| 2465 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2466 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2467 | case PP_CLEAR: |
| 2468 | if (tline->next) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2469 | nasm_error(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2470 | "trailing garbage after `%%clear' ignored"); |
| 2471 | free_macros(); |
| 2472 | init_macros(); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2473 | free_tlist(origline); |
| 2474 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2475 | |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2476 | case PP_DEPEND: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2477 | t = tline->next = expand_smacro(tline->next); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2478 | skip_white_(t); |
| 2479 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2480 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2481 | nasm_error(ERR_NONFATAL, "`%%depend' expects a file name"); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2482 | free_tlist(origline); |
| 2483 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2484 | } |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2485 | if (t->next) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2486 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2487 | "trailing garbage after `%%depend' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2488 | p = t->text; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2489 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2490 | nasm_unquote_cstr(p, i); |
| 2491 | if (dephead && !in_list(*dephead, p)) { |
| 2492 | StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next); |
| 2493 | sl->next = NULL; |
| 2494 | strcpy(sl->str, p); |
| 2495 | *deptail = sl; |
| 2496 | deptail = &sl->next; |
| 2497 | } |
| 2498 | free_tlist(origline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2499 | return DIRECTIVE_FOUND; |
| 2500 | |
| 2501 | case PP_INCLUDE: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2502 | t = tline->next = expand_smacro(tline->next); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2503 | skip_white_(t); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2504 | |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2505 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2506 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2507 | nasm_error(ERR_NONFATAL, "`%%include' expects a file name"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2508 | free_tlist(origline); |
| 2509 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2510 | } |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2511 | if (t->next) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2512 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2513 | "trailing garbage after `%%include' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2514 | p = t->text; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2515 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2516 | nasm_unquote_cstr(p, i); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2517 | inc = nasm_malloc(sizeof(Include)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2518 | inc->next = istk; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2519 | inc->conds = NULL; |
H. Peter Anvin | 2b1c3b9 | 2008-06-06 10:38:46 -0700 | [diff] [blame] | 2520 | inc->fp = inc_fopen(p, dephead, &deptail, pass == 0); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2521 | if (!inc->fp) { |
| 2522 | /* -MG given but file not found */ |
| 2523 | nasm_free(inc); |
| 2524 | } else { |
| 2525 | inc->fname = src_set_fname(nasm_strdup(p)); |
| 2526 | inc->lineno = src_set_linnum(0); |
| 2527 | inc->lineinc = 1; |
| 2528 | inc->expansion = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2529 | inc->mstk = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2530 | istk = inc; |
H. Peter Anvin | 172b840 | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 2531 | lfmt->uplevel(LIST_INCLUDE); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2532 | } |
| 2533 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2534 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2535 | |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2536 | case PP_USE: |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2537 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2538 | static macros_t *use_pkg; |
| 2539 | const char *pkg_macro = NULL; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2540 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2541 | tline = tline->next; |
| 2542 | skip_white_(tline); |
| 2543 | tline = expand_id(tline); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2544 | |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2545 | if (!tline || (tline->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2546 | tline->type != TOK_INTERNAL_STRING && |
| 2547 | tline->type != TOK_ID)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2548 | nasm_error(ERR_NONFATAL, "`%%use' expects a package name"); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2549 | free_tlist(origline); |
| 2550 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2551 | } |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2552 | if (tline->next) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2553 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2554 | "trailing garbage after `%%use' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2555 | if (tline->type == TOK_STRING) |
| 2556 | nasm_unquote_cstr(tline->text, i); |
| 2557 | use_pkg = nasm_stdmac_find_package(tline->text); |
| 2558 | if (!use_pkg) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2559 | nasm_error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2560 | else |
| 2561 | 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] | 2562 | if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2563 | /* Not already included, go ahead and include it */ |
| 2564 | stdmacpos = use_pkg; |
| 2565 | } |
| 2566 | free_tlist(origline); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2567 | return DIRECTIVE_FOUND; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2568 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2569 | case PP_PUSH: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2570 | case PP_REPL: |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2571 | case PP_POP: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2572 | tline = tline->next; |
| 2573 | skip_white_(tline); |
| 2574 | tline = expand_id(tline); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2575 | if (tline) { |
| 2576 | if (!tok_type_(tline, TOK_ID)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2577 | nasm_error(ERR_NONFATAL, "`%s' expects a context identifier", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2578 | pp_directives[i]); |
| 2579 | free_tlist(origline); |
| 2580 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2581 | } |
| 2582 | if (tline->next) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2583 | nasm_error(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2584 | "trailing garbage after `%s' ignored", |
| 2585 | pp_directives[i]); |
| 2586 | p = nasm_strdup(tline->text); |
| 2587 | } else { |
| 2588 | p = NULL; /* Anonymous */ |
| 2589 | } |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2590 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2591 | if (i == PP_PUSH) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2592 | ctx = nasm_malloc(sizeof(Context)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2593 | ctx->next = cstk; |
| 2594 | hash_init(&ctx->localmac, HASH_SMALL); |
| 2595 | ctx->name = p; |
| 2596 | ctx->number = unique++; |
| 2597 | cstk = ctx; |
| 2598 | } else { |
| 2599 | /* %pop or %repl */ |
| 2600 | if (!cstk) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2601 | nasm_error(ERR_NONFATAL, "`%s': context stack is empty", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2602 | pp_directives[i]); |
| 2603 | } else if (i == PP_POP) { |
| 2604 | if (p && (!cstk->name || nasm_stricmp(p, cstk->name))) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2605 | nasm_error(ERR_NONFATAL, "`%%pop' in wrong context: %s, " |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2606 | "expected %s", |
| 2607 | cstk->name ? cstk->name : "anonymous", p); |
| 2608 | else |
| 2609 | ctx_pop(); |
| 2610 | } else { |
| 2611 | /* i == PP_REPL */ |
| 2612 | nasm_free(cstk->name); |
| 2613 | cstk->name = p; |
| 2614 | p = NULL; |
| 2615 | } |
| 2616 | nasm_free(p); |
| 2617 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2618 | free_tlist(origline); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2619 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2620 | case PP_FATAL: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2621 | severity = ERR_FATAL; |
| 2622 | goto issue_error; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2623 | case PP_ERROR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2624 | severity = ERR_NONFATAL; |
| 2625 | goto issue_error; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2626 | case PP_WARNING: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2627 | severity = ERR_WARNING|ERR_WARN_USER; |
| 2628 | goto issue_error; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2629 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2630 | issue_error: |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2631 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2632 | /* Only error out if this is the final pass */ |
| 2633 | if (pass != 2 && i != PP_FATAL) |
| 2634 | return DIRECTIVE_FOUND; |
| 2635 | |
| 2636 | tline->next = expand_smacro(tline->next); |
| 2637 | tline = tline->next; |
| 2638 | skip_white_(tline); |
| 2639 | t = tline ? tline->next : NULL; |
| 2640 | skip_white_(t); |
| 2641 | if (tok_type_(tline, TOK_STRING) && !t) { |
| 2642 | /* The line contains only a quoted string */ |
| 2643 | p = tline->text; |
| 2644 | nasm_unquote(p, NULL); /* Ignore NUL character truncation */ |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2645 | nasm_error(severity, "%s", p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2646 | } else { |
| 2647 | /* Not a quoted string, or more than a quoted string */ |
| 2648 | p = detoken(tline, false); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2649 | nasm_error(severity, "%s", p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2650 | nasm_free(p); |
| 2651 | } |
| 2652 | free_tlist(origline); |
| 2653 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2654 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2655 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2656 | CASE_PP_IF: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2657 | if (istk->conds && !emitting(istk->conds->state)) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2658 | j = COND_NEVER; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2659 | else { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2660 | j = if_condition(tline->next, i); |
| 2661 | tline->next = NULL; /* it got freed */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2662 | j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2663 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2664 | cond = nasm_malloc(sizeof(Cond)); |
| 2665 | cond->next = istk->conds; |
| 2666 | cond->state = j; |
| 2667 | istk->conds = cond; |
| 2668 | if(istk->mstk) |
| 2669 | istk->mstk->condcnt ++; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2670 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2671 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2672 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2673 | CASE_PP_ELIF: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2674 | if (!istk->conds) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2675 | nasm_error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2676 | switch(istk->conds->state) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2677 | case COND_IF_TRUE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2678 | istk->conds->state = COND_DONE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2679 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2680 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2681 | case COND_DONE: |
| 2682 | case COND_NEVER: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2683 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2684 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2685 | case COND_ELSE_TRUE: |
| 2686 | case COND_ELSE_FALSE: |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2687 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND, |
| 2688 | "`%%elif' after `%%else' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2689 | istk->conds->state = COND_NEVER; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2690 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2691 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2692 | case COND_IF_FALSE: |
| 2693 | /* |
| 2694 | * IMPORTANT: In the case of %if, we will already have |
| 2695 | * called expand_mmac_params(); however, if we're |
| 2696 | * processing an %elif we must have been in a |
| 2697 | * non-emitting mode, which would have inhibited |
| 2698 | * the normal invocation of expand_mmac_params(). |
| 2699 | * Therefore, we have to do it explicitly here. |
| 2700 | */ |
| 2701 | j = if_condition(expand_mmac_params(tline->next), i); |
| 2702 | tline->next = NULL; /* it got freed */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2703 | istk->conds->state = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2704 | j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; |
| 2705 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2706 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2707 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2708 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2709 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2710 | case PP_ELSE: |
| 2711 | if (tline->next) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2712 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND, |
| 2713 | "trailing garbage after `%%else' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2714 | if (!istk->conds) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2715 | nasm_fatal(0, "`%%else: no matching `%%if'"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2716 | switch(istk->conds->state) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2717 | case COND_IF_TRUE: |
| 2718 | case COND_DONE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2719 | istk->conds->state = COND_ELSE_FALSE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2720 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2721 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2722 | case COND_NEVER: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2723 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2724 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2725 | case COND_IF_FALSE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2726 | istk->conds->state = COND_ELSE_TRUE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2727 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2728 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2729 | case COND_ELSE_TRUE: |
| 2730 | case COND_ELSE_FALSE: |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2731 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2732 | "`%%else' after `%%else' ignored."); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2733 | istk->conds->state = COND_NEVER; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2734 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2735 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2736 | free_tlist(origline); |
| 2737 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2738 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2739 | case PP_ENDIF: |
| 2740 | if (tline->next) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2741 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND, |
| 2742 | "trailing garbage after `%%endif' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2743 | if (!istk->conds) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2744 | nasm_error(ERR_FATAL, "`%%endif': no matching `%%if'"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2745 | cond = istk->conds; |
| 2746 | istk->conds = cond->next; |
| 2747 | nasm_free(cond); |
| 2748 | if(istk->mstk) |
| 2749 | istk->mstk->condcnt --; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2750 | free_tlist(origline); |
| 2751 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2752 | |
H. Peter Anvin | db8f96e | 2009-07-15 09:07:29 -0400 | [diff] [blame] | 2753 | case PP_RMACRO: |
| 2754 | case PP_IRMACRO: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2755 | case PP_MACRO: |
| 2756 | case PP_IMACRO: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2757 | if (defining) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2758 | nasm_error(ERR_FATAL, "`%s': already defining a macro", |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2759 | pp_directives[i]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2760 | return DIRECTIVE_FOUND; |
| 2761 | } |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 2762 | defining = nasm_zalloc(sizeof(MMacro)); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2763 | defining->max_depth = |
| 2764 | (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0; |
| 2765 | defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO); |
| 2766 | if (!parse_mmacro_spec(tline, defining, pp_directives[i])) { |
| 2767 | nasm_free(defining); |
| 2768 | defining = NULL; |
| 2769 | return DIRECTIVE_FOUND; |
| 2770 | } |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2771 | |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 2772 | src_get(&defining->xline, &defining->fname); |
| 2773 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2774 | mmac = (MMacro *) hash_findix(&mmacros, defining->name); |
| 2775 | while (mmac) { |
| 2776 | if (!strcmp(mmac->name, defining->name) && |
| 2777 | (mmac->nparam_min <= defining->nparam_max |
| 2778 | || defining->plus) |
| 2779 | && (defining->nparam_min <= mmac->nparam_max |
| 2780 | || mmac->plus)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2781 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2782 | "redefining multi-line macro `%s'", defining->name); |
| 2783 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2784 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2785 | mmac = mmac->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2786 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2787 | free_tlist(origline); |
| 2788 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2789 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2790 | case PP_ENDM: |
| 2791 | case PP_ENDMACRO: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2792 | if (! (defining && defining->name)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2793 | nasm_error(ERR_NONFATAL, "`%s': not defining a macro", tline->text); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2794 | return DIRECTIVE_FOUND; |
| 2795 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2796 | mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name); |
| 2797 | defining->next = *mmhead; |
| 2798 | *mmhead = defining; |
| 2799 | defining = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2800 | free_tlist(origline); |
| 2801 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2802 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2803 | case PP_EXITMACRO: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2804 | /* |
| 2805 | * We must search along istk->expansion until we hit a |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2806 | * macro-end marker for a macro with a name. Then we |
| 2807 | * bypass all lines between exitmacro and endmacro. |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2808 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2809 | list_for_each(l, istk->expansion) |
| 2810 | if (l->finishes && l->finishes->name) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2811 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2812 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2813 | if (l) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2814 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2815 | * Remove all conditional entries relative to this |
| 2816 | * macro invocation. (safe to do in this context) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2817 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2818 | for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) { |
| 2819 | cond = istk->conds; |
| 2820 | istk->conds = cond->next; |
| 2821 | nasm_free(cond); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2822 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2823 | istk->expansion = l; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2824 | } else { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2825 | nasm_error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2826 | } |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 2827 | free_tlist(origline); |
| 2828 | return DIRECTIVE_FOUND; |
| 2829 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2830 | case PP_UNMACRO: |
| 2831 | case PP_UNIMACRO: |
| 2832 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2833 | MMacro **mmac_p; |
| 2834 | MMacro spec; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2835 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2836 | spec.casesense = (i == PP_UNMACRO); |
| 2837 | if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) { |
| 2838 | return DIRECTIVE_FOUND; |
| 2839 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2840 | mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL); |
| 2841 | while (mmac_p && *mmac_p) { |
| 2842 | mmac = *mmac_p; |
| 2843 | if (mmac->casesense == spec.casesense && |
| 2844 | !mstrcmp(mmac->name, spec.name, spec.casesense) && |
| 2845 | mmac->nparam_min == spec.nparam_min && |
| 2846 | mmac->nparam_max == spec.nparam_max && |
| 2847 | mmac->plus == spec.plus) { |
| 2848 | *mmac_p = mmac->next; |
| 2849 | free_mmacro(mmac); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2850 | } else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2851 | mmac_p = &mmac->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2852 | } |
| 2853 | } |
| 2854 | free_tlist(origline); |
| 2855 | free_tlist(spec.dlist); |
| 2856 | return DIRECTIVE_FOUND; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2857 | } |
| 2858 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2859 | case PP_ROTATE: |
| 2860 | if (tline->next && tline->next->type == TOK_WHITESPACE) |
| 2861 | tline = tline->next; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2862 | if (!tline->next) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2863 | free_tlist(origline); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2864 | nasm_error(ERR_NONFATAL, "`%%rotate' missing rotate count"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2865 | return DIRECTIVE_FOUND; |
| 2866 | } |
| 2867 | t = expand_smacro(tline->next); |
| 2868 | tline->next = NULL; |
| 2869 | free_tlist(origline); |
| 2870 | tline = t; |
| 2871 | tptr = &t; |
| 2872 | tokval.t_type = TOKEN_INVALID; |
| 2873 | evalresult = |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2874 | evaluate(ppscan, tptr, &tokval, NULL, pass, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2875 | free_tlist(tline); |
| 2876 | if (!evalresult) |
| 2877 | return DIRECTIVE_FOUND; |
| 2878 | if (tokval.t_type) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2879 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2880 | "trailing garbage after expression ignored"); |
| 2881 | if (!is_simple(evalresult)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2882 | nasm_error(ERR_NONFATAL, "non-constant value given to `%%rotate'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2883 | return DIRECTIVE_FOUND; |
| 2884 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2885 | mmac = istk->mstk; |
| 2886 | while (mmac && !mmac->name) /* avoid mistaking %reps for macros */ |
| 2887 | mmac = mmac->next_active; |
| 2888 | if (!mmac) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2889 | nasm_error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2890 | } else if (mmac->nparam == 0) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2891 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2892 | "`%%rotate' invoked within macro without parameters"); |
| 2893 | } else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2894 | int rotate = mmac->rotate + reloc_value(evalresult); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2895 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2896 | rotate %= (int)mmac->nparam; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2897 | if (rotate < 0) |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2898 | rotate += mmac->nparam; |
| 2899 | |
| 2900 | mmac->rotate = rotate; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2901 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2902 | return DIRECTIVE_FOUND; |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 2903 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2904 | case PP_REP: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 2905 | nolist = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2906 | do { |
| 2907 | tline = tline->next; |
| 2908 | } while (tok_type_(tline, TOK_WHITESPACE)); |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 2909 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2910 | if (tok_type_(tline, TOK_ID) && |
| 2911 | nasm_stricmp(tline->text, ".nolist") == 0) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 2912 | nolist = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2913 | do { |
| 2914 | tline = tline->next; |
| 2915 | } while (tok_type_(tline, TOK_WHITESPACE)); |
| 2916 | } |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 2917 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2918 | if (tline) { |
| 2919 | t = expand_smacro(tline); |
| 2920 | tptr = &t; |
| 2921 | tokval.t_type = TOKEN_INVALID; |
| 2922 | evalresult = |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2923 | evaluate(ppscan, tptr, &tokval, NULL, pass, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2924 | if (!evalresult) { |
| 2925 | free_tlist(origline); |
| 2926 | return DIRECTIVE_FOUND; |
| 2927 | } |
| 2928 | if (tokval.t_type) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2929 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2930 | "trailing garbage after expression ignored"); |
| 2931 | if (!is_simple(evalresult)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2932 | nasm_error(ERR_NONFATAL, "non-constant value given to `%%rep'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2933 | return DIRECTIVE_FOUND; |
| 2934 | } |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 2935 | count = reloc_value(evalresult); |
| 2936 | if (count >= REP_LIMIT) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2937 | nasm_error(ERR_NONFATAL, "`%%rep' value exceeds limit"); |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 2938 | count = 0; |
| 2939 | } else |
| 2940 | count++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2941 | } else { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2942 | nasm_error(ERR_NONFATAL, "`%%rep' expects a repeat count"); |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 2943 | count = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2944 | } |
| 2945 | free_tlist(origline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2946 | |
| 2947 | tmp_defining = defining; |
| 2948 | defining = nasm_malloc(sizeof(MMacro)); |
| 2949 | defining->prev = NULL; |
| 2950 | defining->name = NULL; /* flags this macro as a %rep block */ |
| 2951 | defining->casesense = false; |
| 2952 | defining->plus = false; |
| 2953 | defining->nolist = nolist; |
| 2954 | defining->in_progress = count; |
| 2955 | defining->max_depth = 0; |
| 2956 | defining->nparam_min = defining->nparam_max = 0; |
| 2957 | defining->defaults = NULL; |
| 2958 | defining->dlist = NULL; |
| 2959 | defining->expansion = NULL; |
| 2960 | defining->next_active = istk->mstk; |
| 2961 | defining->rep_nest = tmp_defining; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2962 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2963 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2964 | case PP_ENDREP: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2965 | if (!defining || defining->name) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2966 | nasm_error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2967 | return DIRECTIVE_FOUND; |
| 2968 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2969 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2970 | /* |
| 2971 | * Now we have a "macro" defined - although it has no name |
| 2972 | * and we won't be entering it in the hash tables - we must |
| 2973 | * push a macro-end marker for it on to istk->expansion. |
| 2974 | * After that, it will take care of propagating itself (a |
| 2975 | * macro-end marker line for a macro which is really a %rep |
| 2976 | * block will cause the macro to be re-expanded, complete |
| 2977 | * with another macro-end marker to ensure the process |
| 2978 | * continues) until the whole expansion is forcibly removed |
| 2979 | * from istk->expansion by a %exitrep. |
| 2980 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2981 | l = nasm_malloc(sizeof(Line)); |
| 2982 | l->next = istk->expansion; |
| 2983 | l->finishes = defining; |
| 2984 | l->first = NULL; |
| 2985 | istk->expansion = l; |
| 2986 | |
| 2987 | istk->mstk = defining; |
| 2988 | |
H. Peter Anvin | 172b840 | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 2989 | lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2990 | tmp_defining = defining; |
| 2991 | defining = defining->rep_nest; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2992 | free_tlist(origline); |
| 2993 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2994 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2995 | case PP_EXITREP: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2996 | /* |
| 2997 | * We must search along istk->expansion until we hit a |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2998 | * macro-end marker for a macro with no name. Then we set |
| 2999 | * its `in_progress' flag to 0. |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3000 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3001 | list_for_each(l, istk->expansion) |
| 3002 | if (l->finishes && !l->finishes->name) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3003 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3004 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3005 | if (l) |
| 3006 | l->finishes->in_progress = 1; |
| 3007 | else |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3008 | nasm_error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3009 | free_tlist(origline); |
| 3010 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3011 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3012 | case PP_XDEFINE: |
| 3013 | case PP_IXDEFINE: |
| 3014 | case PP_DEFINE: |
| 3015 | case PP_IDEFINE: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3016 | casesense = (i == PP_DEFINE || i == PP_XDEFINE); |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3017 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3018 | tline = tline->next; |
| 3019 | skip_white_(tline); |
| 3020 | tline = expand_id(tline); |
| 3021 | if (!tline || (tline->type != TOK_ID && |
| 3022 | (tline->type != TOK_PREPROC_ID || |
| 3023 | tline->text[1] != '$'))) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3024 | nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3025 | pp_directives[i]); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3026 | free_tlist(origline); |
| 3027 | return DIRECTIVE_FOUND; |
| 3028 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3029 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3030 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3031 | last = tline; |
| 3032 | param_start = tline = tline->next; |
| 3033 | nparam = 0; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3034 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3035 | /* Expand the macro definition now for %xdefine and %ixdefine */ |
| 3036 | if ((i == PP_XDEFINE) || (i == PP_IXDEFINE)) |
| 3037 | tline = expand_smacro(tline); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3038 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3039 | if (tok_is_(tline, "(")) { |
| 3040 | /* |
| 3041 | * This macro has parameters. |
| 3042 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3043 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3044 | tline = tline->next; |
| 3045 | while (1) { |
| 3046 | skip_white_(tline); |
| 3047 | if (!tline) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3048 | nasm_error(ERR_NONFATAL, "parameter identifier expected"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3049 | free_tlist(origline); |
| 3050 | return DIRECTIVE_FOUND; |
| 3051 | } |
| 3052 | if (tline->type != TOK_ID) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3053 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3054 | "`%s': parameter identifier expected", |
| 3055 | tline->text); |
| 3056 | free_tlist(origline); |
| 3057 | return DIRECTIVE_FOUND; |
| 3058 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3059 | tline->type = TOK_SMAC_PARAM + nparam++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3060 | tline = tline->next; |
| 3061 | skip_white_(tline); |
| 3062 | if (tok_is_(tline, ",")) { |
| 3063 | tline = tline->next; |
H. Peter Anvin | ca348b6 | 2008-07-23 10:49:26 -0400 | [diff] [blame] | 3064 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3065 | if (!tok_is_(tline, ")")) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3066 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3067 | "`)' expected to terminate macro template"); |
| 3068 | free_tlist(origline); |
| 3069 | return DIRECTIVE_FOUND; |
| 3070 | } |
| 3071 | break; |
| 3072 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3073 | } |
| 3074 | last = tline; |
| 3075 | tline = tline->next; |
| 3076 | } |
| 3077 | if (tok_type_(tline, TOK_WHITESPACE)) |
| 3078 | last = tline, tline = tline->next; |
| 3079 | macro_start = NULL; |
| 3080 | last->next = NULL; |
| 3081 | t = tline; |
| 3082 | while (t) { |
| 3083 | if (t->type == TOK_ID) { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3084 | list_for_each(tt, param_start) |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3085 | if (tt->type >= TOK_SMAC_PARAM && |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3086 | !strcmp(tt->text, t->text)) |
| 3087 | t->type = tt->type; |
| 3088 | } |
| 3089 | tt = t->next; |
| 3090 | t->next = macro_start; |
| 3091 | macro_start = t; |
| 3092 | t = tt; |
| 3093 | } |
| 3094 | /* |
| 3095 | * Good. We now have a macro name, a parameter count, and a |
| 3096 | * token list (in reverse order) for an expansion. We ought |
| 3097 | * to be OK just to create an SMacro, store it, and let |
| 3098 | * free_tlist have the rest of the line (which we have |
| 3099 | * carefully re-terminated after chopping off the expansion |
| 3100 | * from the end). |
| 3101 | */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 3102 | define_smacro(ctx, mname, casesense, nparam, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3103 | free_tlist(origline); |
| 3104 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3105 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3106 | case PP_UNDEF: |
| 3107 | tline = tline->next; |
| 3108 | skip_white_(tline); |
| 3109 | tline = expand_id(tline); |
| 3110 | if (!tline || (tline->type != TOK_ID && |
| 3111 | (tline->type != TOK_PREPROC_ID || |
| 3112 | tline->text[1] != '$'))) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3113 | nasm_error(ERR_NONFATAL, "`%%undef' expects a macro identifier"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3114 | free_tlist(origline); |
| 3115 | return DIRECTIVE_FOUND; |
| 3116 | } |
| 3117 | if (tline->next) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3118 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3119 | "trailing garbage after macro name ignored"); |
| 3120 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3121 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3122 | /* Find the context that symbol belongs to */ |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3123 | ctx = get_ctx(tline->text, &mname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3124 | undef_smacro(ctx, mname); |
| 3125 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3126 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3127 | |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3128 | case PP_DEFSTR: |
| 3129 | case PP_IDEFSTR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3130 | casesense = (i == PP_DEFSTR); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3131 | |
| 3132 | tline = tline->next; |
| 3133 | skip_white_(tline); |
| 3134 | tline = expand_id(tline); |
| 3135 | if (!tline || (tline->type != TOK_ID && |
| 3136 | (tline->type != TOK_PREPROC_ID || |
| 3137 | tline->text[1] != '$'))) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3138 | nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3139 | pp_directives[i]); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3140 | free_tlist(origline); |
| 3141 | return DIRECTIVE_FOUND; |
| 3142 | } |
| 3143 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3144 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3145 | last = tline; |
| 3146 | tline = expand_smacro(tline->next); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3147 | last->next = NULL; |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3148 | |
| 3149 | while (tok_type_(tline, TOK_WHITESPACE)) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3150 | tline = delete_Token(tline); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3151 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3152 | p = detoken(tline, false); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3153 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3154 | macro_start->next = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3155 | macro_start->text = nasm_quote(p, strlen(p)); |
| 3156 | macro_start->type = TOK_STRING; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3157 | macro_start->a.mac = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3158 | nasm_free(p); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3159 | |
| 3160 | /* |
| 3161 | * We now have a macro name, an implicit parameter count of |
| 3162 | * zero, and a string token to use as an expansion. Create |
| 3163 | * and store an SMacro. |
| 3164 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3165 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3166 | free_tlist(origline); |
| 3167 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3168 | |
H. Peter Anvin | 2f55bda | 2009-07-14 15:04:04 -0400 | [diff] [blame] | 3169 | case PP_DEFTOK: |
| 3170 | case PP_IDEFTOK: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3171 | casesense = (i == PP_DEFTOK); |
| 3172 | |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3173 | tline = tline->next; |
| 3174 | skip_white_(tline); |
| 3175 | tline = expand_id(tline); |
| 3176 | if (!tline || (tline->type != TOK_ID && |
| 3177 | (tline->type != TOK_PREPROC_ID || |
| 3178 | tline->text[1] != '$'))) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3179 | nasm_error(ERR_NONFATAL, |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3180 | "`%s' expects a macro identifier as first parameter", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3181 | pp_directives[i]); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3182 | free_tlist(origline); |
| 3183 | return DIRECTIVE_FOUND; |
| 3184 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3185 | ctx = get_ctx(tline->text, &mname); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3186 | last = tline; |
| 3187 | tline = expand_smacro(tline->next); |
| 3188 | last->next = NULL; |
| 3189 | |
| 3190 | t = tline; |
| 3191 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3192 | t = t->next; |
| 3193 | /* t should now point to the string */ |
Cyrill Gorcunov | 6908e58 | 2010-09-06 19:36:15 +0400 | [diff] [blame] | 3194 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3195 | nasm_error(ERR_NONFATAL, |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3196 | "`%s` requires string as second parameter", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3197 | pp_directives[i]); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3198 | free_tlist(tline); |
| 3199 | free_tlist(origline); |
| 3200 | return DIRECTIVE_FOUND; |
| 3201 | } |
| 3202 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 3203 | /* |
| 3204 | * Convert the string to a token stream. Note that smacros |
| 3205 | * are stored with the token stream reversed, so we have to |
| 3206 | * reverse the output of tokenize(). |
| 3207 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3208 | nasm_unquote_cstr(t->text, i); |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 3209 | macro_start = reverse_tokens(tokenize(t->text)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3210 | |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3211 | /* |
| 3212 | * We now have a macro name, an implicit parameter count of |
| 3213 | * zero, and a numeric token to use as an expansion. Create |
| 3214 | * and store an SMacro. |
| 3215 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3216 | define_smacro(ctx, mname, casesense, 0, macro_start); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3217 | free_tlist(tline); |
| 3218 | free_tlist(origline); |
| 3219 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3220 | |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3221 | case PP_PATHSEARCH: |
| 3222 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3223 | FILE *fp; |
| 3224 | StrList *xsl = NULL; |
| 3225 | StrList **xst = &xsl; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3226 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3227 | casesense = true; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3228 | |
| 3229 | tline = tline->next; |
| 3230 | skip_white_(tline); |
| 3231 | tline = expand_id(tline); |
| 3232 | if (!tline || (tline->type != TOK_ID && |
| 3233 | (tline->type != TOK_PREPROC_ID || |
| 3234 | tline->text[1] != '$'))) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3235 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3236 | "`%%pathsearch' expects a macro identifier as first parameter"); |
| 3237 | free_tlist(origline); |
| 3238 | return DIRECTIVE_FOUND; |
| 3239 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3240 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3241 | last = tline; |
| 3242 | tline = expand_smacro(tline->next); |
| 3243 | last->next = NULL; |
| 3244 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3245 | t = tline; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3246 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3247 | t = t->next; |
| 3248 | |
| 3249 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3250 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3251 | nasm_error(ERR_NONFATAL, "`%%pathsearch' expects a file name"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3252 | free_tlist(tline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3253 | free_tlist(origline); |
| 3254 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 3255 | } |
| 3256 | if (t->next) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3257 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3258 | "trailing garbage after `%%pathsearch' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3259 | p = t->text; |
H. Peter Anvin | 427cc91 | 2008-06-01 21:43:03 -0700 | [diff] [blame] | 3260 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3261 | nasm_unquote(p, NULL); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3262 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3263 | fp = inc_fopen(p, &xsl, &xst, true); |
| 3264 | if (fp) { |
| 3265 | p = xsl->str; |
| 3266 | fclose(fp); /* Don't actually care about the file */ |
| 3267 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3268 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3269 | macro_start->next = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3270 | macro_start->text = nasm_quote(p, strlen(p)); |
| 3271 | macro_start->type = TOK_STRING; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3272 | macro_start->a.mac = NULL; |
| 3273 | if (xsl) |
| 3274 | nasm_free(xsl); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3275 | |
| 3276 | /* |
| 3277 | * We now have a macro name, an implicit parameter count of |
| 3278 | * zero, and a string token to use as an expansion. Create |
| 3279 | * and store an SMacro. |
| 3280 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3281 | define_smacro(ctx, mname, casesense, 0, macro_start); |
| 3282 | free_tlist(tline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3283 | free_tlist(origline); |
| 3284 | return DIRECTIVE_FOUND; |
| 3285 | } |
| 3286 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3287 | case PP_STRLEN: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3288 | casesense = true; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 3289 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3290 | tline = tline->next; |
| 3291 | skip_white_(tline); |
| 3292 | tline = expand_id(tline); |
| 3293 | if (!tline || (tline->type != TOK_ID && |
| 3294 | (tline->type != TOK_PREPROC_ID || |
| 3295 | tline->text[1] != '$'))) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3296 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3297 | "`%%strlen' expects a macro identifier as first parameter"); |
| 3298 | free_tlist(origline); |
| 3299 | return DIRECTIVE_FOUND; |
| 3300 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3301 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3302 | last = tline; |
| 3303 | tline = expand_smacro(tline->next); |
| 3304 | last->next = NULL; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3305 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3306 | t = tline; |
| 3307 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3308 | t = t->next; |
| 3309 | /* t should now point to the string */ |
Cyrill Gorcunov | 4e1d5ab | 2010-07-23 18:51:51 +0400 | [diff] [blame] | 3310 | if (!tok_type_(t, TOK_STRING)) { |
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` requires string as second parameter"); |
| 3313 | free_tlist(tline); |
| 3314 | free_tlist(origline); |
| 3315 | return DIRECTIVE_FOUND; |
| 3316 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3317 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3318 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3319 | macro_start->next = NULL; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 3320 | make_tok_num(macro_start, nasm_unquote(t->text, NULL)); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3321 | macro_start->a.mac = NULL; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3322 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3323 | /* |
| 3324 | * We now have a macro name, an implicit parameter count of |
| 3325 | * zero, and a numeric token to use as an expansion. Create |
| 3326 | * and store an SMacro. |
| 3327 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3328 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3329 | free_tlist(tline); |
| 3330 | free_tlist(origline); |
| 3331 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3332 | |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3333 | case PP_STRCAT: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3334 | casesense = true; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3335 | |
| 3336 | tline = tline->next; |
| 3337 | skip_white_(tline); |
| 3338 | tline = expand_id(tline); |
| 3339 | if (!tline || (tline->type != TOK_ID && |
| 3340 | (tline->type != TOK_PREPROC_ID || |
| 3341 | tline->text[1] != '$'))) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3342 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3343 | "`%%strcat' expects a macro identifier as first parameter"); |
| 3344 | free_tlist(origline); |
| 3345 | return DIRECTIVE_FOUND; |
| 3346 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3347 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3348 | last = tline; |
| 3349 | tline = expand_smacro(tline->next); |
| 3350 | last->next = NULL; |
| 3351 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3352 | len = 0; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3353 | list_for_each(t, tline) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3354 | switch (t->type) { |
| 3355 | case TOK_WHITESPACE: |
| 3356 | break; |
| 3357 | case TOK_STRING: |
| 3358 | len += t->a.len = nasm_unquote(t->text, NULL); |
| 3359 | break; |
| 3360 | case TOK_OTHER: |
| 3361 | if (!strcmp(t->text, ",")) /* permit comma separators */ |
| 3362 | break; |
| 3363 | /* else fall through */ |
| 3364 | default: |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3365 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3366 | "non-string passed to `%%strcat' (%d)", t->type); |
| 3367 | free_tlist(tline); |
| 3368 | free_tlist(origline); |
| 3369 | return DIRECTIVE_FOUND; |
| 3370 | } |
| 3371 | } |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3372 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3373 | p = pp = nasm_malloc(len); |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3374 | list_for_each(t, tline) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3375 | if (t->type == TOK_STRING) { |
| 3376 | memcpy(p, t->text, t->a.len); |
| 3377 | p += t->a.len; |
| 3378 | } |
| 3379 | } |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3380 | |
| 3381 | /* |
| 3382 | * We now have a macro name, an implicit parameter count of |
| 3383 | * zero, and a numeric token to use as an expansion. Create |
| 3384 | * and store an SMacro. |
| 3385 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3386 | macro_start = new_Token(NULL, TOK_STRING, NULL, 0); |
| 3387 | macro_start->text = nasm_quote(pp, len); |
| 3388 | nasm_free(pp); |
| 3389 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3390 | free_tlist(tline); |
| 3391 | free_tlist(origline); |
| 3392 | return DIRECTIVE_FOUND; |
| 3393 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3394 | case PP_SUBSTR: |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3395 | { |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3396 | int64_t start, count; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3397 | size_t len; |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 3398 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3399 | casesense = true; |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3400 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3401 | tline = tline->next; |
| 3402 | skip_white_(tline); |
| 3403 | tline = expand_id(tline); |
| 3404 | if (!tline || (tline->type != TOK_ID && |
| 3405 | (tline->type != TOK_PREPROC_ID || |
| 3406 | tline->text[1] != '$'))) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3407 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3408 | "`%%substr' expects a macro identifier as first parameter"); |
| 3409 | free_tlist(origline); |
| 3410 | return DIRECTIVE_FOUND; |
| 3411 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3412 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3413 | last = tline; |
| 3414 | tline = expand_smacro(tline->next); |
| 3415 | last->next = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3416 | |
Cyrill Gorcunov | 35519d6 | 2010-09-06 23:49:52 +0400 | [diff] [blame] | 3417 | if (tline) /* skip expanded id */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3418 | t = tline->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3419 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3420 | t = t->next; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3421 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3422 | /* t should now point to the string */ |
Cyrill Gorcunov | 35519d6 | 2010-09-06 23:49:52 +0400 | [diff] [blame] | 3423 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3424 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3425 | "`%%substr` requires string as second parameter"); |
| 3426 | free_tlist(tline); |
| 3427 | free_tlist(origline); |
| 3428 | return DIRECTIVE_FOUND; |
| 3429 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3430 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3431 | tt = t->next; |
| 3432 | tptr = &tt; |
| 3433 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3434 | evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3435 | if (!evalresult) { |
| 3436 | free_tlist(tline); |
| 3437 | free_tlist(origline); |
| 3438 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3439 | } else if (!is_simple(evalresult)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3440 | nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3441 | free_tlist(tline); |
| 3442 | free_tlist(origline); |
| 3443 | return DIRECTIVE_FOUND; |
| 3444 | } |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3445 | start = evalresult->value - 1; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3446 | |
| 3447 | while (tok_type_(tt, TOK_WHITESPACE)) |
| 3448 | tt = tt->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3449 | if (!tt) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3450 | count = 1; /* Backwards compatibility: one character */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3451 | } else { |
| 3452 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3453 | evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3454 | if (!evalresult) { |
| 3455 | free_tlist(tline); |
| 3456 | free_tlist(origline); |
| 3457 | return DIRECTIVE_FOUND; |
| 3458 | } else if (!is_simple(evalresult)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3459 | nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3460 | free_tlist(tline); |
| 3461 | free_tlist(origline); |
| 3462 | return DIRECTIVE_FOUND; |
| 3463 | } |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3464 | count = evalresult->value; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3465 | } |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3466 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3467 | len = nasm_unquote(t->text, NULL); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3468 | |
Cyrill Gorcunov | cff031e | 2010-09-07 20:31:11 +0400 | [diff] [blame] | 3469 | /* make start and count being in range */ |
| 3470 | if (start < 0) |
| 3471 | start = 0; |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3472 | if (count < 0) |
| 3473 | count = len + count + 1 - start; |
| 3474 | if (start + count > (int64_t)len) |
Cyrill Gorcunov | cff031e | 2010-09-07 20:31:11 +0400 | [diff] [blame] | 3475 | count = len - start; |
| 3476 | if (!len || count < 0 || start >=(int64_t)len) |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3477 | start = -1, count = 0; /* empty string */ |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3478 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3479 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3480 | macro_start->next = NULL; |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3481 | macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3482 | macro_start->type = TOK_STRING; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3483 | macro_start->a.mac = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3484 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3485 | /* |
| 3486 | * We now have a macro name, an implicit parameter count of |
| 3487 | * zero, and a numeric token to use as an expansion. Create |
| 3488 | * and store an SMacro. |
| 3489 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3490 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3491 | free_tlist(tline); |
| 3492 | free_tlist(origline); |
| 3493 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3494 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3495 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3496 | case PP_ASSIGN: |
| 3497 | case PP_IASSIGN: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3498 | casesense = (i == PP_ASSIGN); |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3499 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3500 | tline = tline->next; |
| 3501 | skip_white_(tline); |
| 3502 | tline = expand_id(tline); |
| 3503 | if (!tline || (tline->type != TOK_ID && |
| 3504 | (tline->type != TOK_PREPROC_ID || |
| 3505 | tline->text[1] != '$'))) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3506 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3507 | "`%%%sassign' expects a macro identifier", |
| 3508 | (i == PP_IASSIGN ? "i" : "")); |
| 3509 | free_tlist(origline); |
| 3510 | return DIRECTIVE_FOUND; |
| 3511 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3512 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3513 | last = tline; |
| 3514 | tline = expand_smacro(tline->next); |
| 3515 | last->next = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3516 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3517 | t = tline; |
| 3518 | tptr = &t; |
| 3519 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3520 | evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3521 | free_tlist(tline); |
| 3522 | if (!evalresult) { |
| 3523 | free_tlist(origline); |
| 3524 | return DIRECTIVE_FOUND; |
| 3525 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3526 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3527 | if (tokval.t_type) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3528 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3529 | "trailing garbage after expression ignored"); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3530 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3531 | if (!is_simple(evalresult)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3532 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3533 | "non-constant value given to `%%%sassign'", |
| 3534 | (i == PP_IASSIGN ? "i" : "")); |
| 3535 | free_tlist(origline); |
| 3536 | return DIRECTIVE_FOUND; |
| 3537 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3538 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3539 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3540 | macro_start->next = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3541 | make_tok_num(macro_start, reloc_value(evalresult)); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3542 | macro_start->a.mac = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3543 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3544 | /* |
| 3545 | * We now have a macro name, an implicit parameter count of |
| 3546 | * zero, and a numeric token to use as an expansion. Create |
| 3547 | * and store an SMacro. |
| 3548 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3549 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3550 | free_tlist(origline); |
| 3551 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3552 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3553 | case PP_LINE: |
| 3554 | /* |
| 3555 | * Syntax is `%line nnn[+mmm] [filename]' |
| 3556 | */ |
| 3557 | tline = tline->next; |
| 3558 | skip_white_(tline); |
| 3559 | if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3560 | nasm_error(ERR_NONFATAL, "`%%line' expects line number"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3561 | free_tlist(origline); |
| 3562 | return DIRECTIVE_FOUND; |
| 3563 | } |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 3564 | k = readnum(tline->text, &err); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3565 | m = 1; |
| 3566 | tline = tline->next; |
| 3567 | if (tok_is_(tline, "+")) { |
| 3568 | tline = tline->next; |
| 3569 | if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3570 | nasm_error(ERR_NONFATAL, "`%%line' expects line increment"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3571 | free_tlist(origline); |
| 3572 | return DIRECTIVE_FOUND; |
| 3573 | } |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 3574 | m = readnum(tline->text, &err); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3575 | tline = tline->next; |
| 3576 | } |
| 3577 | skip_white_(tline); |
| 3578 | src_set_linnum(k); |
| 3579 | istk->lineinc = m; |
| 3580 | if (tline) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 3581 | nasm_free(src_set_fname(detoken(tline, false))); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3582 | } |
| 3583 | free_tlist(origline); |
| 3584 | return DIRECTIVE_FOUND; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3585 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3586 | default: |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3587 | nasm_error(ERR_FATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3588 | "preprocessor directive `%s' not yet implemented", |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 3589 | pp_directives[i]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3590 | return DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3591 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3592 | } |
| 3593 | |
| 3594 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3595 | * Ensure that a macro parameter contains a condition code and |
| 3596 | * nothing else. Return the condition code index if so, or -1 |
| 3597 | * otherwise. |
| 3598 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3599 | static int find_cc(Token * t) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3600 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3601 | Token *tt; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3602 | |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3603 | if (!t) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3604 | return -1; /* Probably a %+ without a space */ |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3605 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3606 | skip_white_(t); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3607 | if (t->type != TOK_ID) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3608 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3609 | tt = t->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3610 | skip_white_(tt); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3611 | if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ","))) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3612 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3613 | |
Cyrill Gorcunov | 1945639 | 2012-05-02 00:18:56 +0400 | [diff] [blame] | 3614 | return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3615 | } |
| 3616 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3617 | /* |
| 3618 | * This routines walks over tokens strem and hadnles tokens |
| 3619 | * pasting, if @handle_explicit passed then explicit pasting |
| 3620 | * term is handled, otherwise -- implicit pastings only. |
| 3621 | */ |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 3622 | static bool paste_tokens(Token **head, const struct tokseq_match *m, |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3623 | size_t mnum, bool handle_explicit) |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3624 | { |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3625 | Token *tok, *next, **prev_next, **prev_nonspace; |
| 3626 | bool pasted = false; |
| 3627 | char *buf, *p; |
| 3628 | size_t len, i; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3629 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3630 | /* |
| 3631 | * The last token before pasting. We need it |
| 3632 | * to be able to connect new handled tokens. |
| 3633 | * In other words if there were a tokens stream |
| 3634 | * |
| 3635 | * A -> B -> C -> D |
| 3636 | * |
| 3637 | * and we've joined tokens B and C, the resulting |
| 3638 | * stream should be |
| 3639 | * |
| 3640 | * A -> BC -> D |
| 3641 | */ |
| 3642 | tok = *head; |
| 3643 | prev_next = NULL; |
| 3644 | |
| 3645 | if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE)) |
| 3646 | prev_nonspace = head; |
| 3647 | else |
| 3648 | prev_nonspace = NULL; |
| 3649 | |
| 3650 | while (tok && (next = tok->next)) { |
| 3651 | |
| 3652 | switch (tok->type) { |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3653 | case TOK_WHITESPACE: |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3654 | /* Zap redundant whitespaces */ |
| 3655 | while (tok_type_(next, TOK_WHITESPACE)) |
| 3656 | next = delete_Token(next); |
| 3657 | tok->next = next; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3658 | break; |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3659 | |
| 3660 | case TOK_PASTE: |
| 3661 | /* Explicit pasting */ |
| 3662 | if (!handle_explicit) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3663 | break; |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3664 | next = delete_Token(tok); |
| 3665 | |
| 3666 | while (tok_type_(next, TOK_WHITESPACE)) |
| 3667 | next = delete_Token(next); |
| 3668 | |
| 3669 | if (!pasted) |
| 3670 | pasted = true; |
| 3671 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3672 | /* Left pasting token is start of line */ |
| 3673 | if (!prev_nonspace) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3674 | nasm_error(ERR_FATAL, "No lvalue found on pasting"); |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3675 | |
Cyrill Gorcunov | 8b5c9fb | 2013-02-04 01:24:54 +0400 | [diff] [blame] | 3676 | /* |
| 3677 | * No ending token, this might happen in two |
| 3678 | * cases |
| 3679 | * |
| 3680 | * 1) There indeed no right token at all |
| 3681 | * 2) There is a bare "%define ID" statement, |
| 3682 | * and @ID does expand to whitespace. |
| 3683 | * |
| 3684 | * So technically we need to do a grammar analysis |
| 3685 | * in another stage of parsing, but for now lets don't |
| 3686 | * change the behaviour people used to. Simply allow |
| 3687 | * whitespace after paste token. |
| 3688 | */ |
| 3689 | if (!next) { |
| 3690 | /* |
| 3691 | * Zap ending space tokens and that's all. |
| 3692 | */ |
| 3693 | tok = (*prev_nonspace)->next; |
| 3694 | while (tok_type_(tok, TOK_WHITESPACE)) |
| 3695 | tok = delete_Token(tok); |
| 3696 | tok = *prev_nonspace; |
| 3697 | tok->next = NULL; |
| 3698 | break; |
| 3699 | } |
| 3700 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3701 | tok = *prev_nonspace; |
| 3702 | while (tok_type_(tok, TOK_WHITESPACE)) |
| 3703 | tok = delete_Token(tok); |
| 3704 | len = strlen(tok->text); |
| 3705 | len += strlen(next->text); |
| 3706 | |
| 3707 | p = buf = nasm_malloc(len + 1); |
| 3708 | strcpy(p, tok->text); |
| 3709 | p = strchr(p, '\0'); |
| 3710 | strcpy(p, next->text); |
| 3711 | |
| 3712 | delete_Token(tok); |
| 3713 | |
| 3714 | tok = tokenize(buf); |
| 3715 | nasm_free(buf); |
| 3716 | |
| 3717 | *prev_nonspace = tok; |
| 3718 | while (tok && tok->next) |
| 3719 | tok = tok->next; |
| 3720 | |
| 3721 | tok->next = delete_Token(next); |
| 3722 | |
| 3723 | /* Restart from pasted tokens head */ |
| 3724 | tok = *prev_nonspace; |
| 3725 | break; |
| 3726 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3727 | default: |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3728 | /* implicit pasting */ |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 3729 | for (i = 0; i < mnum; i++) { |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3730 | if (!(PP_CONCAT_MATCH(tok, m[i].mask_head))) |
| 3731 | continue; |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 3732 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3733 | len = 0; |
| 3734 | while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) { |
| 3735 | len += strlen(next->text); |
| 3736 | next = next->next; |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 3737 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3738 | |
| 3739 | /* No match */ |
| 3740 | if (tok == next) |
| 3741 | break; |
| 3742 | |
| 3743 | len += strlen(tok->text); |
| 3744 | p = buf = nasm_malloc(len + 1); |
| 3745 | |
| 3746 | while (tok != next) { |
| 3747 | strcpy(p, tok->text); |
| 3748 | p = strchr(p, '\0'); |
| 3749 | tok = delete_Token(tok); |
| 3750 | } |
| 3751 | |
| 3752 | tok = tokenize(buf); |
| 3753 | nasm_free(buf); |
| 3754 | |
| 3755 | if (prev_next) |
| 3756 | *prev_next = tok; |
| 3757 | else |
| 3758 | *head = tok; |
| 3759 | |
| 3760 | /* |
| 3761 | * Connect pasted into original stream, |
| 3762 | * ie A -> new-tokens -> B |
| 3763 | */ |
| 3764 | while (tok && tok->next) |
| 3765 | tok = tok->next; |
| 3766 | tok->next = next; |
| 3767 | |
| 3768 | if (!pasted) |
| 3769 | pasted = true; |
| 3770 | |
| 3771 | /* Restart from pasted tokens head */ |
| 3772 | tok = prev_next ? *prev_next : *head; |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 3773 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3774 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3775 | break; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3776 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3777 | |
| 3778 | prev_next = &tok->next; |
| 3779 | |
| 3780 | if (tok->next && |
| 3781 | !tok_type_(tok->next, TOK_WHITESPACE) && |
| 3782 | !tok_type_(tok->next, TOK_PASTE)) |
| 3783 | prev_nonspace = prev_next; |
| 3784 | |
| 3785 | tok = tok->next; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3786 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3787 | |
| 3788 | return pasted; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3789 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3790 | |
| 3791 | /* |
| 3792 | * expands to a list of tokens from %{x:y} |
| 3793 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3794 | static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3795 | { |
| 3796 | Token *t = tline, **tt, *tm, *head; |
| 3797 | char *pos; |
| 3798 | int fst, lst, j, i; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3799 | |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3800 | pos = strchr(tline->text, ':'); |
| 3801 | nasm_assert(pos); |
| 3802 | |
| 3803 | lst = atoi(pos + 1); |
| 3804 | fst = atoi(tline->text + 1); |
| 3805 | |
| 3806 | /* |
| 3807 | * only macros params are accounted so |
| 3808 | * if someone passes %0 -- we reject such |
| 3809 | * value(s) |
| 3810 | */ |
| 3811 | if (lst == 0 || fst == 0) |
| 3812 | goto err; |
| 3813 | |
| 3814 | /* the values should be sane */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3815 | if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) || |
| 3816 | (lst > (int)mac->nparam || lst < (-(int)mac->nparam))) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3817 | goto err; |
| 3818 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3819 | fst = fst < 0 ? fst + (int)mac->nparam + 1: fst; |
| 3820 | lst = lst < 0 ? lst + (int)mac->nparam + 1: lst; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3821 | |
| 3822 | /* counted from zero */ |
| 3823 | fst--, lst--; |
| 3824 | |
| 3825 | /* |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3826 | * It will be at least one token. Note we |
| 3827 | * need to scan params until separator, otherwise |
| 3828 | * only first token will be passed. |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3829 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3830 | tm = mac->params[(fst + mac->rotate) % mac->nparam]; |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3831 | head = new_Token(NULL, tm->type, tm->text, 0); |
| 3832 | tt = &head->next, tm = tm->next; |
| 3833 | while (tok_isnt_(tm, ",")) { |
| 3834 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 3835 | *tt = t, tt = &t->next, tm = tm->next; |
| 3836 | } |
| 3837 | |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3838 | if (fst < lst) { |
| 3839 | for (i = fst + 1; i <= lst; i++) { |
| 3840 | t = new_Token(NULL, TOK_OTHER, ",", 0); |
| 3841 | *tt = t, tt = &t->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3842 | j = (i + mac->rotate) % mac->nparam; |
| 3843 | tm = mac->params[j]; |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3844 | while (tok_isnt_(tm, ",")) { |
| 3845 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 3846 | *tt = t, tt = &t->next, tm = tm->next; |
| 3847 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3848 | } |
| 3849 | } else { |
| 3850 | for (i = fst - 1; i >= lst; i--) { |
| 3851 | t = new_Token(NULL, TOK_OTHER, ",", 0); |
| 3852 | *tt = t, tt = &t->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3853 | j = (i + mac->rotate) % mac->nparam; |
| 3854 | tm = mac->params[j]; |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3855 | while (tok_isnt_(tm, ",")) { |
| 3856 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 3857 | *tt = t, tt = &t->next, tm = tm->next; |
| 3858 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3859 | } |
| 3860 | } |
| 3861 | |
| 3862 | *last = tt; |
| 3863 | return head; |
| 3864 | |
| 3865 | err: |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3866 | nasm_error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range", |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3867 | &tline->text[1]); |
| 3868 | return tline; |
| 3869 | } |
| 3870 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3871 | /* |
| 3872 | * Expand MMacro-local things: parameter references (%0, %n, %+n, |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 3873 | * %-n) and MMacro-local identifiers (%%foo) as well as |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3874 | * macro indirection (%[...]) and range (%{..:..}). |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3875 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3876 | static Token *expand_mmac_params(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3877 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3878 | Token *t, *tt, **tail, *thead; |
H. Peter Anvin | 6125b62 | 2009-04-08 14:02:25 -0700 | [diff] [blame] | 3879 | bool changed = false; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3880 | char *pos; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3881 | |
| 3882 | tail = &thead; |
| 3883 | thead = NULL; |
| 3884 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3885 | while (tline) { |
| 3886 | if (tline->type == TOK_PREPROC_ID && |
Cyrill Gorcunov | ca61119 | 2010-06-04 09:22:12 +0400 | [diff] [blame] | 3887 | (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) || |
| 3888 | (tline->text[1] >= '0' && tline->text[1] <= '9') || |
| 3889 | tline->text[1] == '%')) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 3890 | char *text = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3891 | int type = 0, cc; /* type = 0 to placate optimisers */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 3892 | char tmpbuf[30]; |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3893 | unsigned int n; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3894 | int i; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3895 | MMacro *mac; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3896 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3897 | t = tline; |
| 3898 | tline = tline->next; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3899 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3900 | mac = istk->mstk; |
| 3901 | while (mac && !mac->name) /* avoid mistaking %reps for macros */ |
| 3902 | mac = mac->next_active; |
| 3903 | if (!mac) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3904 | nasm_error(ERR_NONFATAL, "`%s': not in a macro call", t->text); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3905 | } else { |
| 3906 | pos = strchr(t->text, ':'); |
| 3907 | if (!pos) { |
| 3908 | switch (t->text[1]) { |
| 3909 | /* |
| 3910 | * We have to make a substitution of one of the |
| 3911 | * forms %1, %-1, %+1, %%foo, %0. |
| 3912 | */ |
| 3913 | case '0': |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3914 | type = TOK_NUMBER; |
| 3915 | snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam); |
| 3916 | text = nasm_strdup(tmpbuf); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3917 | break; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3918 | case '%': |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3919 | type = TOK_ID; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3920 | snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".", |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3921 | mac->unique); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3922 | text = nasm_strcat(tmpbuf, t->text + 2); |
| 3923 | break; |
| 3924 | case '-': |
| 3925 | n = atoi(t->text + 2) - 1; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3926 | if (n >= mac->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3927 | tt = NULL; |
| 3928 | else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3929 | if (mac->nparam > 1) |
| 3930 | n = (n + mac->rotate) % mac->nparam; |
| 3931 | tt = mac->params[n]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3932 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3933 | cc = find_cc(tt); |
| 3934 | if (cc == -1) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3935 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3936 | "macro parameter %d is not a condition code", |
| 3937 | n + 1); |
| 3938 | text = NULL; |
| 3939 | } else { |
| 3940 | type = TOK_ID; |
| 3941 | if (inverse_ccs[cc] == -1) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3942 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3943 | "condition code `%s' is not invertible", |
| 3944 | conditions[cc]); |
| 3945 | text = NULL; |
| 3946 | } else |
| 3947 | text = nasm_strdup(conditions[inverse_ccs[cc]]); |
| 3948 | } |
| 3949 | break; |
| 3950 | case '+': |
| 3951 | n = atoi(t->text + 2) - 1; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3952 | if (n >= mac->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3953 | tt = NULL; |
| 3954 | else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3955 | if (mac->nparam > 1) |
| 3956 | n = (n + mac->rotate) % mac->nparam; |
| 3957 | tt = mac->params[n]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3958 | } |
| 3959 | cc = find_cc(tt); |
| 3960 | if (cc == -1) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3961 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3962 | "macro parameter %d is not a condition code", |
| 3963 | n + 1); |
| 3964 | text = NULL; |
| 3965 | } else { |
| 3966 | type = TOK_ID; |
| 3967 | text = nasm_strdup(conditions[cc]); |
| 3968 | } |
| 3969 | break; |
| 3970 | default: |
| 3971 | n = atoi(t->text + 1) - 1; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3972 | if (n >= mac->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3973 | tt = NULL; |
| 3974 | else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3975 | if (mac->nparam > 1) |
| 3976 | n = (n + mac->rotate) % mac->nparam; |
| 3977 | tt = mac->params[n]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3978 | } |
| 3979 | if (tt) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3980 | for (i = 0; i < mac->paramlen[n]; i++) { |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3981 | *tail = new_Token(NULL, tt->type, tt->text, 0); |
| 3982 | tail = &(*tail)->next; |
| 3983 | tt = tt->next; |
| 3984 | } |
| 3985 | } |
| 3986 | text = NULL; /* we've done it here */ |
| 3987 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3988 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3989 | } else { |
| 3990 | /* |
| 3991 | * seems we have a parameters range here |
| 3992 | */ |
| 3993 | Token *head, **last; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3994 | head = expand_mmac_params_range(mac, t, &last); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3995 | if (head != t) { |
| 3996 | *tail = head; |
| 3997 | *last = tline; |
| 3998 | tline = head; |
| 3999 | text = NULL; |
| 4000 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4001 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4002 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4003 | if (!text) { |
| 4004 | delete_Token(t); |
| 4005 | } else { |
| 4006 | *tail = t; |
| 4007 | tail = &t->next; |
| 4008 | t->type = type; |
| 4009 | nasm_free(t->text); |
| 4010 | t->text = text; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4011 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4012 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4013 | changed = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4014 | continue; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4015 | } else if (tline->type == TOK_INDIRECT) { |
| 4016 | t = tline; |
| 4017 | tline = tline->next; |
| 4018 | tt = tokenize(t->text); |
| 4019 | tt = expand_mmac_params(tt); |
| 4020 | tt = expand_smacro(tt); |
| 4021 | *tail = tt; |
| 4022 | while (tt) { |
| 4023 | tt->a.mac = NULL; /* Necessary? */ |
| 4024 | tail = &tt->next; |
| 4025 | tt = tt->next; |
| 4026 | } |
| 4027 | delete_Token(t); |
| 4028 | changed = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4029 | } else { |
| 4030 | t = *tail = tline; |
| 4031 | tline = tline->next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4032 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4033 | tail = &t->next; |
| 4034 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4035 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4036 | *tail = NULL; |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 4037 | |
Cyrill Gorcunov | c6a742c | 2011-06-27 01:23:09 +0400 | [diff] [blame] | 4038 | if (changed) { |
| 4039 | const struct tokseq_match t[] = { |
| 4040 | { |
| 4041 | PP_CONCAT_MASK(TOK_ID) | |
| 4042 | PP_CONCAT_MASK(TOK_FLOAT), /* head */ |
| 4043 | PP_CONCAT_MASK(TOK_ID) | |
| 4044 | PP_CONCAT_MASK(TOK_NUMBER) | |
| 4045 | PP_CONCAT_MASK(TOK_FLOAT) | |
| 4046 | PP_CONCAT_MASK(TOK_OTHER) /* tail */ |
| 4047 | }, |
| 4048 | { |
| 4049 | PP_CONCAT_MASK(TOK_NUMBER), /* head */ |
| 4050 | PP_CONCAT_MASK(TOK_NUMBER) /* tail */ |
| 4051 | } |
| 4052 | }; |
| 4053 | paste_tokens(&thead, t, ARRAY_SIZE(t), false); |
| 4054 | } |
H. Peter Anvin | 6125b62 | 2009-04-08 14:02:25 -0700 | [diff] [blame] | 4055 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4056 | return thead; |
| 4057 | } |
| 4058 | |
| 4059 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4060 | * Expand all single-line macro calls made in the given line. |
| 4061 | * Return the expanded version of the line. The original is deemed |
| 4062 | * to be destroyed in the process. (In reality we'll just move |
| 4063 | * Tokens from input to output a lot of the time, rather than |
| 4064 | * actually bothering to destroy and replicate.) |
| 4065 | */ |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4066 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4067 | static Token *expand_smacro(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4068 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4069 | Token *t, *tt, *mstart, **tail, *thead; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4070 | SMacro *head = NULL, *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4071 | Token **params; |
| 4072 | int *paramsize; |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 4073 | unsigned int nparam, sparam; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 4074 | int brackets; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4075 | Token *org_tline = tline; |
| 4076 | Context *ctx; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 4077 | const char *mname; |
H. Peter Anvin | 2a15e69 | 2007-11-19 13:14:59 -0800 | [diff] [blame] | 4078 | int deadman = DEADMAN_LIMIT; |
H. Peter Anvin | 8287daf | 2009-07-07 16:00:58 -0700 | [diff] [blame] | 4079 | bool expanded; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4080 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4081 | /* |
| 4082 | * Trick: we should avoid changing the start token pointer since it can |
| 4083 | * be contained in "next" field of other token. Because of this |
| 4084 | * we allocate a copy of first token and work with it; at the end of |
| 4085 | * routine we copy it back |
| 4086 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4087 | if (org_tline) { |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4088 | tline = new_Token(org_tline->next, org_tline->type, |
| 4089 | org_tline->text, 0); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4090 | tline->a.mac = org_tline->a.mac; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4091 | nasm_free(org_tline->text); |
| 4092 | org_tline->text = NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4093 | } |
| 4094 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4095 | expanded = true; /* Always expand %+ at least once */ |
H. Peter Anvin | 8287daf | 2009-07-07 16:00:58 -0700 | [diff] [blame] | 4096 | |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4097 | again: |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4098 | thead = NULL; |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4099 | tail = &thead; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4100 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4101 | while (tline) { /* main token loop */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4102 | if (!--deadman) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4103 | nasm_error(ERR_NONFATAL, "interminable macro recursion"); |
Cyrill Gorcunov | bd38c8f | 2009-11-21 11:11:23 +0300 | [diff] [blame] | 4104 | goto err; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4105 | } |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4106 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4107 | if ((mname = tline->text)) { |
| 4108 | /* if this token is a local macro, look in local context */ |
Cyrill Gorcunov | c56d9ad | 2010-02-11 15:12:19 +0300 | [diff] [blame] | 4109 | if (tline->type == TOK_ID) { |
| 4110 | head = (SMacro *)hash_findix(&smacros, mname); |
| 4111 | } else if (tline->type == TOK_PREPROC_ID) { |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 4112 | ctx = get_ctx(mname, &mname); |
Cyrill Gorcunov | c56d9ad | 2010-02-11 15:12:19 +0300 | [diff] [blame] | 4113 | head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL; |
| 4114 | } else |
| 4115 | head = NULL; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 4116 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4117 | /* |
| 4118 | * We've hit an identifier. As in is_mmacro below, we first |
| 4119 | * check whether the identifier is a single-line macro at |
| 4120 | * all, then think about checking for parameters if |
| 4121 | * necessary. |
| 4122 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4123 | list_for_each(m, head) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4124 | if (!mstrcmp(m->name, mname, m->casesense)) |
| 4125 | break; |
| 4126 | if (m) { |
| 4127 | mstart = tline; |
| 4128 | params = NULL; |
| 4129 | paramsize = NULL; |
| 4130 | if (m->nparam == 0) { |
| 4131 | /* |
| 4132 | * Simple case: the macro is parameterless. Discard the |
| 4133 | * one token that the macro call took, and push the |
| 4134 | * expansion back on the to-do stack. |
| 4135 | */ |
| 4136 | if (!m->expansion) { |
| 4137 | if (!strcmp("__FILE__", m->name)) { |
| 4138 | int32_t num = 0; |
| 4139 | char *file = NULL; |
| 4140 | src_get(&num, &file); |
| 4141 | tline->text = nasm_quote(file, strlen(file)); |
| 4142 | tline->type = TOK_STRING; |
| 4143 | nasm_free(file); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4144 | continue; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4145 | } |
| 4146 | if (!strcmp("__LINE__", m->name)) { |
| 4147 | nasm_free(tline->text); |
| 4148 | make_tok_num(tline, src_get_linnum()); |
| 4149 | continue; |
| 4150 | } |
| 4151 | if (!strcmp("__BITS__", m->name)) { |
| 4152 | nasm_free(tline->text); |
| 4153 | make_tok_num(tline, globalbits); |
| 4154 | continue; |
| 4155 | } |
| 4156 | tline = delete_Token(tline); |
| 4157 | continue; |
| 4158 | } |
| 4159 | } else { |
| 4160 | /* |
| 4161 | * Complicated case: at least one macro with this name |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4162 | * exists and takes parameters. We must find the |
| 4163 | * parameters in the call, count them, find the SMacro |
| 4164 | * that corresponds to that form of the macro call, and |
| 4165 | * substitute for the parameters when we expand. What a |
| 4166 | * pain. |
| 4167 | */ |
| 4168 | /*tline = tline->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4169 | skip_white_(tline); */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4170 | do { |
| 4171 | t = tline->next; |
| 4172 | while (tok_type_(t, TOK_SMAC_END)) { |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4173 | t->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4174 | t->text = NULL; |
| 4175 | t = tline->next = delete_Token(t); |
| 4176 | } |
| 4177 | tline = t; |
| 4178 | } while (tok_type_(tline, TOK_WHITESPACE)); |
| 4179 | if (!tok_is_(tline, "(")) { |
| 4180 | /* |
| 4181 | * This macro wasn't called with parameters: ignore |
| 4182 | * the call. (Behaviour borrowed from gnu cpp.) |
| 4183 | */ |
| 4184 | tline = mstart; |
| 4185 | m = NULL; |
| 4186 | } else { |
| 4187 | int paren = 0; |
| 4188 | int white = 0; |
| 4189 | brackets = 0; |
| 4190 | nparam = 0; |
| 4191 | sparam = PARAM_DELTA; |
| 4192 | params = nasm_malloc(sparam * sizeof(Token *)); |
| 4193 | params[0] = tline->next; |
| 4194 | paramsize = nasm_malloc(sparam * sizeof(int)); |
| 4195 | paramsize[0] = 0; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4196 | while (true) { /* parameter loop */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4197 | /* |
| 4198 | * For some unusual expansions |
| 4199 | * which concatenates function call |
| 4200 | */ |
| 4201 | t = tline->next; |
| 4202 | while (tok_type_(t, TOK_SMAC_END)) { |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4203 | t->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4204 | t->text = NULL; |
| 4205 | t = tline->next = delete_Token(t); |
| 4206 | } |
| 4207 | tline = t; |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 4208 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4209 | if (!tline) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4210 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4211 | "macro call expects terminating `)'"); |
| 4212 | break; |
| 4213 | } |
| 4214 | if (tline->type == TOK_WHITESPACE |
| 4215 | && brackets <= 0) { |
| 4216 | if (paramsize[nparam]) |
| 4217 | white++; |
| 4218 | else |
| 4219 | params[nparam] = tline->next; |
| 4220 | continue; /* parameter loop */ |
| 4221 | } |
| 4222 | if (tline->type == TOK_OTHER |
| 4223 | && tline->text[1] == 0) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4224 | char ch = tline->text[0]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4225 | if (ch == ',' && !paren && brackets <= 0) { |
| 4226 | if (++nparam >= sparam) { |
| 4227 | sparam += PARAM_DELTA; |
| 4228 | params = nasm_realloc(params, |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4229 | sparam * sizeof(Token *)); |
| 4230 | paramsize = nasm_realloc(paramsize, |
| 4231 | sparam * sizeof(int)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4232 | } |
| 4233 | params[nparam] = tline->next; |
| 4234 | paramsize[nparam] = 0; |
| 4235 | white = 0; |
| 4236 | continue; /* parameter loop */ |
| 4237 | } |
| 4238 | if (ch == '{' && |
| 4239 | (brackets > 0 || (brackets == 0 && |
| 4240 | !paramsize[nparam]))) |
| 4241 | { |
| 4242 | if (!(brackets++)) { |
| 4243 | params[nparam] = tline->next; |
| 4244 | continue; /* parameter loop */ |
| 4245 | } |
| 4246 | } |
| 4247 | if (ch == '}' && brackets > 0) |
| 4248 | if (--brackets == 0) { |
| 4249 | brackets = -1; |
| 4250 | continue; /* parameter loop */ |
| 4251 | } |
| 4252 | if (ch == '(' && !brackets) |
| 4253 | paren++; |
| 4254 | if (ch == ')' && brackets <= 0) |
| 4255 | if (--paren < 0) |
| 4256 | break; |
| 4257 | } |
| 4258 | if (brackets < 0) { |
| 4259 | brackets = 0; |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4260 | nasm_error(ERR_NONFATAL, "braces do not " |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4261 | "enclose all of macro parameter"); |
| 4262 | } |
| 4263 | paramsize[nparam] += white + 1; |
| 4264 | white = 0; |
| 4265 | } /* parameter loop */ |
| 4266 | nparam++; |
| 4267 | while (m && (m->nparam != nparam || |
| 4268 | mstrcmp(m->name, mname, |
| 4269 | m->casesense))) |
| 4270 | m = m->next; |
| 4271 | if (!m) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4272 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4273 | "macro `%s' exists, " |
| 4274 | "but not taking %d parameters", |
| 4275 | mstart->text, nparam); |
| 4276 | } |
| 4277 | } |
| 4278 | if (m && m->in_progress) |
| 4279 | m = NULL; |
| 4280 | if (!m) { /* in progess or didn't find '(' or wrong nparam */ |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 4281 | /* |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4282 | * Design question: should we handle !tline, which |
| 4283 | * indicates missing ')' here, or expand those |
| 4284 | * macros anyway, which requires the (t) test a few |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 4285 | * lines down? |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4286 | */ |
| 4287 | nasm_free(params); |
| 4288 | nasm_free(paramsize); |
| 4289 | tline = mstart; |
| 4290 | } else { |
| 4291 | /* |
| 4292 | * Expand the macro: we are placed on the last token of the |
| 4293 | * call, so that we can easily split the call from the |
| 4294 | * following tokens. We also start by pushing an SMAC_END |
| 4295 | * token for the cycle removal. |
| 4296 | */ |
| 4297 | t = tline; |
| 4298 | if (t) { |
| 4299 | tline = t->next; |
| 4300 | t->next = NULL; |
| 4301 | } |
| 4302 | tt = new_Token(tline, TOK_SMAC_END, NULL, 0); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4303 | tt->a.mac = m; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4304 | m->in_progress = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4305 | tline = tt; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 4306 | list_for_each(t, m->expansion) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4307 | if (t->type >= TOK_SMAC_PARAM) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4308 | Token *pcopy = tline, **ptail = &pcopy; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4309 | Token *ttt, *pt; |
| 4310 | int i; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4311 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4312 | ttt = params[t->type - TOK_SMAC_PARAM]; |
| 4313 | i = paramsize[t->type - TOK_SMAC_PARAM]; |
| 4314 | while (--i >= 0) { |
| 4315 | pt = *ptail = new_Token(tline, ttt->type, |
| 4316 | ttt->text, 0); |
| 4317 | ptail = &pt->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4318 | ttt = ttt->next; |
| 4319 | } |
| 4320 | tline = pcopy; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4321 | } else if (t->type == TOK_PREPROC_Q) { |
| 4322 | tt = new_Token(tline, TOK_ID, mname, 0); |
| 4323 | tline = tt; |
| 4324 | } else if (t->type == TOK_PREPROC_QQ) { |
| 4325 | tt = new_Token(tline, TOK_ID, m->name, 0); |
| 4326 | tline = tt; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4327 | } else { |
| 4328 | tt = new_Token(tline, t->type, t->text, 0); |
| 4329 | tline = tt; |
| 4330 | } |
| 4331 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4332 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4333 | /* |
| 4334 | * Having done that, get rid of the macro call, and clean |
| 4335 | * up the parameters. |
| 4336 | */ |
| 4337 | nasm_free(params); |
| 4338 | nasm_free(paramsize); |
| 4339 | free_tlist(mstart); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4340 | expanded = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4341 | continue; /* main token loop */ |
| 4342 | } |
| 4343 | } |
| 4344 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4345 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4346 | if (tline->type == TOK_SMAC_END) { |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4347 | tline->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4348 | tline = delete_Token(tline); |
| 4349 | } else { |
| 4350 | t = *tail = tline; |
| 4351 | tline = tline->next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4352 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4353 | t->next = NULL; |
| 4354 | tail = &t->next; |
| 4355 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4356 | } |
| 4357 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4358 | /* |
| 4359 | * 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] | 4360 | * after expansion (they can't be produced by tokenize()). The successive |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4361 | * TOK_IDs should be concatenated. |
| 4362 | * Also we look for %+ tokens and concatenate the tokens before and after |
| 4363 | * them (without white spaces in between). |
| 4364 | */ |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4365 | if (expanded) { |
Cyrill Gorcunov | c6a742c | 2011-06-27 01:23:09 +0400 | [diff] [blame] | 4366 | const struct tokseq_match t[] = { |
| 4367 | { |
| 4368 | PP_CONCAT_MASK(TOK_ID) | |
| 4369 | PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */ |
| 4370 | PP_CONCAT_MASK(TOK_ID) | |
| 4371 | PP_CONCAT_MASK(TOK_PREPROC_ID) | |
| 4372 | PP_CONCAT_MASK(TOK_NUMBER) /* tail */ |
| 4373 | } |
| 4374 | }; |
| 4375 | if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) { |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4376 | /* |
| 4377 | * If we concatenated something, *and* we had previously expanded |
| 4378 | * an actual macro, scan the lines again for macros... |
| 4379 | */ |
| 4380 | tline = thead; |
| 4381 | expanded = false; |
| 4382 | goto again; |
| 4383 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4384 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4385 | |
Cyrill Gorcunov | bd38c8f | 2009-11-21 11:11:23 +0300 | [diff] [blame] | 4386 | err: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4387 | if (org_tline) { |
| 4388 | if (thead) { |
| 4389 | *org_tline = *thead; |
| 4390 | /* since we just gave text to org_line, don't free it */ |
| 4391 | thead->text = NULL; |
| 4392 | delete_Token(thead); |
| 4393 | } else { |
| 4394 | /* the expression expanded to empty line; |
| 4395 | we can't return NULL for some reasons |
| 4396 | we just set the line to a single WHITESPACE token. */ |
| 4397 | memset(org_tline, 0, sizeof(*org_tline)); |
| 4398 | org_tline->text = NULL; |
| 4399 | org_tline->type = TOK_WHITESPACE; |
| 4400 | } |
| 4401 | thead = org_tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4402 | } |
| 4403 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4404 | return thead; |
| 4405 | } |
| 4406 | |
| 4407 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4408 | * Similar to expand_smacro but used exclusively with macro identifiers |
| 4409 | * right before they are fetched in. The reason is that there can be |
| 4410 | * identifiers consisting of several subparts. We consider that if there |
| 4411 | * are more than one element forming the name, user wants a expansion, |
| 4412 | * otherwise it will be left as-is. Example: |
| 4413 | * |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4414 | * %define %$abc cde |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4415 | * |
| 4416 | * the identifier %$abc will be left as-is so that the handler for %define |
| 4417 | * will suck it and define the corresponding value. Other case: |
| 4418 | * |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4419 | * %define _%$abc cde |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4420 | * |
| 4421 | * In this case user wants name to be expanded *before* %define starts |
| 4422 | * working, so we'll expand %$abc into something (if it has a value; |
| 4423 | * otherwise it will be left as-is) then concatenate all successive |
| 4424 | * PP_IDs into one. |
| 4425 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4426 | static Token *expand_id(Token * tline) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4427 | { |
| 4428 | Token *cur, *oldnext = NULL; |
| 4429 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4430 | if (!tline || !tline->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4431 | return tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4432 | |
| 4433 | cur = tline; |
| 4434 | while (cur->next && |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4435 | (cur->next->type == TOK_ID || |
| 4436 | cur->next->type == TOK_PREPROC_ID |
| 4437 | || cur->next->type == TOK_NUMBER)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4438 | cur = cur->next; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4439 | |
| 4440 | /* If identifier consists of just one token, don't expand */ |
| 4441 | if (cur == tline) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4442 | return tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4443 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4444 | if (cur) { |
| 4445 | oldnext = cur->next; /* Detach the tail past identifier */ |
| 4446 | cur->next = NULL; /* so that expand_smacro stops here */ |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4447 | } |
| 4448 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4449 | tline = expand_smacro(tline); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4450 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4451 | if (cur) { |
| 4452 | /* expand_smacro possibly changhed tline; re-scan for EOL */ |
| 4453 | cur = tline; |
| 4454 | while (cur && cur->next) |
| 4455 | cur = cur->next; |
| 4456 | if (cur) |
| 4457 | cur->next = oldnext; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4458 | } |
| 4459 | |
| 4460 | return tline; |
| 4461 | } |
| 4462 | |
| 4463 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4464 | * Determine whether the given line constitutes a multi-line macro |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4465 | * 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] | 4466 | * to check for an initial label - that's taken care of in |
| 4467 | * expand_mmacro - but must check numbers of parameters. Guaranteed |
| 4468 | * to be called with tline->type == TOK_ID, so the putative macro |
| 4469 | * name is easy to find. |
| 4470 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4471 | static MMacro *is_mmacro(Token * tline, Token *** params_array) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4472 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4473 | MMacro *head, *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4474 | Token **params; |
| 4475 | int nparam; |
| 4476 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4477 | head = (MMacro *) hash_findix(&mmacros, tline->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4478 | |
| 4479 | /* |
| 4480 | * Efficiency: first we see if any macro exists with the given |
| 4481 | * name. If not, we can return NULL immediately. _Then_ we |
| 4482 | * count the parameters, and then we look further along the |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4483 | * list if necessary to find the proper MMacro. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4484 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4485 | list_for_each(m, head) |
| 4486 | if (!mstrcmp(m->name, tline->text, m->casesense)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4487 | break; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4488 | if (!m) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4489 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4490 | |
| 4491 | /* |
| 4492 | * OK, we have a potential macro. Count and demarcate the |
| 4493 | * parameters. |
| 4494 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4495 | count_mmac_params(tline->next, &nparam, ¶ms); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4496 | |
| 4497 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4498 | * 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] | 4499 | * structure that handles this number. |
| 4500 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4501 | while (m) { |
| 4502 | if (m->nparam_min <= nparam |
| 4503 | && (m->plus || nparam <= m->nparam_max)) { |
| 4504 | /* |
| 4505 | * This one is right. Just check if cycle removal |
| 4506 | * prohibits us using it before we actually celebrate... |
| 4507 | */ |
| 4508 | if (m->in_progress > m->max_depth) { |
| 4509 | if (m->max_depth > 0) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4510 | nasm_error(ERR_WARNING, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4511 | "reached maximum recursion depth of %i", |
| 4512 | m->max_depth); |
| 4513 | } |
| 4514 | nasm_free(params); |
| 4515 | return NULL; |
| 4516 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4517 | /* |
| 4518 | * It's right, and we can use it. Add its default |
| 4519 | * parameters to the end of our list if necessary. |
| 4520 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4521 | if (m->defaults && nparam < m->nparam_min + m->ndefs) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4522 | params = |
| 4523 | nasm_realloc(params, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4524 | ((m->nparam_min + m->ndefs + |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4525 | 1) * sizeof(*params))); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4526 | while (nparam < m->nparam_min + m->ndefs) { |
| 4527 | params[nparam] = m->defaults[nparam - m->nparam_min]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4528 | nparam++; |
| 4529 | } |
| 4530 | } |
| 4531 | /* |
| 4532 | * If we've gone over the maximum parameter count (and |
| 4533 | * we're in Plus mode), ignore parameters beyond |
| 4534 | * nparam_max. |
| 4535 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4536 | if (m->plus && nparam > m->nparam_max) |
| 4537 | nparam = m->nparam_max; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4538 | /* |
| 4539 | * Then terminate the parameter list, and leave. |
| 4540 | */ |
| 4541 | if (!params) { /* need this special case */ |
| 4542 | params = nasm_malloc(sizeof(*params)); |
| 4543 | nparam = 0; |
| 4544 | } |
| 4545 | params[nparam] = NULL; |
| 4546 | *params_array = params; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4547 | return m; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4548 | } |
| 4549 | /* |
| 4550 | * This one wasn't right: look for the next one with the |
| 4551 | * same name. |
| 4552 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4553 | list_for_each(m, m->next) |
| 4554 | if (!mstrcmp(m->name, tline->text, m->casesense)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4555 | break; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4556 | } |
| 4557 | |
| 4558 | /* |
| 4559 | * After all that, we didn't find one with the right number of |
| 4560 | * parameters. Issue a warning, and fail to expand the macro. |
| 4561 | */ |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4562 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4563 | "macro `%s' exists, but not taking %d parameters", |
| 4564 | tline->text, nparam); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4565 | nasm_free(params); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4566 | return NULL; |
| 4567 | } |
| 4568 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4569 | |
| 4570 | /* |
| 4571 | * Save MMacro invocation specific fields in |
| 4572 | * preparation for a recursive macro expansion |
| 4573 | */ |
| 4574 | static void push_mmacro(MMacro *m) |
| 4575 | { |
| 4576 | MMacroInvocation *i; |
| 4577 | |
| 4578 | i = nasm_malloc(sizeof(MMacroInvocation)); |
| 4579 | i->prev = m->prev; |
| 4580 | i->params = m->params; |
| 4581 | i->iline = m->iline; |
| 4582 | i->nparam = m->nparam; |
| 4583 | i->rotate = m->rotate; |
| 4584 | i->paramlen = m->paramlen; |
| 4585 | i->unique = m->unique; |
| 4586 | i->condcnt = m->condcnt; |
| 4587 | m->prev = i; |
| 4588 | } |
| 4589 | |
| 4590 | |
| 4591 | /* |
| 4592 | * Restore MMacro invocation specific fields that were |
| 4593 | * saved during a previous recursive macro expansion |
| 4594 | */ |
| 4595 | static void pop_mmacro(MMacro *m) |
| 4596 | { |
| 4597 | MMacroInvocation *i; |
| 4598 | |
| 4599 | if (m->prev) { |
| 4600 | i = m->prev; |
| 4601 | m->prev = i->prev; |
| 4602 | m->params = i->params; |
| 4603 | m->iline = i->iline; |
| 4604 | m->nparam = i->nparam; |
| 4605 | m->rotate = i->rotate; |
| 4606 | m->paramlen = i->paramlen; |
| 4607 | m->unique = i->unique; |
| 4608 | m->condcnt = i->condcnt; |
| 4609 | nasm_free(i); |
| 4610 | } |
| 4611 | } |
| 4612 | |
| 4613 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4614 | /* |
| 4615 | * Expand the multi-line macro call made by the given line, if |
| 4616 | * 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] | 4617 | * istk->expansion and return 1. Otherwise return 0. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4618 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4619 | static int expand_mmacro(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4620 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4621 | Token *startline = tline; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4622 | Token *label = NULL; |
| 4623 | int dont_prepend = 0; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4624 | Token **params, *t, *tt; |
| 4625 | MMacro *m; |
| 4626 | Line *l, *ll; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4627 | int i, nparam, *paramlen; |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 4628 | const char *mname; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4629 | |
| 4630 | t = tline; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4631 | skip_white_(t); |
H. Peter Anvin | ce2233b | 2008-05-25 21:57:00 -0700 | [diff] [blame] | 4632 | /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */ |
H. Peter Anvin | dce1e2f | 2002-04-30 21:06:37 +0000 | [diff] [blame] | 4633 | 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] | 4634 | return 0; |
| 4635 | m = is_mmacro(t, ¶ms); |
| 4636 | if (m) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4637 | mname = t->text; |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 4638 | } else { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4639 | Token *last; |
| 4640 | /* |
| 4641 | * We have an id which isn't a macro call. We'll assume |
| 4642 | * it might be a label; we'll also check to see if a |
| 4643 | * colon follows it. Then, if there's another id after |
| 4644 | * that lot, we'll check it again for macro-hood. |
| 4645 | */ |
| 4646 | label = last = t; |
| 4647 | t = t->next; |
| 4648 | if (tok_type_(t, TOK_WHITESPACE)) |
| 4649 | last = t, t = t->next; |
| 4650 | if (tok_is_(t, ":")) { |
| 4651 | dont_prepend = 1; |
| 4652 | last = t, t = t->next; |
| 4653 | if (tok_type_(t, TOK_WHITESPACE)) |
| 4654 | last = t, t = t->next; |
| 4655 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4656 | if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, ¶ms))) |
| 4657 | return 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4658 | last->next = NULL; |
Keith Kanios | 891775e | 2009-07-11 06:08:54 -0500 | [diff] [blame] | 4659 | mname = t->text; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4660 | tline = t; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4661 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4662 | |
| 4663 | /* |
| 4664 | * Fix up the parameters: this involves stripping leading and |
| 4665 | * trailing whitespace, then stripping braces if they are |
| 4666 | * present. |
| 4667 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4668 | for (nparam = 0; params[nparam]; nparam++) ; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4669 | paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4670 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4671 | for (i = 0; params[i]; i++) { |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4672 | int brace = 0; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4673 | int comma = (!m->plus || i < nparam - 1); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4674 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4675 | t = params[i]; |
| 4676 | skip_white_(t); |
| 4677 | if (tok_is_(t, "{")) |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4678 | t = t->next, brace++, comma = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4679 | params[i] = t; |
| 4680 | paramlen[i] = 0; |
| 4681 | while (t) { |
| 4682 | if (comma && t->type == TOK_OTHER && !strcmp(t->text, ",")) |
| 4683 | break; /* ... because we have hit a comma */ |
| 4684 | if (comma && t->type == TOK_WHITESPACE |
| 4685 | && tok_is_(t->next, ",")) |
| 4686 | break; /* ... or a space then a comma */ |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4687 | if (brace && t->type == TOK_OTHER) { |
| 4688 | if (t->text[0] == '{') |
| 4689 | brace++; /* ... or a nested opening brace */ |
| 4690 | else if (t->text[0] == '}') |
| 4691 | if (!--brace) |
| 4692 | break; /* ... or a brace */ |
| 4693 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4694 | t = t->next; |
| 4695 | paramlen[i]++; |
| 4696 | } |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4697 | if (brace) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4698 | nasm_error(ERR_NONFATAL, "macro params should be enclosed in braces"); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4699 | } |
| 4700 | |
| 4701 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4702 | * OK, we have a MMacro structure together with a set of |
| 4703 | * parameters. We must now go through the expansion and push |
| 4704 | * copies of each Line on to istk->expansion. Substitution of |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4705 | * parameter tokens and macro-local tokens doesn't get done |
| 4706 | * until the single-line macro substitution process; this is |
| 4707 | * because delaying them allows us to change the semantics |
| 4708 | * later through %rotate. |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4709 | * |
| 4710 | * First, push an end marker on to istk->expansion, mark this |
| 4711 | * macro as in progress, and set up its invocation-specific |
| 4712 | * variables. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4713 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4714 | ll = nasm_malloc(sizeof(Line)); |
| 4715 | ll->next = istk->expansion; |
| 4716 | ll->finishes = m; |
| 4717 | ll->first = NULL; |
| 4718 | istk->expansion = ll; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4719 | |
| 4720 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4721 | * Save the previous MMacro expansion in the case of |
| 4722 | * macro recursion |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4723 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4724 | if (m->max_depth && m->in_progress) |
| 4725 | push_mmacro(m); |
| 4726 | |
| 4727 | m->in_progress ++; |
| 4728 | m->params = params; |
| 4729 | m->iline = tline; |
| 4730 | m->nparam = nparam; |
| 4731 | m->rotate = 0; |
| 4732 | m->paramlen = paramlen; |
| 4733 | m->unique = unique++; |
| 4734 | m->lineno = 0; |
| 4735 | m->condcnt = 0; |
| 4736 | |
| 4737 | m->next_active = istk->mstk; |
| 4738 | istk->mstk = m; |
| 4739 | |
| 4740 | list_for_each(l, m->expansion) { |
| 4741 | Token **tail; |
| 4742 | |
| 4743 | ll = nasm_malloc(sizeof(Line)); |
| 4744 | ll->finishes = NULL; |
| 4745 | ll->next = istk->expansion; |
| 4746 | istk->expansion = ll; |
| 4747 | tail = &ll->first; |
| 4748 | |
| 4749 | list_for_each(t, l->first) { |
| 4750 | Token *x = t; |
| 4751 | switch (t->type) { |
| 4752 | case TOK_PREPROC_Q: |
| 4753 | tt = *tail = new_Token(NULL, TOK_ID, mname, 0); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4754 | break; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4755 | case TOK_PREPROC_QQ: |
| 4756 | tt = *tail = new_Token(NULL, TOK_ID, m->name, 0); |
| 4757 | break; |
| 4758 | case TOK_PREPROC_ID: |
| 4759 | if (t->text[1] == '0' && t->text[2] == '0') { |
| 4760 | dont_prepend = -1; |
| 4761 | x = label; |
| 4762 | if (!x) |
| 4763 | continue; |
| 4764 | } |
| 4765 | /* fall through */ |
| 4766 | default: |
| 4767 | tt = *tail = new_Token(NULL, x->type, x->text, 0); |
| 4768 | break; |
| 4769 | } |
| 4770 | tail = &tt->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4771 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4772 | *tail = NULL; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4773 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4774 | |
| 4775 | /* |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4776 | * If we had a label, push it on as the first line of |
| 4777 | * the macro expansion. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4778 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4779 | if (label) { |
| 4780 | if (dont_prepend < 0) |
| 4781 | free_tlist(startline); |
| 4782 | else { |
| 4783 | ll = nasm_malloc(sizeof(Line)); |
| 4784 | ll->finishes = NULL; |
| 4785 | ll->next = istk->expansion; |
| 4786 | istk->expansion = ll; |
| 4787 | ll->first = startline; |
| 4788 | if (!dont_prepend) { |
| 4789 | while (label->next) |
| 4790 | label = label->next; |
| 4791 | label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4792 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4793 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4794 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4795 | |
H. Peter Anvin | 172b840 | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 4796 | lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4797 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4798 | return 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4799 | } |
| 4800 | |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4801 | /* |
| 4802 | * This function adds macro names to error messages, and suppresses |
| 4803 | * them if necessary. |
| 4804 | */ |
| 4805 | 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] | 4806 | { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4807 | char buff[BUFSIZ]; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4808 | MMacro *mmac = NULL; |
| 4809 | int delta = 0; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4810 | |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4811 | /* |
| 4812 | * If we're in a dead branch of IF or something like it, ignore the error. |
| 4813 | * However, because %else etc are evaluated in the state context |
| 4814 | * of the previous branch, errors might get lost: |
| 4815 | * %if 0 ... %else trailing garbage ... %endif |
| 4816 | * So %else etc should set the ERR_PP_PRECOND flag. |
| 4817 | */ |
| 4818 | if ((severity & ERR_MASK) < ERR_FATAL && |
| 4819 | istk && istk->conds && |
| 4820 | ((severity & ERR_PP_PRECOND) ? |
| 4821 | istk->conds->state == COND_NEVER : |
H. Peter Anvin | eb6653f | 2016-04-05 13:03:10 -0700 | [diff] [blame] | 4822 | !emitting(istk->conds->state))) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4823 | return; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4824 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4825 | /* get %macro name */ |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4826 | if (!(severity & ERR_NOFILE) && istk && istk->mstk) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4827 | mmac = istk->mstk; |
| 4828 | /* but %rep blocks should be skipped */ |
| 4829 | while (mmac && !mmac->name) |
| 4830 | mmac = mmac->next_active, delta++; |
Cyrill Gorcunov | 9900c6b | 2011-10-09 18:58:46 +0400 | [diff] [blame] | 4831 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4832 | |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4833 | if (mmac) { |
| 4834 | vsnprintf(buff, sizeof(buff), fmt, arg); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4835 | |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4836 | nasm_set_verror(real_verror); |
| 4837 | nasm_error(severity, "(%s:%d) %s", |
| 4838 | mmac->name, mmac->lineno - delta, buff); |
| 4839 | nasm_set_verror(pp_verror); |
| 4840 | } else { |
| 4841 | real_verror(severity, fmt, arg); |
| 4842 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4843 | } |
| 4844 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4845 | static void |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4846 | pp_reset(char *file, int apass, StrList **deplist) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4847 | { |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 4848 | Token *t; |
| 4849 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4850 | cstk = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4851 | istk = nasm_malloc(sizeof(Include)); |
| 4852 | istk->next = NULL; |
| 4853 | istk->conds = NULL; |
| 4854 | istk->expansion = NULL; |
| 4855 | istk->mstk = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4856 | istk->fp = fopen(file, "r"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4857 | istk->fname = NULL; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4858 | src_set_fname(nasm_strdup(file)); |
| 4859 | src_set_linnum(0); |
| 4860 | istk->lineinc = 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4861 | if (!istk->fp) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4862 | nasm_fatal(ERR_NOFILE, "unable to open input file `%s'", file); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4863 | defining = NULL; |
Charles Crayne | d4200be | 2008-07-12 16:42:33 -0700 | [diff] [blame] | 4864 | nested_mac_count = 0; |
| 4865 | nested_rep_count = 0; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 4866 | init_macros(); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4867 | unique = 0; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4868 | if (tasm_compatible_mode) { |
H. Peter Anvin | a4835d4 | 2008-05-20 14:21:29 -0700 | [diff] [blame] | 4869 | stdmacpos = nasm_stdmac; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4870 | } else { |
H. Peter Anvin | a4835d4 | 2008-05-20 14:21:29 -0700 | [diff] [blame] | 4871 | stdmacpos = nasm_stdmac_after_tasm; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4872 | } |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 4873 | any_extrastdmac = extrastdmac && *extrastdmac; |
| 4874 | do_predef = true; |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 4875 | |
| 4876 | /* |
| 4877 | * 0 for dependencies, 1 for preparatory passes, 2 for final pass. |
| 4878 | * The caller, however, will also pass in 3 for preprocess-only so |
| 4879 | * we can set __PASS__ accordingly. |
| 4880 | */ |
| 4881 | pass = apass > 2 ? 2 : apass; |
| 4882 | |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 4883 | dephead = deptail = deplist; |
| 4884 | if (deplist) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4885 | StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next); |
| 4886 | sl->next = NULL; |
| 4887 | strcpy(sl->str, file); |
| 4888 | *deptail = sl; |
| 4889 | deptail = &sl->next; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 4890 | } |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 4891 | |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 4892 | /* |
| 4893 | * Define the __PASS__ macro. This is defined here unlike |
| 4894 | * all the other builtins, because it is special -- it varies between |
| 4895 | * passes. |
| 4896 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4897 | t = nasm_malloc(sizeof(*t)); |
| 4898 | t->next = NULL; |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 4899 | make_tok_num(t, apass); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4900 | t->a.mac = NULL; |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 4901 | define_smacro(NULL, "__PASS__", true, 0, t); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4902 | } |
| 4903 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4904 | static char *pp_getline(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4905 | { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4906 | char *line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4907 | Token *tline; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4908 | |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4909 | real_verror = nasm_set_verror(pp_verror); |
| 4910 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4911 | while (1) { |
| 4912 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4913 | * Fetch a tokenized line, either from the macro-expansion |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4914 | * buffer or from the input file. |
| 4915 | */ |
| 4916 | tline = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4917 | while (istk->expansion && istk->expansion->finishes) { |
| 4918 | Line *l = istk->expansion; |
| 4919 | if (!l->finishes->name && l->finishes->in_progress > 1) { |
| 4920 | Line *ll; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4921 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4922 | /* |
| 4923 | * This is a macro-end marker for a macro with no |
| 4924 | * name, which means it's not really a macro at all |
| 4925 | * but a %rep block, and the `in_progress' field is |
| 4926 | * more than 1, meaning that we still need to |
| 4927 | * repeat. (1 means the natural last repetition; 0 |
| 4928 | * means termination by %exitrep.) We have |
| 4929 | * therefore expanded up to the %endrep, and must |
| 4930 | * push the whole block on to the expansion buffer |
| 4931 | * again. We don't bother to remove the macro-end |
| 4932 | * marker: we'd only have to generate another one |
| 4933 | * if we did. |
| 4934 | */ |
| 4935 | l->finishes->in_progress--; |
| 4936 | list_for_each(l, l->finishes->expansion) { |
| 4937 | Token *t, *tt, **tail; |
| 4938 | |
| 4939 | ll = nasm_malloc(sizeof(Line)); |
| 4940 | ll->next = istk->expansion; |
| 4941 | ll->finishes = NULL; |
| 4942 | ll->first = NULL; |
| 4943 | tail = &ll->first; |
| 4944 | |
| 4945 | list_for_each(t, l->first) { |
| 4946 | if (t->text || t->type == TOK_WHITESPACE) { |
| 4947 | tt = *tail = new_Token(NULL, t->type, t->text, 0); |
| 4948 | tail = &tt->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4949 | } |
| 4950 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4951 | |
| 4952 | istk->expansion = ll; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4953 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4954 | } else { |
| 4955 | /* |
| 4956 | * Check whether a `%rep' was started and not ended |
| 4957 | * within this macro expansion. This can happen and |
| 4958 | * should be detected. It's a fatal error because |
| 4959 | * I'm too confused to work out how to recover |
| 4960 | * sensibly from it. |
| 4961 | */ |
| 4962 | if (defining) { |
| 4963 | if (defining->name) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4964 | nasm_panic(0, "defining with name in expansion"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4965 | else if (istk->mstk->name) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4966 | nasm_fatal(0, "`%%rep' without `%%endrep' within" |
| 4967 | " expansion of macro `%s'", |
| 4968 | istk->mstk->name); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4969 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4970 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4971 | /* |
| 4972 | * FIXME: investigate the relationship at this point between |
| 4973 | * istk->mstk and l->finishes |
| 4974 | */ |
| 4975 | { |
| 4976 | MMacro *m = istk->mstk; |
| 4977 | istk->mstk = m->next_active; |
| 4978 | if (m->name) { |
| 4979 | /* |
| 4980 | * This was a real macro call, not a %rep, and |
| 4981 | * therefore the parameter information needs to |
| 4982 | * be freed. |
| 4983 | */ |
| 4984 | if (m->prev) { |
| 4985 | pop_mmacro(m); |
| 4986 | l->finishes->in_progress --; |
| 4987 | } else { |
| 4988 | nasm_free(m->params); |
| 4989 | free_tlist(m->iline); |
| 4990 | nasm_free(m->paramlen); |
| 4991 | l->finishes->in_progress = 0; |
| 4992 | } |
| 4993 | } else |
| 4994 | free_mmacro(m); |
| 4995 | } |
| 4996 | istk->expansion = l->next; |
| 4997 | nasm_free(l); |
H. Peter Anvin | 172b840 | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 4998 | lfmt->downlevel(LIST_MACRO); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4999 | } |
| 5000 | } |
| 5001 | while (1) { /* until we get a line we can use */ |
| 5002 | |
| 5003 | if (istk->expansion) { /* from a macro expansion */ |
| 5004 | char *p; |
| 5005 | Line *l = istk->expansion; |
| 5006 | if (istk->mstk) |
| 5007 | istk->mstk->lineno++; |
| 5008 | tline = l->first; |
| 5009 | istk->expansion = l->next; |
| 5010 | nasm_free(l); |
| 5011 | p = detoken(tline, false); |
H. Peter Anvin | 172b840 | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 5012 | lfmt->line(LIST_MACRO, p); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5013 | nasm_free(p); |
| 5014 | break; |
| 5015 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5016 | line = read_line(); |
| 5017 | if (line) { /* from the current input file */ |
| 5018 | line = prepreproc(line); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5019 | tline = tokenize(line); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5020 | nasm_free(line); |
| 5021 | break; |
| 5022 | } |
| 5023 | /* |
| 5024 | * The current file has ended; work down the istk |
| 5025 | */ |
| 5026 | { |
| 5027 | Include *i = istk; |
| 5028 | fclose(i->fp); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5029 | if (i->conds) { |
| 5030 | /* nasm_error can't be conditionally suppressed */ |
H. Peter Anvin | 4108706 | 2016-03-03 14:27:34 -0800 | [diff] [blame] | 5031 | nasm_fatal(0, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5032 | "expected `%%endif' before end of file"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5033 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5034 | /* only set line and file name if there's a next node */ |
| 5035 | if (i->next) { |
| 5036 | src_set_linnum(i->lineno); |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5037 | src_set_fname(nasm_strdup(i->fname)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5038 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5039 | istk = i->next; |
H. Peter Anvin | 172b840 | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 5040 | lfmt->downlevel(LIST_INCLUDE); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5041 | nasm_free(i); |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5042 | if (!istk) { |
| 5043 | line = NULL; |
| 5044 | goto done; |
| 5045 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5046 | if (istk->expansion && istk->expansion->finishes) |
| 5047 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5048 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5049 | } |
| 5050 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5051 | /* |
| 5052 | * We must expand MMacro parameters and MMacro-local labels |
| 5053 | * _before_ we plunge into directive processing, to cope |
| 5054 | * with things like `%define something %1' such as STRUC |
| 5055 | * uses. Unless we're _defining_ a MMacro, in which case |
| 5056 | * those tokens should be left alone to go into the |
| 5057 | * definition; and unless we're in a non-emitting |
| 5058 | * condition, in which case we don't want to meddle with |
| 5059 | * anything. |
| 5060 | */ |
| 5061 | if (!defining && !(istk->conds && !emitting(istk->conds->state)) |
| 5062 | && !(istk->mstk && !istk->mstk->in_progress)) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5063 | tline = expand_mmac_params(tline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5064 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5065 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5066 | /* |
| 5067 | * Check the line to see if it's a preprocessor directive. |
| 5068 | */ |
| 5069 | if (do_directive(tline) == DIRECTIVE_FOUND) { |
| 5070 | continue; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5071 | } else if (defining) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5072 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5073 | * We're defining a multi-line macro. We emit nothing |
| 5074 | * at all, and just |
| 5075 | * shove the tokenized line on to the macro definition. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5076 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5077 | Line *l = nasm_malloc(sizeof(Line)); |
| 5078 | l->next = defining->expansion; |
| 5079 | l->first = tline; |
| 5080 | l->finishes = NULL; |
| 5081 | defining->expansion = l; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5082 | continue; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5083 | } else if (istk->conds && !emitting(istk->conds->state)) { |
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 in a non-emitting branch of a condition block. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5086 | * Emit nothing at all, not even a blank line: when we |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5087 | * emerge from the condition we'll give a line-number |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5088 | * directive so we keep our place correctly. |
| 5089 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5090 | free_tlist(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5091 | continue; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5092 | } else if (istk->mstk && !istk->mstk->in_progress) { |
| 5093 | /* |
| 5094 | * We're in a %rep block which has been terminated, so |
| 5095 | * we're walking through to the %endrep without |
| 5096 | * emitting anything. Emit nothing at all, not even a |
| 5097 | * blank line: when we emerge from the %rep block we'll |
| 5098 | * give a line-number directive so we keep our place |
| 5099 | * correctly. |
| 5100 | */ |
| 5101 | free_tlist(tline); |
| 5102 | continue; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5103 | } else { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5104 | tline = expand_smacro(tline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5105 | if (!expand_mmacro(tline)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5106 | /* |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5107 | * De-tokenize the line again, and emit it. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5108 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5109 | line = detoken(tline, true); |
| 5110 | free_tlist(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5111 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5112 | } else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5113 | continue; /* expand_mmacro calls free_tlist */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5114 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5115 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5116 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5117 | |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5118 | done: |
| 5119 | nasm_set_verror(real_verror); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5120 | return line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5121 | } |
| 5122 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5123 | static void pp_cleanup(int pass) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5124 | { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5125 | real_verror = nasm_set_verror(pp_verror); |
| 5126 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5127 | if (defining) { |
| 5128 | if (defining->name) { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5129 | nasm_error(ERR_NONFATAL, |
| 5130 | "end of file while still defining macro `%s'", |
| 5131 | defining->name); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5132 | } else { |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5133 | nasm_error(ERR_NONFATAL, "end of file while still in %%rep"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5134 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5135 | |
| 5136 | free_mmacro(defining); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5137 | defining = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5138 | } |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5139 | |
| 5140 | nasm_set_verror(real_verror); |
| 5141 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5142 | while (cstk) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5143 | ctx_pop(); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 5144 | free_macros(); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5145 | while (istk) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5146 | Include *i = istk; |
| 5147 | istk = istk->next; |
| 5148 | fclose(i->fp); |
| 5149 | nasm_free(i->fname); |
Cyrill Gorcunov | 8dcfd88 | 2011-03-03 09:18:56 +0300 | [diff] [blame] | 5150 | nasm_free(i); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5151 | } |
| 5152 | while (cstk) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5153 | ctx_pop(); |
H. Peter Anvin | 86877b2 | 2008-06-20 15:55:45 -0700 | [diff] [blame] | 5154 | nasm_free(src_set_fname(NULL)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5155 | if (pass == 0) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5156 | IncPath *i; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5157 | free_llist(predef); |
Cyrill Gorcunov | dae24d7 | 2014-06-28 10:17:39 +0400 | [diff] [blame] | 5158 | predef = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5159 | delete_Blocks(); |
Cyrill Gorcunov | dae24d7 | 2014-06-28 10:17:39 +0400 | [diff] [blame] | 5160 | freeTokens = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5161 | while ((i = ipath)) { |
| 5162 | ipath = i->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5163 | if (i->path) |
| 5164 | nasm_free(i->path); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5165 | nasm_free(i); |
| 5166 | } |
H. Peter Anvin | 11dfa1a | 2008-07-02 18:11:04 -0700 | [diff] [blame] | 5167 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5168 | } |
| 5169 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5170 | static void pp_include_path(char *path) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5171 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5172 | IncPath *i; |
H. Peter Anvin | 37a321f | 2007-09-24 13:41:58 -0700 | [diff] [blame] | 5173 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5174 | i = nasm_malloc(sizeof(IncPath)); |
| 5175 | i->path = path ? nasm_strdup(path) : NULL; |
| 5176 | i->next = NULL; |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5177 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 5178 | if (ipath) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5179 | IncPath *j = ipath; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 5180 | while (j->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5181 | j = j->next; |
| 5182 | j->next = i; |
| 5183 | } else { |
| 5184 | ipath = i; |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5185 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5186 | } |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5187 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5188 | static void pp_pre_include(char *fname) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5189 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5190 | Token *inc, *space, *name; |
| 5191 | Line *l; |
| 5192 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5193 | name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0); |
| 5194 | space = new_Token(name, TOK_WHITESPACE, NULL, 0); |
| 5195 | inc = new_Token(space, TOK_PREPROC_ID, "%include", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5196 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5197 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5198 | l->next = predef; |
| 5199 | l->first = inc; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5200 | l->finishes = NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5201 | predef = l; |
| 5202 | } |
| 5203 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5204 | static void pp_pre_define(char *definition) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5205 | { |
| 5206 | Token *def, *space; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5207 | Line *l; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5208 | char *equals; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5209 | |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5210 | real_verror = nasm_set_verror(pp_verror); |
| 5211 | |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5212 | equals = strchr(definition, '='); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5213 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 5214 | def = new_Token(space, TOK_PREPROC_ID, "%define", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5215 | if (equals) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5216 | *equals = ' '; |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5217 | space->next = tokenize(definition); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5218 | if (equals) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5219 | *equals = '='; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5220 | |
Cyrill Gorcunov | 6d42e9b | 2015-02-08 11:07:17 +0300 | [diff] [blame] | 5221 | if (space->next->type != TOK_PREPROC_ID && |
| 5222 | space->next->type != TOK_ID) |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5223 | nasm_error(ERR_WARNING, "pre-defining non ID `%s\'\n", definition); |
Cyrill Gorcunov | 6d42e9b | 2015-02-08 11:07:17 +0300 | [diff] [blame] | 5224 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5225 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5226 | l->next = predef; |
| 5227 | l->first = def; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5228 | l->finishes = NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5229 | predef = l; |
H. Peter Anvin | 215186f | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5230 | |
| 5231 | nasm_set_verror(real_verror); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5232 | } |
| 5233 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5234 | static void pp_pre_undefine(char *definition) |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5235 | { |
| 5236 | Token *def, *space; |
| 5237 | Line *l; |
| 5238 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5239 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 5240 | def = new_Token(space, TOK_PREPROC_ID, "%undef", 0); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5241 | space->next = tokenize(definition); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5242 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5243 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5244 | l->next = predef; |
| 5245 | l->first = def; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5246 | l->finishes = NULL; |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5247 | predef = l; |
| 5248 | } |
| 5249 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5250 | static void pp_extra_stdmac(macros_t *macros) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5251 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 5252 | extrastdmac = macros; |
| 5253 | } |
| 5254 | |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 5255 | static void make_tok_num(Token * tok, int64_t val) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5256 | { |
Cyrill Gorcunov | ce65274 | 2013-05-06 23:43:43 +0400 | [diff] [blame] | 5257 | char numbuf[32]; |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 5258 | snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5259 | tok->text = nasm_strdup(numbuf); |
| 5260 | tok->type = TOK_NUMBER; |
| 5261 | } |
| 5262 | |
H. Peter Anvin | 3736895 | 2016-05-09 14:10:32 -0700 | [diff] [blame] | 5263 | static void pp_list_one_macro(MMacro *m, int severity) |
| 5264 | { |
| 5265 | if (!m) |
| 5266 | return; |
| 5267 | |
| 5268 | /* We need to print the next_active list in reverse order */ |
| 5269 | pp_list_one_macro(m->next_active, severity); |
| 5270 | |
| 5271 | if (m->name && !m->nolist) { |
| 5272 | src_set_linnum(m->xline + m->lineno); |
| 5273 | src_set_fname(m->fname); |
| 5274 | nasm_error(severity, "... from macro `%s' defined here", m->name); |
| 5275 | } |
| 5276 | } |
| 5277 | |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5278 | static void pp_error_list_macros(int severity) |
| 5279 | { |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5280 | int32_t saved_line; |
| 5281 | const char *saved_fname = NULL; |
| 5282 | |
| 5283 | severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY; |
| 5284 | saved_line = src_get_linnum(); |
| 5285 | saved_fname = src_get_fname(); |
| 5286 | |
H. Peter Anvin | 3736895 | 2016-05-09 14:10:32 -0700 | [diff] [blame] | 5287 | pp_list_one_macro(istk->mstk, severity); |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5288 | |
| 5289 | src_set_fname((char *)saved_fname); |
| 5290 | src_set_linnum(saved_line); |
| 5291 | } |
| 5292 | |
| 5293 | const struct preproc_ops nasmpp = { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5294 | pp_reset, |
| 5295 | pp_getline, |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5296 | pp_cleanup, |
| 5297 | pp_extra_stdmac, |
| 5298 | pp_pre_define, |
| 5299 | pp_pre_undefine, |
| 5300 | pp_pre_include, |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 5301 | pp_include_path, |
| 5302 | pp_error_list_macros, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5303 | }; |